Windows程式設計_Chap01_起步_學習筆記

來源:互聯網
上載者:User
Windows程式設計_Chap01_起步_學習筆記

――By: Neicole(2013.05.21)

 

01. 開篇

 

    今天是2013.05.20,開始學習《windows程式設計》這書,期間,會藉助MSDN和網路資料展開學習。書本第一章,簡要介紹了Windows的曆史,簡要提到動態連結程式庫的概念,講述Windows編程的準備工作,還有最重要的第一個Windows程式。這次學習筆記以翻譯MSDN為主,目的是學會使用兩個最基本的WindowsAPI函數。

 

02. WindowsAPI重要的標頭檔(動態連結程式庫)

 

  02.01 核心WINBASE.H (KERNEL32.DLL) 負責作業系統的五個基本工作:進程管理、儲存管理、裝置管理、檔案管理、作業系統介面。

  02.02 使用者WINUSER.H (USER32.DLL) 使用者介面,負責所有的視窗管理。

  02.03 圖形裝置介面(GDI)WINGDI.H(GDI32.DLL) 負責在螢幕或印表機顯示文本與圖形。

  02.04 基礎資料型別 (Elementary Data Type)定義WINDEF.H

  02.05 支援Unicode的類型定義WINNT.H

 

03. 我的第一個Windows程式

 

03.01 效果示範

 

03.02 原始碼

/** * 檔案:HahaWorld.cpp * 簡介:第一個WindowsAPI實現的程式,顯示一個MessageBox。 * IDE:VS2010 SP1 * 製作者:Neicole * 連絡方式:http://blog.csdn.net/neicole **/#include <Windows.h>int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow){MessageBox(NULL, TEXT("HaHa World!"), TEXT("HahaMsg"), MB_DEFBUTTON1|MB_YESNO);return 0;}

 

04. 程式解釋

 

04.01 WinMain函數

  主要使用了WinMain函數作為windows程式入口。經過查閱MSDN得到關於WinMain的介紹(以下內容個人理解翻譯,如有錯誤,歡迎指出希望能給出更正意見):

    Every Windowsprogram includes an entry-point function that is named either WinMain orwWinMain.

    一個空的windows程式包含了一個入口函數WinMain或者wWinMain。

 

 04.01.01 參數介紹

    hInstance is something called a "handle to an instance" or "handle to a module." The operating system uses this value to identify the executable (EXE) when it is loaded in memory. The instance handle is needed for certain Windows functions — for example,
to load icons or bitmaps.

    hInstance叫“執行個體控制代碼”或者“模組控制代碼”。作業系統使用這個控制代碼的值去定義載入到記憶體的構件。使用像載入表徵圖或者位元影像這類視窗函數需要用上執行個體控制代碼。
    hPrevInstance has no meaning. It was used in 16-bit Windows, but is now always zero.
    hPrevInstance沒有含義。它以前被用於16位系統,但現在常常為零。
    pCmdLine contains the command-line arguments as a Unicode string.
    pCmdLine包含程式的(Unicode編程字串)命令列參數。
    nCmdShow is a flag that says whether the main application window will be minimized, maximized, or shown normally.
    nCmdShow是一個說明主應用視窗如何顯示的標誌,如最小化,最大化或者正常顯示。

04.01.02 傳回值

    The function returns an int value. The return value is not used by the operating system, but you can use the return value to convey a status code to some other program that you write.
    這個函數返回一個int類型的值。這傳回值不被作業系統使用,但是你可以使用傳回值去傳達一個狀態代碼到一些你寫的程式。
    WINAPI is the calling convention. A calling convention defines how a function receives parameters from the caller. For example, it defines the order that parameters appear on the stack. Just make sure to declare your wWinMain function as shown.
    WINAPI是一個呼叫慣例。定義了函數接收參數的方式。例如,它定義了參數的壓棧順序(從右至左),這樣只是確認可以使你的wWinMain函數正常顯示。

 

04.01.03 備忘

    The WinMain function is identical to wWinMain, except the command-line arguments are passed as an ANSI string. The Unicode version is preferred. You can use the ANSI WinMain function even if you compile your program as Unicode. To get a Unicode copy
