Win32_列表框

一 列表框

  1 列表框的样式
    1.1 单选列表框 默认
    1.2 多选列表框 LBS_MULTIPLESEL

  2 列表框的使用
    2.1 列表框的创建
      窗口类名为 LISTBOX,使用 CreateWindow/Ex
    2.2 增加选项
      LB_ADDSTRING
      LB_INSERTSTRING
    2.3 删除选项
      LB_DELETESTRING 删除指定选项
      LB_RESETCONTENT 清空所有选项
    2.4 选择项的获取和设置
      LB_GETCURSEL 获取被选择项
      LB_SETCURSEL 设置当前被选择项
      LB_GETSEL 获取指定项的选择状态
      LB_SETSEL 设置指定选项的选择状态
      LB_GETSELCOUNT 获取被选择的数量
      LB_GETSELITEMS 获取被选择项索引

    2.5 查找 LB_FINDSTRING

    2.6 获取指定项的文字
      LB_GETEXT 指定项的文字
      LB_GETTEXTLEN 指定项的文字长度

    2.7 选项的数据
      LB_SETITEMDATA 将自定义数据保存到指定的选项当中
      LB_GETITEMDATA 将自定义数据从指定选项中取出

    2.8 目录信息
      LB_DIR 显示指定目录下的文件和子目录信息

    2.9 通知消息
      LBN_SELCHANGE 当 ListBox 中的选择项发生变化时,通知父窗口
    注:对于 ListBox 需要增加 LBS_NOTIFY 的样式才能发送通知消息

