#include <windows.h> #include <WinUser.h> #include <CommCtrl.h> #include "controlid.h" #include "globallists.h" #include <cwchar> #include <vector> //---------------------------------------------------- //BS_PUSHBUTTON //---------------------------------------------------- void CreatePushButton(const HWND &ParentHwnd) { CreateWindow( L"Button", L"文本", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 10,10,90,30, ParentHwnd, (HMENU)PUSHBUTTONID, (HINSTANCE)GetWindowLong(ParentHwnd,GWL_HINSTANCE), NULL); } void HandlePushButtonMessage(const HWND &ParentHwnd, const UINT &Message, const WPARAM &WParam, const LPARAM &LParam) { switch(Message) { case WM_COMMAND: switch(LOWORD(WParam)) { case PUSHBUTTONID: switch(HIWORD(WParam)) { case BN_CLICKED: HRESULT messageboxResult=MessageBox(ParentHwnd,L"消息",L"标题",MB_ICONEXCLAMATION|MB_YESNO); switch(messageboxResult) { case IDYES: MessageBox(ParentHwnd,L"点击了\"是\"",L"标题",NULL); break; case IDNO: MessageBox(ParentHwnd,L"点击了\"否\"",L"标题",NULL); break; } break; } } break; } } //---------------------------------------------------- //BS_AUTO3STATE //---------------------------------------------------- void CreateAuto3StateButton(const HWND &ParentHwnd) { CreateWindow( L"Button", L"文本", WS_VISIBLE | WS_CHILD | BS_AUTO3STATE, 10,50,90,30, ParentHwnd, (HMENU)AUTO3STATEBUTTONID, (HINSTANCE)GetWindowLong(ParentHwnd,GWL_HINSTANCE), NULL ); //默认设置为未选定 SendMessage(GetDlgItem(ParentHwnd,AUTO3STATEBUTTONID),BM_SETCHECK,BST_INDETERMINATE,0); } void HandleAuto3StateButtonMessage(const HWND &ParentHwnd, const UINT &Message, const WPARAM &WParam, const LPARAM &LParam) { switch (Message) { case WM_COMMAND: switch(LOWORD(WParam)) { case AUTO3STATEBUTTONID: switch(HIWORD(WParam)) { case BN_CLICKED: UINT state=SendMessage(GetDlgItem(ParentHwnd,AUTO3STATEBUTTONID), BM_GETCHECK ,0,0); switch(state) { case BST_CHECKED: MessageBox(ParentHwnd,L"选择状态",L"标题",NULL); break; case BST_INDETERMINATE: MessageBox(ParentHwnd,L"未定状态",L"标题",NULL); break; case BST_UNCHECKED: MessageBox(ParentHwnd,L"未选择状态",L"标题",NULL); break; } break; } } break; } } //---------------------------------------------------- //BS_AUTOCHECKBOX //---------------------------------------------------- void CreateAutoCheckBox(const HWND &ParentHwnd) { CreateWindow( L"Button", L"文本", WS_VISIBLE | WS_CHILD | BS_AUTOCHECKBOX, 10,90,90,30, ParentHwnd, (HMENU)AUTOCHECKBUTTONID, (HINSTANCE)GetWindowLong(ParentHwnd,GWL_HINSTANCE), NULL ); } void HandleAutoCheckBoxMessage(const HWND &ParentHwnd, const UINT &Message, const WPARAM &WParam, const LPARAM &LParam) { switch(Message) { case WM_COMMAND: switch(LOWORD(WParam)) { case AUTOCHECKBUTTONID: switch(HIWORD(WParam)) { case BN_CLICKED: UINT state=SendMessage(GetDlgItem(ParentHwnd,AUTOCHECKBUTTONID), BM_GETCHECK ,0,0); switch(state) { case BST_CHECKED: MessageBox(ParentHwnd,L"选择状态",L"标题",NULL); break; case BST_UNCHECKED: MessageBox(ParentHwnd,L"未选择状态",L"标题",NULL); break; } break; } } break; } } //---------------------------------------------------- //BS_GROUPBOX BS_AUTORADIOBUTTON //---------------------------------------------------- void CreateRadioButtonGroup(const HWND &ParentHwnd) { CreateWindow( L"Button", L"文本", WS_VISIBLE | WS_CHILD | BS_GROUPBOX, 10,130,80,90, ParentHwnd, (HMENU)GROUPBOXID, (HINSTANCE)GetWindowLong(ParentHwnd,GWL_HINSTANCE), NULL ); CreateWindow( L"Button", L"文本1", WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON|WS_GROUP, 15,150,60,20, ParentHwnd, (HMENU)AUTORADIOBUTTON1ID, (HINSTANCE)GetWindowLong(ParentHwnd,GWL_HINSTANCE), NULL ); CreateWindow( L"Button", L"文本2", WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON, 15,170,60,20, ParentHwnd, (HMENU)AUTORADIOBUTTON2ID, (HINSTANCE)GetWindowLong(ParentHwnd,GWL_HINSTANCE), NULL ); CreateWindow( L"Button", L"文本3", WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON, 15,190,60,20, ParentHwnd, (HMENU)AUTORADIOBUTTON3ID, (HINSTANCE)GetWindowLong(ParentHwnd,GWL_HINSTANCE), NULL ); //设置第二个为默认选中 SendMessage(GetDlgItem(ParentHwnd,AUTORADIOBUTTON2ID),BM_SETCHECK,BST_CHECKED,0); } void HandleRadioButtonGroupMessage(const HWND &ParentHwnd, const UINT &Message, const WPARAM &WParam, const LPARAM &LParam) { UINT buttons[3]={AUTORADIOBUTTON1ID,AUTORADIOBUTTON2ID,AUTORADIOBUTTON3ID}; switch(Message) { case WM_COMMAND: switch(LOWORD(WParam)) { case AUTORADIOBUTTON1ID: case AUTORADIOBUTTON2ID: case AUTORADIOBUTTON3ID: for (INT i=0;i<3;i++) { if (BST_CHECKED==SendMessage(GetDlgItem(ParentHwnd,buttons[i]), BM_GETCHECK ,0,0)) { wchar_t buf[30]; std::swprintf(buf,29,L"单选按钮%d",i+1); MessageBox(ParentHwnd,buf,L"标题",NULL); } } break; } break; } } //---------------------------------------------------- //TRACKBAR_CLASS STATIC //---------------------------------------------------- void CreateTrackBar(const HWND &ParentHwnd) { //范围过大的时候可能出现问题 HWND hwndDlg=ParentHwnd; // handle of dialog box (parent window) UINT iMin=-100; // minimum value in trackbar range UINT iMax=20000; // maximum value in trackbar range HWND hwndTrack = CreateWindow( TRACKBAR_CLASS, // class name L"Trackbar Control", // title (caption) WS_CHILD | WS_VISIBLE | TBS_TOOLTIPS| TBS_AUTOTICKS, // style 30, 230, // position 200, 40, // size hwndDlg, // parent window (HMENU)TRACKBARID, // control identifier (HINSTANCE)GetWindowLong(ParentHwnd,GWL_HINSTANCE), // instance NULL // no WM_CREATE parameter ); SendMessage(hwndTrack, TBM_SETRANGE, (WPARAM) TRUE, // redraw flag (LPARAM) MAKELONG(iMin, iMax)); // min. & max. positions SendMessage(hwndTrack, TBM_SETPAGESIZE, 0, (LPARAM) 4); // new page size SendMessage(hwndTrack, TBM_SETSEL, (WPARAM) FALSE, // redraw flag (LPARAM) MAKELONG(iMin, iMax)); SendMessage(hwndTrack, TBM_SETPOS, (WPARAM) TRUE, // redraw flag (LPARAM) (long)((iMax-iMin)/2)); //创建一个静态文本,显示当前值 CreateWindow( L"STATIC", L"文本", WS_VISIBLE | WS_CHILD | SS_LEFT, 30,280,150,20, ParentHwnd, (HMENU)TRACKBARSTATICID, (HINSTANCE)GetWindowLong(ParentHwnd,GWL_HINSTANCE), NULL ); int pos=SendMessage(hwndTrack, TBM_GETPOS,0,0); wchar_t buf[30]; std::swprintf(buf,29,L"当前值为:%d",pos); SetWindowText(GetDlgItem(ParentHwnd,TRACKBARSTATICID),buf); SetFocus(hwndTrack); } void HandleTrackBarMessage(const HWND &ParentHwnd, const UINT &Message, const WPARAM &WParam, const LPARAM &LParam) { switch(Message) { //本滑动条是横向的 case WM_HSCROLL: //拖拽的同时,同步更新变量,更新频率会很高 if ((HWND)LParam==GetDlgItem(ParentHwnd,TRACKBARID)) { int pos=SendMessage((HWND)LParam, TBM_GETPOS,0,0); wchar_t buf[30]; std::swprintf(buf,29,L"当前值为:%d",pos); SetWindowText(GetDlgItem(ParentHwnd,TRACKBARSTATICID),buf); SetFocus((HWND)LParam); } } } //---------------------------------------------------- //WC_COMBOBOXEX //---------------------------------------------------- void CreateComboBox(const HWND &ParentHwnd) { // Create the Combobox // // Uses the CreateWindow function to create a child window of // the application window. The WC_COMBOBOX window style specifies // that it is a combobox. int xpos = 10; // Horizontal position of the window. int ypos = 320; // Vertical position of the window. int nwidth = 200; // Width of the window int nheight = 20; // Height of the window HWND hwndParent = ParentHwnd; // Handle to the parent window HWND hWndComboBox = CreateWindow(WC_COMBOBOX, TEXT(""), CBS_DROPDOWNLIST | CBS_HASSTRINGS | WS_CHILD | WS_OVERLAPPED | WS_VISIBLE, xpos, ypos, nwidth, nheight, hwndParent, (HMENU)COMBOBOXID, (HINSTANCE)GetWindowLong(ParentHwnd, GWL_HINSTANCE), NULL); // load the combobox with item list. // Send a CB_ADDSTRING message to load each item TCHAR Planets[9][10] = { TEXT("Mercury"), TEXT("Venus"), TEXT("Terra"), TEXT("Mars"), TEXT("Jupiter"), TEXT("Saturn"), TEXT("Uranus"), TEXT("Neptune"), TEXT("Pluto??") }; TCHAR A[16]; int k = 0; memset(&A, 0, sizeof(A)); for (k = 0; k <= 8; k += 1) { wcscpy_s(A, sizeof(A) / sizeof(TCHAR), (TCHAR*)Planets[k]); // Add string to combobox. SendMessage(hWndComboBox, (UINT)CB_ADDSTRING, (WPARAM)0, (LPARAM)A); } // Send the CB_SETCURSEL message to display an initial item // in the selection field SendMessage(hWndComboBox, CB_SETCURSEL, (WPARAM)3, (LPARAM)0); } void HandleComboBoxMessage(const HWND &ParentHwnd, const UINT &Message, const WPARAM &WParam, const LPARAM &LParam) { switch (Message) { case WM_COMMAND: //注意!当使用 SendMessage(hWndComboBox, CB_SETCURSEL, (WPARAM)3, (LPARAM)0); //改变当前选定值时,不会产生CBN_SELCHANGE消息。 if (HIWORD(WParam)== CBN_SELCHANGE && LOWORD(WParam)== COMBOBOXID) { // If the user makes a selection from the list: // Send CB_GETCURSEL message to get the index of the selected list item. // Send CB_GETLBTEXT message to get the item. // Display the item in a messagebox. int ItemIndex = SendMessage((HWND)LParam, (UINT)CB_GETCURSEL,(WPARAM)0, (LPARAM)0); TCHAR ListItem[256]; (TCHAR)SendMessage((HWND)LParam, (UINT)CB_GETLBTEXT,(WPARAM)ItemIndex, (LPARAM)ListItem); MessageBox(ParentHwnd, (LPCWSTR)ListItem, TEXT("Item Selected"), MB_OK); } default: break; } } //---------------------------------------------------- //WC_LISTBOX //---------------------------------------------------- void CreateListBoxSingle(const HWND &ParentHwnd) { //创建 HWND hwndTrack = CreateWindow( WC_LISTBOX, // class name L"List Box", // title (caption) WS_CHILD | WS_VISIBLE | LBS_STANDARD | LBS_NOTIFY, // style 10, 350, // position 200, 100, // size ParentHwnd, // parent window (HMENU)LISTBOXID, // control identifier (HINSTANCE)GetWindowLong(ParentHwnd, GWL_HINSTANCE), // instance NULL // no WM_CREATE parameter ); //初始化信息 typedef struct { TCHAR achName[MAX_PATH]; TCHAR achPosition[12]; int nGamesPlayed; int nGoalsScored; } Player; Player Roster[] = { { TEXT("Haas, Jonathan"), TEXT("Midfield"), 18, 4 }, { TEXT("Pai, Jyothi"), TEXT("Forward"), 36, 12 }, { TEXT("Hanif, Kerim"), TEXT("Back"), 26, 0 }, { TEXT("Anderberg, Michael"), TEXT("Back"), 24, 2 }, { TEXT("Jelitto, Jacek"), TEXT("Midfield"), 26, 3 }, { TEXT("Raposo, Rui"), TEXT("Back"), 24, 3 }, { TEXT("Joseph, Brad"), TEXT("Forward"), 13, 3 }, { TEXT("Bouchard, Thomas"), TEXT("Forward"), 28, 5 }, { TEXT("Salmre, Ivo "), TEXT("Midfield"), 27, 7 }, { TEXT("Camp, David"), TEXT("Midfield"), 22, 3 }, { TEXT("Kohl, Franz"), TEXT("Goalkeeper"), 17, 0 }, }; // Add items to list. HWND hwndList = GetDlgItem(ParentHwnd, LISTBOXID); for (int i = 0; i < ARRAYSIZE(Roster); i++) { int pos = (int)SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)Roster[i].achName); // Set the array index of the player as item data. // This enables us to retrieve the item from the array // even after the items are sorted by the list box. SendMessage(hwndList, LB_SETITEMDATA, pos, (LPARAM)i); } SendMessage(hwndList, LB_SETCURSEL, 2, 0); } void HandleListBoxSingleMessage(const HWND &ParentHwnd, const UINT &Message, const WPARAM &WParam, const LPARAM &LParam) { switch (Message) { case WM_COMMAND: if (HIWORD(WParam) == LBN_SELCHANGE && LOWORD(WParam) == LISTBOXID) { int ItemIndex = SendMessage((HWND)LParam, (UINT)LB_GETSEL, (WPARAM)0, (LPARAM)0); TCHAR ListItem[256]; (TCHAR)SendMessage((HWND)LParam, (UINT)LB_GETTEXT, (WPARAM)ItemIndex, (LPARAM)ListItem); MessageBox(ParentHwnd, (LPCWSTR)ListItem, TEXT("Item Selected"), MB_OK); } default: break; } } //---------------------------------------------------- //Edit //---------------------------------------------------- void CreateEdit(const HWND &ParentHwnd) { //创建 HWND hwndedit = CreateWindow( L"EDIT", // class name L"edit", // title (caption) WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL, // style 10, 460, // position 200,150, // size ParentHwnd, // parent window (HMENU)EDITID, // control identifier (HINSTANCE)GetWindowLong(ParentHwnd, GWL_HINSTANCE), // instance NULL // no WM_CREATE parameter ); } void HandleEditMessage(const HWND &ParentHwnd, const UINT &Message, const WPARAM &WParam, const LPARAM &LParam) { switch (Message) { case WM_COMMAND: if (HIWORD(WParam) == EN_CHANGE && LOWORD(WParam) == EDITID) { int length =GetWindowTextLength((HWND)LParam); std::wstring buf; buf.resize(length); GetWindowText((HWND)LParam,(LPWSTR)buf.c_str(),length); MessageBox(ParentHwnd, buf.c_str(), TEXT("Item Selected"), MB_OK); } default: break; } }
时间: 2024-11-08 05:02:01