VC++ MFC提供的绘图函数有哪些?这是一个简答题,就要函数有哪些

VC++ MFC提供的绘图函数有哪些?这是一个简答题,就要函数有哪些,第1张

1 画像素点

画像素点就是设置像素点的颜色,从前面3)(2)已知道这可由CDC的成员函数SetPixel来做,该函数的原型为:

COLORREF SetPixel( int x, int y, COLORREF crColor ); 或

COLORREF SetPixel( POINT point, COLORREF crColor );

其中,x与y分别为像素点的横坐标与纵坐标,crColor为像素的颜色值。例如

pDC->SetPixel(i, j, RGB(r, g, b));

2.画线状图

在Windows中,线状图必须用笔来画(笔的创建与使用见前面的3)(3)),下面是CDC类中可以绘制线状图的常用成员函数:

当前位置:设置当前位置为(x, y)或point:(返回值为原当前位置的坐标)

CPoint MoveTo( int x, int y ); 或 CPoint MoveTo( POINT point);

画线:使用DC中的笔从当前位置画线到点(x, y)或point:(若成功返回非0值):

BOOL LineTo( int x, int y ); 或BOOL LineTo( POINT point );

画折线:使用DC中的笔,依次将点数组lpPoints中的nCount(≥2)个点连接起来,形成一条折线:

BOOL Polyline( LPPOINT lpPoints, int nCount );

画多边形:似画折线,但还会将最后的点与第一个点相连形成多边形,并用DC中的刷填充其内部区域

BOOL Polygon( LPPOINT lpPoints, int nCount );

矩形:使用DC中的笔画左上角为(x1, y1)、右下角为(x2,y2)或范围为lpRect的矩形的边线,并用DC中的刷填充其内部区域:

BOOL Rectangle( int x1, int y1, int x2, int y2 ); 或

BOOL Rectangle( LPCRECT lpRect );

有时需要根据用户给定的两个任意点来重新构造左上角和右下角的点,例如:

rect = CRect(min(p0x, pointx), min(p0y, pointy), max(p0x,pointx), max(p0y, pointy));

画圆角矩形:使用DC中的笔画左上角为(x1, y1)、右下角为(x2,y2)或范围为lpRect的矩形的边线,并用宽x3或pointx高y3或pointy矩形的内接椭圆倒角,再用DC中的刷填充其内部区域:

BOOL RoundRect( int x1, int y1, int x2, int y2, int x3, int y3);

BOOL RoundRect( LPCRECT lpRect, POINT point );

例如:

int d = min(rectWidth(), rectHeight()) / 4;

pDC-> RoundRect(rect, CPoint(d, d));

画(椭)圆:使用DC中的笔在左上角为(x1, y1)、右下角为(x2,y2)或范围为lpRect的矩形中画内接(椭)圆的边线,并用DC中的刷填充其内部区域:

BOOL Ellipse( int x1, int y1, int x2, int y2 );

BOOL Ellipse( LPCRECT lpRect );

注意,CDC中没有画圆的专用函数。在这里,圆是作为椭圆的(宽高相等)特例来画的。

画弧:(x1, y1)与(x2, y2)或lpRect的含义同画(椭)圆,(x3, y3)或ptStart为弧的起点,(x4,y4)或ptEnd为弧的终点:(逆时针方向旋转)

BOOL Arc( int x1, int y1, int x2, int y2, int x3, int y3, intx4, int y4 );

BOOL Arc( LPCRECT lpRect, POINT ptStart, POINT ptEnd );

BOOL ArcTo(int x1, int y1, int x2, int y2, int x3, int y3, int x4,int y4);

BOOL ArcTo(LPCRECT lpRect, POINT ptStart, POINT ptEnd);

画圆弧:(其中(x, y)为圆心、nRadius为半径、fStartAngle为起始角、fSweepAngle为弧段跨角)

BOOL AngleArc(int x, int y, int nRadius, float fStartAngle, floatfSweepAngle);

画弓弦:参数的含义同上,只是用一根弦连接弧的起点和终点,形成一个弓形,并用DC中的刷填充其内部区域:

