Chapter Two Windows Programming and MFC Application Framewor

时间:2025-04-23

c++ 学习

C++ and Visualized Programming

Chaper 6 Windows Programming and MFC Introducing

c++ 学习

Index

2.1 The Windows Programming Model

2.1.1 Message Processing 2.1.2 Handles 2.1.3 Windows APIs 2.1.4 A Windows Program

2.2 Introducing MFC

2.2.1 The Visual C++ Components

2.2.2 MFC Application Framework

c++ 学习

2.1 The Windows Programming Model

Programs 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 it 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 program—not the operating system—that determines what gets called and when.

c++ 学习

2.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 mouse click, or a command for a window to repaint itself, among other things.

c++ 学习

2.1 The Windows Programming Model

c++ 学习

2.1.1 Message Processing

Where 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 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 program's unique way of responding to messages that gives it its personality.typedef struct tagMSG { HWND hwnd; UINT message; WPARAM wParam; LPARAM lParam; DWORD time; POINT pt; } MSG;

c++ 学习

2.1.1 Message ProcessingCommon Windows MessagesMessage WM_CHAR WM_COMMAND WM_CREATE WM_DESTROY WM_LBUTTONDOWN WM_LBUTTONUP WM_MOUSEMOVE Sent When A character is input from the keyboard. The user selects an item from a menu, or a control sends a notification to its parent. A window is created. A window is destroyed. The left mouse button is pressed. The left mouse button is released. The mouse pointer is moved.

WM_PAINTWM_QUIT WM_SIZE

A window needs repainting.The application is about to terminate. A window is resized.

c++ 学习

2.1.2

Handles

Handles are used 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 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 knows how to use it to reference the object.

c++ 学习

2.1.2

Handles

Common HandlesIdentifier HINSTANCE HWND HDC Meaning Handle to an "instance"—the program itself Handle to a window Handle to a device context

c++ 学习

2.1.3 Windows APIsTo a programmer, an operating system is defined by its API. An API encompasses all the function calls 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 new Win32 API, either directly or indirectly.

c++ 学习

2.1.4 A Windows Program

The 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 window 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 messages, it does little except wait for the next message to arrive.

c++ 学习

c++ 学习

2.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 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. That's why RegisterClass is called at the outset of the program.

c++ 学习

2.1.4 A Windows ProgramStep 2:

Once the WNDCLASS is registered, WinMain calls the allimportant CreateWindow function to create the application's window. The first parameter to CreateWindow is the name of the WNDCLASS from which the window will be created. 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 wou …… 此处隐藏:5054字,全部文档内容请下载后查看。喜欢就下载吧 ……

Chapter Two Windows Programming and MFC Application Framewor.doc 将本文的Word文档下载到电脑

    精彩图片

    热门精选

    大家正在看

    × 游客快捷下载通道(下载后可以自由复制和排版)

    限时特价:7 元/份 原价:20元

    支付方式:

    开通VIP包月会员 特价:29元/月

    注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
    微信:fanwen365 QQ:370150219