View Code
  1 // WinListBox.cpp : Defines the entry point for the application.
  2 //
  3 
  4 #include "stdafx.h"
  5 #include "resource.h"
  6 
  7 HINSTANCE g_hInst   = NULL;
  8 HWND      g_hSingle = NULL;
  9 HWND      g_hMulti  = NULL;
 10 void OnCreate( HWND hWnd, UINT nMsg, 
 11     WPARAM wParam, LPARAM lParam )
 12 {
 13     g_hSingle = CreateWindow( 
 14         "LISTBOX", "SINGLE", 
 15         WS_CHILD|WS_VISIBLE|LBS_NOTIFY,
 16         50, 50, 200, 400, hWnd,
 17         (HMENU)1001, g_hInst, NULL );
 18     g_hMulti = CreateWindow( 
 19         "LISTBOX", "MULTI", 
 20         WS_CHILD|WS_VISIBLE|LBS_MULTIPLESEL|LBS_NOTIFY,
 21         300, 50, 200, 400, hWnd,
 22         (HMENU)1002, g_hInst, NULL );
 23 }
 24 
 25 void OnAdd( )
 26 {
 27     CHAR szText[] = "ListBox 0";
 28     for( int nIndex=0; nIndex<10; nIndex++ )
 29     {    //添加项
 30         LRESULT nItem = 
 31             SendMessage( g_hSingle, LB_ADDSTRING,
 32             0, (LPARAM)szText );
 33         SendMessage( g_hMulti,  LB_ADDSTRING,
 34             0, (LPARAM)szText );
 35         szText[8]++;
 36     }
 37 }
 38 
 39 void OnSelect( )
 40 {    //设置当前选择项
 41     SendMessage( g_hSingle, LB_SETCURSEL, 2, 0 );
 42     //设置指定项的选择状态
 43     for( int nIndex=0; nIndex<10; nIndex++ )
 44     {
 45         SendMessage( g_hMulti, LB_SETSEL, 
 46             TRUE, nIndex );
 47     }
 48     //获取所有选择项的数量
 49     LRESULT nCount = SendMessage( g_hMulti,
 50         LB_GETSELCOUNT, 0, 0 );
 51     if( nCount == 10 )
 52     {
 53         MessageBox( NULL, "10", "10", MB_OK );
 54     }
 55     //获取选择项的索引号
 56     INT nSel[20] = { 0 };
 57     SendMessage( g_hMulti, LB_GETSELITEMS,
 58         20, (LPARAM)nSel );
 59 }
 60 
 61 void OnCommand( HWND hWnd, UINT nMsg, 
 62     WPARAM wParam, LPARAM lParam )
 63 {
 64     int nNotifyCode = HIWORD( wParam );
 65     int nCtrlID = LOWORD( wParam );
 66     switch( nCtrlID )
 67     {
 68     case ID_ADD:
 69         OnAdd( );
 70         break;
 71     case ID_SELECT:
 72         OnSelect( );
 73         break;
 74     case 1001:
 75         {
 76             switch( nNotifyCode )
 77             {
 78             case LBN_SELCHANGE:
 79                 MessageBox( NULL, "LBN_SELCHANGE",
 80                     "ListBox", MB_OK );
 81                 break;
 82             }
 83         }
 84         break;
 85     }
 86 }
 87 
 88 LRESULT CALLBACK WndProc( HWND   hWnd, 
 89                           UINT   nMsg,
 90                           WPARAM wParam,
 91                           LPARAM lParam )
 92 {
 93     switch( nMsg )
 94     {
 95     case WM_CREATE:
 96         OnCreate( hWnd, nMsg, wParam, lParam );
 97         break;
 98     case WM_COMMAND:
 99         OnCommand( hWnd, nMsg, wParam, lParam );
100         break;
101     case WM_DESTROY:
102         PostQuitMessage( 0 );
103         return 0;
104     }
105     return DefWindowProc( hWnd, nMsg,
106         wParam, lParam );
107 }
108 
109 BOOL RegisterWnd( LPSTR pszClassName )
110 {
111     WNDCLASSEX wce = { 0 };
112     wce.cbSize        = sizeof( wce );
113     wce.cbClsExtra    = 0;
114     wce.cbWndExtra    = 0;
115     wce.hbrBackground = HBRUSH(COLOR_BTNFACE+1);
116     wce.hCursor       = NULL;
117     wce.hIcon         = NULL;
118     wce.hIconSm       = NULL;
119     wce.hInstance     = g_hInst;
120     wce.lpfnWndProc   = WndProc;
121     wce.lpszClassName = pszClassName;
122     wce.lpszMenuName  = NULL;
123     wce.style         = CS_HREDRAW|CS_VREDRAW;
124 
125     ATOM nAtom = RegisterClassEx( &wce );
126     if( 0 ==  nAtom )
127     {
128         return FALSE;
129     }
130 
131     return TRUE;
132 }
133 
134 HWND CreateWnd( LPSTR pszClassName )
135 {
136     HMENU hMenu = LoadMenu( g_hInst, 
137         MAKEINTRESOURCE(IDR_MAIN) );
138     HWND hWnd = CreateWindowEx( 0,
139         pszClassName, "MyWnd", 
140         WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
141         CW_USEDEFAULT, CW_USEDEFAULT,
142         CW_USEDEFAULT, NULL, hMenu, g_hInst,
143         NULL );
144     return hWnd;
145 }
146 
147 void DisplayWnd( HWND hWnd )
148 {
149     ShowWindow( hWnd, SW_SHOW );
150     UpdateWindow( hWnd );
151 }
152 
153 void Message( )
154 {
155     MSG msg = { 0 };
156     while( GetMessage( &msg, NULL, 0, 0 ) )
157     {
158         TranslateMessage( &msg );
159         DispatchMessage( &msg );
160     }
161 }
162 
163 int APIENTRY WinMain(HINSTANCE hInstance,
164                      HINSTANCE hPrevInstance,
165                      LPSTR     lpCmdLine,
166                      int       nCmdShow)
167 {
168     g_hInst = hInstance;
169     RegisterWnd( "MYWND" );
170     HWND hWnd = CreateWnd( "MYWND" );
171     DisplayWnd( hWnd );
172     Message( );
173     return 0;
174 }

 

posted @ 2012-10-12 16:44  吟唱黑暗  阅读(731)  评论(0编辑  收藏  举报