Windows核心编程笔记第四篇(代码精读VOID Dlg_PopulateModuleList(HWND hwnd))

ContractedBlock.gif ExpandedBlockStart.gif Code
 1ExpandedBlockStart.gifContractedBlock.gifVOID 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   //定义进程入口点信息并初始长度
14ExpandedSubBlockStart.gifContractedSubBlock.gif   PROCESSENTRY32 pe = sizeof(pe) };
15   //获取第一个进程入口点信息存入pe
16   BOOL fOk = thProcesses.ProcessFirst(&pe);
17ExpandedSubBlockStart.gifContractedSubBlock.gif   for (; fOk; fOk = thProcesses.ProcessNext(&pe)) {
18      //获取进程加载模块的信息
19      CToolhelp thModules(TH32CS_SNAPMODULE, pe.th32ProcessID);
20          //定义模块入口点信息并初始化长度
21ExpandedSubBlockStart.gifContractedSubBlock.gif      MODULEENTRY32 me = sizeof(me) };
22          //获取该进程第一个模块入口点信息并存入me
23      BOOL fOk = thModules.ModuleFirst(&me);
24ExpandedSubBlockStart.gifContractedSubBlock.gif      for (; fOk; fOk = thModules.ModuleNext(&me)) {
25                  //循环获取模块入口点信息
26                  //在列表中查找当前模块信息是否已经被添加
27        int n = ListBox_FindStringExact(hwndModuleHelp, -1, me.szExePath);
28ExpandedSubBlockStart.gifContractedSubBlock.gif         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);
43ExpandedSubBlockStart.gifContractedSubBlock.gif   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

To send this message, call the SendMessage function as follows.

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

wParam
This parameter is not used.
lParam
Pointer 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.

转载于:https://www.cnblogs.com/wParma/archive/2009/10/29/1592429.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值