SP: Key points to create a Windows application

Source: Internet
Author: User
Tags win32 window

The main function is to summarize the knowledge content of the preceding parts and to analyze the main functions entry point in detail.

The first thing you need to do in main.cpp is to include the header files and define the function entry points that the WIN32 program requires. It is important to note that the main function entry point of the Win32 form application is the WinMain function (the entry point of the console program is the main function). For now, we just need to include the Windows.h header file at the top of the source file. The header file of the source file main.cpp and the empty WinMain function are found in the following programs:

#include<Windows.h>int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPWSTR int cmdShow){return0;}

Use wWinMain instead of WinMain. The difference between these two main functions is that the third parameter of wWinMain is cmdline using Unicode encoding, and the other using ANSI encoding will convert Unicode to ANSI. As a result, characters in the Unicode string are lost, and using wWinMain allows us to correctly handle the Unicode parameters of the incoming application.

(W) the WinMain function has four parameters, which are defined as follows:
hinstance hinstance: Handle to the current instance of the application (translator Note: The basics of understanding Win32 are best seen in Windows programs
Design Fifth edition).
hinstance previnstance: A handle to the previous instance of the application. According to the MSDN documentation, this parameter will always be NULL. Although this parameter has always been NULL, if you want to determine whether the application already has an instance running, the document recommends using the CreateMutex function to create a unique name for the mutex (mutex). When an instance is already running, the mutex is created again, and the CreateMutex function returns error_already_exists.
LPSTR cmdline : (or LPWSTR with Unicode encoding): The command line for an application is entered by the program. Allows you to pass commands to a program, such as through a CMD command terminal, or through a shortcut to provide command parameters, and so on.
int cmdshow: The window is displayed as the ID number of the pattern (translator Note: For example, minimize, normal, maximize, etc.).

Window initialization

Although the above program can be compiled and run, the runtime does not show anything because the window is not created. So, the next step is to create the Win32 window. First, you register a window class and create the window itself. The application must register its window in the system
The program loop is an infinite loop until the user jumps out of the loop. The end instruction takes place by accepting the Wm_quit event (Win32 's exit message), if the user presses the ESC key in the main menu to generate the event (when the game is pressed, the ESC key is not exited, but is paused, unless the creator has designed the Exit method), or you set it to exit any other way.
MSG is a WIN32 structure that is used to hold window messages from the operating system and is used by programs to respond to these messages. If a certain number of messages are sent after the program has not responded, the operating system will report that the program is not responding. Usually we assume that it means that the program is frozen or some error has occurred, which means that the application is not recovering from the operating system. If the program is designed to be responsive, assume that the program has been frozen and that the window displays a "Not responding" message or that the Task Manager shows that the app is not responding, and that it is still safe for a while. I've been running a lot of complicated and long queries on the web for Microsoft Access. The operating system does not respond to the reporting program, although the program is only busy doing some tasks without handling events.
For the window message, we need to do two things. You first need to get new messages and process them, and then you need to schedule (respond) these messages. The PeekMessage function gets the message of the relevant window (the window we created using the CreateWindow function). The first parameter of this function is the address of the structure holding the message, followed by the window handle (optional), the minimum maximum message Filter flag, and the removal flag. Pm_remove removes the flag and will remove the message from its queue. Because we process the message, it will not need to remain in the queue once processed. If a message is obtained by the PeekMessage function, we call the TranslateMessage function and the DispatchMessage function to respond to the message. The Win32 function TranslateMessage translates the virtual key message into a character message, the dispatch function DispatchMessage passes the message to the window process, and the window procedure performs the actual action on the message it receives. If you do not receive the message, only update updates and render rendering are done in the program. For example, update operations and render scene geometry based on detected user input, physical calculations, animations, updates to the audio cache, and so on.
Take a familiar video game for example, see the game loop is at a glance. The game program for each frame of the rendering do a series of specific things. The frame in the game is a single iterative process of the game loop. Most games strive to reach 30 or 60 frames per second, in other words, the game does 30 to 60 cycles compared to every second in the real world. When the game becomes more and more complex, this goal is more difficult to achieve. The number of frames per second is often used to measure the speed of the game rendering, but the game does not just have the rendering code, but also the physics, collisions, AI (AI) data streams and game effects of the game in every number of frames.

Window procedure (callback function)

The last part before we can compile and run our program is the window procedure provided, which is the window procedure function. A window procedure function is a callback function, which means that the function is called when the message is obtained in our program. Callback functions are generally written in the following form:

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){switch(message){case WM_DESTROY:PostQuitMessage(0);break;default:return DefWindowProc(hwnd, message, wParam, lParam);}return0; }

The window procedure function returns the LRESULT type and has a callback CALLBACK adornment. The name of this function follows the normal function naming standard The callback function takes a handle to the parameter window to dispatch the window's message, unsigned integer message designator, two parameters that carry additional information (WParam and LParam). The last two parameters are used to supplement the data when processing the message in the callback function requires more data.
The exit message is handled by calling the Win32 function PostQuitMessage, which causes the MSG object in the application loop to get the wm_quit message, then ends the application loop and exits the program as we set it. If you want to quit and do not exit immediately, you only need to send an exit message. For example, in Microsoft's Word software, if you try to exit without saving, a dialog box will appear asking if you want to save and then exit.
The last part of the callback function is to call the DefWindowProc function, which has the same parameters as the window procedure function. This function is only called by messages that we do not write a custom response code to. In this Demo we only respond to draw messages and exit messages, but there are a number of other messages that can be found, such as keystrokes, mouse clicks, mouse movements, timers, and so on. The DefWindowProc function is a Win32 function that invokes the default behavior to process all messages to ensure that we respond to every message, even if we are not interested in those messages.

SP: Key points to create a Windows application

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.