windows的绘图工具:画笔CPen 画刷CBrush 调色板CPalette 画笔通常具有宽度 样式和颜色3中属性 构造函数 1.CPen( ); 2.CPen( int nPenStyle, int nWidth, COLORREF crColor ); 3.CPen( int nPenStyle, int nWidth, const LOGBRUSH* pLogBrush, int nStyleCount = 0, const DWORD* lpStyle = NULL ); 当使用第一种构造函数时,还得继续调用一下函数 CPen::CreatePen BOOL CreatePen( int nPenStyle, int nWidth, COLORREF crColor ); BOOL CreatePen( int nPenStyle, int nWidth, const LOGBRUSH* pLogBrush, int nStyleCount = 0, const DWORD* lpStyle = NULL ); BOOL CreatePenIndirect( LPLOGPEN lpLogPen ); 涉及的结构体 typedef struct tagLOGPEN { /* lgpn */ UINT lopnStyle; POINT lopnWidth; COLORREF lopnColor; } LOGPEN; typedef struct tagLOGBRUSH { UINT lbStyle; COLORREF lbColor; LONG lbHatch; } LOGBRUSH, *PLOGBRUSH; typedef struct tagPOINT { LONG x; LONG y; } POINT; 例子 1. CPen pen; pen.CreatePen(PS_SOLID,1,RGB(0,0,225)); 2. CPen *pen1=new CPen(PS_SOLID,1,RGB(0,0,225)) 画刷通常具有填充色 填充图案和填充样式3种属性 构造函数 CBrush( ); CBrush( COLORREF crColor ); CBrush( int nIndex, COLORREF crColor ); CBrush( CBitmap* pBitmap ); If you use the constructor with no arguments, you must initialize the resulting CBrush object with CreateSolidBrush, CreateHatchBrush, CreateBrushIndirect, CreatePatternBrush, or CreateDIBPatternBrush. 例子 1.CBrush brush1; // Must initialize! brush1.CreateSolidBrush(RGB(0, 0, 255)); // Blue brush. 2. CBrush brush3(HS_DIAGCROSS, RGB(0, 255, 0)); CBrush brush3(HS_DIAGCROSS, RGB(0, 255, 0)); 3. CBitmap bmp; bmp.LoadBitmap(IDB_BRUSH); CBrush brush4(&bmp); ------------------------点------------------------------------ 画点 CDC::SetPixel COLORREF SetPixel( int x, int y, COLORREF crColor ); COLORREF SetPixel( POINT point, COLORREF crColor ); CDC::SetPixelV BOOL SetPixelV(int x, int y, COLORREF crColor); BOOL SetPixelV( POINT point, COLORREF crColor ); ------------------------线------------------------------------ 画线 CDC::MoveTo (获取当前点CDC::GetCurrentPosition ) CPoint MoveTo( int x, int y ); CPoint MoveTo( POINT point ); Moves the current position to the point specified by x and y (or by point). CDC::LineTo BOOL LineTo( int x, int y ); BOOL LineTo( POINT point ); 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. ------------------------椭圆和弧------------------------------------ 画椭圆 CDC::Ellipse BOOL Ellipse( int x1, int y1, int x2, int y2 ); BOOL Ellipse( LPCRECT lpRect ); 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. 画椭圆弧 CDC::Arc BOOL Arc( int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4 ); BOOL Arc( LPCRECT lpRect, POINT ptStart, POINT ptEnd ); Since an arc is not a closed figure, it is not filled. 画带弦的椭圆弧 CDC::Chord BOOL Chord( int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4 ); BOOL Chord( LPCRECT lpRect, POINT ptStart, POINT ptEnd ); The chord is drawn by using the selected pen and filled by using the selected brush. 画一条椭圆弧并且弧的两个端点与圆心连线 CDC::Pie BOOL Pie( int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4 ); BOOL Pie( LPCRECT lpRect, POINT ptStart, POINT ptEnd ); Draws a pie-shaped wedge by drawing an elliptical arc whose center and two endpoints are joined by lines. The pie-shaped area is filled with the current brush ------------------------线段------------------------------------ 画连续的线段(从数组中的第一个点开始) CDC::Polyline BOOL Polyline( LPPOINT lpPoints, int nCount ); Return Value Nonzero if the function is successful; otherwise 0. Parameters lpPoints Points to an array of POINT structures or CPoint objects to be connected. nCount Specifies the number of points in the array. This value must be at least 2. 画连续的线段(会从当前的点开始) CDC::PolylineTo BOOL PolylineTo( const POINT* lpPoints, int nCount ); A line is drawn from the current position to the first point specified by the lpPoints parameter by using the current pen. 画贝塞尔曲线 CDC::PolyBezier BOOL PolyBezier( const POINT* lpPoints, int nCount ); CDC::PolyBezierTo BOOL PolyBezierTo( const POINT* lpPoints, int nCount ); The first spline is drawn from the current position to the third point by using the first two points as control points. 画多组连接的线段 CDC::PolyPolyline BOOL PolyPolyline( const POINT* lpPoints, const DWORD* lpPolyPoints, int nCount ); 参数 lpPolyPoints Points to an array of variables specifying the number of points in the lpPoints array for the corresponding polygon. Each entry must be greater than or equal to 2. 绘任意多边形 CDC::Polygon BOOL Polygon( LPPOINT lpPoints, int nCount ); ------------------------矩形------------------------------------ 填充矩形 CDC::Rectangle BOOL Rectangle( int x1, int y1, int x2, int y2 ); BOOL Rectangle( LPCRECT lpRect ); Draws a rectangle using the current pen. The interior of the rectangle is filled using the current brush. 画一个带圆角的矩形 CDC::RoundRect BOOL RoundRect( int x1, int y1, int x2, int y2, int x3, int y3 ); BOOL RoundRect( LPCRECT lpRect, POINT point ); 用指定的颜色填充矩形 CDC::FillSolidRect void FillSolidRect( LPCRECT lpRect, COLORREF clr ); void FillSolidRect( int x, int y, int cx, int cy, COLORREF clr ); Remarks Call this member function to fill the given rectangle with the specified solid color. FillSolidRect is very similar to CDC::FillRect; however, FillSolidRect uses only solid colors (indicated by the COLORREF parameter), while FillRect takes a brush and therefore can be used to fill a rectangle with a solid color, a dithered color, hatched brushes, or a pattern. FillSolidRect usually is faster than FillRect. 使用指定的画刷填充矩形 CDC::FillRect void FillRect( LPCRECT lpRect, CBrush* pBrush ); Remarks Call this member function to fill a given rectangle using the specified brush. The function fills the complete rectangle, including the left and top borders, but it does not fill the right and bottom borders. 使用指定的画刷填充矩形,可以指定样式 CDC::ExtFloodFill BOOL ExtFloodFill( int x, int y, COLORREF crColor, UINT nFillType ); Remarks Fills an area of the display surface with the current brush. This member function offers more flexibility than FloodFill because you can specify a fill type in nFillType. 使用当前画刷填充显示区域 CDC::FloodFill BOOL FloodFill( int x, int y, COLORREF crColor ); Remarks Fills an area of the display surface with the current brush. The area is assumed to be bounded as specified by crColor. The FloodFill function begins at the point specified by x and y and continues in all directions to the color boundary. Only memory-device contexts and devices that support raster-display technology support the FloodFill member function. 以下是例子 void CpicView::OnDraw(CDC* pDC) { CpicDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; // TODO: 在此处为本机数据添加绘制代码 /* ///使用画刷 // CBrush::CBrush. CBrush brush1; // Must initialize! brush1.CreateSolidBrush(RGB(0, 0, 255)); // Blue brush. CBrush* pTempBrush = NULL; CBrush OrigBrush; CRect rc; GetClientRect(&rc); ScreenToClient(&rc); //第0 pTempBrush = (CBrush*)pDC->SelectObject(brush1); // Save original brush. OrigBrush.FromHandle((HBRUSH)pTempBrush); // Paint upper left corner with blue brush. pDC->Rectangle(0, 0, rc.Width() / 2, rc.Height() / 2); // These constructors throw resource exceptions. try { // CBrush::CBrush(COLORREF crColor) CBrush brush2(RGB(255, 0, 0)); // Solid red brush. // CBrush::CBrush(int nIndex, COLORREF crColor) // Hatched green brush. CBrush brush3(HS_DIAGCROSS, RGB(0, 255, 0)); // CBrush::CBrush(CBitmap* pBitmap) CBitmap bmp; // Load a resource bitmap. bmp.LoadBitmap(IDB_BRUSH); CBrush brush4(&bmp); //*第1 pTempBrush = (CBrush*)pDC->SelectObject(brush2); // Paint upper right corner with red brush. pDC->Rectangle(rc.Width() / 2, 0, rc.Width(), rc.Height() / 2); //*第2 pTempBrush = (CBrush*)pDC->SelectObject(brush3); // Paint lower left corner with green hatched brush. pDC->Rectangle(0, rc.Height() / 2, rc.Width() / 2, rc.Height()); //*第3 pTempBrush = (CBrush*)pDC->SelectObject(brush4); // Paint lower right corner with resource brush. pDC->Rectangle(rc.Width() / 2, rc.Height() / 2, rc.Width(), rc.Height()); } catch (CResourceException* e) { e->ReportError(); e->Delete(); } // Reselect original brush into device context. pDC->SelectObject(&OrigBrush); */ /* //填充 // create and select a solid blue brush CBrush brushBlue(RGB(0, 0, 255)); CBrush* pOldBrush = pDC->SelectObject(&brushBlue); // create and select a thick, black pen CPen penBlack; penBlack.CreatePen(PS_SOLID, 10, RGB(255, 0, 0)); CPen* pOldPen = pDC->SelectObject(&penBlack); // get our client rectangle CRect rect; GetClientRect(rect); // shrink our rect 20 pixels in each direction rect.DeflateRect(20, 20); // draw a thick black rectangle filled with blue pDC->Rectangle(rect); // put back the old objects pDC->SelectObject(pOldBrush); pDC->SelectObject(pOldPen); */ /* //画线 CPen penBlack; penBlack.CreatePen(PS_SOLID, 10, RGB(255, 0, 0)); CPen* pOldPen = pDC->SelectObject(&penBlack); pDC->MoveTo(20,20); pDC->LineTo(40, 40); pDC->SelectObject(pOldPen); */ /* //画椭圆 CBrush brushBlue(RGB(0, 0, 255)); CBrush* pOldBrush = pDC->SelectObject(&brushBlue); // create and select a thick, black pen CPen penBlack; penBlack.CreatePen(PS_SOLID, 10, RGB(255, 0, 0)); CPen* pOldPen = pDC->SelectObject(&penBlack); CRect rect2(10, 10, 200, 500); //CRect rectDeflate(1, 2, 3, 4); //rect2.DeflateRect(&rectDeflate); pDC->Ellipse(rect2); pDC->SelectObject(pOldBrush); pDC->SelectObject(pOldPen); */ /* //画椭圆弧 CRect rectClient; GetClientRect(rectClient); // Make a couple of pens. CPen penBlue; CPen penRed; CPen* pOldPen; penBlue.CreatePen(PS_SOLID | PS_COSMETIC, 5, RGB(0, 0, 255)); penRed.CreatePen(PS_SOLID | PS_COSMETIC, 5, RGB(255, 0, 0)); // Draw from 3 o'clock to 6 o'clock, counterclockwise, // in a blue pen. pOldPen = pDC->SelectObject(&penBlue); pDC->Arc(rectClient, CPoint(rectClient.right, rectClient.CenterPoint().y), CPoint(rectClient.CenterPoint().x, rectClient.right)); //--------------------------------------------------------------- // Draw from 6 o'clock to 3 o'clock, counterclockwise, // in a red pen. pDC->SelectObject(penRed); // Keep the same parameters, but reverse start // and end points. pDC->Arc(rectClient, CPoint(rectClient.CenterPoint().x, rectClient.right), CPoint(rectClient.right, rectClient.CenterPoint().y)); // Restore the previous pen. pDC->SelectObject(pOldPen); */ /* //画带弦的椭圆弧 // Get the client area. CRect rectClient; GetClientRect(rectClient); // Make a couple of pens and similar brushes. CPen penBlue, penRed; CBrush brushBlue, brushRed; CBrush* pOldBrush; CPen* pOldPen; brushBlue.CreateSolidBrush(RGB(0, 0, 255)); brushRed.CreateHatchBrush(HS_FDIAGONAL, RGB(255, 0, 0)); penBlue.CreatePen(PS_SOLID | PS_COSMETIC, 1, RGB(0, 0, 255)); penRed.CreatePen(PS_SOLID | PS_COSMETIC, 5, RGB(255, 0, 0)); // Draw from 3 o'clock to 6 o'clock, counterclockwise, // in a blue pen with a solid blue fill. pOldPen = pDC->SelectObject(&penBlue); pOldBrush = pDC->SelectObject(&brushBlue); pDC->Chord(rectClient, CPoint(rectClient.right, rectClient.CenterPoint().y), CPoint(rectClient.CenterPoint().x, rectClient.right)); // Draw the remaining quarter chord from 6 o'clock // to 3 o'clock, counterclockwise, in a red pen // with the hatched brush. pDC->SelectObject(&penRed); pDC->SelectObject(&brushRed); // Keep the same parameters, but reverse start and // end points. pDC->Chord(rectClient, CPoint(rectClient.CenterPoint().x, rectClient.right), CPoint(rectClient.right, rectClient.CenterPoint().y)); // Restore the previous pen. pDC->SelectObject(pOldPen); */ /* //画一条椭圆弧并且弧的两个端点与圆心连线 // Get the client area. CRect rectClient; GetClientRect(rectClient); // Make a couple of pens and similar brushes. CPen penBlue, penRed; CBrush brushBlue, brushRed; CBrush* pOldBrush; CPen* pOldPen; brushBlue.CreateSolidBrush(RGB(0, 0, 255)); brushRed.CreateHatchBrush(HS_FDIAGONAL, RGB(255, 0, 0)); penBlue.CreatePen(PS_SOLID | PS_COSMETIC, 1, RGB(0, 0, 255)); penRed.CreatePen(PS_SOLID | PS_COSMETIC, 5, RGB(255, 0, 0)); // Draw from 3 o'clock to 6 o'clock, counterclockwise, // in a blue pen with a solid blue fill. pOldPen = pDC->SelectObject(&penBlue); pOldBrush = pDC->SelectObject(&brushBlue); pDC->Pie(rectClient, CPoint(rectClient.right, rectClient.CenterPoint().y), CPoint(rectClient.CenterPoint().x, rectClient.right)); // Draw the remaining quarter slice from 6 o'clock // to 3 o'clock, counterclockwise, in a red pen with // the hatched brush. pDC->SelectObject(&penRed); pDC->SelectObject(&brushRed); // Same parameters, but reverse start and end points. pDC->Pie(rectClient, CPoint(rectClient.CenterPoint().x, rectClient.right), CPoint(rectClient.right, rectClient.CenterPoint().y)); // Restore the previous pen. pDC->SelectObject(pOldPen); */ /* // 画一个带圆角的矩形 // create and select a solid blue brush CBrush brushBlue(RGB(0, 0, 255)); CBrush* pOldBrush = pDC->SelectObject(&brushBlue); // create and select a thick, black pen CPen penBlack; penBlack.CreatePen(PS_SOLID, 3, RGB(0, 0, 0)); CPen* pOldPen = pDC->SelectObject(&penBlack); // get our client rectangle CRect rect; GetClientRect(rect); // shrink our rect 20 pixels in each direction rect.DeflateRect(20, 20); // Draw a thick black rectangle filled with blue // corners rounded at a 17-unit radius. Note that // a radius of three or less is not noticable because // the pen is three units wide. pDC->RoundRect(rect, CPoint(50, 30)); // put back the old objects pDC->SelectObject(pOldBrush); pDC->SelectObject(pOldPen); */ /* //画连续的线段 CPen penBlack; penBlack.CreatePen(PS_SOLID, 5, RGB(0, 255, 0)); CPen* pOldPen = pDC->SelectObject(&penBlack); POINT pt[4] = { { 40, 30 }, { 60, 160 }, { 120, 200 }, { 30, 320 } }; pDC->Polyline(pt, 4); pDC->PolylineTo(pt, 4); pDC->SelectObject(pOldPen); */ /* //绘任意多边形 // find the client area CRect rect; GetClientRect(rect); // draw with a thick blue pen CPen penBlue(PS_SOLID, 5, RGB(0, 0, 255)); CPen* pOldPen = pDC->SelectObject(&penBlue); // and a solid red brush CBrush brushRed(RGB(255, 0, 0)); CBrush* pOldBrush = pDC->SelectObject(&brushRed); // Find the midpoints of the top, right, left, and bottom // of the client area. They will be the vertices of our polygon. CPoint pts[4]; pts[0].x = rect.left + rect.Width() / 2; pts[0].y = rect.top; pts[1].x = rect.right; pts[1].y = rect.top + rect.Height() / 2; pts[2].x = pts[0].x; pts[2].y = rect.bottom; pts[3].x = rect.left; pts[3].y = pts[1].y; // Calling Polygon() on that array will draw three lines // between the points, as well as an additional line to // close the shape--from the last point to the first point // we specified. pDC->Polygon(pts, 4); // Put back the old objects. pDC->SelectObject(pOldPen); pDC->SelectObject(pOldBrush); */ /* pDC->FillSolidRect(0, 0, 200, 200, RGB(0, 255, 0)); */ }
原文地址:http://blog.csdn.net/h1023417614/article/details/42584823