C程序設(shè)計(jì)教學(xué)課件:Chapter Two Windows Programming and MFC Application Framework_第1頁
C程序設(shè)計(jì)教學(xué)課件:Chapter Two Windows Programming and MFC Application Framework_第2頁
C程序設(shè)計(jì)教學(xué)課件:Chapter Two Windows Programming and MFC Application Framework_第3頁
C程序設(shè)計(jì)教學(xué)課件:Chapter Two Windows Programming and MFC Application Framework_第4頁
C程序設(shè)計(jì)教學(xué)課件:Chapter Two Windows Programming and MFC Application Framework_第5頁
已閱讀5頁,還剩21頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、C+ ProgrammingChaper 7 Windows Programming and MFC IntroducingIndex7.1 The Windows Programming Model7.1.1 Message Processing7.1.2 Handles7.1.3 Windows APIs 7.1.4 A Windows Program7.2 Introducing MFC7.2.1 The Visual C+ Components 7.2.2 MFC Application Framework 7.1 The Windows Programming ModelProgra

2、ms written for traditional operating environments use a procedural programming model in which programs execute from top to bottom in an orderly fashion. The path taken from start to finish may vary with each invocation of the program depending on the input it receives or the conditions under which i

3、t is run, but the path remains fairly predictable. In a C program, execution begins with the first line in the function named main and ends when main returns. In between, main might call other functions and these functions might call even more functions, but ultimately it is the programnot the opera

4、ting systemthat determines what gets called and when. 7.1 The Windows Programming ModelWindows programs operate differently. They use the event-driven programming model, in which applications respond to events by processing messages sent by the operating system. An event could be a keystroke, a mous

5、e click, or a command for a window to repaint itself, among other things. 7.1 The Windows Programming Model7.1.1 Message ProcessingWhere do messages come from, and what kinds of information do they convey? Windows defines hundreds of different message types. Most messages have names that begin with

6、the letters WM and an underscore, as in WM_CREATE and WM_PAINT. One way to characterize a Windows program is to think of it as a collection of message handlers. To a large extent, it is a programs unique way of responding to messages that gives it its personality.typedef struct tagMSG HWND hwnd; UIN

7、T message; WPARAM wParam; LPARAM lParam; DWORD time; POINT pt; MSG; 7.1.1 Message ProcessingCommon Windows MessagesMessageSent WhenWM_CHARA character is input from the keyboard.WM_COMMANDThe user selects an item from a menu, or a control sends a notification to its parent.WM_CREATEA window is create

8、d.WM_DESTROYA window is destroyed.WM_LBUTTONDOWNThe left mouse button is pressed.WM_LBUTTONUPThe left mouse button is released.WM_MOUSEMOVE The mouse pointer is moved.WM_PAINTA window needs repainting.WM_QUITThe application is about to terminate.WM_SIZEA window is resized.7.1.2 Handles Handles are u

9、sed quite frequently in Windows. Like HICON (a handle to an icon), HCURSOR (a handle to a mouse cursor), and HBRUSH (a handle to a graphics brush). A handle is simply a number (usually 32 bits in size) that refers to an object. The handles in Windows are similar to file handles used in conventional

10、C or MS-DOS programming. A program almost always obtains a handle by calling a Windows function. The program uses the handle in other Windows functions to refer to the object. The actual value of the handle is unimportant to your program, but the Windows module that gives your program the handle kno

11、ws how to use it to reference the object.7.1.2 HandlesIdentifierMeaningHINSTANCEHandle to an instancethe program itselfHWNDHandle to a windowHDCHandle to a device contextCommon Handles7.1.3 Windows APIsTo a programmer, an operating system is defined by its API. An API encompasses all the function ca

12、lls that an application program can make of an operating system, as well as definitions of associated data types and structures. Early Windows programmers wrote applications in C for the Win16 application programming interface (API). Today, if you want to write 32-bit applications, you must use the

13、new Win32 API, either directly or indirectly.7.1.4 A Windows ProgramThe entry point for a Windows program is a function named WinMain, but most of the action takes place in a function known as the window procedure.The window procedure processes messages sent to the window. WinMain creates that windo

14、w and then enters a message loop, alternately retrieving messages and dispatching them to the window procedure. Messages wait in a message queue until they are retrieved. A typical Windows application performs the bulk of its processing in response to the messages it receives, and in between message

15、s, it does little except wait for the next message to arrive. 7.1.4 A Windows ProgramStep 1:WinMain begins by calling the API function RegisterClass to register a window class. The window class defines important characteristics of a window such as its window procedure address, its default background

