静态子窗口类型
wndclass:static
源码
1 #include<Windows.h> 2 #include<Windowsx.h> 3 4 HINSTANCE G_h; 5 LRESULT CALLBACK WindProc(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 G_h = hInst; 16 WndClass.cbClsExtra = 0; 17 WndClass.cbWndExtra = 0; 18 WndClass.hbrBackground = hBrush; 19 WndClass.hCursor = LoadCursor(NULL, IDC_ARROW); 20 WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); 21 WndClass.hInstance = hInst; 22 WndClass.lpfnWndProc = WindProc; 23 WndClass.lpszClassName = ClassName; 24 WndClass.lpszMenuName = NULL; 25 WndClass.style = CS_VREDRAW | CS_HREDRAW; 26 27 if (!RegisterClass(&WndClass)) 28 { 29 MessageBox(NULL, TEXT("Gegister Class Fail!!"), TEXT("error"), MB_OK); 30 return 0; 31 } 32 33 //CreateWindow返回之前,会发送WM_CREATE消息 34 hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 300, NULL, NULL, hInst, NULL); 35 if (hwnd == NULL) 36 { 37 MessageBox(NULL, TEXT("Create Window Fail!!"), TEXT("error"), MB_OK); 38 return 0; 39 } 40 ShowWindow(hwnd, nShow); 41 UpdateWindow(hwnd); 42 43 while (GetMessage(&msg, NULL, 0, 0)) 44 { 45 TranslateMessage(&msg); 46 DispatchMessage(&msg); 47 } 48 49 return 0; 50 } 51 52 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 53 { 54 PAINTSTRUCT pt; 55 static HWND chwnd; 56 int cx, cy; 57 HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 240)); 58 switch (message) 59 { 60 case WM_CREATE: 61 //法1 获得主程序句柄 62 chwnd = CreateWindow(TEXT("static"), TEXT("中国"), WS_CHILD | WS_VISIBLE|SS_CENTER, 0, 0, 0, 0, hwnd, (HMENU)1, ((LPCREATESTRUCT)lParam)->hInstance, NULL); 63 //法2 获得主程序句柄 64 //hwnd = CreateWindow(TEXT("static"), TEXT("中国"), WS_CHILD | WS_VISIBLE, 0, 0, 50, 50, hwnd, (HMENU)1, G_h, NULL); 65 //发3 获得主程序句柄 66 //hwnd = CreateWindow(TEXT("static"), TEXT("中国"), WS_CHILD | WS_VISIBLE, 0, 0, 50, 50, hwnd, (HMENU)1, GetModuleHandle(NULL), NULL); 67 return 0; 68 case WM_SIZE: 69 cx = LOWORD(lParam); 70 cy = HIWORD(lParam); 71 MoveWindow(chwnd, cx / 20,cy / 20, cx / 2, cy / 10, TRUE); 72 return 0; 73 case WM_CTLCOLORSTATIC: 74 SetTextColor((HDC)wParam, RGB(255, 0, 0)); 75 SetBkMode((HDC)wParam, TRANSPARENT); 76 return (LRESULT)hBrush; 77 case WM_COMMAND: 78 return 0; 79 case WM_DESTROY: 80 PostQuitMessage(0); 81 return 0; 82 default: 83 break; 84 } 85 86 return DefWindowProc(hwnd, message, wParam, lParam); 87 }
替换子窗口窗口处理过程,源码
1 #include<Windows.h> 2 #include<Windowsx.h> 3 //#define _AFXDLL 4 //#include<afx.h> 5 6 HINSTANCE G_h; 7 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); 8 LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); 9 10 int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow) 11 { 12 WNDCLASS WndClass; 13 TCHAR* ClassName = TEXT("MyClass"); 14 HWND hwnd; 15 MSG msg; 16 HBRUSH hBrush = CreateSolidBrush(RGB(200, 200, 200)); 17 18 G_h = hInst; 19 WndClass.cbClsExtra = 0; 20 WndClass.cbWndExtra = 0; 21 WndClass.hbrBackground = hBrush; 22 WndClass.hCursor = LoadCursor(NULL, IDC_ARROW); 23 WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); 24 WndClass.hInstance = hInst; 25 WndClass.lpfnWndProc = WindProc; 26 WndClass.lpszClassName = ClassName; 27 WndClass.lpszMenuName = NULL; 28 WndClass.style = CS_VREDRAW | CS_HREDRAW; 29 30 if (!RegisterClass(&WndClass)) 31 { 32 MessageBox(NULL, TEXT("Gegister Class Fail!!"), TEXT("error"), MB_OK); 33 return 0; 34 } 35 36 //CreateWindow返回之前,会发送WM_CREATE消息 37 hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 300, NULL, NULL, hInst, NULL); 38 if (hwnd == NULL) 39 { 40 MessageBox(NULL, TEXT("Create Window Fail!!"), TEXT("error"), MB_OK); 41 return 0; 42 } 43 ShowWindow(hwnd, nShow); 44 UpdateWindow(hwnd); 45 46 while (GetMessage(&msg, NULL, 0, 0)) 47 { 48 TranslateMessage(&msg); 49 DispatchMessage(&msg); 50 } 51 52 return 0; 53 } 54 55 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 56 { 57 PAINTSTRUCT pt; 58 static HWND chwnd; 59 int cx, cy; 60 HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 240)); 61 switch (message) 62 { 63 case WM_CREATE: 64 //法1 获得主程序句柄 65 chwnd = CreateWindow(TEXT("static"), TEXT("中国"), WS_CHILD | WS_VISIBLE | SS_CENTER, 0, 0, 0, 0, hwnd, (HMENU)1, ((LPCREATESTRUCT)lParam)->hInstance, NULL); 66 //法2 获得主程序句柄 67 //hwnd = CreateWindow(TEXT("static"), TEXT("中国"), WS_CHILD | WS_VISIBLE, 0, 0, 50, 50, hwnd, (HMENU)1, G_h, NULL); 68 //发3 获得主程序句柄 69 //hwnd = CreateWindow(TEXT("static"), TEXT("中国"), WS_CHILD | WS_VISIBLE, 0, 0, 50, 50, hwnd, (HMENU)1, GetModuleHandle(NULL), NULL); 70 //这个函数将窗口chwnd的窗口处理过程改变为MyProc。这个函数的作用,相当于把static窗口类中的lpfnWndProc值设置为MyProc 71 SetWindowLong(chwnd, GWL_WNDPROC, (LONG)MyProc); 72 return 0; 73 case WM_SIZE: 74 cx = LOWORD(lParam); 75 cy = HIWORD(lParam); 76 MoveWindow(chwnd, cx / 20, cy / 20, cx / 2, cy / 10, TRUE); 77 return 0; 78 case WM_CTLCOLORSTATIC: 79 //TRACE("WM_CTLCOLORSTATIC "); 80 SetTextColor((HDC)wParam, RGB(255, 0, 0)); 81 SetBkMode((HDC)wParam, TRANSPARENT); 82 return (LRESULT)hBrush; 83 case WM_COMMAND: 84 return 0; 85 case WM_DESTROY: 86 PostQuitMessage(0); 87 return 0; 88 default: 89 break; 90 } 91 92 return DefWindowProc(hwnd, message, wParam, lParam); 93 } 94 95 LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 96 { 97 //TRACE("MyProc "); 98 PAINTSTRUCT ps; 99 HDC hdc; 100 static int cx, cy; 101 switch (message) 102 { 103 case WM_SIZE: 104 cx = LOWORD(lParam); 105 cy = HIWORD(lParam); 106 return 0; 107 case WM_PAINT: 108 hdc = BeginPaint(hwnd, &ps); 109 Rectangle(hdc, 0, 0, cx, cy); 110 TextOut(hdc, 10, 5, TEXT("child"), 5); 111 EndPaint(hwnd, &ps); 112 return 0; 113 case WM_DESTROY: 114 PostQuitMessage(0); 115 return 0; 116 default: 117 break; 118 } 119 return DefWindowProc(hwnd, message, wParam, lParam); 120 }
拦截窗口处理消息
1 #include<Windows.h> 2 #include<Windowsx.h> 3 //#define _AFXDLL 4 //#include<afx.h> 5 6 DWORD proc; 7 HINSTANCE G_h; 8 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); 9 LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); 10 11 int WinMain(HINSTANCE hInst, HINSTANCE tmp, LPSTR szCmd, int nShow) 12 { 13 WNDCLASS WndClass; 14 TCHAR* ClassName = TEXT("MyClass"); 15 HWND hwnd; 16 MSG msg; 17 HBRUSH hBrush = CreateSolidBrush(RGB(200, 200, 200)); 18 19 G_h = hInst; 20 WndClass.cbClsExtra = 0; 21 WndClass.cbWndExtra = 0; 22 WndClass.hbrBackground = hBrush; 23 WndClass.hCursor = LoadCursor(NULL, IDC_ARROW); 24 WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); 25 WndClass.hInstance = hInst; 26 WndClass.lpfnWndProc = WindProc; 27 WndClass.lpszClassName = ClassName; 28 WndClass.lpszMenuName = NULL; 29 WndClass.style = CS_VREDRAW | CS_HREDRAW; 30 31 if (!RegisterClass(&WndClass)) 32 { 33 MessageBox(NULL, TEXT("Gegister Class Fail!!"), TEXT("error"), MB_OK); 34 return 0; 35 } 36 37 //CreateWindow返回之前,会发送WM_CREATE消息 38 hwnd = CreateWindow(ClassName, TEXT("Hello"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 300, NULL, NULL, hInst, NULL); 39 if (hwnd == NULL) 40 { 41 MessageBox(NULL, TEXT("Create Window Fail!!"), TEXT("error"), MB_OK); 42 return 0; 43 } 44 ShowWindow(hwnd, nShow); 45 UpdateWindow(hwnd); 46 47 while (GetMessage(&msg, NULL, 0, 0)) 48 { 49 TranslateMessage(&msg); 50 DispatchMessage(&msg); 51 } 52 53 return 0; 54 } 55 56 LRESULT CALLBACK WindProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 57 { 58 PAINTSTRUCT pt; 59 static HWND chwnd; 60 int cx, cy; 61 HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 240)); 62 switch (message) 63 { 64 case WM_CREATE: 65 //法1 获得主程序句柄 66 chwnd = CreateWindow(TEXT("static"), TEXT("中国"), WS_CHILD | WS_VISIBLE | SS_CENTER|SS_NOTIFY, 0, 0, 0, 0, hwnd, (HMENU)1, ((LPCREATESTRUCT)lParam)->hInstance, NULL); 67 //法2 获得主程序句柄 68 //hwnd = CreateWindow(TEXT("static"), TEXT("中国"), WS_CHILD | WS_VISIBLE, 0, 0, 50, 50, hwnd, (HMENU)1, G_h, NULL); 69 //发3 获得主程序句柄 70 //hwnd = CreateWindow(TEXT("static"), TEXT("中国"), WS_CHILD | WS_VISIBLE, 0, 0, 50, 50, hwnd, (HMENU)1, GetModuleHandle(NULL), NULL); 71 //这个函数将窗口chwnd的窗口处理过程改变为MyProc。这个函数的作用,相当于把static窗口类中的lpfnWndProc值设置为MyProc 72 proc=SetWindowLong(chwnd, GWL_WNDPROC, (LONG)MyProc); 73 return 0; 74 case WM_SIZE: 75 cx = LOWORD(lParam); 76 cy = HIWORD(lParam); 77 MoveWindow(chwnd, cx / 20, cy / 20, cx / 2, cy / 10, TRUE); 78 return 0; 79 case WM_CTLCOLORSTATIC: 80 //TRACE("WM_CTLCOLORSTATIC "); 81 SetTextColor((HDC)wParam, RGB(255, 0, 0)); 82 SetBkMode((HDC)wParam, TRANSPARENT); 83 return (LRESULT)hBrush; 84 case WM_COMMAND: 85 return 0; 86 case WM_DESTROY: 87 PostQuitMessage(0); 88 return 0; 89 default: 90 break; 91 } 92 93 return DefWindowProc(hwnd, message, wParam, lParam); 94 } 95 96 LRESULT CALLBACK MyProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 97 { 98 //TRACE("MyProc "); 99 PAINTSTRUCT ps; 100 HDC hdc; 101 static int cx, cy; 102 //TRACE("%d %d %d ", message, wParam, lParam); 103 switch (message) 104 { 105 case WM_SIZE: 106 cx = LOWORD(lParam); 107 cy = HIWORD(lParam); 108 //return 0; 109 break; 110 case WM_PAINT: 111 hdc = BeginPaint(hwnd, &ps); 112 Rectangle(hdc, 0, 0, cx, cy); 113 TextOut(hdc, 10, 5, TEXT("child"), 5); 114 EndPaint(hwnd, &ps); 115 //return 0; 116 break; 117 default: 118 break; 119 } 120 return ((LRESULT (CALLBACK*)(HWND , UINT , WPARAM , LPARAM ))proc)(hwnd, message, wParam, lParam); 121 }