MiniGUI

CTRL_LISTBOX
 
类型
单选列表框(默认)
多选列表框
高级列表框,含图标、位图或者复选框
 
风格选项
LBS_MULTIPLESEL 多选
LBS_USEICON 使用位图bitmap或图标icon
LBS_CHECKBOX 带复选框
LBS_NOTIFY 产生通知消息
LBS_SORT 排序
WS_BORDER 边框
WS_VSCROLL 垂直滚动条
WS_HSCROLL 水平滚动条
 
消息
1. 添加条目
1) 添加纯字符串的条目
添加字符串到列表框最后一项:SendMessage (hwndList, LB_ADDSTRING, 0, (LPARAM)string);
创建列表框时指定LBS_SORT选项则自动排序
 
添加字符串到列表框指定位置:SendMessage (hwndList, LB_INSERTSTRING, index, (LPARAM)string);
index=-1时,添加到最后一项
创建列表框时指定LBS_SORT选项则自动排序,忽略index
2) 添加含图标的条目
LISTBOXITEMINFO lbii;
 
lbii.hIcon = LoadIconFromFile (HDC_SCREEN, "res.ico", 1); 
lbii.cmFlag = CMFLAG_CHECKED;
lbii.string = "abcdefg";
 
SendMessage (hwndList , LB_ADDSTRING, 0, (LPARAM)&lbii);
3) 添加含位图的条目
LISTBOXITEMINFO lbii;
 
lbii.hIcon = (DWORD) GetSystemBitmap (SYSBMP_MAXIMIZE);
lbii.cmFlag = CMFLAG_CHECKED | IMGFLAG_BITMAP;
lbii.string = "abcdefg";
 
SendMessage (hwndList , LB_ADDSTRING, 0, (LPARAM)&lbii);
4) 添加多个纯字符串条目
int num = 2;
const char text[num][] = {“item1”, “item2”};
 
SendMessage (hwndList, LB_MULTIADDITEM, num, (LPARAM)text);
5) 添加多个含图标、位图或复选框的条目
int num = 2;
LISTBOXITEMINFO lbii[num]; 
 
lbii[0].hIcon = LoadIconFromFile (HDC_SCREEN, "res1.ico", 1);
lbii[0].cmFlag = CMFLAG_CHECKED;
lbii[0].string = "item1";
 
lbii[1].hIcon =LoadIconFromFile (HDC_SCREEN, "res2.ico", 1);
lbii[1].cmFlag = CMFLAG_CHECKED;
lbii[1].string = "item2";
 
SendMessage (hwndList, LB_MULTIADDITEM, num, (LPARAM)lbii);
返回值:
LB_ERRSPACE 内存不足
LB_ERR 其他原因错误
LB_OKAY 成功
 
2. 删除条目
删除指定条目:SendMessage (hwndList, LB_DELETESTRING, index, 0);
清空列表框内容:SendMessage (hwndList, LB_RESETCONTENT, 0, 0);
 
3. 选择和取得条目
获取条目个数:int count = SendMessage (hwndList, LB_GETCOUNT, 0, 0);
获取指定条目字符串长度:int length = SendMessage (hwndList, LB_GETTEXTLEN, index, 0);
获取指定条目字符串: int length = SendMessage (hwndList, LB_GETTEXT, index, (LPARAM)buffer);
设置指定条目字符串:SendMessage (hwndList, LB_SETTEXT, index, buffer);
设置高级列表框指定条目:
LISTBOXITEMINFO lbii;
lbii.hIcon = LoadIconFromFile (HDC_SCREEN, "res.ico", 1);
lbii.cmFlag = CMFLAG_CHECKED;
lbii.string = "set item";
SendMessage (hwndList  , LB_SETITEMDATA, index, (LPARAM)&lbii);
 
