【正文】
如果 VC++ 已經(jīng)在該機器上使用過了,則所顯示的窗口可能有些不同,因為 V。在第二部分中我們會仔細研究這段程序,以理解其結(jié)構(gòu)。第二,應(yīng)用程序建立了一個窗口來作為應(yīng) 用程序的主窗口。第一,它建立了一個應(yīng)用程序?qū)ο蟆?} 上面的這段程序如果用 C 來實現(xiàn),得需要幾頁的代碼。 // 建立靜態(tài)標簽 cs = new CStatic()。 return TRUE。 m_pMainWndShowWindow(m_nCmdShow)。 }。 // 說明主窗口類 class CHelloWindow : public CFrameWnd {CStatic* cs。 }?,F(xiàn)在你只要用 Visual C++ 環(huán)境中建立、編譯和運行它就可以了。如果你是第一次看到這類程序,也許比較難理解。下面的程序是一段簡單的“ hello world”程序。第四部分介紹了怎樣使用消息映射來處理事件。該函數(shù)包含有對該按鈕作出相應(yīng)操作的代碼。如果你把某一用戶界面對象放在屏幕上,你只需要兩行代碼來建立它。 幸運的是,你可以在比較高級的方法來進行編程,這就是使用 MFC。當(dāng)屏幕上有很多對象時, 應(yīng)用程序會變得很大。例如,你用某種類型的結(jié)構(gòu)來接收單擊鼠標事件。事件驅(qū)動系統(tǒng)把用戶的動作如鼠標單擊和鍵盤操作定義為事件,也把系統(tǒng)操作如更新屏幕定義為事件。例如,用戶用鼠標單擊一個按鈕。應(yīng)用程序通常通過一 段稱為事件循環(huán)的的代碼來響應(yīng)用戶的操作。具體的細節(jié)對不同的系統(tǒng)可能有些不同,但是其基本概念是類似的。 如果你從未做過事件驅(qū)動程序設(shè)計,則所有這些對你來說可能是很陌生的。這些控制通常被稱為子窗口 它們就象一些在主窗口中的更小更特殊的子窗口。 Windows 所用的模式也是類似的。例如,如果用戶單擊退出按鈕,則該按鈕必須更新屏幕、加亮它自己。所做的響應(yīng)依賴于用戶使用鼠標或鍵盤在不同控制上的操作。在這個簡單的程序中,程序員可能需要用戶在一個可編輯的編輯框中輸入溫度值,在一個不可編輯的編輯框中顯示轉(zhuǎn)換結(jié)果,然后讓用戶可以單擊一 個標有“退出”的按鈕來退出應(yīng)用程序。為了建立 GUI 程序,程序員第一步要把所有需要的用戶界面控制都放到窗口上。例如,滾動條對于 Windows、 Mac 和 Motif 可能有些不同,但他們的作用完全是一樣的。從用戶角度來看,各系統(tǒng)的界面對象都是相同的:按鈕、滾動條、圖標、對話框以及下拉菜單等等。 事件驅(qū)動軟件和詞匯所有基于窗口的 GUI 都包含相同的基本元素,它們的操作方式都是相同的。應(yīng)用程序界面的好壞取決于你如何選擇和組織這些界面對象。 MDI 界面可能會給你一種第二桌面的感覺,它對窗口的管理和刪除混亂的窗口有很大的幫助。在 MDI 框架中,主窗口有一個主菜單,它對主框架中最頂端窗口有 效。例如,一個文本編輯器可以允許用戶同時打開多個文本文件。 Windows 也提供了一種稱為多文檔界面的組織形式,它可用于更復(fù)雜的應(yīng)用程序。 最簡單的 Windows 應(yīng)用程序是使用單文檔界面 (SDI),只有一個框架窗口。模式對話框一旦出現(xiàn)在屏幕上,只有當(dāng)它退出時,屏幕上該應(yīng)用程序的其余部分才能響應(yīng)。一個框架窗口是一個全功能的主窗口,用戶可以改變尺寸、最小化、最大化等。 Windows 支持幾種類型的應(yīng)用程序窗口。 Windows 應(yīng)用程序使用幾個標準的控制 : 靜態(tài)文本標簽 按鈕 列表框 組合框 (一種更高級的列表框 ) 單選按鈕 檢查按鈕 編輯框 (單行和多行 ) 滾動條 你可以通過代碼或“資源編輯器”來建立這些控制,在資源編輯器中可以建立對話框和這些控制。 Windows 詞匯 在 Windows 用戶界面和軟件開發(fā)中所要用到的詞匯都是基本和唯一的。本教程的目的是比較詳細地教你如何快速建立專業(yè)級的應(yīng)用程 序的技術(shù)。一旦應(yīng)用程序正確的響應(yīng)了所有允許的控制,它的任務(wù)也就完成了。例如,如果用戶單擊一個按鈕時,你應(yīng)該有代碼來響應(yīng)。 當(dāng)是使用 MFC 時,你編寫的代碼是用來建立必要的用戶界面控制并定制其外觀。 MFC 是可移植的,例如,在 Windows 下編寫的代碼可以很容易地移植到 Windows NT 或 Windows 95 上。它減少了大量在建立 Windows 程序時必須編寫的代碼。 Microsoft 也提供了 C++ 庫,它位于任何 Windows API 之上,能夠使程序員的沈陽航空工業(yè)學(xué)院北方科技學(xué)院畢業(yè)設(shè)計 (外文翻譯 ) 13 工作更容易。該界面是由幾百個 C 函數(shù)所組成,這些函數(shù)在 Windows API 參考手冊中都有介紹。為任何 Windows 平臺建立應(yīng)用程序時,程序員都有兩種選擇: C 或 C++。這對于一些比較小的項目,以及一些大項目的早期原型階段是可以的。 要記住的是,作為程序員必須選擇一組控制并決定如何把它們安排到屏幕上。首先,你要決定什么樣的用戶能使用該程序并根據(jù)需要來設(shè)置相應(yīng)的用戶界面對象。第四部分將介紹消息映射,你將會處理 MFC 的事件。下一節(jié)中將向你詳細解釋這些代碼。 這篇文章將向你介紹 MFC 的基本概念和術(shù) 語以及事件驅(qū)動程序設(shè)計方法。這種層次結(jié)構(gòu)適用于所有版本的 Windows 并彼此兼容。掌握了 C++,你就必須掌握 Microsoft 基本類庫 (MFC) 的層次結(jié)構(gòu)。它是一個全面的應(yīng) 用程序開發(fā)環(huán)境,使用它你充分利用具有面向?qū)ο筇匦缘? C++ 來開發(fā)出專業(yè)級的 Windows 應(yīng)用程序。 csCreate(hello world, WS_CHILD|WS_VISIBLE|SS_CENTER, CRect(50,80,150,150), this)。 } // The constructor for the window class CHelloWindow::CHelloWindow() { 沈陽航空工業(yè)學(xué)院北方科技學(xué)院畢業(yè)設(shè)計 (外文翻譯 ) 7 // Create the window itself Create(NULL, Hello World!, WS_OVERLAPPEDWINDOW, CRect(0,0,200,200))。 m_pMainWndUpdateWindow()。 // The InitInstance function is called each // time the application first executes. BOOL CHelloApp::InitInstance() { m_pMainWnd = new CHelloWindow()。 public: CHelloWindow()。 // Create an instance of the application class CHelloApp HelloApp。t worry about that. We will examine the code in detail in the next tutorial. For now, the goal is to use the Visual C++ environment to create, pile and execute this simple program. // include // Declare the application class class CHelloApp : public CWinApp { public: virtual BOOL InitInstance()。s actions on the different controls using the mouse and the keyboard. Each user interface object on the screen will respond to events differently. For example, if the user clicks the Quit button, the button must update the screen appropriately, highlighting itself as necessary. Then the program must respond by quitting. Normally the button manages its appearance itself, and the program in some way receives a message from the button that says, The quit button was pressed. Do something about it. The program responds by exiting. Windows follows this same general pattern. In a typical application you will create a main window and place inside it different user interface controls. These controls are often referred to as child windowseach control is like a smaller and more specialized subwindow inside the main application window. As the application programmer, you 沈陽航空工業(yè)學(xué)院北方科技學(xué)院畢業(yè)設(shè)計 (外文翻譯 ) 5 manipulate the controls by sending messages via function calls, and they respond to user actions by sending messages back to your code. If you have never done any eventdriven programming, then all of this may seem foreign to you. However, the eventdriven style of programming is easy to understand. The exact details depend on the system and the level at which you are interfacing with it, but the basic concepts are similar. In an eventdriven interface, the application paints several (or many) user interface objects such as buttons, text areas, and menus onto the screen. Now the application waitstypically in a piece of code called an event loopfor the user to do something. The user can do anything to any of the objects on screen using either the mouse or the keyboard. The user might click one of the buttons, for example. The mouse click is called an event. Event driven systems define events for user actions such as mouse clicks and keystrokes, as well as for system activities such as screen updating. At the lowest level of abstraction, you have to respond to each event in a fair amount of detail. This is the case when you are writing normal C code directly to the API. In such a scenario, you receive the mouseclick event in some sort of structure. Code in your event loop looks at different fields in the structure, determines which user interface object was affected, perhaps highlights the object in some way to give the user visual feedback, and then performs the appropriate action for that object and event. When there are many objects on the screen the application bees very