Read windows program design (3)

Source: Internet
Author: User

 

"In Windows programming, we are actually implementing an object-oriented Programming (OOP ). This is the most obvious performance of the most used objects in windows ."

"As an object, the user will see these windows on the screen and interact with them directly through the keyboard and mouse. What's more interesting is that the author's point of view of the program is very similar to that of the user. The window receives input from the window in the form of "message". The window also uses messages to communicate with other windows. Understanding the message is one of the obstacles to learning how to write a Windows program ."

"Windows sends messages to a program" refers to a function in a windows call program. The parameters of this function describe the specific message. This type of function in a Windows program is called "window message processing program 」."

"Each window created by the Program has a corresponding window message processing program. This window message processing program is a function, either in the program or in the dynamic link library. Windows sends messages to the window through the call window message processing program. The window message processing program processes the message based on the message and then sends the control back to Windows ."

"The window message processing program processes messages sent to the window. These messages are often used to inform the window that the user is entering with the keyboard or mouse. This is exactly how the key window knows that it is "pressed. When the window size is changed or the window surface needs to be re-painted, other Message notification windows are used ."

"In object-oriented programming, objects are a combination of programs and data. A window is an object whose program is a window message processing program. Data is the information stored by the window message processing program and the information stored by windows for each window and the window category in the system ."

"After the windows program starts to run, Windows creates a" message queue "for the program 」. This message queue is used to store messages in different Windows that may be created by the program. The program contains a short piece of code called "message loop", which is used to retrieve messages from the queue and send them to the corresponding window message processing program. Some messages are directly sent to the window message processing program without being placed in the message queue ."

/*------------------------------------------------------------------------
Hellowin. c -- displays "Hello, Windows 98! "In client area
(C) Charles Petzold, 1998
-----------------------------------------------------------------------*/
# Include <windows. h>

Lresult callback wndproc (hwnd, uint, wparam, lparam );

Int winapi winmain (hinstance, hinstance hprevinstance,
Pstr szcmdline, int icmdshow)
{
Static tchar szappname [] = text ("hellowin ");
Hwnd;
MSG;
Wndclass;
Wndclass. Style = cs_hredraw | cs_vredraw;
Wndclass. lpfnwndproc = wndproc;
Wndclass. cbclsextra = 0;
Wndclass. cbwndextra = 0;
Wndclass. hinstance = hinstance;
Wndclass. hicon = loadicon (null, idi_application );
Wndclass. hcursor = loadcursor (null, idc_arrow );
Wndclass. hbrbackground = (hbrush) getstockobject (white_brush );
Wndclass. lpszmenuname = NULL;
Wndclass. lpszclassname = szappname;

If (! Registerclass (& wndclass ))
{
MessageBox (null, text ("this program requires Windows NT! "),
Szappname, mb_iconerror );
Return 0;
}
Hwnd = createwindow (szappname, // window class name
Text ("the hello program"), // window caption
Ws_overlappedwindow, // window style
Cw_usedefault, // initial X position
Cw_usedefault, // initial y position
Cw_usedefault, // initial X size
Cw_usedefault, // initial y size
Null, // parent window handle
Null, // Window menu handle
Hinstance, // program instance handle
Null); // creation Parameters

Showwindow (hwnd, icmdshow );
Updatewindow (hwnd );
 
While (getmessage (& MSG, null, 0, 0 ))
{
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}
Return msg. wparam;
}

Lresult callback wndproc (hwnd, uint message, wparam, lparam)
{
HDC;
Paintstruct pS;
Rect;

Switch (Message)
{
Case wm_create:
Playsound (text ("hellowin.wav"), null, snd_filename | snd_async );
Return 0;

Case wm_paint:
HDC = beginpaint (hwnd, & PS );
Getclientrect (hwnd, & rect );
Drawtext (HDC, text ("Hello, Windows 98! "),-1, & rect,
Dt_singleline | dt_center | dt_vcenter );
Endpaint (hwnd, & PS );
Return 0;
Case wm_destroy:
Postquitmessage (0 );
Return 0;
}
Return defwindowproc (hwnd, message, wparam, lparam );
}

"Although there are only 80 lines of program code, this window has many surprising features. You can press and hold the title column to move the window on the screen. You can press and hold the border to change the window size. When the window size changes, the program automatically changes "Hello, Windows 98 !」 The string is located in the center of the display area. You can press the maximize button to enlarge hellowin to fill the screen. You can also press the Minimize button to zoom out the program into a graph. You can execute all the options in the System menu (that is, press the small icon at the far left of the title column), or selectCloseOption, or click the close button on the rightmost side of the title column, or double-click the icon on the leftmost side of the title column to close the window to terminate the program execution ."

"Do not bother to remember the numerical constants in Windows programming. In fact, each numerical constant used in Windows has an identifier defined in the header file ."

"Hellowin. C also has a winmain function, but it also has another function named wndproc. This is the window message processing program. Note that no program code for calling wndproc is found in hellowin. C. Of course, there is a reference for wndproc in winmain, and this is why the function should be declared near the beginning of the program"

 

Four data structures defined in the Windows header file

Structure

Description

MSG

Message structure

Wndclass

Window category structure

Paintstruct

Drawing Structure

Rect

Rectangular structure

There are also three uppercase identifiers for different types of "handles 」:

Identifier

Description

Hinstance

Execution entity (program itself) Handle

Hwnd

Window handle

HDC

Device content handle

Handles are frequently used in windows. There are also hicon, hcursor, and hbrush ).

"A handle is a (usually 32-bit) integer that represents an object. Handles in windows are similar to file handles used in traditional C or MS-DOS programming. The program almost always gets the handle by calling the WINDOWS function. The program uses this handle in other Windows functions to use the objects it represents. The actual value of the Code is irrelevant to the program. However, the Windows module that provides code to your program knows how to use it to use the corresponding objects ."

Frequently Used variable prefix

Prefix

Data Type

C

Char, wchar, or tchar

By

Byte (No plus or minus characters)

N

Short

I

Int

X, Y

Int is used as the X coordinate and Y coordinate respectively.

CX, Cy

Int is used as the x length and Y length respectively. C indicates the "counter 」

B or F

Bool (INT); F indicates the flag 」

W

Word (no positive or negative short integer)

L

Long (long integer)

DW

DWORD (long integer without plus or minus signs)

FN

Function)

S

String (string)

SZ

String ending with a byte value of 0

H

Handle

P

Metrics

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.