Now that you want to use C ++ to write dx, you should understand some common knowledge about C ++ programming with Windows SDK ~

Source: Internet
Author: User
Tags case statement

**************************************** *************
Windows SDK programming example
**************************************** **************/

/* The Win32 application framework consists of "initialization window class", "window registration class", "Window Creation", and "window message function */
# Include "stdafx. H"

Lresult callback wndproc (hwnd, uint, wparam, lparam); // Window Function Description

/*
The winmain function is the entry to all Windows applications. Similar to the main function in C, it is used to complete a series of definition and initialization tasks and generate a message loop. Message loop is the core of the entire program running. The winmain function provides the following functions.
1. register the window class, create a window, and execute other necessary initialization work;
2. Enter the message loop and call the corresponding processing process based on the messages received from the application message queue.
3. Terminate the program when the wm_quit message is retrieved cyclically.

The winmain function has three basic components: function description, initialization, and message loop.

The winmain function is described as follows:
Int winapi winmain (// description of the winmain Function
Hinstance, // current instance handle of the program
Hinstance hprevinstance, // other application instance handles
Lpstr lpcmdline, // pointer to the program command line parameter
Int ncmdshow // The integer ID of the window display mode when the application starts to execute
)
Windows is a multi-task operating system that can manage multiple tasks. Therefore, Windows applications may be executed multiple times in parallel, therefore, multiple windows of the same program may exist simultaneously. In Windows, each execution of an application is called an instance of the application ), it is uniquely identified by an instance handle.

*/
Int apientry winmain (hinstance, // description of the winmain Function
Hinstance hprevinstance,
Lpstr lpcmdline,
Int ncmdshow)
{
// Todo: Place code here.
/*
We recommend that you use Pascal's variable definition style, that is, to define all variables at the beginning of a program (function ).
Although the definition of C ++ variables is flexible, this method is not used in order to make the program easy to understand.
*/

Char lpszclassname [] = "window"; // window class name
Char lpsztitle [] = "Windows SDK programming example program"; // window title name

// --------------- Window class definition -------------------------------------
/*
Window class definition
In Windows applications, window Damage defines the window form and functions. The window class definition is completed by assigning values to the wndclass data structure of the window class. The data structure includes various properties of the window class. The following functions are often used in the window class definition process:
*/
Wndclass;
Wndclass. Style = 0; // The window type is the default type.
Wndclass. lpfnwndproc = wndproc; // The Window handler is wndproc.
Wndclass. cbclsextra = 0; // No window class Extension
Wndclass. cbwndextra = 0; // window instance no extension
Wndclass. hinstance = hinstance; // handle of the current instance
 
Wndclass. hicon = loadicon (null, idi_application); // use the default icon
/*
Loadicon (): loads a window icon in the Application
The loadicon () function is prototype:
Hicon loadicon (
Hinstance, // The module handle of the icon resource. If it is null, the system predefines the icon.
Lpctstr lpiconname // Resource Name or predefined icon Identification name
)
*/

Wndclass. hcursor = loadcursor (null, idc_arrow); // The Arrow cursor is used in the window.
/*
Loadcursor (): loads a window cursor in the Application
The loadcursor () function is prototype:
Hcursor loadcursor (
Hinstance, // The module handle where the cursor resource is located. If it is null, the system predefines the cursor.
Lpctstr lpcursorname // The Name Of The cursor resource or the predefined name of the cursor.
)
*/

Wndclass. hbrbackground = (hbrush) getstockobject (white_brush); // The window background is white
/*
Getstockobject (): gets the handles of defined objects such as paint brushes, paint brushes, and fonts.
The prototype of the getstockobject () function is:
Hgdiobj getstockobject (INT fnobject); // fnobject is the object's Identification name

*/

Wndclass. lpszmenuname = NULL; // no menu in the window
Wndclass. lpszclassname = lpszclassname; // The window class name is 'window instance'

// ------------------ Registration of window classes ---------------------------
 
/*
Registration window class
Windows provides some pre-defined window classes. programmers can also customize window classes. Window classes must be registered before use. The registration of window classes is implemented by the registry function registerclass. The format is:
Registerclass (& wndclass)
& Wndclass: window class structure
The Return Value of the registerclass function is a Boolean value. If the registration is successful, the return value is true.
*/
If (! Registerclass (& wndclass) // The registration window. If it fails, a sound is triggered.
{Messagebeep (0 );
Return false;
}

/*
Create a window instance
The createwindow () function is used to create an instance of a window class. The prototype of this function is as follows:
Hwnd createwindow (lpctstr lpszclassname, // create a window, window class name
Lpctstr lpsztitle, // name of the title of the window instance
DWORD dwstyle, // window style
Int X, // coordinates of the upper left corner of the window
Int y, // coordinates of the upper left corner of the window
Int nwidth, // The window width
Int nheight, // The window height
Hwnd hwndparent, // The parent window of this window
Hwenu hmenu, // Main Menu of this window
Hinstance, // current handle of the application
Lpvoid lpparam); // pointer to a parameter value passed to the window
*/
 
// Create a window
Hwnd; // window structure
Hwnd = createwindow (lpszclassname, // create a window, window class name
Lpsztitle, // name of the title of the window instance
Ws_overlappedwindow, // window style
Cw_usedefault, cw_usedefault, // the coordinates in the upper left corner of the window are the default values.
Cw_usedefault, cw_usedefault, // the height and width of the window are the default values.
Null, // No parent window in this window
Null, // No main menu in this window
Hinstance, // current handle of the application
Null); // do not use this value

Showwindow (hwnd, ncmdshow); // display window

Updatewindow (hwnd); // draw the user area
 

/*
Message Loop
Messages are the core of Windows applications. Windows puts the generated messages into the application's message queue, and the application's winmain function extracts messages from the Message Queue cyclically and passes them to the window function for processing.
MSG; // message structure

While (getmessage (& MSG, null, 0, 0) // message loop
{Translatemessage (& MSG );
Dispatchmessage (& MSG );
}
*/

MSG; // message structure
/* Getmessage (): read a message from the message queue and put the message in a MSG structure:
Bool getmessage (
Lpmsg, // pointer to the MSG structure
Hwnd,
Uint wmsgfiltermin, // minimum information number used for message filtering
Uint wmsgfiltermax // The maximum information number used for message filtering. If both the minimum and maximum values are 0, messages are not filtered.
);
When getmessage returns 0, wm_quit is retrieved. The program ends the loop and exits.

Bool translatemessage (const MSG * lpmsg); responsible for converting the virtual key value of the message to character information
Lresult dispatchmessage (const MSG * lpmsg); transmits the message that the parameter lpmsg points to the specified window

*/
While (getmessage (& MSG, null, 0, 0) // message loop
{Translatemessage (& MSG );
Dispatchmessage (& MSG );
}

Return msg. wparam; // when the program is terminated, the information is returned to the Operating System
}

// ----------------------------- Window Function ---------------------------------------
/*
The window message processing function defines the response of different messages received by the application. It contains the processing process of various available received messages by the application. Generally, window functions are composed of one or more switches... case statement, each case statement
Corresponds to a message. When an application receives a message, the corresponding case statement is activated and the corresponding response program module is executed.
The common form of window functions is as follows:
Lresult callback windowproc (hwnd,
Uint umsg,
Wparam,
Lparam
);
Parameters

Hwnd: [in] handle to the window.
Umsg: [in] specifies the message.
Wparam: [in] specifies additional message information. The contents of this parameter depend on the value of the umsg parameter.
Lparam: [in] specifies additional message information. The contents of this parameter depend on the value of the umsg parameter.
Return Value
The return value is the result of the Message Processing and depends on the message sent.

Lresult callback wndproc (hwnd, uint message, wparam, lparam)
{
Switch (Message)
{
Case...
....
Break;
.........
Case wm_destroy:
// The Void postquitmessage (INT nexitcode) function sends a wm_quit message to the program. The nexitcode application exits the code.
Postquitmessage (0); // call this function to send a wm_quit message
Default: // default message processing function to ensure that all messages sent to the window can be processed
Return defwindowproc (hwnd, message, wparam, lparam );
}

Return (0 );
}

*/
Lresult callback wndproc (hwnd, uint message, wparam, lparam)
{
Switch (Message)
{
Case wm_destroy:
Postquitmessage (0); // call this function to send a wm_quit message
Default: // Default Message Processing Function
Return defwindowproc (hwnd, message, wparam, lparam );
}

Return (0 );
}
/*
Note:
Event-driven features:
The windows program is designed to run message processing functions around the event or message generation driver. The execution sequence of Windows programs depends on the order in which events occur. program execution is driven by messages generated sequentially. programmers can write message processing programs for message types to process received messages, or send other messages to drive other processing programs, but you do not have to determine the order in which messages are generated in advance. This is a notable feature of event-driven in object-oriented programming.
The event-driven programming method is very useful for writing interactive programs. The program compiled using this method avoids the rigid operation mode of the program, this allows users to adopt flexible and variable operation modes as they wish.
Message transmission mechanism in Windows applications:
There are several types of messages defined by the system in VC. common messages include window messages, initialization messages, input messages, system messages, clipboard messages, text-to-text interface messages, and DDE (Dynamic Data Exchange) messages, custom messages of applications, etc. The message sent by the application is sent to the Message Queue. The system processes the message in the order of arrival and calls the code of the message processing module in the response.
*/

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.