1.使用画刷
(1)HBRUSH brush_1 = NULL;
brush_1 = GetStockObject(LTGRAY_BRUSH);
(2)创建纯色画刷
函数原型:HBRUSH CreateSolidBrush(COLORREF crColor);//brush color
(3)将画刷选定到图象设备描述表中
HBRUSH old_brush = NULL;
old_brush = SelectObject(hdc,green_brush);
(4)删除画刷
DeleteObject(green_brush);
(5)创建一个带阴影线的画刷:
函数原型HBRUSH CreateHatchBrush(int fnStyle,
COLORREF clrref)
2直接绘制点
(1)函数原型
COLORREF SetPixel(HDC hdc,
int x,
int y,
COLORREF crColor);
该函数在窗口中使用HDC以及(x,y)坐标和颜色,然后直接绘制上该象素点,并返回实际绘制的颜色。
(2)在绘制时,请注意应该用GetWindowDc()而不是GetDc,因为前者仅在用户区检索,而后者在整个窗口检索.
例子:
int x = rand()%1024;
int y = rand()%768;
SetPixel(hdc,x,y,RGB(rand()%255,rand()%255,rand()%255));
(以上为一个循环)
3绘制线条
(1)GDi绘制线条一般分为三个步骤:
a.创建画笔,并在图形设备描述表中选定画笔。所有线条都将使用该画笔来绘制
b.设定该线条的初试位置。
c.从起使位置到终点位置绘制线条(该终点为下一段线条的初试位置)
d.如果想绘制更多的线条,重复步骤3
(2)设定初试位置的函数
a.原型:BOOL MoveToEx(HDC hdc,
int x,
int y,
LPPOINT lpPoint);
b.MoveToEx(hdc,10,10,NULL);
c.假如要保存最后一个位置:
POINT last_pos;
MoveToEx(hdc,10,10,&last_pos);
(3)一旦设定了线条的初试位置,可以调用LineTo()函数来绘制一端线条:
BOOl LineTo(HDC hdc,
int xEnd,
int yEnd);
4绘制矩形
(1)函数原型:BOOL Rectangle(HDC hdc,
int nLeftRect,
int nTopRect,
int nRightRect,
int nBottomRect)
注意:你要注意一个非常重要的细节。传递到Rectangle()函数的坐标是该矩形的边界框。这就意味着如果线条样式为NULL的话,将会得到一个实心的矩形,而没有四个边界。
(2)还有两个其他的绘制矩形更专用的函数:FillRect和FrameRect具体查阅MSDN
(3)例子:hdc = GetDC(hwnd);
RECT rect; // used to hold rect info
// create a random rectangle
rect.left = rand()%WINDOW_WIDTH;
rect.top = rand()%WINDOW_HEIGHT;
rect.right = rand()%WINDOW_WIDTH;
rect.bottom = rand()%WINDOW_HEIGHT;
// create a random brush
HBRUSH hbrush = CreateSolidBrush(RGB(rand()%256,rand()%256,rand()%256));
// draw either a filled rect or a wireframe rect
if ((rand()%2)==1)
FillRect(hdc,&rect,hbrush);
else
FrameRect(hdc,&rect,hbrush);
// now delete the brush
DeleteObject(hbrush);
// release the device context
ReleaseDC(hwnd,hdc);
// main game processing goes here
if (KEYDOWN(VK_ESCAPE))
SendMessage(hwnd, WM_CLOSE, 0,0);
} // end while
// return to Windows like this
return(msg.wParam);