vc++绘图函数

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));
*/

}

时间: 2024-10-13 23:22:09

vc++绘图函数的相关文章

Scilab 的绘图函数(1)

Scilab 的绘图函数 plot 函数 最基本的是 plot 函数,与 matlab 中的plot 函数类似. xdata = linspace(1,10,50); ydata = sin(xdata); plot(xdata, ydata); 对函数绘图,不需要事先计算出 ydata,比如下面的例子画出的结果是相同的. plot (xdata, sin); 这样还能节省些内存占用. 如果只设置总的标题,可以这样操作: title("My Plot"); 如果还要设置XY坐标轴的标题

opencv学习之路(4)、Mat类介绍,基本绘图函数

一.Mat类创建 1 #include <opencv2/opencv.hpp> 2 using namespace cv; 3 4 void main(){ 5 Mat img1=imread("E://1.jpg"); 6 Mat img2(img1); 7 Mat img3=img1; 8 Mat img4=img1.clone(); 9 Mat img5; 10 img1.copyTo(img5); 11 12 cvtColor(img1,img1,CV_BGR2H

5、opencv中的绘图函数

1.目标 a.学习使用 OpenCV 绘制不同几何图形 b. 你将会学习到这些函数: cv2.line(), cv2.circle(), cv2.rectangle(),cv2.ellipse(),cv2.putText() 等. 2.代码 上面所有的这些绘图函数需要设置下面这些参数: img:你想要绘制图形的那幅图像. color: 形状的颜色. 以 RGB 为例, 需要传入一个元组, 例如: (255,0,0)代表蓝色.对于灰度图只需要传入灰度值. thickness:线条的粗细.如果给一个

OpenCV for Python 学习 (一 绘图函数)

本人的学习笔记主要记录的是学习opencv-python-tutorials这本书中的笔记 今天晚上简单学习OpenCV for Python如何绘图,主要用了这几个函数(这几个函数可在:http://docs.opencv.org/modules/core/doc/drawing_functions.html 找到): cv2.line(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) cv2.circle(img, center,

VC++常用函数

// 获取文件运行路径 void GetAppFilePath(const CString& strProjectName) { TCHAR szFileName[MAX_PATH]; HMODULE hModule = GetModuleHandle(strProjectName); if (0 != GetModuleFileName(hModule, szFileName, MAX_PATH - 1)) { szFileName[MAX_PATH - 1] = '\0'; } TCHAR

Scilab 的绘图函数(2)

一幅图是由许多元素组成的.包括图标题,x轴标签,y轴标签,刻度线等.图1给出了各个元素的一个示意图. 这些所有的元素在scilab中都是可以用代码控制的. 标题 上个笔记上介绍了用xtitle()函数可以在图上添加标题.比如: title("My Plot"); 实际上,title函数有三种形式: title(my_title) title(my_title,<Property>) title(<axes_handle>,<my_title>,<

R语言——绘图函数深入学习

利用R自带数据集 通过data()函数可以查看R自带数据集. > data() 返回以下结果,每一条记录都是一个数据,键入相应的数据名称可以查看具体信息. Data sets in package ¡®datasets¡¯: AirPassengers Monthly Airline Passenger Numbers 1949-1960 BJsales Sales Data with Leading Indicator BJsales.lead (BJsales) Sales Data wit

Matlab绘图函数一览

要查看Matlab所有绘图函数,请从Matlab主界面菜单查看"绘图目录",或从Matlab帮助文档查看"Types of MATLAB Plots"(在线版本).本文的图和英文解释摘自Matlab帮助文档. 类别 Function 图 维度 描述 曲线 plot 2 绘制曲线,相邻点之间被插值 fplot     输入函数或函数句柄.自变量取值区间,绘制曲线 plotyy 2 双纵坐标图,两个纵坐标的数量级不同 plot3 3 绘制3D曲线 loglog 2 X,

VC 绘图技巧--自定义形状图形

自定义形状图形,定义几个点围城的图形,然后进行描边和填充: [cpp] view plaincopy if (m_memDC.m_hDC!=NULL) { CPoint point[4]; point[0].x=nLeft+(int)(0.1*m_nWidth); point[0].y=m_nYmargin; point[1].x=nLeft+(int)(0.9*m_nWidth); point[1].y=m_nYmargin; point[2].x=nLeft+(int)(0.7*m_nWid