Win32_组合框

一 组合框

  1 组合框的样式
    1.1 简单组合框 CBS_SIMPLE
    1.2 下拉式组合框 CBS_DROPDOWN 能输入或者从下拉列表中选择
    1.3 下拉列表式组合框 CBS_DROPDOWNLIST 只能从下拉列表中选择

  2 组合框的使用
    2.1 组合框的创建
      窗口类名称是 COMBOBX,使用 CreateWindow 创建,创建时要注意
      窗口高度(弹出的列表)
    2.2 增加选项
      CB_ADDSTRING / CB_INSERTSTRING
      CB_ADDSTRING : WPARAM 为0不使用
                LPARAM 是添加字符串的地址
    2.3 删除选项
      CB_DELETESTRING 删除指定选项
      CB_RESETCONTENT 清空所有选项

    2.4 选择和查找
      CB_GETCURSEL 获取被选择项
      CB_SETCURSEL 设置当前的选择项
      CB_SELECTSTRING 设置选择指定的字符串所对应项

      CB_FINDSTRING 根据指定的字符串,找到对应项的索引,
      大小写不敏感;字符串和选项从左至右匹配;选项包含字符串即可。

    2.5 获取选项或被选择字符串
      CB_GETLBTEXT 获取选项的字符
      WM_GETTEX  获取被选择到窗口字符串

    2.6 选项的数据
      可以将自定义数据信息,在选项中设置和获取
      CB_SETITEMDATA 将数据保存到选项中
      CB_GETITEMDATA 从选项当中获取数据

    2.7 目录显示
      CB_DIR 将指定路径下的文件和路径信息显示在ComboBox当中
      wParam - 显示的类型
      lParam - 路径字符串地址

    2.8 通知消息
      通过 WM_COMMAND 消息,通知父窗口
      CBN_SELCHANGE 当窗口的选项发生变化情况下,通知父窗口

