矩形应用,画一个3行3列的矩形组合
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <windows.h> LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; #define edge 30 //矩形边长 HBRUSH hbrush[2];//刷子数组 void paint(HDC hdc);//画图声明 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT ("HelloWin") ; HWND hwnd ; MSG msg ; WNDCLASS wndclass ; wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = 0 ; wndclass.hInstance = hInstance ; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = NULL ; wndclass.lpszClassName = szAppName ; if (!RegisterClass (&wndclass))return 0 ; hwnd = CreateWindow (szAppName, TEXT ("一个简单的Win32程序"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ; ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg) ; DispatchMessage (&msg) ; } return msg.wParam ; } LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; switch (message) { case WM_PAINT: hdc=BeginPaint(hwnd,&ps); paint(hdc); EndPaint(hwnd,&ps); return 0; case WM_DESTROY: DeleteObject(hbrush[0]); DeleteObject(hbrush[1]); PostQuitMessage (0) ; //在消息队列中插入一条“退出”消息 return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam);//执行默认的消息处理 } void paint(HDC hdc) { int i,j; hbrush[0]=CreateSolidBrush(RGB(255,0,0)); hbrush[1]=CreateSolidBrush(RGB(255,255,0)); for(j=0;j<3;j++) //行 for(i=0;i<3;i++)//列 { if(j%2==0) { Sleep(50); SelectObject(hdc,hbrush[0]); Rectangle(hdc,i*edge,j*edge,(i+1)*edge,(j+1)*edge); } else { Sleep(100); SelectObject(hdc,hbrush[1]); Rectangle(hdc,i*edge,j*edge,(i+1)*edge,(j+1)*edge); } } }