




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年少先队辅导员网络培训考试题及答案
- 施工现场安全文化的建设与评价体系试题及答案
- 网络直播规范化发展中的商业模式创新与用户互动性提升报告
- 施工现场安全责任制考核试题及答案
- 2025年度农业文化发展知识竞赛试题(附答案)
- 思维决定未来试题及答案
- 环卫中心面试真题及答案
- 环保员面试真题及答案
- 当代家居设计中的功能与美学平衡试题及答案
- 架子工作业试题及答案
- 2021年修订版《中华人民共和国安全生产法》考试题库
- 高温熔融金属企业安全知识培训
- 水利信息化水情监测系统单元工程质量验收评定表、检查记录
- 2024至2030年中国高密度聚乙烯树脂行业投资前景及策略咨询研究报告
- 仿制药与原研药竞争分析
- 临时聘用司机合同范本
- ipo上市商业计划书
- 抖音短陪跑合同范本
- HJ 636-2012 水质 总氮的测定 碱性过硫酸钾消解紫外分光光度法
- 现代风险导向审计在天衡会计师事务所的应用研究
- 拔牙技巧必成高手
评论
0/150
提交评论