of the command-line arguments, call the GetCommandLine function. This function returns all of the arguments in a single string. If you want the arguments as an argv-style array, pass this string to CommandLineToArgvW.
    WinMain函數與wWinMain除了命令列pCmdLine參數是使用ANSI字串外其它完全相同, Unicode版本優先使用。當你的程式用Unicode編譯時間,你也可以使用ANSI命令列參數。獲得Unicode命令列的副本,調用GetCommandLine函數。這個函數將這些參數返回唯一的字串。如果你想這些參數是命令列參數風格數組,將這字串作為參數執行CommandLine函數。
    How does the compiler know to invoke wWinMain instead of the standard main function? What actually happens is that the Microsoft C runtime library (CRT) provides an implementation of main that calls either WinMain or wWinMain.
    編譯器如何知道調用wWinMain來代替標準的main函數?這是由於微軟的C的標準執行階段程式庫(CRT)提供了WinMain或者wWinMain的函數調用介面。
    Note:The CRT does some additional work inside main. For example, any static initializers are called before wWinMain. Although you can tell the linker to use a different entry-point function, use the default if you link to the CRT. Otherwise, the CRT initialization
code will be skipped, with unpredictable results. (For example, global objects will not be initialized correctly.)
    注意,標準執行階段程式庫會在main裡做一些額外的操作。例如,一些靜態初始化對象會在wWinMain執行之前初始化。如果你串連到CRT,你會使用預設的入口函數,即使你可以告訴鍵接者去使用不同的入口函數。否則,CRT初始化對象代碼會被跳過,可能會出現不可預知的結果。(例如,全部變數不能正確地被初始化。)

 
04.02 Message函數

  int MessageBox(HWND hWnd,LPCTSTR lpText,LPCTSTR lpCaption,UINT UType);
    這是在Windows編程裡面常常用上的一個函數,正如在Win32控制台編程時常用cout一樣。

 

04.02.01 參數介紹

    hWnd  [in] Handle to the owner window of the message box to be created. If this parameter is NULL, the message box has no owner window.
    訊息框的父視窗控制代碼。如果這個參數為NULL,則訊息框沒有父視窗。
    lpText  [in] Pointer to a null-terminated string that contains the message to be displayed.
    指向一個以NULL結尾、含有將被顯示訊息的字串的指標。
    lpCaption  [in] Pointer to a null-terminated string that contains the dialog box title. If this parameter is NULL, the default title Error is used.
    指向一個以NULL結尾的、用於對話方塊標題的字串的指標。如果參數為空白,則預設標題錯誤會被使用。
    uType  [in] Specifies the contents and behavior of the dialog box. This parameter can be a combination of flags from the following groups of flags.
    指定對話方塊的內容和行為的位標誌集。這個參數可以為下列標誌組中標誌的組合。(這裡不一一列出)

 

04.02.02 傳回值

    If a message box has a Cancel button, the function returns the IDCANCEL value if either the ESC key is pressed or the Cancel button is selected. If the message box has no Cancel button, pressing ESC has no effect.
    如果訊息框有取消按鈕,如果ESC鍵被按下或者取消按鈕被選中,函數返回IDCANCEL值。如果訊息框沒有訊息按鈕,按下ESC鍵將不會產生效果。
    If the function fails, the return value is zero. To get extended error information, call GetLastError.
    如果函數失敗,傳回值為0。調用GetLastError能獲得更詳細的錯誤資訊。
    If the function succeeds, the return value is one of the following menu-item values.
    如果函數成功,傳回值將會是下列列表中的其中一個。(這裡不一一列出)

 

04.02.03 備忘

    Adding two right-to-left marks (RLMs), represented by Unicode formatting character U+200F, in the beginning of a MessageBox display string is interpreted by the Win32 MessageBox rendering engine so as to cause the reading order of the MessageBox to be
rendered as right-to-left (RTL).
    增加兩個從右至左的標誌(RLMs)為代表的Unicode格式字元U+200F,在開始的一個訊息框顯示字串被Win32快顯視窗渲染引擎解讀,使對話方塊的讀取順序被顯示為從右至左(RTL)。
    When you use a system-modal message box to indicate that the system is low on memory, the strings pointed to by the lpText and lpCaption parameters should not be taken from a resource file because an attempt to load the resource may fail.
    當你使用一個系統模態訊息框來指示系統是低記憶體,這個字串所指向的lpText和lpCaption參數不能來自資源檔,這是因為試圖載入資源可能會失敗。
    If you create a message box while a dialog box is present, use a handle to the dialog box as the hWnd parameter. The hWnd parameter should not identify a child window, such as a control in a dialog box.
    如果在一個對話方塊出現時你建立一個訊息框,使用對話方塊的控制代碼作為參數。這個控制代碼參數不能作為一個子視窗,正如對話方塊裡的控制項。

 

05. 結尾語

    第一個Windows API程式出來啦啦啦~ 使用HahaWorld代替了HelloWorld,這是因為,Haha這詞,好像顯得更加有活力。現在只是開篇,Windows API編程,更精彩的知識還在後頭呢~

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.