获取高级列表框指定条目:
LISTBOXITEMINFO lbii;
SendMessage (hwndList  , LB_GETITEMDATA, index, (LPARAM)&lbii);
单选列表框设置选中条目:SendMessage (hwndList, LB_SETCURSEL, index, 0);
单选列表框获取选中条目的索引项:int index = SendMessage (hwndList, LB_GETCURSEL, 0, 0);
如果没有条目被选中,返回LB_ERR
多选列表框设置指定条目的选中状态:SendMessage (hwndList, LB_SETSEL, wParam, (LPARAM)index);
wParam非0则选择并加亮某一条目,0则取消选中
多选列表框获取指定条目的选中状态:int select = SendMessage (hwndList, LB_GETSEL, index, 0);
select非0则选中,0则没选中
多选列表框获取当前选中的条目个数:int sel_count = SendMessage (hwndList, LB_GETSELCOUNT, 0, 0L);
多选列表框获取当前选中的条目索引:
int* sel_items;
sel_items = alloca (sizeof(int)*sel_count);
SendMessage (hwndList, LB_GETSELITEMS, sel_count, sel_items);
查找含有指定字符串的条目:
模糊匹配:int index = SendMessage (hwndList, LB_FINDSTRING, (LPARAM)string);
精确匹配:int index = SendMessage (hwndList, LB_FINDSTRINGEXACT, (LPARAM)string);
 
4. 设置和获取复选框状态
获取指定条目的复选框状态:int status = SendMessage (hwndList, LB_GETCHECKMARK, index, 0);
返回值:
CMFLAG_CHECKED 选择状态
CMFLAG_PARTCHECKED 部分选择状态
CMFLAG_BLANK 未选择状态
LB_ERR 没有找到指定条目
 
设置指定条目的复选框状态:int ret = SendMessage (hwndList, LB_SETCHECKMARK, index, (LPARAM)status);
返回值:
LB_OKAY 成功
LB_ERR 没有找到指定条目
 
5. 设置指定条目加粗显示
int ret = SendMessage (hwndList, LB_SETITEMBOLD, index, (LPARAM)status) ; 
参数:status为1则加粗,0则正常显示
 
6. 自定义排序方式
static int my_strcmp (const char* s1, const char* s2, size_t n);
SendMessage (hwndList, LB_SETSTRCMPFUNC, 0, (LPARAM)my_strcmp);
 
7. 附加数据
设置附加数据:
int addData
SendMessage(hwndList , LB_SETITEMADDDATA, index , addData);
 
获取附加数据:
int addData
addData = SendMessage(hwndList , LB_GETITEMADDDATA, curSel, 0);
8. 设置和获取条目高度
设置条目高度:SendMessage(hwndList , LB_SETITEMHEIGHT, 0, (LPARAM)height);
获取条目高度:int height = SendMessage(hwndList , LB_GETITEMHEIGHT, 0, 0);
 
