1VOID Dlg_PopulateModuleList(HWND hwnd) {
2 //为何要费此周章.只是因为
3 //C++的ComboBox的索引不支持字符串
4 //自定义了一个IDC_MODULEHELP只是为了将
5 //模块的路径存储
6 //根据父窗口句柄和资源ID获取控件句柄
7 HWND hwndModuleHelp = GetDlgItem(hwnd, IDC_MODULEHELP);
8 //禁止窗体重绘
9 //ListBox_ResetContent(hwndModuleHelp);
10 SendMessage(hwndModuleHelp,WM_SETREDRAW,(WPARAM)FALSE,0);
11 //获取系统快照
12 CToolhelp thProcesses(TH32CS_SNAPPROCESS);
13 //定义进程入口点信息并初始长度
14 PROCESSENTRY32 pe = { sizeof(pe) };
15 //获取第一个进程入口点信息存入pe
16 BOOL fOk = thProcesses.ProcessFirst(&pe);
17 for (; fOk; fOk = thProcesses.ProcessNext(&pe)) {
18 //获取进程加载模块的信息
19 CToolhelp thModules(TH32CS_SNAPMODULE, pe.th32ProcessID);
20 //定义模块入口点信息并初始化长度
21 MODULEENTRY32 me = { sizeof(me) };
22 //获取该进程第一个模块入口点信息并存入me
23 BOOL fOk = thModules.ModuleFirst(&me);
24 for (; fOk; fOk = thModules.ModuleNext(&me)) {
25 //循环获取模块入口点信息
26 //在列表中查找当前模块信息是否已经被添加
27 int n = ListBox_FindStringExact(hwndModuleHelp, -1, me.szExePath);
28 if (n == LB_ERR) {
29 // This module hasn't been added before
30 //模块信息还未被添加则添加
31 ListBox_AddString(hwndModuleHelp, me.szExePath);
32 }
33 }
34 }
35
36 //获取进程列表框句柄
37 HWND hwndList = GetDlgItem(hwnd, IDC_PROCESSMODULELIST);
38 //设置进程列表框为不可重绘
39 SetWindowRedraw(hwndList, FALSE);
40 ComboBox_ResetContent(hwndList);
41 //获取当前所有已添加的模块数量
42 int nNumModules = ListBox_GetCount(hwndModuleHelp);
43 for (int i = 0; i < nNumModules; i++) {
44 TCHAR sz[1024];
45 //循环获取模块信息存储sz
46 ListBox_GetText(hwndModuleHelp, i, sz);
47 // Place module name (without its path) in the list
48 //将模块名加入ComboBox
49 int nIndex = ComboBox_AddString(hwndList, _tcsrchr(sz, TEXT('\\')) + 1);
50 // Associate the index of the full path with the added item
51 //设置索引
52 //这里在跟踪的时候有个不解的地方其实应该早发现的
53 //哈哈现在突然明白了nIndex的值是不确定的总是跳来跳去
54 //因为那是按照某种顺序添加到列表里并不是添加到最后的
55 //可以认为是当前位置吧比如按字母排序原有a,c开头的列表各一个
56 //现在来个b肯定是排在ac之间啊哈哈但是呢i的作用就是
57 //规范排序并且将ComboBox与IDC_MODULEHELP一一对应
58 //这里参见MSDN摘录
59 ComboBox_SetItemData(hwndList, nIndex, i);
60 }
61
62 ComboBox_SetCurSel(hwndList, 0); // Select the first entry
63
64 // Simulate the user selecting this first item so that the
65 // results pane shows something interesting
66 FORWARD_WM_COMMAND(hwnd, IDC_PROCESSMODULELIST,
67 hwndList, CBN_SELCHANGE, SendMessage);
68
69 SetWindowRedraw(hwndList, TRUE);
70 InvalidateRect(hwndList, NULL, FALSE);
71}
CB_ADDSTRING Message
An application sends a CB_ADDSTRING message to add a string to the list box of a combo box. If the combo box does not have the CBS_SORT style, the string is added to the end of the list. Otherwise, the string is inserted into the list, and the list is sorted.
Syntax
lResult = SendMessage( // returns LRESULT in lResult (HWND) hWndControl, // handle to destination control (UINT) CB_ADDSTRING, // message ID (WPARAM) wParam, // = 0; not used, must be zero (LPARAM) lParam // = (LPARAM) (LPCTSTR) lParam;);
Parameters
wParamThis parameter is not used.lParamPointer to the null-terminated string to be added. If you create the combo box with an owner-drawn style but without the CBS_HASSTRINGS style, the value of the lParam parameter is stored as item data rather than the string it would otherwise point to. The item data can be retrieved or modified by sending the CB_GETITEMDATA or CB_SETITEMDATA message.
Return Value
The return value is the zero-based index to the string in the list box of the combo box. If an error occurs, the return value is CB_ERR. If insufficient space is available to store the new string, it is CB_ERRSPACE.
Remarks
If you create an owner-drawn combo box with the CBS_SORT style but without the CBS_HASSTRINGS style, the WM_COMPAREITEM message is sent one or more times to the owner of the combo box so the new item can be properly placed in the list.
To insert a string at a specific location within the list, use the CB_INSERTSTRING message.
If the combo box has WS_HSCROLL style and you add a string wider than the combo box, send a LB_SETHORIZONTALEXTENT message to ensure the horizontal scrollbar appears.
Comclt32.dll version 5.0 or later: If CBS_LOWERCASE or CBS_UPPERCASE is set, the Unicode version of CB_ADDSTRING alters the string. If using read-only global memory, this causes the application to fail.