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. }

来自为知笔记(Wiz)

时间: 2024-11-16 14:32:18

17windows_17_listbox列表框的相关文章

Swing列表框组件

public class JListTest extends JFrame{    private static final long serialVersionUID=1L;        public JListTest()    {        Container cp=getContentPane();        cp.setLayout(null);        JList<String> jl=new JList<>(new MyListModel());  

20151214学习内容:Web之列表框

列表框: ListBox 1. 绑定数据 方法一: testDataContext context = new testDataContext(); ListBox1.DataSource = context.Nation; ListBox1.DataValueField = "Code"; ListBox1.DataTextField = "Name"; ListBox1.DataBind(); 绑定数据 方法二: ListItem item = new List

WB 列表框

列表框 HTML代码: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&qu

MVC4.0中下来列表框的,两种使用方法DropDownList

后台控制器代码 public ActionResult Drop() { var list = new List<SchoolInfo>(); list.Add(new SchoolInfo() { SchoolID = 1, SName = "北京大学" }); list.Add(new SchoolInfo() { SchoolID = 2, SName = "上海大学" }); var model = new UserViewInfo(); mod

积累的VC编程小技巧之列表框

1.列表框中标题栏(Column)的添加 创建一个List Control,其ID为IDC_LIST,在其Styles属性项下的View项里选择Report.Align项里选择Top.Sort项里选择None. 然后在该List所在对话框的类(头文件)里创建ClistCtrl的一个对象m_list然后在.cpp文件的OnInitDialog()之类的函数里实现如下代码: CString strname[3]; strname[0]="Screen Name"; strname[1]=&

webform下拉列表、列表框

下拉列表:DropDownList 1.绑定数据: DropDownList1.DataSource = context.Nation; DropDownList1.DataValueField = "Code"; DropDownList1.DataTextField = "Name"; DropDownList1.DataBind(); 2.取选中项的值 DropDownList1.SelectedValue.ToString(); 3.设置哪一项选中 Drop

java界面编程(9) ------ 列表框

本文是自己学习所做笔记,欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020 列表框和JComboBox组合框明显不同,这不仅仅是体现在外观上.当激活JComboBox组合框时,会出现下拉列表:而JList总是在屏幕上占据固定行的空间,大小也不会改变.如果要得到列表框中被选中的项目,只需调用getSelectValues(),它可以产生一个字符串数组,里面是被选中的项目名称. 另外,JList组件也允许多重选择,要是按住ctrl键,连续在多个项目上单击,

文本框和列表框的click事件

#region 文本框和列表框的click事件        private void tbORcbb_click(object sender, EventArgs e)        {            if (typeof(TextBox).IsInstanceOfType(sender))            {                if (!ifSelected)                {                    ((TextBox)sender)

如何解决列表框控件宽度不够的问题

在设计界面的时候,我们经常会用到类似于列表框.组合框之类的控件,他们可以显示一系列的选项.但有一个常见的问题就是:如果列表项的文本长度超过了控件的宽度,那么就只能看到一部分内容.针对这个问题,该如何解决了? 我们分别以windows程序和web程序为例来讲解(前提是,组合框的宽度不允许放大) 下面这个图演示了如果列表项的文本宽度不大于控件宽度时的样子,这是正常的 下面这个图演示了如果列表项的文本宽度大于控件宽度时的样子,这时会有一部分文本是看不到的 那么如何解决这个问题呢?我们一般比较简单的做法