void CTestDlg::OnButtonDraw()
{
DrawCoordinate(CPoint(100,250), 200, 6, 150, 10);
}
// 顶点坐标, x轴长度, x轴刻度数(含原点), y轴长度, y轴刻度数(含原点)
void CTestDlg::DrawCoordinate(CPoint point, int xLen, int xNum, int yLen, int yNum)
{
int x = xLen + 5;
int y = yLen + 5;
CDC *pDC = GetDC();
pDC->SetBkMode(TRANSPARENT); // 设置透明
pDC->MoveTo(point);
pDC->LineTo(CPoint(point.x+x, point.y)); // 绘制x轴
pDC->MoveTo(point);
pDC->LineTo(CPoint(point.x, point.y-y)); // 绘制y轴
pDC->TextOut(point.x-5, point.y+5, _T("0"));
pDC->TextOut(point.x+x+20, point.y-5, _T("X"));
pDC->TextOut(point.x-5, point.y-y-20, _T("Y"));
// 计算刻度宽度
int x_width = xLen / xNum;
int y_width = yLen / yNum;
int x_pixel = 0; // 当前x坐标
int y_pixel = 0; // 当前y坐标
for(int i=0; i
x_pixel = i*x_width;
pDC->MoveTo(CPoint(point.x+x_pixel, point.y-2)); // 绘制x轴刻度
pDC->LineTo(CPoint(point.x+x_pixel, point.y+4));
if(i > 0)
{
CString str;
str.Format(_T("%d"), i);
pDC->TextOut(point.x+x_pixel-2, point.y+3, str);
}
}
for(int j=0; j
y_pixel = j*y_width;
pDC->MoveTo(CPoint(point.x-2, point.y-y_pixel)); // 绘制y轴刻度
pDC->LineTo(CPoint(point.x+4, point.y-y_pixel));
if(j > 0)
{
CString str;
str.Format(_T("%d"), j);
pDC->TextOut(point.x-15, point.y-y_pixel-6, str);
}
}
ReleaseDC(pDC);
}
当然你也可以通过定义POINT数组,数组中记录每个点的坐标,接着使用CDC的PolyLineTo或PolyLine等多边形函数进行线段描绘,效果一样,看你喜欢哪种了
DrawCoordinate(CPoint(100,250), 200, 6, 150, 10);
}
// 顶点坐标, x轴长度, x轴刻度数(含原点), y轴长度, y轴刻度数(含原点)
void CTestDlg::DrawCoordinate(CPoint point, int xLen, int xNum, int yLen, int yNum)
{
int x = xLen + 5;
int y = yLen + 5;
CDC *pDC = GetDC();
pDC->SetBkMode(TRANSPARENT); // 设置透明
pDC->MoveTo(point);
pDC->LineTo(CPoint(point.x+x, point.y)); // 绘制x轴
pDC->MoveTo(point);
pDC->LineTo(CPoint(point.x, point.y-y)); // 绘制y轴
pDC->TextOut(point.x-5, point.y+5, _T("0"));
pDC->TextOut(point.x+x+20, point.y-5, _T("X"));
pDC->TextOut(point.x-5, point.y-y-20, _T("Y"));
// 计算刻度宽度
int x_width = xLen / xNum;
int y_width = yLen / yNum;
int x_pixel = 0; // 当前x坐标
int y_pixel = 0; // 当前y坐标
for(int i=0; i
x_pixel = i*x_width;
pDC->MoveTo(CPoint(point.x+x_pixel, point.y-2)); // 绘制x轴刻度
pDC->LineTo(CPoint(point.x+x_pixel, point.y+4));
if(i > 0)
{
CString str;
str.Format(_T("%d"), i);
pDC->TextOut(point.x+x_pixel-2, point.y+3, str);
}
}
for(int j=0; j
y_pixel = j*y_width;
pDC->MoveTo(CPoint(point.x-2, point.y-y_pixel)); // 绘制y轴刻度
pDC->LineTo(CPoint(point.x+4, point.y-y_pixel));
if(j > 0)
{
CString str;
str.Format(_T("%d"), j);
pDC->TextOut(point.x-15, point.y-y_pixel-6, str);
}
}
ReleaseDC(pDC);
}
用画线的方法,输出
比如一个箭头就画三条线
有一个setpitch的指令,你可以搜索一下