ComboBoxDemo.cpp
上传用户:laohuji888
上传日期:2021-04-13
资源大小:2075k
文件大小:8k
源码类别:

组合框控件

开发平台:

Visual C++

  1. // ComboBoxDemo.cpp : Defines the entry point for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "ComboBoxDemo.h"
  5. #define MAX_LOADSTRING 100
  6. // Global Variables:
  7. HINSTANCE hInst; // current instance
  8. TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
  9. TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
  10. // Forward declarations of functions included in this code module:
  11. ATOM MyRegisterClass(HINSTANCE hInstance);
  12. BOOL InitInstance(HINSTANCE, int);
  13. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  14. LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
  15. int APIENTRY _tWinMain(HINSTANCE hInstance,
  16.                      HINSTANCE hPrevInstance,
  17.                      LPTSTR    lpCmdLine,
  18.                      int       nCmdShow)
  19. {
  20.   // TODO: Place code here.
  21. MSG msg;
  22. HACCEL hAccelTable;
  23. // Initialize global strings
  24. LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  25. LoadString(hInstance, IDC_COMBOBOXDEMO, szWindowClass, MAX_LOADSTRING);
  26. MyRegisterClass(hInstance);
  27. // Perform application initialization:
  28. if (!InitInstance (hInstance, nCmdShow)) 
  29. {
  30. return FALSE;
  31. }
  32. hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_COMBOBOXDEMO);
  33. // Main message loop:
  34. while (GetMessage(&msg, NULL, 0, 0)) 
  35. {
  36. if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
  37. {
  38. TranslateMessage(&msg);
  39. DispatchMessage(&msg);
  40. }
  41. }
  42. return (int) msg.wParam;
  43. }
  44. //
  45. //  FUNCTION: MyRegisterClass()
  46. //
  47. //  PURPOSE: Registers the window class.
  48. //
  49. //  COMMENTS:
  50. //
  51. //    This function and its usage are only necessary if you want this code
  52. //    to be compatible with Win32 systems prior to the 'RegisterClassEx'
  53. //    function that was added to Windows 95. It is important to call this function
  54. //    so that the application will get 'well formed' small icons associated
  55. //    with it.
  56. //
  57. ATOM MyRegisterClass(HINSTANCE hInstance)
  58. {
  59. WNDCLASSEX wcex;
  60. wcex.cbSize = sizeof(WNDCLASSEX); 
  61. wcex.style = CS_HREDRAW | CS_VREDRAW;
  62. wcex.lpfnWndProc = (WNDPROC)WndProc;
  63. wcex.cbClsExtra = 0;
  64. wcex.cbWndExtra = 0;
  65. wcex.hInstance = hInstance;
  66. wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_COMBOBOXDEMO);
  67. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  68. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  69. wcex.lpszMenuName = (LPCTSTR)IDC_COMBOBOXDEMO;
  70. wcex.lpszClassName = szWindowClass;
  71. wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
  72. return RegisterClassEx(&wcex);
  73. }
  74. HWND hComboBox;
  75. HWND hEditBox;
  76. HWND hEditBoxItem;
  77. //
  78. //   FUNCTION: InitInstance(HANDLE, int)
  79. //
  80. //   PURPOSE: Saves instance handle and creates main window
  81. //
  82. //   COMMENTS:
  83. //
  84. //        In this function, we save the instance handle in a global variable and
  85. //        create and display the main program window.
  86. //
  87. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  88. {
  89.    HWND hWnd;
  90.    hInst = hInstance; // Store instance handle in our global variable
  91.    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  92.       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  93.    if (!hWnd)
  94.    {
  95.       return FALSE;
  96.    }
  97.    //Tao Combox Box 
  98.    hComboBox= CreateWindow(_T("COMBOBOX"),_T(""),WS_CHILD|WS_VISIBLE|CBS_DROPDOWNLIST|CBS_AUTOHSCROLL|CBS_UPPERCASE|CBS_SORT,50,250,100,200,hWnd,(HMENU)10001,hInst,NULL);
  99.    hEditBox= CreateWindow(_T("EDIT"),_T(""),WS_CHILD|WS_VISIBLE|ES_MULTILINE|WS_BORDER|WS_HSCROLL|WS_VSCROLL,50,50,100,200,hWnd,(HMENU)10002,hInst,NULL);
  100.    hEditBoxItem= CreateWindow(_T("EDIT"),_T(""),WS_CHILD|WS_VISIBLE|ES_MULTILINE|WS_BORDER,250,50,200,100,hWnd,(HMENU)10003,hInst,NULL);
  101.    ShowWindow(hWnd, nCmdShow);
  102.    UpdateWindow(hWnd);
  103.    return TRUE;
  104. }
  105. //
  106. //  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
  107. //
  108. //  PURPOSE:  Processes messages for the main window.
  109. //
  110. //  WM_COMMAND - process the application menu
  111. //  WM_PAINT - Paint the main window
  112. //  WM_DESTROY - post a quit message and return
  113. //
  114. //
  115. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  116. {
  117. int wmId, wmEvent;
  118. PAINTSTRUCT ps;
  119. HDC hdc;
  120. switch (message) 
  121. {
  122. case WM_COMMAND:
  123. wmId    = LOWORD(wParam); 
  124. wmEvent = HIWORD(wParam); 
  125. // Parse the menu selections:
  126. switch (wmId)
  127. {
  128. case IDM_ABOUT:
  129. DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
  130. break;
  131. case IDM_EXIT:
  132. DestroyWindow(hWnd);
  133. break;
  134. case 10001:
  135. {
  136. switch (wmEvent)
  137. {
  138. case CBN_CLOSEUP:
  139. {
  140. TCHAR buf[1000];
  141. GetWindowText(hEditBox,buf,1000);
  142. _tcscat(buf,_T("rnclose up"));
  143. SetWindowText(hEditBox,buf);
  144. break;
  145. }
  146. case CBN_DROPDOWN:
  147. {
  148. TCHAR buf[1000];
  149. GetWindowText(hEditBox,buf,1000);
  150. _tcscat(buf,_T("rndropdown"));
  151. SetWindowText(hEditBox,buf);
  152. break;
  153. }
  154. case CBN_EDITCHANGE:
  155. {
  156. TCHAR buf[1000];
  157. GetWindowText(hEditBox,buf,1000);
  158. _tcscat(buf,_T("rneditchange"));
  159. SetWindowText(hEditBox,buf);
  160. break;
  161. }
  162. case CBN_SELCHANGE:
  163. {
  164. TCHAR buf[1000];
  165. GetWindowText(hEditBox,buf,1000);
  166. _tcscat(buf,_T("rnCBN_SELCHANGE"));
  167. SetWindowText(hEditBox,buf);
  168. break;
  169. }
  170. case CBN_SELENDOK:
  171. {
  172. TCHAR buf[1000];
  173. GetWindowText(hEditBox,buf,1000);
  174. _tcscat(buf,_T("rnCBN_SELENDOK"));
  175. SetWindowText(hEditBox,buf);
  176. break;
  177. }
  178. case CBN_SETFOCUS:
  179. {
  180. TCHAR buf[1000];
  181. GetWindowText(hEditBox,buf,1000);
  182. _tcscat(buf,_T("rnCBN_SETFOCUS"));
  183. SetWindowText(hEditBox,buf);
  184. break;
  185. }
  186. case CBN_KILLFOCUS :
  187. {
  188. TCHAR buf[1000];
  189. GetWindowText(hEditBox,buf,1000);
  190. _tcscat(buf,_T("rnCBN_KILLFOCUS"));
  191. SetWindowText(hEditBox,buf);
  192. break;
  193. }
  194. }
  195. break;
  196. }
  197. case ID_DEMO1_ADDSTRING:
  198. {
  199. TCHAR buf [1000];
  200. GetWindowText(hEditBoxItem,buf,1000);
  201. SendMessage(hComboBox,CB_ADDSTRING,0,(LPARAM)buf);
  202. break;
  203. }
  204. case ID_DEMO1_INSERTSTRING:
  205. {
  206. TCHAR buf [1000];
  207. GetWindowText(hEditBoxItem,buf,1000);
  208. SendMessage(hComboBox,CB_INSERTSTRING,1,(LPARAM)buf);
  209. break;
  210. }
  211. case ID_DEMO1_DELETESTRING:
  212. {
  213. TCHAR buf [1000];
  214. GetWindowText(hEditBoxItem,buf,1000);
  215. int i = SendMessage(hComboBox,CB_FINDSTRINGEXACT,0,(LPARAM)buf);
  216. SendMessage(hComboBox,CB_DELETESTRING,(WPARAM)i,0);
  217. break;
  218. }
  219. case ID_DEMO2_CB_FINDSTRING:
  220. {
  221. int vt=SendMessage(hComboBox,CB_FINDSTRING, 0,(LPARAM) _T("A"));
  222. break;
  223. }
  224. case ID_DEMO2_CB_FINDSTRINGEXACT:
  225. {
  226. int vt=SendMessage(hComboBox,CB_FINDSTRINGEXACT, 0,(LPARAM) _T("A"));
  227. break;
  228. }
  229. case ID_DEMO2_CB_SELECTSTRING:
  230. {
  231. break;
  232. }
  233. case ID_DEMO3_CB_GETCURSEL:
  234. {
  235. int curIndex=SendMessage(hComboBox,CB_GETCURSEL,0,0);
  236. break;
  237. }
  238. case ID_DEMO3_CB_SETCURSEL:
  239. {
  240. int result=SendMessage(hComboBox,CB_SETCURSEL,2,0);
  241. break;
  242. }
  243. case ID_DEMO3_CB_SETEDITSEL:
  244. {
  245. SendMessage(hComboBox,CB_SETEDITSEL,(WPARAM) 2,(LPARAM)5);
  246. break;
  247. }
  248. case ID_DEMO3_CB_GETLBTEXT:
  249. {
  250. TCHAR buf [1000];
  251. SendMessage(hComboBox,CB_GETLBTEXT,2,(LPARAM)buf);
  252. break;
  253. }
  254. case ID_DEMO3_CB_RESETCONTENT:
  255. {
  256. TCHAR buf[1000];
  257. SendMessage(hComboBox,CB_SETITEMDATA,1,(LPARAM)_T("Toi di hoc"));
  258. _tcscpy(buf,(TCHAR*)SendMessage(hComboBox,CB_GETITEMDATA,1,0));
  259. break;
  260. }
  261. default:
  262. return DefWindowProc(hWnd, message, wParam, lParam);
  263. }
  264. break;
  265. case WM_PAINT:
  266. hdc = BeginPaint(hWnd, &ps);
  267. // TODO: Add any drawing code here...
  268. EndPaint(hWnd, &ps);
  269. break;
  270. case WM_DESTROY:
  271. PostQuitMessage(0);
  272. break;
  273. default:
  274. return DefWindowProc(hWnd, message, wParam, lParam);
  275. }
  276. return 0;
  277. }
  278. // Message handler for about box.
  279. LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  280. {
  281. switch (message)
  282. {
  283. case WM_INITDIALOG:
  284. return TRUE;
  285. case WM_COMMAND:
  286. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
  287. {
  288. EndDialog(hDlg, LOWORD(wParam));
  289. return TRUE;
  290. }
  291. break;
  292. }
  293. return FALSE;
  294. }