• <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热| 亚洲AV无码一区东京热久久 | 国内精品伊人久久久影院| 久久国产AVJUST麻豆| 亚洲伊人久久精品影院| 99热精品久久只有精品| 色天使久久综合网天天| 久久亚洲国产中v天仙www | 久久精品国产精品亚洲精品| 国产精品久久久久jk制服| 狠狠色丁香婷婷综合久久来来去| 久久中文字幕精品| 91精品国产91久久久久久| 少妇久久久久久久久久| 天堂无码久久综合东京热| 亚洲国产天堂久久综合网站| 无码人妻久久久一区二区三区| 久久九九久精品国产免费直播| 99久久99这里只有免费费精品 | 九九久久自然熟的香蕉图片| 性高湖久久久久久久久AAAAA| 日本三级久久网| 国产91色综合久久免费| 久久无码中文字幕东京热| 久久久久这里只有精品| 国产Av激情久久无码天堂 | 免费一级做a爰片久久毛片潮| 成人国内精品久久久久影院| 亚洲国产精品成人久久| 2021国内精品久久久久久影院| 久久AⅤ人妻少妇嫩草影院| 久久香蕉国产线看观看乱码| 国产成人久久精品区一区二区| 亚洲午夜久久久久久久久电影网| 久久综合亚洲鲁鲁五月天| 亚洲伊人久久综合影院| 久久亚洲日韩看片无码| 麻豆精品久久久久久久99蜜桃| 久久综合鬼色88久久精品综合自在自线噜噜| 久久久久无码国产精品不卡| 久久久久国产|