#include <tlhelp32.h>
#define ID_LIST 1
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HWND hwndList;
int cxChar, cyChar;
switch (message) {
case WM_CREATE :
cxChar = LOWORD (GetDialogBaseUnits ()) ; //获取行高
cyChar = HIWORD (GetDialogBaseUnits ()) ;//获取列宽
hwndList = CreateWindow(TEXT ("listbox"), NULL,
WS_CHILD | WS_VISIBLE | LBS_STANDARD,
0, cyChar + 2,
cxChar * 100 + GetSystemMetrics (SM_CXVSCROLL), //加上滚动条宽度
cyChar * 10,
hwnd, (HMENU) ID_LIST,
(HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
NULL) ;
showProcList (hwndList) ;
case WM_SETFOCUS :
SetFocus (hwndList) ; //让listbox一运行就获得焦点,这样就可以直接使用上下键
return 0 ;
}
}
/*
* 列举当前运行中的进程pid
*/
void showProcList(HWND hwndList) {
HANDLE hProcessSnap;
HANDLE hProcess;
PROCESSENTRY32 pe32;
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);/*进程快照*/
if(hProcessSnap == INVALID_HANDLE_VALUE) {
MessageBox(NULL,"获取进程快照失败! 有可能是系统不支持.",NULL, MB_OK);
return;
}
/*在使用 PROCESSENTRY32 结构之间需要先设置好该结构的大小 */
pe32.dwSize = sizeof(PROCESSENTRY32);
/*获取第一个进程 */
if(!Process32First(hProcessSnap, &pe32)) {
CloseHandle(hProcessSnap);
return;
}
/*采用 Do - While 遍历所有进程 */
do {
SendMessage (hwndList, LB_ADDSTRING, 0, (LPARAM) pe32.szExeFile) ;/*pe32.th32ProcessID*/
} while(Process32Next(hProcessSnap, &pe32));
CloseHandle(hProcessSnap);
}