作业要求在窗口中分两列显示进程,我额外增加了 定时更新进程列表,垂直滚动条,鼠标滚轮。。。
代码:
Hello.h
1
//***************************************************************************************
2
//***************************************************************************************
3![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
4
// Prototype for Window Function
5
LRESULT CALLBACK MainWndProc(HWND,UINT,WPARAM,LPARAM);
6![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
7
// Prototypes of functions called by WinMain
8
BOOL InitApplication(HINSTANCE);
9
BOOL InitInstance(HINSTANCE,int);
10![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
11
//***************************************************************************************
12
//***************************************************************************************
13![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Hello.cpp
1
#include <windows.h>
2
#include "hello.h" // 自定义头文件
3
#include <tlhelp32.h>
4![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
5
//***************************************************************************************
6![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
7
int WINAPI WinMain(HINSTANCE hInstance, // 入口函数
8
HINSTANCE,
9
LPSTR lpCmdLine,
10
int nCmdShow )
11![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
12
if (!InitApplication(hInstance)) // 应用初始化
13
return FALSE;
14![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
15
if (!InitInstance(hInstance,nCmdShow)) // 实例初始化
16
return FALSE;
17![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
18
MSG msg;
19
while (GetMessage(&msg, NULL, 0, 0)) // 消息循环
20![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
21
TranslateMessage(&msg);
22
DispatchMessage(&msg);
23
}
24![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
25
return (int)msg.wParam;
26
}
27![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
28
//***************************************************************************************
29![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
30
BOOL InitApplication(HINSTANCE hInstance) // 应用初始化
31![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
32
WNDCLASS wc; // Data structure of the window class
33![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
34
wc.style = CS_HREDRAW | CS_VREDRAW;
35
wc.lpfnWndProc = (WNDPROC)MainWndProc; // Name of the Window Function
36
wc.cbClsExtra = 0;
37
wc.cbWndExtra = 0;
38
wc.hInstance = hInstance;
39
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
40
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
41
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
42
wc.lpszMenuName = NULL;
43
wc.lpszClassName = TEXT("My1stWClass"); // Name of the window class
44![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
45
return RegisterClass(&wc);
46
}
47![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
48
//***************************************************************************************
49![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
50
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) // 实例初始化
51![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
52
HWND hWnd = CreateWindow(TEXT("My1stWClass"), // Name of the window class
53
TEXT("My First Window"), // Title of the window
54
WS_OVERLAPPEDWINDOW | WS_VSCROLL,
55
CW_USEDEFAULT,
56
CW_USEDEFAULT,
57
CW_USEDEFAULT,
58
CW_USEDEFAULT,
59
NULL,
60
NULL,
61
hInstance,
62
NULL );
63
if (!hWnd) return FALSE;
64![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
65
ShowWindow(hWnd, nCmdShow);
66
UpdateWindow(hWnd);
67![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
68
return TRUE;
69
}
70![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
71
//***************************************************************************************
72![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
73
// 窗口过程函数
74![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
75
#define PROCESS_MAX 512
76
#define PROCESS_NAME_MAX 256
77![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
78
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
79![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
80
static int cyChar, cyClient, iVscrollPos, iVscrollMax;
81![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
82
static int iProcessNum;
83
static TCHAR szProcessName[ PROCESS_MAX ][ PROCESS_NAME_MAX ];
84
static DWORD dwProcessId[ PROCESS_MAX ];
85![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
86![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
switch (message)
{
87![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
88
case WM_CREATE :
89![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
90
TEXTMETRIC tm;
91
HDC hdc;
92![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
93
::SetTimer( hWnd, 0, 1000, NULL );
94
95
hdc = ::GetDC( hWnd );
96
::GetTextMetrics( hdc, &tm );
97
cyChar = tm.tmHeight + tm.tmExternalLeading;
98
::ReleaseDC( hWnd, hdc );
99![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
100
::SendMessage( hWnd, WM_TIMER, 0, 0 );
101
}
102
return 0;
103![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
104
case WM_SIZE :
105
cyClient = HIWORD( lParam );
106
return 0;
107![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
108
case WM_TIMER :
109![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
110
PROCESSENTRY32 pe32;
111
HANDLE hProcessSnap;
112
BOOL bMore;
113![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
114
pe32.dwSize = sizeof(pe32);
115
hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
116
bMore = ::Process32First(hProcessSnap, &pe32);
117
iProcessNum = 0;
118![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while ( bMore )
{
119
::lstrcpy( szProcessName[ iProcessNum ], pe32.szExeFile );
120
dwProcessId[ iProcessNum++ ] = pe32.th32ProcessID;
121
bMore = ::Process32Next(hProcessSnap, &pe32);
122
}
123
::CloseHandle(hProcessSnap);
124
iVscrollMax = ( iProcessNum + 1 ) / 2 * 3 - 1;
125![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ( iVscrollPos > iVscrollMax )
{
126
iVscrollPos = iVscrollMax;
127
}
128
::SetScrollRange( hWnd, SB_VERT, 0, iVscrollMax, FALSE );
129
::SetScrollPos( hWnd, SB_VERT, iVscrollPos, TRUE );
130
::InvalidateRect( hWnd, NULL, TRUE );
131
}
132
return 0;
133![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
134
case WM_MOUSEWHEEL :
135![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
136
short delta = HIWORD( wParam );
137![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ( delta < 0 )
{
138
::SendMessage( hWnd, WM_VSCROLL, SB_LINEDOWN, 0 );
139
}
140![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ( delta > 0 )
{
141
::SendMessage( hWnd, WM_VSCROLL, SB_LINEUP, 0 );
142
}
143
}
144
return 0;
145![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
146
case WM_VSCROLL :
147![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
switch ( LOWORD( wParam ) )
{
148
case SB_LINEUP :
149
--iVscrollPos;
150
break;
151
case SB_LINEDOWN :
152
++iVscrollPos;
153
break;
154
case SB_PAGEUP :
155
iVscrollPos -= cyClient / cyChar;
156
break;
157
case SB_PAGEDOWN :
158
iVscrollPos += cyClient / cyChar;
159
break;
160
case SB_THUMBPOSITION :
161
iVscrollPos = HIWORD( wParam );
162
break;
163
default :
164
break;
165
}
166![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ( iVscrollPos > iVscrollMax )
{
167
iVscrollPos = iVscrollMax;
168
}
169![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ( iVscrollPos < 0 )
{
170
iVscrollPos = 0;
171
}
172![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ( iVscrollPos != ::GetScrollPos( hWnd, SB_VERT ) )
{
173
::SetScrollPos( hWnd, SB_VERT, iVscrollPos, TRUE );
174
::InvalidateRect( hWnd, NULL, TRUE );
175
}
176
return 0;
177![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
178
case WM_PAINT:
179![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
180
PAINTSTRUCT ps;
181
HDC hdc;
182
TCHAR str[ PROCESS_NAME_MAX + 100 ];
183
int i, x, y, lef = 0;
184![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
185
hdc = ::BeginPaint( hWnd, &ps );
186![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
187![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for ( i = 0; i < iProcessNum; ++i )
{
188
x = lef * 300;
189
y = ( ( (i+1+1) / 2 * 3 - 3 ) - iVscrollPos ) * cyChar;
190
lef ^= 1;
191![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
192
::wsprintf( str, TEXT(" 进程名称:%s "), szProcessName[ i ] );
193
::TextOut( hdc, x, y, str, lstrlen(str) );
194
y += cyChar;
195
::wsprintf( str, TEXT(" 进程ID号:%u "), dwProcessId[ i ] );
196
::TextOut( hdc, x, y, str, lstrlen(str) );
197
}
198![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
199
::EndPaint( hWnd, &ps );
200
}
201
return 0;
202![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
203
case WM_DESTROY: // 窗口关闭
204
::KillTimer( hWnd, 0 );
205
PostQuitMessage(0);
206
return 0;
207![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
208
default: // 缺省消息的处理
209
return DefWindowProc(hWnd, message, wParam, lParam);
210
}
211
}
212![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)