coreBugZJ

此 blog 已弃。

Hello 显示进程——Windows编程上机作业之一

作业要求在窗口中分两列显示进程,我额外增加了 定时更新进程列表,垂直滚动条,鼠标滚轮。。。


代码:

Hello.h
 1//***************************************************************************************
 2//***************************************************************************************
 3
 4// Prototype for Window Function
 5LRESULT CALLBACK MainWndProc(HWND,UINT,WPARAM,LPARAM);
 6
 7// Prototypes of functions called by WinMain 
 8BOOL InitApplication(HINSTANCE);
 9BOOL InitInstance(HINSTANCE,int);
10
11//***************************************************************************************
12//***************************************************************************************
13


Hello.cpp
  1#include <windows.h>
  2#include "hello.h"    // 自定义头文件
  3#include <tlhelp32.h>
  4
  5//***************************************************************************************
  6
  7int WINAPI WinMain(HINSTANCE hInstance,                  // 入口函数
  8        HINSTANCE,
  9        LPSTR           lpCmdLine,
 10        int                   nCmdShow )
 11{
 12        if (!InitApplication(hInstance))       // 应用初始化
 13                return FALSE;
 14
 15        if (!InitInstance(hInstance,nCmdShow)) // 实例初始化
 16                return FALSE;
 17
 18        MSG msg;
 19        while (GetMessage(&msg, NULL, 00))   // 消息循环
 20        {
 21                TranslateMessage(&msg);
 22                DispatchMessage(&msg);
 23        }

 24
 25        return (int)msg.wParam;
 26}

 27
 28//***************************************************************************************
 29
 30BOOL InitApplication(HINSTANCE hInstance)   // 应用初始化
 31{
 32        WNDCLASS  wc;  // Data structure of the window class
 33
 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
 45        return RegisterClass(&wc);
 46}

 47
 48//***************************************************************************************
 49
 50BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)  // 实例初始化
 51{
 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
 65        ShowWindow(hWnd, nCmdShow);
 66        UpdateWindow(hWnd);
 67
 68        return TRUE;
 69}

 70
 71//***************************************************************************************
 72
 73// 窗口过程函数
 74
 75#define  PROCESS_MAX       512
 76#define  PROCESS_NAME_MAX  256
 77
 78LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
 79{
 80        static int cyChar, cyClient, iVscrollPos, iVscrollMax;
 81
 82        static int iProcessNum;
 83        static TCHAR  szProcessName[ PROCESS_MAX ][ PROCESS_NAME_MAX ];
 84        static DWORD  dwProcessId[ PROCESS_MAX ];
 85
 86        switch (message) {
 87
 88        case WM_CREATE : 
 89                {
 90                        TEXTMETRIC tm;
 91                        HDC hdc;
 92
 93                        ::SetTimer( hWnd, 01000, NULL );
 94                        
 95                        hdc = ::GetDC( hWnd );
 96                        ::GetTextMetrics( hdc, &tm );
 97                        cyChar = tm.tmHeight + tm.tmExternalLeading;
 98                        ::ReleaseDC( hWnd, hdc );
 99
100                        ::SendMessage( hWnd, WM_TIMER, 00 );
101                }

102                return 0;
103
104        case WM_SIZE : 
105                cyClient = HIWORD( lParam );
106                return 0;
107
108        case WM_TIMER : 
109                {
110                        PROCESSENTRY32 pe32;
111                        HANDLE  hProcessSnap;
112                        BOOL bMore;
113
114                        pe32.dwSize = sizeof(pe32);
115                        hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
116                        bMore = ::Process32First(hProcessSnap, &pe32);
117                        iProcessNum = 0;
118                        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                        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
134        case WM_MOUSEWHEEL : 
135                {
136                        short delta = HIWORD( wParam );
137                        if ( delta < 0 ) {
138                                ::SendMessage( hWnd, WM_VSCROLL, SB_LINEDOWN, 0 );
139                        }

140                        if ( delta > 0 ) {
141                                ::SendMessage( hWnd, WM_VSCROLL, SB_LINEUP, 0 );
142                        }

143                }

144                return 0;
145
146        case WM_VSCROLL : 
147                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                if ( iVscrollPos > iVscrollMax ) {
167                        iVscrollPos = iVscrollMax;
168                }

169                if ( iVscrollPos < 0 ) {
170                        iVscrollPos = 0;
171                }

172                if ( iVscrollPos != ::GetScrollPos( hWnd, SB_VERT ) ) {
173                        ::SetScrollPos( hWnd, SB_VERT, iVscrollPos, TRUE );
174                        ::InvalidateRect( hWnd, NULL, TRUE );
175                }

176                return 0;
177
178        case WM_PAINT: 
179                {
180                        PAINTSTRUCT ps;
181                        HDC hdc;
182                        TCHAR str[ PROCESS_NAME_MAX + 100 ];
183                        int i, x, y, lef = 0;
184
185                        hdc = ::BeginPaint( hWnd, &ps );
186
187                        for ( i = 0; i < iProcessNum; ++i ) {
188                                x = lef * 300;
189                                y = ( ( (i+1+1/ 2 * 3 - 3 ) - iVscrollPos ) * cyChar;
190                                lef ^= 1;
191
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
199                        ::EndPaint( hWnd, &ps );
200                }

201                return 0;
202
203        case WM_DESTROY: // 窗口关闭
204                ::KillTimer( hWnd, 0 );
205                PostQuitMessage(0);
206                return 0;
207
208        default:  // 缺省消息的处理
209                return DefWindowProc(hWnd, message, wParam, lParam);
210        }

211}

212

posted on 2011-03-22 19:02 coreBugZJ 阅读(261) 评论(0)  编辑 收藏 引用 所属分类: 课内作业


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理