17windows_17_listbox列表框

Posted 养老保险年审

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了17windows_17_listbox列表框相关的知识,希望对你有一定的参考价值。

17windows_17_listbox列表框

  1. #include <windows.h>
  2. #include <iostream>
  3. #include "resource.h"
  4. CHAR szText[256] = { 0 };
  5. #define PrintLog(x) WriteConsole(g_hStdout, x, strlen(x), NULL, NULL)
  6. HINSTANCE g_hInst = NULL; //窗口句柄
  7. HANDLE g_hStdout = NULL; //控制台句柄
  8. HWND g_hSingle = NULL;
  9. HWND g_hMulti = NULL;
  10. void OnCreate(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  11. {
  12. g_hSingle = CreateWindow("LISTBOX", "SINGLE", WS_CHILD | WS_VISIBLE|LBS_NOTIFY/*发送LBN_SELCHANG消息需要*/,
  13. 50, 50, 200, 400, hWnd, (HMENU)1001, g_hInst, NULL);
  14. g_hMulti = CreateWindow("LISTBOX", "SINGLE", WS_CHILD | WS_VISIBLE | LBS_MULTIPLESEL|LBS_NOTIFY/*发送LBN_SELCHANG消息需要*/,
  15. 350, 50, 200, 400, hWnd, (HMENU)1002, g_hInst, NULL);
  16. }
  17. void OnAdd()
  18. {
  19. for (int i = 0; i < 10; i++)
  20. {
  21. //添加项
  22. sprintf_s(szText, 256, "LISTBOX %d",i);
  23. SendMessage(g_hSingle, LB_ADDSTRING,
  24. 0, (LPARAM)szText);
  25. SendMessage(g_hMulti, LB_ADDSTRING,
  26. 0, (LPARAM)szText);
  27. }
  28. }
  29. void OnSelect()
  30. {
  31. //设置当前选择项
  32. SendMessage(g_hSingle, LB_SETCURSEL, 2, 0); //和Combo一样
  33. //Multi,设置指定项的选择状态
  34. for (int i = 0; i < 10; i++)
  35. {
  36. SendMessage(g_hMulti, LB_SETSEL, TRUE, i);
  37. }
  38. //获取所有选择项的数量
  39. LRESULT nCount = SendMessage(g_hMulti, LB_GETSELCOUNT, 0, 0);
  40. if (nCount == 10)
  41. {
  42. MessageBox(NULL, "it‘s ten", "OK", MB_OK);
  43. }
  44. //获取选择项的索引号
  45. INT nSel[20] = { 0 };
  46. SendMessage(g_hMulti, LB_GETSELITEMS, 20, (LPARAM)nSel);
  47. }
  48. void OnCommand(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  49. {
  50. int nNotifyCode = HIWORD(wParam);
  51. int nCtrlID = LOWORD(wParam);
  52. switch (nCtrlID)
  53. {
  54. case ID_LISTBOX_ADDITEM:
  55. OnAdd();
  56. break;
  57. case ID_LISTBOX_SELETEITEM:
  58. OnSelect();
  59. break;
  60. case 1001:
  61. switch (nNotifyCode)
  62. {
  63. case LBN_SELCHANGE:
  64. MessageBox(NULL, "LBN_SELCHANG", "LBN", MB_OK);
  65. break;
  66. }
  67. break;
  68. case 1002:
  69. switch (nNotifyCode)
  70. {
  71. case LBN_SELCHANGE:
  72. MessageBox(NULL, "LBN_SELCHANGMulti", "LBNMulti", MB_OK);
  73. break;
  74. }
  75. break;
  76. }
  77. }
  78. LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  79. {
  80. switch (nMsg)
  81. {
  82. //Create WM_CREATE
  83. case WM_CREATE:
  84. OnCreate(hWnd, nMsg, wParam, lParam);
  85. break;
  86. //menu use
  87. case WM_COMMAND:
  88. OnCommand(hWnd, nMsg, wParam, lParam);
  89. break;
  90. case WM_DESTROY:
  91. PostQuitMessage(0);
  92. break;
  93. }
  94. return DefWindowProc(hWnd, nMsg, wParam, lParam);
  95. }
  96. BOOL RegisterWnd(LPSTR pszClassName)
  97. {
  98. WNDCLASSEX wce = { 0 };
  99. wce.cbSize = sizeof(wce);
  100. wce.cbClsExtra = 0;
  101. wce.cbWndExtra = 0;
  102. wce.hbrBackground = HBRUSH(COLOR_BTNFACE + 1);
  103. wce.hCursor = NULL;
  104. wce.hIcon = NULL;
  105. wce.hIconSm = NULL;
  106. wce.hInstance = g_hInst;
  107. wce.lpfnWndProc = WndProc;
  108. wce.lpszClassName = pszClassName;
  109. wce.lpszMenuName = NULL;
  110. wce.style = CS_HREDRAW | CS_VREDRAW;
  111. ATOM atom = RegisterClassEx(&wce);
  112. if (atom == NULL)
  113. {
  114. return FALSE;
  115. }
  116. else
  117. {
  118. return TRUE;
  119. }
  120. }
  121. HWND CreateWnd(LPSTR pszClassName)
  122. {
  123. HMENU hMainMENU = LoadMenu(g_hInst, MAKEINTRESOURCE(IDR_MENU1));
  124. HWND hWnd = CreateWindowEx(0, pszClassName, "灭天", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  125. CW_USEDEFAULT, NULL, hMainMENU, g_hInst, 0);
  126. return hWnd;
  127. }
  128. void ShowWnd(HWND hWnd)
  129. {
  130. ShowWindow(hWnd, SW_SHOW);
  131. UpdateWindow(hWnd);
  132. }
  133. void Msg()
  134. {
  135. MSG msg = { 0 };
  136. while (GetMessage(&msg, NULL, 0, 0))
  137. {
  138. TranslateMessage(&msg);
  139. DispatchMessage(&msg);
  140. }
  141. }
  142. void ConsoleWnd()
  143. {
  144. AllocConsole();
  145. g_hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  146. CHAR szText[] = "Debug start:\n";
  147. WriteConsole(g_hStdout, szText, strlen(szText), NULL, NULL);
  148. }
  149. int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
  150. {
  151. g_hInst = hInstance;
  152. //ConsoleWnd();
  153. RegisterWnd("oooo");
  154. HWND hWnd = CreateWnd("oooo");
  155. ShowWnd(hWnd);
  156. Msg();
  157. return 0;
  158. }





以上是关于17windows_17_listbox列表框的主要内容,如果未能解决你的问题,请参考以下文章

如何在选中复选框时选择列表框中的所有项目?

从列表框中选择,从另一个列表框中选择填充

Windows 窗体同时滚动多个列表框

通过列表框中的某些文本分隔字符串

17_开发弹出提示框

VB.NET 如何将ListBox列表框的所有项复制到一个数组中,便于排序?谢谢解答