1. 程式人生 > >[VC] 創建子窗口/控件

[VC] 創建子窗口/控件

分享圖片 arrow tor ctr erro wce dex 子窗口 lpar

  1 #include <Windows.h>
  2 #include <tchar.h>
  3 #include <CommCtrl.h>
  4 
  5 
  6 #pragma comment(linker, "\"/manifestdependency:type=‘win32‘   7                         name=Microsoft.Windows.Common-Controls version=6.0.0.0   8                         processorArchitecture=
* publicKeyToken=6595b64144ccf1df language=*\"") 9 10 11 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); 12 void OnCreate(HWND); 13 void OnCommand(HWND, WPARAM); 14 15 16 int APIENTRY _tWinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPTSTR lpCmdLine, int nCmdShow) 17 {
18 MSG msg; 19 WNDCLASSEX wcex; 20 HWND hWnd; 21 22 wcex.cbClsExtra = 0; 23 wcex.cbSize = sizeof(WNDCLASSEX); 24 wcex.cbWndExtra = 0; 25 wcex.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1); 26 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
27 wcex.hIcon = NULL; 28 wcex.hIconSm = NULL; 29 wcex.hInstance = hInst; 30 wcex.lpfnWndProc = WndProc; 31 wcex.lpszClassName = _T("Wnd"); 32 wcex.lpszMenuName = NULL; 33 wcex.style = CS_VREDRAW | CS_HREDRAW; 34 if (!RegisterClassEx(&wcex)) 35 { 36 MessageBox(NULL, _T("RegisterClassEx()"), NULL, MB_ICONERROR); 37 return 0; 38 } 39 40 hWnd = CreateWindowEx(0, _T("Wnd"), _T("Controls"), 41 WS_OVERLAPPEDWINDOW | WS_VISIBLE, 42 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 43 NULL, NULL, hInst, NULL); 44 if (!hWnd) 45 { 46 MessageBox(NULL, _T("CreateWindowEx()"), NULL, MB_ICONERROR); 47 return 0; 48 } 49 50 while (GetMessage(&msg, NULL, 0, 0)) 51 { 52 TranslateMessage(&msg); 53 DispatchMessage(&msg); 54 } 55 56 return 0; 57 } 58 59 LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 60 { 61 switch (uMsg) 62 { 63 case WM_CREATE: 64 OnCreate(hWnd); 65 break; 66 case WM_COMMAND: 67 OnCommand(hWnd, wParam); 68 break; 69 case WM_DESTROY: 70 PostQuitMessage(0); 71 break; 72 default: 73 return DefWindowProc(hWnd, uMsg, wParam, lParam); 74 } 75 76 return 0; 77 } 78 79 void OnCreate(HWND hWnd) 80 { 81 CreateWindowEx(0, WC_BUTTON, _T("Group box button"), 82 WS_CHILD | WS_VISIBLE | BS_GROUPBOX, 83 5, 5, 660, 120, hWnd, NULL, NULL, NULL); 84 CreateWindowEx(0, WC_BUTTON, _T("Push button"), 85 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 86 10, 30, 130, 30, hWnd, (HMENU)101, NULL, NULL); 87 CreateWindowEx(0, WC_BUTTON, _T("Default push button"), 88 WS_CHILD | WS_VISIBLE |BS_DEFPUSHBUTTON, 89 150, 30, 140, 30, hWnd, NULL, NULL, NULL); 90 CreateWindowEx(0, WC_BUTTON, _T("Check box button"), 91 WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX, 92 300, 30, 130, 30, hWnd, NULL, NULL, NULL); 93 CreateWindowEx(0, WC_BUTTON, _T("Radio button"), 94 WS_CHILD | WS_VISIBLE | BS_AUTORADIOBUTTON, 95 440, 30, 100, 30, hWnd, NULL, NULL, NULL); 96 CreateWindowEx(0, WC_BUTTON, _T("3 State button"), 97 WS_CHILD | WS_VISIBLE | BS_AUTO3STATE, 98 550, 30, 110, 30, hWnd, NULL, NULL, NULL); 99 CreateWindowEx(0, WC_BUTTON, _T("Split button"), 100 WS_CHILD | WS_VISIBLE | BS_SPLITBUTTON, 101 10, 70, 120, 30, hWnd, NULL, NULL, NULL); 102 CreateWindowEx(0, WC_BUTTON, _T("Command link button"), 103 WS_CHILD | WS_VISIBLE | BS_COMMANDLINK, 104 140, 70, 200, 50, hWnd, NULL, NULL, NULL); 105 106 CreateWindowEx(0, WC_EDIT, _T("Edit"), 107 WS_CHILD | WS_VISIBLE | ES_LEFT, 108 10, 160, 80, 24, hWnd, NULL, NULL, NULL); 109 CreateWindowEx(0, WC_EDIT, _T("Edit password"), 110 WS_CHILD | WS_VISIBLE | ES_PASSWORD, 111 100, 160, 150, 24, hWnd, NULL, NULL, NULL); 112 CreateWindowEx(0, WC_EDIT, _T("Edit readonly"), 113 WS_CHILD | WS_VISIBLE | ES_READONLY, 114 260, 160, 120, 24, hWnd, NULL, NULL, NULL); 115 } 116 117 void OnCommand(HWND hWnd, WPARAM wParam) 118 { 119 if (LOWORD(wParam) == 101) 120 { 121 MessageBox(hWnd, _T("Push button"), _T("Message"), 0); 122 } 123 }

技術分享圖片

想要查看更多的控件樣式請使用Microsoft Control Spy:

技術分享圖片

下載地址:http://download.microsoft.com/download/a/3/1/a315b133-03a8-4845-b428-ec585369b285/ControlSpy.msi

[VC] 創建子窗口/控件