Windows Programming Basics

Source: Internet
Author: User

1. Event driver
To implement this windows multi-task mode, procedural programming is obviously not suitable. As a result, a programming mode adapted to this mode of work is generated: event-driven programming mode.
The event driver is window-centered and activates the code for processing the corresponding event according to the user's different operations and completes the corresponding tasks.
Event-driven programming is a passive programming method. When a program starts running, it is waiting for the user to enter the event state, and then obtains the event and responds accordingly, after processing is completed, the system returns and is in the waiting event status.

2. Messages
A message is a notification of an event.
In Windows, a message is a Windows data structure that contains the message name, parameters when a message occurs, and function pointers for processing the message.
User operation --> trigger event --> notify Windows System by message
The application obtains a message --> calls the message processing function to process the message.
Messages can be divided into the following four types based on different message sources:
(1) input message
(2) Control Messages
(3) system messages
(4) user messages

Interpreting Win32 applications
In the simplest Win32 program, there are five global functions and three global variables.

1. Handle
A handle is a long integer that uniquely identifies an object. It can be understood as a pointer, but it is actually not the same. Windows system defines many handles, such as window handles and device environment handles, which are encapsulated in MFC, so they are generally invisible. However, there are still many functions in MFC that follow the API habits and use handles as function parameters. If you use API programming, you have to deal with handles frequently.

2. Windows program entry point -- winmain Function
The winmain function mainly performs two actions: one is to generate the main window interface required by almost every Windows program; the other is to establish the heart of the Windows program: Message loop.

Analysis Code:
Int apientry _ twinmain (hinstance,
Hinstance hprevinstance,
Lptstr lpcmdline,
Int ncmdshow)
{
MSG; // defines a Windows message structure

// Initialize the global string
Myregisterclass (hinstance); // first call this function to register a window class

// Execute application initialization:
If (! Initinstance (hinstance, ncmdshow) // in this function, the main window interface is generated.
{
Return false; // if the main window fails to be generated, the program returns directly and exits.
}

// The first task of the winmain () function is to generate the main window.
// The second task of the winmain () function is to establish a master message loop.
// The following cycle is the heart of the Windows program: The main message loop. Once you exit the main message loop, the program ends.

// Main message loop:
While (getmessage (& MSG, null, 0, 0) // use the getmessage function to retrieve messages from the Message Queue;
{
If (! Translateaccelerator (msg. hwnd, hacceltable, & MSG) // explains the acceleration key
{
Translatemessage (& MSG); // use the translatemessage function to explain messages;
Dispatchmessage (& MSG); // send the message to the main window function.
}
}
// Exit the message loop. The winmain function returns the message and the program ends.
Return (INT) msg. wparam;
}

3. Message Queue
Message Queue is the place where the system stores various messages. in windows, there is a message queue, and each application also has its own message queue. The above getmessage function obtains messages from the two message queues. When an Exit message is obtained, the message loop ends and the program ends.

Related Article

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.