• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            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 閱讀(269) 評論(0)  編輯 收藏 引用 所屬分類: 課內作業

            久久久久一区二区三区| 亚洲精品无码久久久久去q | 久久99精品久久久久久动态图| 久久高潮一级毛片免费| 国产精品九九久久精品女同亚洲欧美日韩综合区 | 久久人人超碰精品CAOPOREN| 99久久精品免费观看国产| 久久精品国产福利国产秒| 欧美综合天天夜夜久久| 91精品国产高清久久久久久91| 9191精品国产免费久久| 久久伊人影视| 国产激情久久久久久熟女老人| 久久久久久国产精品无码下载| 狠狠色丁香久久婷婷综合_中| 亚洲中文字幕伊人久久无码| 久久91精品国产91久| 久久久久久无码Av成人影院| 99久久国产综合精品麻豆| 久久婷婷国产麻豆91天堂| 美女久久久久久| 久久亚洲私人国产精品vA| 26uuu久久五月天| 中文成人久久久久影院免费观看| 亚洲中文字幕无码久久精品1| 久久99精品久久只有精品| 国产伊人久久| 久久婷婷五月综合97色一本一本 | 久久综合久久综合九色| 欧美成a人片免费看久久| 人妻无码αv中文字幕久久琪琪布| 91视频国产91久久久| 久久久久人妻一区精品| 久久久女人与动物群交毛片| 精品视频久久久久| 久久国产亚洲高清观看| 久久大香萑太香蕉av| 国内精品久久久久久久久电影网| 无码人妻久久一区二区三区免费 | 国产成人无码久久久精品一| 久久国产精品免费一区|