View Code
  1 // WinDlg.cpp : Defines the entry point for the application.
  2 //
  3 
  4 #include "stdafx.h"
  5 #include "resource.h"
  6 #include "stdio.h"
  7 
  8 HINSTANCE g_hInst = NULL;
  9 
 10 /*************************************************
 11 * 函数:OnCreate() 
 12 * 功能:添加组合框
 13 **************************************************/
 14 
 15 void OnCreate(HWND hWnd,UINT nMsg,
 16               WPARAM wParam,LPARAM lParam)
 17 {
 18     //简单组合框的创建
 19     CreateWindow("COMBOBOX","SIMPLE",WS_CHILD | WS_VISIBLE |
 20         CBS_SIMPLE,50,50,100,300,hWnd,(HMENU)1001,g_hInst,NULL);
 21     
 22     //下拉式组合框的创建
 23     CreateWindow("COMBOBOX","DROPDOWD",WS_CHILD | WS_VISIBLE | WS_VSCROLL |
 24         CBS_DROPDOWN,200,50,100,300,hWnd,(HMENU)1002,g_hInst,NULL);
 25     //下拉列表式组合框的创建
 26     CreateWindow("COMBOBOX","DROPDOWNLIST",WS_CHILD | WS_VISIBLE | WS_VSCROLL |
 27         CBS_DROPDOWNLIST,350,50,100,300,hWnd,(HMENU)1003,g_hInst,NULL);
 28 }
 29 
 30 /*************************************************
 31 * 函数:OnAdd ()
 32 * 功能:添加选项
 33 **************************************************/
 34 
 35 void OnAdd(HWND hWnd)
 36 {
 37     
 38     HWND hSimple = GetDlgItem(hWnd,1001);//获取COMBOBOX窗口句柄
 39     HWND hDropDown = GetDlgItem(hWnd,1002);
 40     HWND hDropList = GetDlgItem(hWnd,1003);
 41     CHAR szText[] = "Hello  0";
 42     for(int nIndex = 0;nIndex <10;nIndex++)//添加选项
 43     {
 44         LRESULT nItem= SendMessage(hSimple,CB_ADDSTRING,0,(LPARAM)szText);
 45         //设置项的数据
 46         SendMessage(hSimple,CB_SETITEMDATA,nItem,100 + nIndex);
 47 
 48         SendMessage(hDropDown,CB_ADDSTRING,0,(LPARAM)szText);
 49         SendMessage(hDropList,CB_ADDSTRING,0,(LPARAM)szText);
 50         szText[7]++;
 51     }
 52 }
 53 
 54 /*************************************************
 55 * 函数:OnDel()
 56 * 功能:删除指定选项
 57 **************************************************/
 58 
 59 void OnDel(HWND hWnd)
 60 {
 61     //获取 COMBOBOX 窗口句柄
 62     HWND hSimple   = GetDlgItem(hWnd,1001);
 63     HWND hDropDowm = GetDlgItem(hWnd,1002);
 64     HWND hDropList = GetDlgItem(hWnd,1003);
 65     //删除指定项
 66     SendMessage(hSimple,CB_DELETESTRING,5,0);
 67     SendMessage(hDropDowm,CB_DELETESTRING,5,0);
 68     SendMessage(hDropList,CB_DELETESTRING,5,0);
 69 }
 70 
 71 /*************************************************
 72 * 函数:OnReset() 
 73 * 功能:清空选项
 74 **************************************************/
 75 
 76 void OnReset(HWND hWnd)
 77 {
 78     //获取 COMBOBOX 窗口句柄
 79     HWND hSimple   = GetDlgItem(hWnd,1001);
 80     HWND hDropDowm = GetDlgItem(hWnd,1002);
 81     HWND hDropList = GetDlgItem(hWnd,1003);
 82     //清空所有项
 83     SendMessage(hSimple,CB_RESETCONTENT,0,0);
 84     SendMessage(hDropDowm,CB_RESETCONTENT,0,0);
 85     SendMessage(hDropList,CB_RESETCONTENT,0,0);
 86 }
 87 /*************************************************
 88 * 函数:OnSelect() 
 89 * 功能:设置选择项
 90 **************************************************/
 91 
 92 void OnSelect(HWND hWnd)
 93 {
 94     //获取 COMBOBOX 窗口句柄
 95     HWND hSimple   = GetDlgItem(hWnd,1001);
 96     HWND hDropDowm = GetDlgItem(hWnd,1002);
 97     HWND hDropList = GetDlgItem(hWnd,1003);
 98     //获取和设置选项
 99     LRESULT nSel = SendMessage(hSimple,CB_GETCURSEL,0,0);
100     //获取数据
101     LRESULT nValue = SendMessage(hSimple,CB_GETITEMDATA,nSel,0);
102     CHAR szInfo[260] = {0};
103     sprintf(szInfo,"%d",nValue);
104     MessageBox(NULL,szInfo,"WinCombo",MB_OK);
105 
106     //设置选择项
107     SendMessage(hDropDowm,CB_SETCURSEL,nSel,0);
108     CHAR szText[] = "Hello  2";
109     SendMessage(hDropList,CB_SELECTSTRING,-1,(LPARAM)szText);
110 }
111 /*************************************************
112 * 函数:OnFind() 
113 * 功能:查找
114 **************************************************/
115 
116 void OnFind(HWND hWnd)
117 {
118     //获取 COMBOBOX 窗口句柄
119     HWND hSimple   = GetDlgItem(hWnd,1001);
120     HWND hDropDowm = GetDlgItem(hWnd,1002);
121     HWND hDropList = GetDlgItem(hWnd,1003);
122     
123     //查找字符串
124     CHAR szText[] = "Hello  3";
125     LRESULT nFind1 = SendMessage(hSimple,CB_FINDSTRING,
126         -1,(LPARAM)szText);
127     SendMessage(hDropDowm,CB_SETCURSEL,nFind1,0);
128     SendMessage(hDropList,CB_SETCURSEL,nFind1,0);
129 }
130 /*************************************************
131 * 函数:OnPath() 
132 * 功能:显示目录
133 **************************************************/
134 
135 void OnPath(HWND hWnd)
136 {
137     //获取 COMBOBOX 窗口句柄
138     HWND hSimple   = GetDlgItem(hWnd,1001);
139 
140     //显示目录
141     CHAR szPath[MAX_PATH] = "C:\\*.*";
142     SendMessage(hSimple,CB_DIR,DDL_DIRECTORY | DDL_ARCHIVE,
143         (LPARAM)szPath);
144 }
145 
146 void OnCommand(HWND hWnd,UINT nMsg,
147                WPARAM wParam,LPARAM lParam)
148 {
149     int nNotifyCode = HIWORD(wParam);
150     int nEventID = LOWORD(wParam);
151     switch(nEventID)
152     {
153     case ID_ADD:
154         OnAdd(hWnd);
155         break;
156     case ID_DEL:
157         OnDel(hWnd);
158         break;
159     case ID_RESET:
160         OnReset(hWnd);
161         break;
162     case ID_SEL:
163         OnSelect(hWnd);
164         break;
165     case ID_FIND:
166         OnFind(hWnd);
167         break;
168     case ID_PATH:
169         OnPath(hWnd);
170         break;
171     case 1001:
172         {
173             HWND hSimple   = GetDlgItem(hWnd,1001);
174             HWND hDropDowm = GetDlgItem(hWnd,1002);
175             HWND hDropList = GetDlgItem(hWnd,1003);
176             LRESULT nSel   = 0;
177         switch(nNotifyCode)
178         {
179         case CBN_SELCHANGE:
180             nSel = SendMessage(hSimple,CB_GETCURSEL,0,0);
181             SendMessage(hDropDowm,CB_SETCURSEL,nSel,0);
182             SendMessage(hDropList,CB_SETCURSEL,nSel,0);
183             break;
184         }
185         }
186         break;
187     }
188 }
189 
190 LRESULT CALLBACK WndProc(HWND hWnd,
191                          UINT nMsg,
192                          WPARAM wParam,
193                          LPARAM lParam)
194 {
195     switch(nMsg)
196     {
197     case WM_CREATE:
198         OnCreate(hWnd,nMsg,wParam,lParam);
199         break;
200     case WM_COMMAND:
201         OnCommand(hWnd,nMsg,wParam,lParam);
202         break;
203     case WM_DESTROY:
204         PostQuitMessage(0);
205         return 0;
206     }
207     return DefWindowProc(hWnd,nMsg,wParam,lParam);
208 }
209 
210 BOOL RegisterWnd(LPSTR pszClassName)
211 {
212     WNDCLASSEX wce = {0};
213     wce.style    = CS_HREDRAW | CS_VREDRAW;
214     wce.cbSize    = sizeof(wce);
215     wce.cbClsExtra = 0;
216     wce.cbWndExtra = 0;
217     wce.hbrBackground = HBRUSH(COLOR_BTNFACE + 1);
218     wce.hCursor    = NULL;
219     wce.hIcon    = NULL;
220     wce.hIconSm    = NULL;
221     wce.hInstance    = g_hInst;
222     wce.lpfnWndProc = WndProc;
223     wce.lpszClassName = pszClassName;
224     wce.lpszMenuName    = NULL;
225     
226     ATOM nAtom = RegisterClassEx(&wce);
227     if (0 == nAtom)
228     {
229         return FALSE;
230     }
231     return TRUE;
232 }
233 
234 HWND CreateWnd(LPSTR pszClassName)
235 {
236     HMENU hMenu = LoadMenu(g_hInst,MAKEINTRESOURCE(IDR_MAIN));
237     HWND hWnd = CreateWindowEx(0,pszClassName,"MyWnd",
238         WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,
239         CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,hMenu,g_hInst,NULL);
240     return hWnd;
241 }
242 
243 void DisplayWnd(HWND hWnd)
244 {
245     ShowWindow(hWnd,SW_SHOW);
246     UpdateWindow(hWnd);
247 }
248 
249 void Message()
250 {
251     MSG msg = {0};
252     while(GetMessage(&msg,NULL,0,0))
253     {
254         TranslateMessage(&msg);
255         DispatchMessage(&msg);
256     }
257 }
258 
259 int APIENTRY WinMain(HINSTANCE hInstance,
260                      HINSTANCE hPrevInstance,
261                      LPSTR     lpCmdLine,
262                      int       nCmdShow)
263 {
264     // TODO: Place code here.
265     g_hInst = hInstance;
266     RegisterWnd("MYWND");
267     HWND hWnd = CreateWnd("MYWND");
268     DisplayWnd(hWnd);
269     Message();
270     
271     return 0;
272 }

 

转载于:https://www.cnblogs.com/wjay/archive/2012/10/12/2721077.html

  • 0
    点赞
  • 2
    收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值