BOOL Chord( int x1, int y1, int x2, int y2, int x3, int y3, intx4, int y4 );

BOOL Chord( LPCRECT lpRect, POINT ptStart, POINT ptEnd );

3.画填充图

在Windows中,面状图必须用刷来填充(刷的创建与使用见前面的3)(4))。上面(2)中的Polygon、Rectangle、Ellipse和Chord等画闭合线状图的函数,只要DC中的刷不是空刷,都可以用来画对应的面状图(边线用当前笔画,内部用当前刷填充)。下面介绍的是CDC类中只能绘制面状图的其他常用成员函数:

画填充矩形:用指定的刷pBrush画一个以lpRect为区域的填充矩形,无边线,填充区域包括矩形的左边界和上边界,但不包括矩形的右边界和下边界:

void FillRect( LPCRECT lpRect, CBrush pBrush );

画单色填充矩形:似FillRect,但只能填充单色,不能填充条纹和图案:

void FillSolidRect( LPCRECT lpRect, COLORREF clr );

void FillSolidRect( int x, int y, int cx, int cy, COLORREF clr);

画饼图(扇形):参数含义同Arc,但将起点和终点都与外接矩形的中心相连接,形成一个扇形区域,用DC中的刷填充整个扇形区域,无另外的边线:

BOOL Pie( int x1, int y1, int x2, int y2, int x3, int y3, intx4, int y4 );

BOOL Pie( LPCRECT lpRect, POINT ptStart, POINT ptEnd );

画拖动的矩形:先擦除线宽为sizeLast、填充刷为pBrushLast的原矩形lpRectLast,然后再以线宽为size、填充刷为pBrush画新矩形lpRectLast。矩形的边框用灰色的点虚线画,缺省的填充刷为空刷:

void DrawDragRect( LPCRECT lpRect, SIZE size, LPCRECTlpRectLast,

SIZE sizeLast, CBrush pBrush = NULL, CBrush pBrushLast = NULL);

如:pDC->DrawDragRect(rect, size, rect0, size);

填充区域:

用当前刷从点(x, y)开始向四周填充到颜色为crColor的边界:

BOOL FloodFill(int x, int y, COLORREF crColor); // 成功返回非0

用当前刷从点(x, y)开始向四周填充:

BOOL ExtFloodFill(int x, int y, COLORREF crColor,

UINT nFillType); // 成功返回非0

nFillType =FLOODFILLBORDER:填充到颜色为crColor的边界(同FloodFill);(用于填充内部颜色不同但边界颜色相同的区域)

nFillType =FLOODFILLSURFACE:填充所有颜色为crColor的点,直到碰到非crColor颜色的点为止。(点(x,y)的颜色也必须为crColor),(用于填充内部颜色相同但边界颜色可以不同的区域)。例如:

pDC->ExtFloodFill(pointx, pointy,pDC-> GetPixel_r(point), FLOODFILLSURFACE);

4.清屏

Windows没有提供专门的清屏函数,可以调用CWnd的下面两个函数调用来完成该功能:

void Invalidate(BOOL bErase = TRUE);

void UpdateWindow( );

或调用CWnd的函数

BOOL RedrawWindow(

LPCRECT lpRectUpdate =NULL,

CRgn prgnUpdate =NULL,

UINT flags = RDW_INVALIDATE |RDW_UPDATENOW | RDW_ERASE

);

来完成。

例如(菜单项ID_CLEAR的事件处理函数):

CDrawView::OnClear() { // 调用OnDraw来清屏

//Invalidate();

//UpdateWindow( );

RedrawWindow( );

}

也可以用画填充背景色矩形的方法来清屏,如:

RECT rect;

GetClientRect_r(&rect);

pDC->FillSolidRect(&rect, RGB(255,255, 255));

画之前为CClientDC添加一个实心画刷就行了。

CBrush brush,oldbrush;

brushCreateSolidBrush(RGB(0,0,0));

oldbrush=ClientDCSelectObject(&brush);

ClientDCEllipse(10,10,100,100);

