BOOL InitWindowsClass(HINSTANCE hInstance)//定义窗口类{
WNDCLASS WndClass;
WndClass.cbClsExtra=0;WndClass.cbWndExtra=0;
WndClass.hbrBackground=(HBRUSH)(GetStockObject(WHITE_BRUSH));WndClass.hCursor=LoadCursor(NULL,IDC_ARROW);WndClass.hIcon=LoadIcon(NULL,\WndClass.hInstance=hInstance;WndClass.lpfnWndProc=WndProc;
WndClass.lpszClassName=\WinFill\WndClass.lpszMenuName=NULL;
WndClass.style=CS_HREDRAW|CS_VREDRAW;return RegisterClass(&WndClass);}
例4-2:应用画笔和画刷等工具绘制如图所示的图形
1… #include
4…long WINAPI WndProc(HWND hWnd,UINT iMessage,UINT wParam,LONG lParam);5… BOOL InitWindowsClass(HINSTANCE hInstance);
6… BOOL InitWindows(HINSTANCE hInstance,int nCmdShow);7… //主函数
8… int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
9… {MSG Message;
10…if(!InitWindowsClass(hInstance))return FALSE;11…if(!InitWindows(hInstance,nCmdShow))return FALSE;12…while(GetMessage(&Message,0,0,0))//消息循环13…{TranslateMessage(&Message);14…DispatchMessage(&Message); }15…return Message.wParam;}
long WINAPI WndProc(HWND hWnd,UINT iMessage,UINT wParam,LONG lParam)1… {HDC hDC; //定义设备环境句柄2… HBRUSH hBrush; //定义画刷的句柄3… HPEN hPen;//定义画笔的句柄4… PAINTSTRUCT PtStr; //定义指向包含绘图信息的结构体变量//定义一个POINT数组,包括6个点
5… POINT points[6]={{100,212},{70,227},{70,250},{130,250},{130,227},{100,212}};6… switch(iMessage) //处理消息7… { case WM_PAINT: //处理绘图消息8… hDC=BeginPaint(hWnd,&PtStr);9…hPen=(HPEN)GetStockObject(NULL_PEN); //获取系统定义的空画笔10…SelectObject(hDC,hPen); //选择画笔11…hBrush=(HBRUSH)GetStockObject(BLACK_BRUSH); //获取系统定义的画刷12…SelectObject(hDC,hBrush); //选择画刷13…LineTo(hDC,50,50); //画线14…DeleteObject(hPen); //删除画笔15…hPen=CreatePen(PS_SOLID,2,RGB(255,0,0)); //创建画笔16…SelectObject(hDC,hPen); //选择画笔
//画一个三角形
17…LineTo(hDC,150,50);18…LineTo(hDC,100,137);19…LineTo(hDC,50,50);