• <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)  編輯 收藏 引用 所屬分類: 課內作業

            99久久国产热无码精品免费久久久久| 99久久国产亚洲综合精品| 久久AV高潮AV无码AV| 亚洲精品乱码久久久久久蜜桃| 国内精品久久久久久中文字幕| 久久久精品人妻无码专区不卡 | 久久伊人五月丁香狠狠色| 亚洲午夜久久久| 成人国内精品久久久久一区 | 久久最新免费视频| 亚洲精品乱码久久久久久久久久久久| 午夜天堂av天堂久久久| 大蕉久久伊人中文字幕| 色综合久久夜色精品国产| 奇米影视7777久久精品| 国产精自产拍久久久久久蜜| 久久久久久午夜精品| 99热成人精品免费久久| 久久久久久久97| 久久久久18| 国产成人久久激情91| 色天使久久综合网天天| 91精品无码久久久久久五月天| 久久久久国产精品嫩草影院| 久久香蕉综合色一综合色88| 2021国产精品午夜久久| 久久精品无码一区二区日韩AV| 亚洲精品无码久久久久久| 久久久精品日本一区二区三区 | 久久久久亚洲av毛片大| 久久久久久综合一区中文字幕| 无码AV波多野结衣久久| 无码国内精品久久人妻蜜桃| 欧美精品久久久久久久自慰| 亚洲国产精品综合久久网络| 国产精品免费久久久久久久久| 久久美女网站免费| 久久99精品国产99久久6男男| 久久美女人爽女人爽| 国产精品久久久久9999| 久久免费视频观看|