ClientDCSelectObject(oldbrush);

CBrush brush(RGB(255,0,0));

pDC->SelectObject(&brush);

pDC->Ellipese();

MoveTo( int x, int y );

Moves the current position to the point specified by x and y

(or by point)

画线段的起点

LineTo( int x, int y );

Draws a line from the current position up to, but not including, the point

specified by x and y (or point) The line is drawn with the

selected pen The current position is set to x,y or to

point

画线段的终点

BOOL Ellipse( int x1, int y1,

int x2, int y2 );

Draws an ellipse The center of the ellipse is the center of the bounding

rectangle specified by x1, y1, x2, and y2, or

lpRect The ellipse is drawn with the current pen, and its interior is

filled with the current brush

根据两个点画椭圆

上次刚写过,在VC下运行的,

int

dx,dy,incrE,incrNE,d,x,y;

if

((point[1]x-point[0]x)==0){

//垂直的直线

x=point[0]x;

for(y=point[0]y;y<point[1]y;y++)

pDC->SetPixel(x,y,50);

}

else

if(abs((point[1]y-point[0]y)/(point[1]x-point[0]x))<=1){

//斜率

-1到

1

之间

dx=point[1]x-point[0]x;

dy=point[0]y-point[1]y;

d=dx-2dy;

incrE=-2dy;

incrNE=2(dx-dy);

x=point[0]x,y=point[0]y;

pDC->SetPixel(x,y,50);

if(point[0]y>point[1]y){

while(x<point[1]x)

{

if(d>=0){

d+=incrE;

x++;

}

else

{d+=incrNE;

x++;

y--;

}

pDC->SetPixel(x,y,50);

}

}

else

if(point[0]y<=point[1]y){

dy=point[1]y-point[0]y;

incrE=-2dy;

incrNE=2(dx-dy);

x=point[0]x,y=point[0]y;

pDC->SetPixel(x,y,50);

while(x<point[1]x)

{

if(d>=0){

d+=incrE;

x++;

}

else

{d+=incrNE;

x++;

y++;

}

pDC->SetPixel(x,y,50);

}

}

}

else

{

//斜率

<-1

>1的直线

if(point[1]x>=point[0]x){

dx=point[1]x-point[0]x;

dy=point[1]y-point[0]y;

d=2dx-dy;

incrE=2dx;

incrNE=2(dx-dy);

x=point[0]x,y=point[0]y;

pDC->SetPixel(x,y,50);

while(x<point[1]x)

{

if(d<0){

d+=incrE;

y++;

}

else

{d+=incrNE;

pDC->SetPixel(x,y,50);

x++;

y++;

}

pDC->SetPixel(x,y,50);

}

}

else

if((point[1]y-point[0]y)/(point[1]x-point[0]x)<-1){

dx=point[1]x-point[0]x;

dy=point[0]y-point[1]y;

d=2dx-dy;

incrE=2dx;

incrNE=2(dx-dy);

x=point[0]x,y=point[0]y;

pDC->SetPixel(x,y,50);

while(y<point[1]y)

{

if(d>0){

d+=incrE;

y++;

}

else

{d+=incrNE;

x--;

y++;

}

pDC->SetPixel(x,y,50);

}

}

}

如果只是在view中画圆,只需要在OnLButtonDown中添加就可以了,比如:

void CMyStatusBarView::OnLButtonDown(UINT nFlags, CPoint point)

{

// TODO: Add your message handler code here and/or call default

CDC pdc = GetDC();

pdc->Ellipse(CRect(pointx - 10, pointy - 10, pointx + 10, pointy + 10)); //10是你要画的圆的半径

CView::OnLButtonDown(nFlags, point);

}

这种做法最小化窗口,或是变化大小后图像会消失,因为在view的onpaint或是别的函数中重画了,但没有画先前的圆。如果要想在变化窗口之后还看到圆,必须把之前画的保存起来,然后在onpaint或是ondraw中依次画出来就ok了。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/12182115.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-21
下一篇2023-05-21

发表评论

登录后才能评论

评论列表(0条)

    保存