• 14 Windows编程——SetWindowLong


    使用默认窗口处理函数,源码

      1 #include<Windows.h>
      2 #include<Windowsx.h>
      3 
      4 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
      5 LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
      6 
      7 int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow)
      8 {
      9     WNDCLASS WndClass;
     10     TCHAR* ClassName = TEXT("MyClass");
     11     HWND hwnd;
     12     MSG msg;
     13     HBRUSH hBrush = CreateSolidBrush(RGB(200, 200, 200));
     14 
     15     WndClass.cbClsExtra = 0;
     16     WndClass.cbWndExtra = 0;
     17     WndClass.hbrBackground = hBrush;
     18     WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
     19     WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
     20     WndClass.hInstance = hInst;
     21     WndClass.lpfnWndProc = WindProc;
     22     WndClass.lpszClassName = ClassName;
     23     WndClass.lpszMenuName = NULL;
     24     WndClass.style = CS_VREDRAW | CS_HREDRAW;
     25 
     26     if (!RegisterClass(&WndClass))
     27     {
     28         MessageBox(NULL, TEXT("Gegister Class Fail!!"), TEXT("error"), MB_OK);
     29         return 0;
     30     }
     31 
     32     //CreateWindow返回之前,会发送WM_CREATE消息
     33     hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 300, NULL, NULL, hInst, NULL);
     34     if (hwnd == NULL)
     35     {
     36         MessageBox(NULL, TEXT("Create Window Fail!!"), TEXT("error"), MB_OK);
     37         return 0;
     38     }
     39     ShowWindow(hwnd, nShow);
     40     UpdateWindow(hwnd);
     41 
     42     while (GetMessage(&msg, NULL, 0, 0))
     43     {
     44         TranslateMessage(&msg);
     45         DispatchMessage(&msg);
     46     }
     47 
     48     return 0;
     49 }
     50 
     51 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
     52 {
     53     PAINTSTRUCT pt;
     54     static int cx, cy;
     55     HDC hdc;
     56     HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 240));
     57     switch (message)
     58     {
     59     case WM_CREATE:
     60         //SetWindowLong(hwnd, GWL_WNDPROC, (LONG)MyProc);
     61         return 0;
     62     case WM_SIZE:
     63         cx = LOWORD(lParam);
     64         cy = HIWORD(lParam);
     65         return 0;
     66     case WM_PAINT:
     67         hdc = BeginPaint(hwnd, &pt);
     68         HBRUSH hBrush = CreateSolidBrush(RGB(0,255,0));
     69         SelectObject(hdc, hBrush);
     70         Rectangle(hdc, 0, 0, cx/2, cy/2);
     71         DeleteObject(hBrush);
     72         EndPaint(hwnd, &pt);
     73         return 0;
     74     case WM_DESTROY:
     75         PostQuitMessage(0);
     76         return 0;
     77     default:
     78         break;
     79     }
     80 
     81     return DefWindowProc(hwnd, message, wParam, lParam);
     82 }
     83 
     84 LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
     85 {
     86     PAINTSTRUCT ps;
     87     HDC hdc;
     88     static int cx, cy;
     89 
     90     switch (message)
     91     {
     92     case WM_SIZE:
     93         cx = LOWORD(lParam);
     94         cy = HIWORD(lParam);
     95         return 0;
     96     case WM_PAINT:
     97         hdc = BeginPaint(hwnd, &ps);
     98     
     99         EndPaint(hwnd, &ps);
    100         return 0;
    101     default:
    102         break;
    103     }
    104     return DefWindowProc(hwnd, message, wParam, lParam);
    105 }
    View Code

    运行结果(只能使用Alt+F4关闭):

    使用SetWindowLong替换窗户口处理函数,源码

      1 #include<Windows.h>
      2 #include<Windowsx.h>
      3 
      4 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
      5 LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
      6 
      7 int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow)
      8 {
      9     WNDCLASS WndClass;
     10     TCHAR* ClassName = TEXT("MyClass");
     11     HWND hwnd;
     12     MSG msg;
     13     HBRUSH hBrush = CreateSolidBrush(RGB(200, 200, 200));
     14 
     15     WndClass.cbClsExtra = 0;
     16     WndClass.cbWndExtra = 0;
     17     WndClass.hbrBackground = hBrush;
     18     WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
     19     WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
     20     WndClass.hInstance = hInst;
     21     WndClass.lpfnWndProc = WindProc;
     22     WndClass.lpszClassName = ClassName;
     23     WndClass.lpszMenuName = NULL;
     24     WndClass.style = CS_VREDRAW | CS_HREDRAW;
     25 
     26     if (!RegisterClass(&WndClass))
     27     {
     28         MessageBox(NULL, TEXT("Gegister Class Fail!!"), TEXT("error"), MB_OK);
     29         return 0;
     30     }
     31 
     32     //CreateWindow返回之前,会发送WM_CREATE消息
     33     hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 300, NULL, NULL, hInst, NULL);
     34     if (hwnd == NULL)
     35     {
     36         MessageBox(NULL, TEXT("Create Window Fail!!"), TEXT("error"), MB_OK);
     37         return 0;
     38     }
     39     ShowWindow(hwnd, nShow);
     40     UpdateWindow(hwnd);
     41 
     42     while (GetMessage(&msg, NULL, 0, 0))
     43     {
     44         TranslateMessage(&msg);
     45         DispatchMessage(&msg);
     46     }
     47 
     48     return 0;
     49 }
     50 
     51 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
     52 {
     53     PAINTSTRUCT pt;
     54     static int cx, cy;
     55     HDC hdc;
     56     HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 240));
     57     switch (message)
     58     {
     59     case WM_CREATE:
     60         SetWindowLong(hwnd, GWL_WNDPROC, (LONG)MyProc);
     61         return 0;
     62     case WM_SIZE:
     63         cx = LOWORD(lParam);
     64         cy = HIWORD(lParam);
     65         return 0;
     66     case WM_PAINT:
     67         hdc = BeginPaint(hwnd, &pt);
     68         HBRUSH hBrush = CreateSolidBrush(RGB(0,255,0));
     69         SelectObject(hdc, hBrush);
     70         Rectangle(hdc, 0, 0, cx/2, cy/2);
     71         DeleteObject(hBrush);
     72         EndPaint(hwnd, &pt);
     73         return 0;
     74     case WM_DESTROY:
     75         PostQuitMessage(0);
     76         return 0;
     77     default:
     78         break;
     79     }
     80 
     81     return DefWindowProc(hwnd, message, wParam, lParam);
     82 }
     83 
     84 LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
     85 {
     86     PAINTSTRUCT ps;
     87     HDC hdc;
     88     static int cx, cy;
     89 
     90     switch (message)
     91     {
     92     case WM_SIZE:
     93         cx = LOWORD(lParam);
     94         cy = HIWORD(lParam);
     95         return 0;
     96     case WM_PAINT:
     97         hdc = BeginPaint(hwnd, &ps);
     98         HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 0));
     99         SelectObject(hdc, hBrush);
    100         Rectangle(hdc, cx / 2, cy / 2, cx, cy);
    101         DeleteObject(hBrush);
    102         EndPaint(hwnd, &ps);
    103         return 0;
    104     default:
    105         break;
    106     }
    107     return DefWindowProc(hwnd, message, wParam, lParam);
    108 }
    View Code

    运行结果:

    使用SetWindowLong接管原来窗口处理函数后,把WM_PAINT消息先在MyProc处理一次 ,然后交给原来的窗口处理函数再处理一次。但是发现并没有绘制出原来窗口处理函数绘制的图形

      1 #include<Windows.h>
      2 #include<Windowsx.h>
      3 
      4 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
      5 LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
      6 
      7 int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow)
      8 {
      9     WNDCLASS WndClass;
     10     TCHAR* ClassName = TEXT("MyClass");
     11     HWND hwnd;
     12     MSG msg;
     13     HBRUSH hBrush = CreateSolidBrush(RGB(200, 200, 200));
     14 
     15     WndClass.cbClsExtra = 0;
     16     WndClass.cbWndExtra = 0;
     17     WndClass.hbrBackground = hBrush;
     18     WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
     19     WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
     20     WndClass.hInstance = hInst;
     21     WndClass.lpfnWndProc = WindProc;
     22     WndClass.lpszClassName = ClassName;
     23     WndClass.lpszMenuName = NULL;
     24     WndClass.style = CS_VREDRAW | CS_HREDRAW;
     25 
     26     if (!RegisterClass(&WndClass))
     27     {
     28         MessageBox(NULL, TEXT("Gegister Class Fail!!"), TEXT("error"), MB_OK);
     29         return 0;
     30     }
     31 
     32     //CreateWindow返回之前,会发送WM_CREATE消息
     33     hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 300, NULL, NULL, hInst, NULL);
     34     if (hwnd == NULL)
     35     {
     36         MessageBox(NULL, TEXT("Create Window Fail!!"), TEXT("error"), MB_OK);
     37         return 0;
     38     }
     39     ShowWindow(hwnd, nShow);
     40     UpdateWindow(hwnd);
     41 
     42     while (GetMessage(&msg, NULL, 0, 0))
     43     {
     44         TranslateMessage(&msg);
     45         DispatchMessage(&msg);
     46     }
     47 
     48     return 0;
     49 }
     50 
     51 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
     52 {
     53     PAINTSTRUCT pt;
     54     static int cx, cy;
     55     HDC hdc;
     56     HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 240));
     57     switch (message)
     58     {
     59     case WM_CREATE:
     60         SetWindowLong(hwnd, GWL_WNDPROC, (LONG)MyProc);
     61         return 0;
     62     case WM_SIZE:
     63         cx = LOWORD(lParam);
     64         cy = HIWORD(lParam);
     65         return 0;
     66     case WM_PAINT:
     67         hdc = BeginPaint(hwnd, &pt);
     68         HBRUSH hBrush = CreateSolidBrush(RGB(0,255,0));
     69         SelectObject(hdc, hBrush);
     70         Rectangle(hdc, 0, 0, cx/2, cy/2);
     71         DeleteObject(hBrush);
     72         EndPaint(hwnd, &pt);
     73         return 0;
     74     case WM_DESTROY:
     75         PostQuitMessage(0);
     76         return 0;
     77     default:
     78         break;
     79     }
     80 
     81     return DefWindowProc(hwnd, message, wParam, lParam);
     82 }
     83 
     84 LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
     85 {
     86     PAINTSTRUCT ps;
     87     HDC hdc;
     88     static int cx, cy;
     89 
     90     switch (message)
     91     {
     92     case WM_SIZE:
     93         cx = LOWORD(lParam);
     94         cy = HIWORD(lParam);
     95         return 0;
     96     case WM_PAINT:
     97         hdc = BeginPaint(hwnd, &ps);
     98         HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 0));
     99         SelectObject(hdc, hBrush);
    100         Rectangle(hdc, cx / 2, cy / 2, cx, cy);
    101         DeleteObject(hBrush);
    102         EndPaint(hwnd, &ps);
    103         //return 0;
    104         break;
    105     default:
    106         break;
    107     }
    108     return WindProc(hwnd, message, wParam, lParam);
    109 }
    View Code

    经调试,发现WindProc函数中WM_PAINT消息,cx=0,cy=0。绘制了,只不过绘制的矩形坐标时( 0 0 0 0),修改如下

    方式①  源码

      1 #include<Windows.h>
      2 #include<Windowsx.h>
      3 
      4 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
      5 LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
      6 
      7 int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow)
      8 {
      9     WNDCLASS WndClass;
     10     TCHAR* ClassName = TEXT("MyClass");
     11     HWND hwnd;
     12     MSG msg;
     13     HBRUSH hBrush = CreateSolidBrush(RGB(200, 200, 200));
     14 
     15     WndClass.cbClsExtra = 0;
     16     WndClass.cbWndExtra = 0;
     17     WndClass.hbrBackground = hBrush;
     18     WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
     19     WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
     20     WndClass.hInstance = hInst;
     21     WndClass.lpfnWndProc = WindProc;
     22     WndClass.lpszClassName = ClassName;
     23     WndClass.lpszMenuName = NULL;
     24     WndClass.style = CS_VREDRAW | CS_HREDRAW;
     25 
     26     if (!RegisterClass(&WndClass))
     27     {
     28         MessageBox(NULL, TEXT("Gegister Class Fail!!"), TEXT("error"), MB_OK);
     29         return 0;
     30     }
     31 
     32     //CreateWindow返回之前,会发送WM_CREATE消息
     33     hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 300, NULL, NULL, hInst, NULL);
     34     if (hwnd == NULL)
     35     {
     36         MessageBox(NULL, TEXT("Create Window Fail!!"), TEXT("error"), MB_OK);
     37         return 0;
     38     }
     39     ShowWindow(hwnd, nShow);
     40     UpdateWindow(hwnd);
     41 
     42     while (GetMessage(&msg, NULL, 0, 0))
     43     {
     44         TranslateMessage(&msg);
     45         DispatchMessage(&msg);
     46     }
     47 
     48     return 0;
     49 }
     50 
     51 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
     52 {
     53     PAINTSTRUCT pt;
     54     static int cx, cy;
     55     HDC hdc;
     56     HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 240));
     57     switch (message)
     58     {
     59     case WM_CREATE:
     60         SetWindowLong(hwnd, GWL_WNDPROC, (LONG)MyProc);
     61         return 0;
     62     case WM_SIZE:
     63         cx = LOWORD(lParam);
     64         cy = HIWORD(lParam);
     65         return 0;
     66     case WM_PAINT:
     67         InvalidateRect(hwnd, NULL, FALSE);
     68         hdc = BeginPaint(hwnd, &pt);
     69         HBRUSH hBrush = CreateSolidBrush(RGB(0, 255, 0));
     70         SelectObject(hdc, hBrush);
     71         Rectangle(hdc, 0, 0, cx / 2, cy / 2);
     72         DeleteObject(hBrush);
     73         EndPaint(hwnd, &pt);
     74         return 0;
     75     case WM_DESTROY:
     76         PostQuitMessage(0);
     77         return 0;
     78     default:
     79         break;
     80     }
     81 
     82     return DefWindowProc(hwnd, message, wParam, lParam);
     83 }
     84 
     85 LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
     86 {
     87     PAINTSTRUCT ps;
     88     HDC hdc;
     89     static int cx, cy;
     90 
     91     switch (message)
     92     {
     93     case WM_SIZE:
     94         cx = LOWORD(lParam);
     95         cy = HIWORD(lParam);
     96         break;
     97     case WM_PAINT:
     98         hdc = BeginPaint(hwnd, &ps);
     99         HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 0));
    100         SelectObject(hdc, hBrush);
    101         Rectangle(hdc, cx / 2, cy / 2, cx, cy);
    102         DeleteObject(hBrush);
    103         EndPaint(hwnd, &ps);
    104         break;
    105     default:
    106         break;
    107     }
    108     return WindProc(hwnd, message, wParam, lParam);
    109 }
    View Code

    方式② 源码

      1 #include<Windows.h>
      2 #include<Windowsx.h>
      3 
      4 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
      5 LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
      6 
      7 int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow)
      8 {
      9     WNDCLASS WndClass;
     10     TCHAR* ClassName = TEXT("MyClass");
     11     HWND hwnd;
     12     MSG msg;
     13     HBRUSH hBrush = CreateSolidBrush(RGB(200, 200, 200));
     14 
     15     WndClass.cbClsExtra = 0;
     16     WndClass.cbWndExtra = 0;
     17     WndClass.hbrBackground = hBrush;
     18     WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
     19     WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
     20     WndClass.hInstance = hInst;
     21     WndClass.lpfnWndProc = WindProc;
     22     WndClass.lpszClassName = ClassName;
     23     WndClass.lpszMenuName = NULL;
     24     WndClass.style = CS_VREDRAW | CS_HREDRAW;
     25 
     26     if (!RegisterClass(&WndClass))
     27     {
     28         MessageBox(NULL, TEXT("Gegister Class Fail!!"), TEXT("error"), MB_OK);
     29         return 0;
     30     }
     31 
     32     //CreateWindow返回之前,会发送WM_CREATE消息
     33     hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 300, NULL, NULL, hInst, NULL);
     34     if (hwnd == NULL)
     35     {
     36         MessageBox(NULL, TEXT("Create Window Fail!!"), TEXT("error"), MB_OK);
     37         return 0;
     38     }
     39     ShowWindow(hwnd, nShow);
     40     UpdateWindow(hwnd);
     41 
     42     while (GetMessage(&msg, NULL, 0, 0))
     43     {
     44         TranslateMessage(&msg);
     45         DispatchMessage(&msg);
     46     }
     47 
     48     return 0;
     49 }
     50 
     51 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
     52 {
     53     PAINTSTRUCT pt;
     54     static int cx, cy;
     55     HDC hdc;
     56     RECT rect;
     57     HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 240));
     58     switch (message)
     59     {
     60     case WM_CREATE:
     61         SetWindowLong(hwnd, GWL_WNDPROC, (LONG)MyProc);
     62         return 0;
     63     case WM_SIZE:
     64         cx = LOWORD(lParam);
     65         cy = HIWORD(lParam);
     66         return 0;
     67     case WM_PAINT:
     68         rect.left = 0;
     69         rect.bottom = cy / 2;
     70         rect.right = cx / 2;
     71         rect.top = 0;
     72         InvalidateRect(hwnd, &rect, TRUE);
     73         hdc = BeginPaint(hwnd, &pt);
     74         HBRUSH hBrush = CreateSolidBrush(RGB(0,255,0));
     75         SelectObject(hdc, hBrush);
     76         Rectangle(hdc, 0, 0, cx/2, cy/2);
     77         DeleteObject(hBrush);
     78         EndPaint(hwnd, &pt);
     79         return 0;
     80     case WM_DESTROY:
     81         PostQuitMessage(0);
     82         return 0;
     83     default:
     84         break;
     85     }
     86 
     87     return DefWindowProc(hwnd, message, wParam, lParam);
     88 }
     89 
     90 LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
     91 {
     92     PAINTSTRUCT ps;
     93     HDC hdc;
     94     static int cx, cy;
     95 
     96     switch (message)
     97     {
     98     case WM_SIZE:
     99         cx = LOWORD(lParam);
    100         cy = HIWORD(lParam);
    101         break;
    102     case WM_PAINT:
    103         hdc = BeginPaint(hwnd, &ps);
    104         HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 0));
    105         SelectObject(hdc, hBrush);
    106         Rectangle(hdc, cx / 2, cy / 2, cx, cy);
    107         DeleteObject(hBrush);
    108         EndPaint(hwnd, &ps);
    109         break;
    110     default:
    111         break;
    112     }
    113     return WindProc(hwnd, message, wParam, lParam);
    114 }
    View Code

    运行结果:

    借助SetWindowLong创建白板窗口

    源码

      1 #include<Windows.h>
      2 #include<Windowsx.h>
      3 
      4 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
      5 LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
      6 
      7 int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow)
      8 {
      9     WNDCLASS WndClass;
     10     TCHAR* ClassName = TEXT("MyClass");
     11     HWND hwnd;
     12     MSG msg;
     13     HBRUSH hBrush = CreateSolidBrush(RGB(200, 200, 200));
     14 
     15     WndClass.cbClsExtra = 0;
     16     WndClass.cbWndExtra = 0;
     17     WndClass.hbrBackground = hBrush;
     18     WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
     19     WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
     20     WndClass.hInstance = hInst;
     21     WndClass.lpfnWndProc = WindProc;
     22     WndClass.lpszClassName = ClassName;
     23     WndClass.lpszMenuName = NULL;
     24     WndClass.style = CS_VREDRAW | CS_HREDRAW;
     25 
     26     if (!RegisterClass(&WndClass))
     27     {
     28         MessageBox(NULL, TEXT("Gegister Class Fail!!"), TEXT("error"), MB_OK);
     29         return 0;
     30     }
     31 
     32     //CreateWindow返回之前,会发送WM_CREATE消息
     33     hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 300, NULL, NULL, hInst, NULL);
     34     if (hwnd == NULL)
     35     {
     36         MessageBox(NULL, TEXT("Create Window Fail!!"), TEXT("error"), MB_OK);
     37         return 0;
     38     }
     39     ShowWindow(hwnd, nShow);
     40     UpdateWindow(hwnd);
     41 
     42     while (GetMessage(&msg, NULL, 0, 0))
     43     {
     44         TranslateMessage(&msg);
     45         DispatchMessage(&msg);
     46     }
     47 
     48     return 0;
     49 }
     50 
     51 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
     52 {
     53     PAINTSTRUCT pt;
     54     static int cx, cy;
     55     HDC hdc;
     56     RECT rect;
     57     HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 240));
     58     switch (message)
     59     {
     60     case WM_CREATE:
     61         SetWindowLong(hwnd, GWL_STYLE, (LONG)WS_POPUP);
     62         return 0;
     63     case WM_SIZE:
     64         cx = LOWORD(lParam);
     65         cy = HIWORD(lParam);
     66         return 0;
     67     case WM_PAINT:
     68         rect.left = 0;
     69         rect.bottom = cy / 2;
     70         rect.right = cx / 2;
     71         rect.top = 0;
     72         InvalidateRect(hwnd, &rect, TRUE);
     73         hdc = BeginPaint(hwnd, &pt);
     74         HBRUSH hBrush = CreateSolidBrush(RGB(0,255,0));
     75         SelectObject(hdc, hBrush);
     76         Rectangle(hdc, 0, 0, cx/2, cy/2);
     77         DeleteObject(hBrush);
     78         EndPaint(hwnd, &pt);
     79         return 0;
     80     case WM_DESTROY:
     81         PostQuitMessage(0);
     82         return 0;
     83     default:
     84         break;
     85     }
     86 
     87     return DefWindowProc(hwnd, message, wParam, lParam);
     88 }
     89 
     90 LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
     91 {
     92     PAINTSTRUCT ps;
     93     HDC hdc;
     94     static int cx, cy;
     95 
     96     switch (message)
     97     {
     98     case WM_SIZE:
     99         cx = LOWORD(lParam);
    100         cy = HIWORD(lParam);
    101         break;
    102     case WM_PAINT:
    103         hdc = BeginPaint(hwnd, &ps);
    104         HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 0));
    105         SelectObject(hdc, hBrush);
    106         Rectangle(hdc, cx / 2, cy / 2, cx, cy);
    107         DeleteObject(hBrush);
    108         EndPaint(hwnd, &ps);
    109         break;
    110     default:
    111         break;
    112     }
    113     return WindProc(hwnd, message, wParam, lParam);
    114 }
    View Code

    运行结果

  • 相关阅读:
    Worker Threads in C#
    Opera和各种下载工具的右键整合
    两种不用的电动车刹车装置价格竟差了一倍
    SQL Server中CONVERT  将日期格式化
    javascript获得当前文档的相对路径
    arcgis sever9.3 for flex API
    常用ArcGIS Server for java网址
    点线面查询闪烁
    arcmap 中建鹰眼
    将一个包含有exe运行文件的文件夹压缩成exe文件
  • 原文地址:https://www.cnblogs.com/kelamoyujuzhen/p/9340539.html
Copyright © 2020-2023  润新知