【正文】
CRect(0,0,200,200))。它是一個全面的應用程序開發(fā)環(huán)境,使用它你充分利用具有面向對象特性的 C++ 來開發(fā)出專業(yè)級的 Windows 應用程序。這種層次結構適用于所有版本的 Windows 并彼此兼容。下一節(jié)中將向你詳細解釋這些代碼。首先,你要決定什么樣的用戶能使用該程序并根據需要來設置相應的用戶界面對象。這對于一些比較小的項目,以及一些大項目的早期原型階段是可以的。該界面是由幾百個 C 函數所組成,這些函數在 Windows API 參考手冊中都有介紹。它減少了大量在建立 Windows 程序時必須編寫的代碼。 當是使用 MFC 時,你編寫的代碼是用來建立必要的用戶界面控制并定制其外觀。一旦應用程序正確的響應了所有允許的控制,它的任務也就完成了。 Windows 詞匯 在 Windows 用戶界面和軟件開發(fā)中所要用到的詞匯都是基本和唯一的。 Windows 支持幾種類型的應用程序窗口。模式對話框一旦出現在屏幕上,只有當它退出時,屏幕上該應用程序的其余部分才能響應。 Windows 也提供了一種稱為多文檔界面的組織形式,它可用于更復雜的應用程序。在 MDI 框架中,主窗口有一個主菜單,它對主框架中最頂端窗口有效。應用程序界面的好壞取決于你如何選擇和組織這些界面對象。從用戶角度來看 ,各系統的界面對象都是相同的:按鈕、滾動條、圖標、對話框以及下拉菜單等等。為了建立 GUI 程序,程序員第一步要把所有需要的用戶界面控制都放到窗口上。所做的響應依賴于用戶使用鼠標或鍵盤在不同控制上的操作。 Windows 所用的模式也是類似的。 如果你從未做過事件驅動程序設計,則所有這些對你來說可能是很陌生的。應用程序通常通過一段稱為事件循環(huán)的的代碼來響應用戶的操作。事件驅動系統把用戶的動作如鼠標單擊和鍵盤操作定義為事件,也把系統操作如更新屏幕定義為事件。當屏幕上有很多對象時,應用程序會變得很大。如果你把某一用戶界面對象放在屏幕上,你只需要兩行代碼來建立它。第四部分介紹了怎樣使用消息映射來處理事件。如果你是第一次看到這類程序,也許比較難理解。 }。 }。 return TRUE。 } 上面的這段程序如果用 C 來實現,得需要幾頁的代碼。第二,應用程序建立了一個窗口來作為應用程序的主窗口。如果 VC++ 已經在該機器上使用過了,則所顯示的窗口可能有些不同,因為 VC++ 會記憶和自動重新打開上次使用后退出時的項目和文件。在第二部分中我們會仔細研究這段程序, 以理解其結構。第一,它建立了一個應用程序對象。 // 建立靜態(tài)標簽 cs = new CStatic()。 m_pMainWndShowWindow(m_nCmdShow)。 // 說明主窗口類 class CHelloWindow : public CFrameWnd {CStatic* cs?,F在你只要用 Visual C++ 環(huán)境中建立、編譯和運行它就可以了。下面的程序是一段簡單的“ hello world”程序。該函數包含有對該按鈕作出相應操作的代碼。 幸運的是,你可以在比較高級的方法來進行編程,這就是使用 MFC。例如,你用某種類型的結構來接收單擊鼠標事件。例如,用戶用鼠標單擊一個按鈕。具體的細節(jié)對不同的系統可能有些不同,但是其基本概念是類似的。這些控制通常被稱為子窗口 它們就象一些在主窗口中的更小更特殊的子窗口。例如,如果用戶單擊退出按鈕,則該按鈕必須更新屏幕、加亮它自己。 在這個簡單的程序中,程序員可能需要用戶在一個可編輯的編輯框中輸入溫度值,在一個不可編輯的編輯框中顯示轉換結果,然后讓用戶可以單擊一 個標有“退出”的按鈕來退出應用程序。例如,滾動條對于 Windows、 Mac 和 Motif 可能有些不同,但他們的作用完全是一樣的。 事件驅動軟件和詞匯所有基于窗口的 GUI 都包含相同的基本元素,它們的操作方式都是相同的。 MDI 界面可能會給你一種第二桌面的感覺,它對窗口的管理和刪除混亂的窗口有很大的幫助。例如,一個文本編輯器可以允許用戶同時打開多個文本文件。 最簡單的 Windows 應用程序是使用單文檔 界面 (SDI),只有一個框架窗口。一個框架窗口是一個全功能的主窗口,用戶可以改變尺寸、最小化、最大化等。 Windows 應用程序使用幾個標準的控制 : 靜態(tài)文本標簽 按鈕 列表框 組合框 (一種更高級的列表框 ) 單選按鈕 檢查按鈕 編輯框 (單行和多行 ) 滾動條 你可以通過代碼或“資源編輯器”來建立這些 控制,在資源編輯器中可以建立對話框和這些控制。本教程的目的是比較詳細地教你如何快速建立專業(yè)級的應用程序的技術。例如,如果用戶單擊一個按鈕時,你應該有代碼來響應。 MFC 是可移植的,例如,在 Windows 下編寫的代碼可以很容易地移植到 Windows NT 或 Windows 95 上。 Microsoft 也提供了 C++ 庫,它位于任何 Windows API 之上,能夠使程序員的沈陽航空工業(yè)學院北方科技學院畢業(yè)設計 (外文翻譯 ) 13 工作更容易。為任何 Windows 平臺建立應用程序時,程序員都有兩種選擇: C 或 C++。 要記住的是,作為程序員必須選擇一組控制并決定如何把它們安排 到屏幕上。第四部分將介紹消息映射,你將會處理 MFC 的事件。 這篇文章將向你介紹 MFC 的基本概念和術語以及事件驅動程序設計方法。掌握了 C++,你就必須掌握 Microsoft 基本類庫 (MFC) 的層次結構。 csCreate(hello world, WS_CHILD|WS_VISIBLE|SS_CENTER, CRect(50,80,150,150), this)。 m_pMainWndUpdateWindow()。 public: CHelloWindow()。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 standpoint, the systems are all similar in concept, although they differ radically in their specifics. To create a GUI program, the programmer first puts all of the needed user interface controls into a window. For example, if the programmer is trying to create a simple program such as a Fahrenheit to Celsius converter, then the programmer selects user interface objects appropriate to the task and displays them on screen. In this example, the programmer might let the user enter a temperature in an editable text area, display the converted temperature in another uneditable text area, and let the user exit the program by clicking on a pushbutton labeled quit. As the user manipulates the application39。s API Reference books. For Window39。s say you want to create a Windows application. You might, for example, need to create a specialized text or drawing editor, or a program that finds files on a large hard disk, or an application that lets a user visualize the interrelationships in a big data set. Where do you begin? A good starting place is the design of the user interface. First, decide what the user should be able to do with the program and then pick a set of user interface objects accordingly. The Windows user interface has a number of standard controls, such as buttons, menus, scroll bars, and lists, that are already familiar to Windows users. With this in mind, the programmer must choose a set of controls and decide how they should be arranged on screen. A timehonored procedure is to make a rough sketch of the proposed user interface (by tradition on a napkin or the back of an envelope) and play with the elements until they feel right. For small projects, or for the early prototyping phase of a larger project, this is sufficient. The next step is to implement the code. When creating a program for any Windows 沈陽航空工業(yè)學院北方科技學院畢業(yè)設計 (外文翻譯 ) 2 platform, the programmer has two choices: C or C++. With C, the programmer codes at the level of the Windows Application Program Interface (API). This interface consists of a collection of hundreds of C functions described in the Window39。s primary advantage is efficiency. It greatly reduces the amount of code that must be written to create a Windows program. It also provides all the advantages normally found in C++ programming, such as inheritance and encapsulation. MFC is portable, so that, for example, code created under Windows can move to Windows NT or Windows 95 very easily. MFC is therefore the preferred method for developing Windows applications and will be used throughout these tutorials. When you use MFC, you write code that creates the necessary user interface controls and customizes their appearance. You also write code that responds when the user manipulates these controls. For example, if the user clicks a button, you want to have code in place that responds appropriately. It is this sort of eventhandling code t