16、 color, and its icon. These and other properties are defined by filling in the fields of a WNDCLASS structure, which is subsequently passed to RegisterClass. An application must specify a window class when it creates a window, and a class must be registered before it can be used. Thats why RegisterC

17、lass is called at the outset of the program. 7.1.4 A Windows ProgramStep 2:Once the WNDCLASS is registered, WinMain calls the all-important CreateWindow function to create the applications window. The first parameter to CreateWindow is the name of the WNDCLASS from which the window will be created.

18、The window that CreateWindow creates is not initially visible on the screen because it was not created with the WS_VISIBLE style. (Had it been used, WS_VISIBLE would have been combined with WS_OVERLAPPEDWINDOW in the call to CreateWindow.) Therefore, WinMain follows CreateWindow with calls to ShowWi

19、ndow and UpdateWindow, which make the window visible and ensure that its WM_PAINT handler is called immediately. 7.1.4 A Windows ProgramStep 3:Next comes the message loop. In order to retrieve and dispatch messages, WinMain executes a simple while loop that calls the GetMessage, TranslateMessage, an

20、d DispatchMessage API functions repeatedly. GetMessage checks the message queue. If a message is available, it is removed from the queue and copied to msg; otherwise, GetMessage blocks on the empty message queue until a message is available. TranslateMessage converts a keyboard message denoting a ch

21、aracter key to an easier-to-use WM_CHAR message.DispatchMessage dispatches the message to the window procedure. The message loop executes until GetMessage returns 0, which happens only when a WM_QUIT message is retrieved from the message queue. When this occurs, WinMain ends and the program terminat

22、es. 7.1.4 A Windows ProgramStep 4:Messages dispatched with DispatchMessage generate calls to the window procedure WndProc. The real action occurs in the window procedure. The window procedure determines what the window displays in its client area and how the window responds to user input. Generally,

23、 Windows programmers use a switch and case construction to determine what message the window procedure is receiving and how to process it accordingly. All messages that a window procedure chooses not to process must be passed to a Windows function named DefWindowProc. 7.2.1 The Visual C+ ComponentsM

24、icrosoft Visual C+ is two complete Windows application development systems in one product. If you so choose, you can develop C-language Windows programs using only the Win32 API. Otherwise, you can develop C+ programs within the MFC library application framework.A quick run-through of the Visual C+

25、components will help you get your bearings before you zero in on the application framework. An overview of the Visual C+ application build process 7.2.1 The Visual C+ ComponentsThe files that Visual C+ generates in the workspace.File ExtensionDescriptionAPSSupports ResourceViewBSCBrowser information

26、 fileCLWSupports ClassWizardDEPDependency fileDSPProject file*DSWWorkspace file*MAKExternal makefileNCBSupports ClassViewOPTHolds workspace configurationPLGBuilds log file7.2.1 The Visual C+ ComponentsThe Visual C+ componentsThe Resource EditorsWorkspace ResourceViewThe C/C+ CompilerThe Source Code

27、EditorThe Resource CompilerThe LinkerThe Debugger7.2.1 The Visual C+ ComponentsAppWizardAppWizard is a code generator that creates a working skeleton of a Windows application with features, class names, and source code filenames that you specify through dialog boxes. AppWizard code is minimalist cod

28、e; the functionality is inside the application framework base classes. AppWizard gets you started quickly with a new application. 7.2.1 The Visual C+ ComponentsClassWizardClassWizard is a program (implemented as a DLL) thats accessible from Visual C+s View menu. ClassWizard takes the drudgery out of

29、 maintaining Visual C+ class code. Need a new class, a new virtual function, or a new message-handler function? ClassWizard writes the prototypes, the function bodies, and (if necessary) the code to link the Windows message to the function. ClassWizard can update class code that you write, so you av

30、oid the maintenance problems common to ordinary code generators.7.2.2 MFC Application FrameworkWhy Use the Application Framework?The MFC library is the C+ Microsoft Windows API.Application framework applications use a standard structure. Application framework applications are small and fast.The Visu

31、al C+ tools reduce coding drudgery. The MFC library application framework is feature rich.7.2.2 MFC Application FrameworkWhats an Application Framework?One reason that C+ is a popular language is that it can be extended with class libraries. A class library is a set of related C+ classes that can be

32、 used in an application.An application framework is a superset of a class library. An ordinary library is an isolated set of classes designed to be incorporated into any program, but an application framework defines the structure of the program itself.7.2.2 MFC Application FrameworkThe Document/View ArchitectureThe cornerstone of MFCs application framework is the document/view architecture, which defines a program structure that relies on document objects to hold an applications data and on view objec

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論