通知
1. 具有 LBS_NOTIFY 风格的列表框可能产生的通知消息
LBN_ERRSPACE    内存分配失败。
LBN_SELCHANGE    单项选择列表框的当前选择项发生变化。
LBN_CLICKED    用户在列表框某条目上单击了鼠标左键。
LBN_DBLCLK    用户在列表框某条目上双击了鼠标左键。
LBN_SELCANCEL    用户取消了某个条目的选择。
LBN_SETFOCUS    列表框获得了输入焦点。
LBN_KILLFOCUS    列表框失去了输入焦点。
LBN_CLICKCHECKMARK    用户单击了条目的检查框。
LBN_ENTER    用户在列表框中按下 ENTER 键
2. 如果调用SetNotificationCallback函数设定了列表框控件的通知回调函数,则控件不会向父窗口发送 MSG_COMMAND 通知消息,而是会直接调用设定的通知回调函数
 
 mg-samples的列表框示例
  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <stdarg.h>
  4 #include <string.h>
  5 #include <sys/stat.h>
  6 #include <sys/time.h>
  7 #include <sys/types.h>
  8 #include <unistd.h>
  9 #include <dirent.h>
 10 #include <pwd.h>
 11 #include <errno.h>
 12 
 13 #include <minigui/common.h>
 14 #include <minigui/minigui.h>
 15 #include <minigui/gdi.h>
 16 #include <minigui/window.h>
 17 #include <minigui/control.h>
 18 
 19 #define IDL_DIR    100
 20 #define IDL_FILE   110
 21 #define IDC_PATH   120
 22 
 23 #define     deleting_the_files              "Deleting the files"
 24 #define     directories                     "Directories:"
 25 #define     files_                          "Files:"
 26 #define     path_                           "Path: "
 27 #define     delete_                         "Delete"
 28 #define     cancel_                         "Cancel"
 29 #define     deleting_files_                 "Deleting files"
 30 
 31 static DLGTEMPLATE DlgDelFiles =
 32 {
 33     WS_BORDER | WS_CAPTION,
 34     WS_EX_NONE,
 35     100, 100, 500, 250,
 36     deleting_the_files,
 37     0, 0,
 38     7, NULL,
 39     0
 40 };
 41 
 42 static CTRLDATA CtrlDelFiles[] =
 43 { 
 44     {
 45         CTRL_STATIC,
 46         WS_VISIBLE | SS_SIMPLE, 
 47         10, 10, 130, 15,
 48         IDC_STATIC,
 49         directories,
 50         0
 51     },
 52     {
 53         CTRL_LISTBOX,
 54         WS_VISIBLE | WS_VSCROLL | WS_BORDER | LBS_SORT | LBS_NOTIFY,
 55         10, 30, 130, 100,
 56         IDL_DIR,
 57         "",
 58         0
 59     },
 60     {
 61         CTRL_STATIC,
 62         WS_VISIBLE | SS_SIMPLE, 
 63         150, 10, 130, 15, 
 64         IDC_STATIC, 
 65         files_,
 66         0
 67     },
 68     {
 69         CTRL_LISTBOX,
 70         WS_VISIBLE | WS_VSCROLL | WS_BORDER | LBS_SORT | LBS_AUTOCHECKBOX,
 71         150, 30, 260, 100,
 72         IDL_FILE,
 73         "",
 74         0
 75     },
 76     {
 77         CTRL_STATIC,
 78         WS_VISIBLE | SS_SIMPLE, 
 79         10, 150, 290, 15, 
 80         IDC_PATH, 
 81         path_,
 82         0
 83     },
 84     {
 85         "button",
 86         WS_VISIBLE | BS_DEFPUSHBUTTON | WS_TABSTOP | WS_GROUP,
 87         10, 170, 130, 25,
 88         IDOK, 
 89         delete_,
 90         0
 91     },
 92     {
 93         "button",
 94         WS_VISIBLE | BS_PUSHBUTTON | WS_TABSTOP,
 95         150, 170, 130, 25,
 96         IDCANCEL,
 97         cancel_,
 98         0
 99     },
100 };
101 
102 static void fill_boxes (HWND hDlg, const char* path)
103 {
104 #ifdef __NOUNIX__
105     LISTBOXITEMINFO lbii;
106 
107     lbii.string = "file.1";
108     lbii.cmFlag = CMFLAG_BLANK;
109     lbii.hIcon = 0;
110     SendDlgItemMessage (hDlg, IDL_FILE, LB_ADDSTRING, 0, (LPARAM)&lbii);
111 
112     lbii.string = "file.2";
113     SendDlgItemMessage (hDlg, IDL_FILE, LB_ADDSTRING, 0, (LPARAM)&lbii);
114 
115     lbii.string = "file.3";
116     SendDlgItemMessage (hDlg, IDL_FILE, LB_ADDSTRING, 0, (LPARAM)&lbii);
117 #else
118     struct dirent* dir_ent;
119     DIR*   dir;
120     struct stat ftype;
121     char   fullpath [PATH_MAX + 1];
122 
123     SendDlgItemMessage (hDlg, IDL_DIR, LB_RESETCONTENT, 0, (LPARAM)0);
124     SendDlgItemMessage (hDlg, IDL_FILE, LB_RESETCONTENT, 0, (LPARAM)0);
125     SetWindowText (GetDlgItem (hDlg, IDC_PATH), path);
126     
127     if ((dir = opendir (path)) == NULL)
128          return;
129 
130     while ( (dir_ent = readdir ( dir )) != NULL ) {
131 
132         /* Assemble full path name. */
133         strncpy (fullpath, path, PATH_MAX);
134         strcat (fullpath, "/");
135         strcat (fullpath, dir_ent->d_name);
136         
137         if (stat (fullpath, &ftype) < 0 ) {
138            continue;
139         }
140 
141         if (S_ISDIR (ftype.st_mode))
142             SendDlgItemMessage (hDlg, IDL_DIR, LB_ADDSTRING, 0, (LPARAM)dir_ent->d_name);
143         else if (S_ISREG (ftype.st_mode)) {
144             LISTBOXITEMINFO lbii;
145 
146             lbii.string = dir_ent->d_name;
147             lbii.cmFlag = CMFLAG_BLANK;
148             lbii.hIcon = 0;
149             SendDlgItemMessage (hDlg, IDL_FILE, LB_ADDSTRING, 0, (LPARAM)&lbii);
150         }
151     }
152 
153     closedir (dir);
154 #endif
155 }
156 
157 static void dir_notif_proc (HWND hwnd, int id, int nc, DWORD add_data)
158 {
159     if (nc == LBN_DBLCLK || nc == LBN_ENTER) {
160         int cur_sel = SendMessage (hwnd, LB_GETCURSEL, 0, 0L);
161         if (cur_sel >= 0) {
162             char cwd [MAX_PATH + 1];
163             char dir [MAX_NAME + 1];
164             GetWindowText (GetDlgItem (GetParent (hwnd), IDC_PATH), cwd, MAX_PATH);
165             SendMessage (hwnd, LB_GETTEXT, cur_sel, (LPARAM)dir);
166 
167             if (strcmp (dir, ".") == 0)
168                 return;
169 
170             if (strcmp (dir, "..") == 0) {
171                 char* slash;
172 
173                 if (strcmp (cwd, "/") == 0)
174                     return;
175 
176                 slash = strrchr (cwd, '/');
177                 if (slash == NULL)
178                     return;
179                 if (slash == cwd)
180                     strcpy (cwd, "/");
181                 else
182                     *slash = '';
183             }
184             else {
185                 if (strcmp (cwd, "/") != 0)
186                     strcat (cwd, "/");
187                 strcat (cwd, dir);
188             }
189 
190             fill_boxes (GetParent (hwnd), cwd);
191         }
192     }
193 }
194 
195 static void file_notif_proc (HWND hwnd, int id, int nc, DWORD add_data)
196 {
197     /* Do nothing */
198 }
199 
200 static void prompt (HWND hDlg)
201 {
202     int i;
203 #ifdef _LANG_ZHCN
204     char files [1024] = "你选择要删除的文件是:
";
205 #else
206     char files [1024] = "The files followed will be deleted
";
207 #endif
208     for (i = 0; i < SendDlgItemMessage (hDlg, IDL_FILE, LB_GETCOUNT, 0, 0L); i++) {
209     char file [MAX_NAME + 1];
210         int status = SendDlgItemMessage (hDlg, IDL_FILE, LB_GETCHECKMARK, i, 0);
211         if (status == CMFLAG_CHECKED) {
212             SendDlgItemMessage (hDlg, IDL_FILE, LB_GETTEXT, i, (LPARAM)file);
213         strcat (files, file);
214         strcat (files, "
");
215     }
216     }
217 
218     MessageBox (hDlg, files, deleting_files_, MB_OK | MB_ICONINFORMATION);
219 
220 }
221 
222 static int DelFilesBoxProc (HWND hDlg, int message, WPARAM wParam, LPARAM lParam)
223 {
224     switch (message) {
225     case MSG_INITDIALOG:
226     {
227         char cwd [MAX_PATH + 1];
228         SetNotificationCallback (GetDlgItem (hDlg, IDL_DIR), dir_notif_proc);
229         SetNotificationCallback (GetDlgItem (hDlg, IDL_FILE), file_notif_proc);
230         fill_boxes (hDlg, getcwd (cwd, MAX_PATH));
231         return 1;
232     }
233         
234     case MSG_COMMAND:
235         switch (wParam) {
236         case IDOK:
237             prompt (hDlg);
238         case IDCANCEL:
239             EndDialog (hDlg, wParam);
240             break;
241         }
242         break;
243         
244     }
245     
246     return DefaultDialogProc (hDlg, message, wParam, lParam);
247 }
248 
249 int MiniGUIMain (int argc, const char* argv[])
250 {
251 #ifdef _MGRM_PROCESSES
252     JoinLayer(NAME_DEF_LAYER , "listbox" , 0 , 0);
253 #endif
254 
255     DlgDelFiles.controls = CtrlDelFiles;
256     
257     DialogBoxIndirectParam (&DlgDelFiles, HWND_DESKTOP, DelFilesBoxProc, 0L);
258 
259     return 0;
260 }
261 
262 #ifdef _MGRM_THREADS
263 #include <minigui/dti.c>
264 #endif

原文地址:https://www.cnblogs.com/paullam/p/3654322.html