• <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程序內部運行機制

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

            3,消息隊列:
            每個應用程序OS都為它建立一個消息隊列,消息隊列是個先進先出的緩沖區,其中每個元素都是一個消息,OS將生成的每個消息按先后順序放進消息隊列中,應用程序總是取走當前消息隊列中的第一條消息,應用程序取走消息后便知道用戶的操作和程序的狀態,然后對其處理即消息響應,消息響應通過編碼實現。

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

            5,Window程序入口:
            int WINAPI WinMain(
              HINSTANCE hInstance,  // 當前事例句柄。
              HINSTANCE hPrevInstance,  // 先前事例句柄。
              LPSTR lpCmdLine,      // 命令行指針
              int nCmdShow          // (窗口)顯示的狀態
            );
            說明:WinMain函數是Windows程序入口點函數,由OS調用,當OS啟動應用程序的時候,winmain函數的參數由OS傳遞的。

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

            說明:創建窗口的時候一定要基于已經注冊的窗口類.

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

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

            9,創建窗口:
            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,消息循環:
            MSG msg;
            while(GetMessage(&msg,...))    //從消息隊列中取出一條消息
            {
             TranslateMessage(&msg); //進行消息(如鍵盤消息)轉換
             DispatchMessage(&msg); //分派消息到窗口的回調函數處理,(OS調用窗口回調函數進行處理)。
            }

            其中:
            //**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,窗口過程函數(回調函數)原型:
            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是個代號名字。
              HWND hwnd,      // handle to window
              UINT uMsg,      // message identifier
              WPARAM wParam,  // first message parameter
              LPARAM lParam   // second message parameter
            );

            說明:兩種函數調用約定(__stdcall 和 __cdecl):
            #define CALLBACK    __stdcall
            //__stdcall 標準調用預定,是PASCAL 調用約定,象DELPHI使用的就是標準調用約定
            #define WINAPIV     __cdecl 
            // __cdecl 是C 語言形式的調用約定。


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


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

            13,DestroyWindow()函數和PostQuitMessage()函數原型:
            //**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(預示,通知) 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,關于DC句柄獲取:
            a)使用BeginPaint(),EndPaint()對。注意只能在響應WM_PAINT消息時使用。
            b)使用GetDc(),ReleaseDC()對。注意他們不能在響應WM_PAINT中使用。

            posted on 2008-03-11 15:02 弱水一瓢 閱讀(155) 評論(0)  編輯 收藏 引用 所屬分類: MFC

            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導航

            統計

            文章分類

            最新評論

            91精品国产乱码久久久久久| 91精品国产9l久久久久| 色婷婷久久综合中文久久一本| 国产精品久久久久久久久鸭| 久久婷婷国产麻豆91天堂| 天天综合久久一二三区| 久久青青草原精品国产| 激情久久久久久久久久| 无码精品久久久天天影视| 国产—久久香蕉国产线看观看| 亚州日韩精品专区久久久| 国色天香久久久久久久小说| 99久久无色码中文字幕| 久久久久久精品成人免费图片| 欧美一区二区精品久久| 久久久久亚洲AV成人片| 亚洲美日韩Av中文字幕无码久久久妻妇| 久久亚洲欧美国产精品| 久久久精品久久久久久 | 国产产无码乱码精品久久鸭| 国内精品免费久久影院| 久久人人爽人人爽人人AV东京热 | 久久www免费人成看国产片| 色综合久久无码中文字幕| 日日狠狠久久偷偷色综合96蜜桃| 99久久国产综合精品麻豆| 亚洲成色WWW久久网站| 久久这里只精品99re66| 久久综合精品国产一区二区三区 | 2019久久久高清456| 武侠古典久久婷婷狼人伊人| 久久AAAA片一区二区| 久久最近最新中文字幕大全 | 亚洲午夜久久影院| 精品久久久噜噜噜久久久| 色欲久久久天天天综合网| 国产精品成人久久久| 久久久久久久精品妇女99| 亚洲成色WWW久久网站| A狠狠久久蜜臀婷色中文网| 久久精品国产99久久久|