• <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>

            Windows程序內(nèi)部運(yùn)行機(jī)制 收藏

            Windows程序內(nèi)部運(yùn)行機(jī)制

            1,windows程序設(shè)計(jì)是種事件驅(qū)動(dòng)方式的程序設(shè)計(jì),主要基于消息的。當(dāng)用戶需要完成某種功能時(shí),需要調(diào)用OS某種支持,然后OS將用戶的需要包裝成消息,并投入到消息隊(duì)列中,最后應(yīng)用程序從消息隊(duì)列中取走消息并進(jìn)行響應(yīng)。
            2,消息結(jié)構(gòu):
            typedef struct tagMSG {     // msg
                HWND   hwnd;     //接收消息的窗口句柄。和哪個(gè)窗口相關(guān)聯(lián)。
                UINT   message; //消息標(biāo)識(shí)。消息本身是什么。
                WPARAM wParam;   //消息的附加信息。具體取決于消息本身。  
                LPARAM lParam;
                DWORD time;     //消息投遞時(shí)間。
                POINT pt;       //消息投遞時(shí),光標(biāo)在屏幕上的位置。
            } MSG;

            3,消息隊(duì)列:
            每個(gè)應(yīng)用程序OS都為它建立一個(gè)消息隊(duì)列,消息隊(duì)列是個(gè)先進(jìn)先出的緩沖區(qū),其中每個(gè)元素都是一個(gè)消息,OS將生成的每個(gè)消息按先后順序放進(jìn)消息隊(duì)列中,應(yīng)用程序總是取走當(dāng)前消息隊(duì)列中的第一條消息,應(yīng)用程序取走消息后便知道用戶的操作和程序的狀態(tài),然后對其處理即消息響應(yīng),消息響應(yīng)通過編碼實(shí)現(xiàn)。

            4,使用VC編程除了良好的C基礎(chǔ)外還需要掌握兩方面:
            一,消息本身。不同消息所代表的用戶操作和應(yīng)用程序的狀態(tài)。
            二,對于某個(gè)特定的消息來說,要讓OS執(zhí)行某個(gè)特定的功能去響應(yīng)消息。

            5,Window程序入口:
            int WINAPI WinMain(
            HINSTANCE hInstance, // 當(dāng)前事例句柄。
            HINSTANCE hPrevInstance, // 先前事例句柄。
            LPSTR lpCmdLine,      // 命令行指針
            int nCmdShow          // (窗口)顯示的狀態(tài)
            );
            說明:WinMain函數(shù)是Windows程序入口點(diǎn)函數(shù),由OS調(diào)用,當(dāng)OS啟動(dòng)應(yīng)用程序的時(shí)候,winmain函數(shù)的參數(shù)由OS傳遞的。

            6,創(chuàng)建一個(gè)完整的窗口需要經(jīng)過下面四個(gè)操作步驟:
            一,設(shè)計(jì)一個(gè)窗口類;如:WNDCLASS wndcls;
            二,注冊窗口類;    如:RegisterClass(&wndcls);
            三,創(chuàng)建窗口;      如:CreateWindow(),CreateWindowEX();
            四,顯示及更新窗口。如:ShowWindow(),UpdateWindow();

            說明:創(chuàng)建窗口的時(shí)候一定要基于已經(jīng)注冊的窗口類.

            7,Windows提供的窗口類:
            typedef struct _WNDCLASS {
                UINT    style;        //窗口的類型
                WNDPROC lpfnWndProc; //窗口過程函數(shù)指針(回調(diào)函數(shù))
                int     cbClsExtra; //窗口類附加字節(jié),為該類窗口所共享。通常0。
                int     cbWndExtra; //窗口附加字節(jié)。通常設(shè)為0。
                HANDLE hInstance; //當(dāng)前應(yīng)用程序事例句柄。
                HICON   hIcon;      //圖標(biāo)句柄 LoadIcon();
                HCURSOR hCursor;    //光標(biāo)句柄 LoadCursor();
                HBRUSH hbrBackground; //畫刷句柄 (HBRUSH)GetStockObject();
                LPCTSTR lpszMenuName; //菜單名字
                LPCTSTR lpszClassName; //類的名字
            } WNDCLASS;

            8,窗口類注冊:
            ATOM RegisterClass(
            CONST WNDCLASS *lpWndClass   // address of structure with class
                                           // data
            );

            9,創(chuàng)建窗口:
            HWND CreateWindow(
            LPCTSTR lpClassName, // pointer to registered class name
            LPCTSTR lpWindowName, // pointer to window name
            DWORD dwStyle,        // window style
            int x,                // horizontal position of window
            int y,                // vertical position of window
            int nWidth,           // window width
            int nHeight,          // window height
            HWND hWndParent,      // handle to parent or owner window
            HMENU hMenu,          // handle to menu or child-window identifier
            HANDLE hInstance,     // handle to application instance
            LPVOID lpParam        // pointer to window-creation data
            );

            10,顯示和更新窗口窗口:
            BOOL ShowWindow(
            HWND hWnd,     // handle to window
            int nCmdShow   // show state of window
            );
            BOOL UpdateWindow(
            HWND hWnd   // handle of window
            );

            11,消息循環(huán):
            MSG msg;
            while(GetMessage(&msg,...))    //從消息隊(duì)列中取出一條消息
            {
            TranslateMessage(&msg); //進(jìn)行消息(如鍵盤消息)轉(zhuǎn)換
            DispatchMessage(&msg); //分派消息到窗口的回調(diào)函數(shù)處理,(OS調(diào)用窗口回調(diào)函數(shù)進(jìn)行處理)。
            }

            其中:
            //**The GetMessage function retrieves a message from the calling thread's message queue and places it in the specified structure.
            //**If the function retrieves a message other than WM_QUIT, the return value is nonzero.If the function retrieves the WM_QUIT message, the return value is zero. If there is an error, the return value is -1.

            BOOL GetMessage(
            LPMSG lpMsg,         // address of structure with message
            HWND hWnd,           // handle of window
            UINT wMsgFilterMin, // first message
            UINT wMsgFilterMax   // last message
            );


            //The TranslateMessage function translates virtual-key messages into character messages. The character messages are posted to the calling thread's message queue, to be read the next time the thread calls the GetMessage or PeekMessage function.
            BOOL TranslateMessage(
            CONST MSG *lpMsg   // address of structure with message
            );

            //The DispatchMessage function dispatches a message to a window procedure.
            LONG DispatchMessage(
            CONST MSG *lpmsg   // pointer to structure with message
            );


            12,窗口過程函數(shù)(回調(diào)函數(shù))原型:
            The WindowProc function is an application-defined function that processes messages sent to a window. The WNDPROC type defines a pointer to this callback function. WindowProc is a placeholder(占位符) for the application-defined function name.

            LRESULT CALLBACK WindowProc( //這里WindowProc是個(gè)代號(hào)名字。
            HWND hwnd,      // handle to window
            UINT uMsg,      // message identifier
            WPARAM wParam, // first message parameter
            LPARAM lParam   // second message parameter
            );

            說明:兩種函數(shù)調(diào)用約定(__stdcall 和 __cdecl):
            #define CALLBACK    __stdcall
            //__stdcall 標(biāo)準(zhǔn)調(diào)用預(yù)定,是PASCAL 調(diào)用約定,象DELPHI使用的就是標(biāo)準(zhǔn)調(diào)用約定
            #define WINAPIV     __cdecl
            // __cdecl 是C 語言形式的調(diào)用約定。


            主要區(qū)別:函數(shù)參數(shù)傳遞順序 和 對堆棧的清除上。
            問題:除了那些可變參數(shù)的函數(shù)調(diào)用外,其余的一般都是__stdcall約定。但 C/C++編譯默然的是__cdecl約定。所以如果在VC等環(huán)境中調(diào)用__stdcall約定的函數(shù),必須要在函數(shù)聲明的時(shí)加上 __stdcall 修飾符,以便對這個(gè)函數(shù)的調(diào)用是使用__stdcall約定(如使用DELPHI編寫的DLL時(shí)候)。
            (VC中可通過這途徑修改:project|settings..|c/c++|...)


            在窗口過程函數(shù)中通過一組switch語句來對消息進(jìn)行處理:
            如:
            LRESULT CALLBACK WindowProc(
            HWND hwnd,
            UINT uMsg,
            WPARAM wParam,
            LPARAM lParam  
            )
            {
                switch(uMsg)
                {
            case WM_PAINT:
               ...
               break;
            case ...
               break;
            case WM_CLOSE:
               //DestroyWindow(hwnd);
                //銷毀窗口,并發(fā)送WM_DESTROY消息。
               break;
            case WM_DESTROY:
               //PostQuitMessage(0);
               //發(fā)送WM_QUIT消息到消息隊(duì)列中,請求終止。
                     //GetMessage()取到WM_QUIT消息后,返回0,退出消息循                //   環(huán),從而終止應(yīng)用程序。
               break;
            default:
               return DefWindowProc(hwnd,uMsg,wParam,lParam);
            //用缺省的窗口過程處理我們不感興趣的消息(其它消息)。
            //這是必須的。
                }//switch
            return 0;
            }//WindowProc

            13,DestroyWindow()函數(shù)和PostQuitMessage()函數(shù)原型:
            //**The DestroyWindow function destroys the specified window. The function sends WM_DESTROY and WM_NCDESTROY messages。

            BOOL DestroyWindow(
            HWND hWnd   // handle to window to destroy
            );

            //**The PostQuitMessage function indicates to the system that a thread has made a request to terminate (quit). It is typically used in response to a WM_DESTROY message.
            //**The PostQuitMessage function posts a WM_QUIT message to the thread's message queue and returns immediately; the function simply indicates(預(yù)示,通知) to the system that the thread is requesting to quit at some time in the future.

            When the thread retrieves the WM_QUIT message from its message queue, it should exit its message loop and return control to the system.

            VOID PostQuitMessage(
            int nExitCode   // exit code
            );

            14,關(guān)于DC句柄獲取:
            a)使用BeginPaint(),EndPaint()對。注意只能在響應(yīng)WM_PAINT消息時(shí)使用。
            b)使用GetDc(),ReleaseDC()對。注意他們不能在響應(yīng)WM_PAINT中使用。

            posted on 2010-06-21 22:41 lhking 閱讀(136) 評論(0)  編輯 收藏 引用

            導(dǎo)航

            <2010年6月>
            303112345
            6789101112
            13141516171819
            20212223242526
            27282930123
            45678910

            統(tǒng)計(jì)

            常用鏈接

            留言簿

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久综合伊人77777麻豆| 精品久久久久久久| 久久99热这里只有精品国产| 久久国产AVJUST麻豆| 久久天天躁狠狠躁夜夜不卡| 久久国产色AV免费看| 久久免费大片| 久久久久久久尹人综合网亚洲 | 波多野结衣久久| 97精品久久天干天天天按摩| 精品久久久久久久中文字幕| 色欲久久久天天天综合网| 国产精久久一区二区三区| 欧美喷潮久久久XXXXx| 久久国产AVJUST麻豆| 99久久免费只有精品国产| 久久综合给合久久狠狠狠97色69| 日本精品久久久中文字幕| 久久亚洲中文字幕精品有坂深雪 | 精品无码人妻久久久久久| 99久久精品国产高清一区二区| 少妇无套内谢久久久久| 狠狠精品干练久久久无码中文字幕 | 久久精品国产清高在天天线| 欧美性猛交xxxx免费看久久久| 国内精品久久久久久久97牛牛| 久久人人爽人人爽人人片AV东京热| 狠狠色丁香婷婷综合久久来来去| 精品一区二区久久| 九九99精品久久久久久| 久久精品人人做人人爽97| 色欲久久久天天天综合网| 伊人久久大香线蕉AV色婷婷色| 久久久久亚洲AV成人网人人网站| 亚洲精品午夜国产va久久| 欧美国产成人久久精品| 久久午夜福利无码1000合集| 久久久噜噜噜久久中文字幕色伊伊 | 99久久99久久精品免费看蜜桃| 91视频国产91久久久| 国产精品成人久久久久三级午夜电影|