C++/MFC project [3] - pixel drawing

1. Three steps to follow when drawing with GDI objects

1. Before drawing starts, create a new GDI object and select it into the current device context, while keeping the pointer to the original GDI object;

2. Use the new GDI object drawing;

3. After the drawing is finished, use the saved pointer of the original GDI object to restore the device context to its original state.

2. The main drawing tools provided by GDI

1. CGdiObject: Provides the base class of various Windows GDI drawing tools;

2. CBitmap: Provide bitmap operation interface;

3. CBrush: can be selected as the current brush of the device context;

4. CFont: can be selected as the current font of the device context;

5. CPalette: Provides a color interface between the application and the display;

6. CPen: Can be selected as the current pen of the device context.

3. Draw pixel function

1. SetPixel function

Return value: If the function is called successfully, it returns the RGB value of the drawn pixel; otherwise, it returns -1.

2. SetPixelV function

Return value: return Boolean - if the function call is successful, return "not 0"; otherwise, return "0".

Note: In general, the SetPixelV function is used to draw pixels, because the SetPixel function needs to return RGB values, and the execution speed is slower than the former.

RGB is a method of color representation, and there is a brief introduction in C++/MFC Project [1] - Creating a New Project and Introduction to Commonly Used Classes .

4. Get pixel color function

GetPixel function

Return value: If the function is called successfully, it returns the RGB color value of the specified pixel point; otherwise, it returns "-1".

Five, examples

1. Define P0 point (100,100), and move it to the right by 100 pixels to get P1 point. Draw point P0 as red, obtain the pixel color of this point, and assign the color to point P1.

CPoint P0(100, 100);//定义P0点
CPoint P1(P0.x + 100, P0.y);//右移100个像素得到P1点
pDC->SetPixel(P0, RGB(255, 0, 0));//绘制P0点为红色
COLORREF crColor = pDC->GetPixel(P0);//定义COLORREF型变量,获取P0点像素
pDC->SetPixel(P1, crColor);//颜色赋值给P1点

2. Customize the coordinate system, draw a square on the right side of the Y axis, and fill it with random colors; call the GetPixel function to obtain the coordinates and colors of each pixel in the square, and redraw (Copy) the square on the negative symmetrical position of the X axis point set.

//自定义坐标系
CRect rect;//不是单纯的一个数值
GetClientRect(rect);
pDC->SetMapMode(MM_ANISOTROPIC);//变量是一种映射模式;->:指向成员运算符
pDC->SetWindowExt(rect.Width(), rect.Height());//设置窗口大小(宽度、高度)
pDC->SetViewportExt(rect.Width(), -rect.Height());//设置视区的大小,使Y轴向上
pDC->SetViewportOrg(rect.Width() / 2, rect.Height() / 2);//设置原点位置
rect.OffsetRect(-rect.Width() / 2, -rect.Height() / 2);//偏移函数,向左偏移
//绘制,左下角(50,-50),右上角(150,50)
srand((unsigned)time(NULL));//随机数发生器,使用time函数(秒数)获得一个随机值
COLORREF crColor;
for (int x = 50; x < 150; x++)//横坐标
	for (int y = -50; y < 50; y++)//纵坐标
		pDC->SetPixelV(x, y, RGB(rand() % 255, rand() % 255, rand() % 255));//绘制像素点
for(int x=50;x<150;x++)
	for (int y = -50; y < 50; y++)
	{
		crColor = pDC->GetPixel(x, y);//获取RGB值
		pDC->SetPixelV(-x, y, crColor);//X轴负向对称位置
	}

The running effect is as follows: 

 \Rightarrow  

 It can be seen that the program first draws the square on the right, and the square on the left that is symmetrically scanned and drawn from right to left at a speed visible to the naked eye. So, if the code is changed to the following situation, what is the result?

srand((unsigned)time(NULL));//随机数发生器,使用time函数(秒数)获得一个随机值
COLORREF crColor;
for (int x = 50; x < 150; x++)//横坐标
	for (int y = -50; y < 50; y++)//纵坐标
	{
		pDC->SetPixelV(x, y, RGB(rand() % 255, rand() % 255, rand() % 255));//绘制像素点
		crColor = pDC->GetPixel(x, y);//获取RGB值
		pDC->SetPixelV(-x, y, crColor);//X轴负向对称位置
	}

 \Rightarrow 

 It is easy to observe that at this time, the program draws the right column and the left column (in fact, copying pixel by pixel), which is consistent with the order reflected in the code.

Guess you like

Origin blog.csdn.net/zhou2622/article/details/129801867