Windows application Run mechanism (note)

Source: Internet
Author: User

Windows Applications: applications that run on Windows as a platform.


API Functions


API functions:Windows provides a number of functions that can be called in the application and written in the C language.

the features of the Windows API functions are as follows:

1. Window management functions: To create, move and modify the window functions.

2. Graphics device functions: To achieve the drawing and operation of graphics functions, this type of function is called a set of graphics device interface.

3. System service function: Implement some functions related to the operating system.

window and handle


In a Windows Application, a window is identified by a window handle (HWND) ; To manipulate a window, you have to get a handle to the window.

Handle: A handle is a unique integer value that is used, that is, a 4- byte ( 8 -byte ) Long value in a five -bit program To identify different objects in the application and different instances of similar objects, such as a window, button, icon, scroll bar, output device, control, or file.

Message and Message Loops


       windows After the application has started and created the program window, it enters a circular wait state until a message is obtained. The system establishes a storage space called Message Queuing for each application, and if an event occurs during program operation, windows Span style= "font-family: Song body" will wait for the message queue for this event to be used. The application can be provided by calling windows api hwnd Find the program window that should receive the message, and according to the window provide information, the message as a parameter to invoke a user-written function called "window function" in the window function to process the message. Once processing is complete, the message loop is returned immediately, waiting for the next message to be obtained.

Windows Programming is an event-driven mode of program design, mainly based on the message. the Windows application must accept the message to initiate an operation. When a user wants to complete a feature, it invokes some kind of support for the operating system. The operating system then wraps the user's needs into messages and posts them to the message queue, and finally, the application takes the message out of the message queue and responds.


basic structure of Windows applications

A Windows program typically headers files, source files. Dynamic link library, resources and other components.


<span style= "FONT-SIZE:18PX;" > #include <windows.h>//write the header files that Windows programs must contain LRESULT CALLBACK WndProc (HWND hwnd,//The window handle of the dispatch message UINT message,// The message indicated by the system WPARAM the additional parameters of the wparam,//message LPARAM LPARAM)//The additional parameter of the message {//corresponding to the corresponding message handler switch (message) {case wm_lbuttondown:// The left mouse button is pressed {messagebeep (0);//The API function that emits the sound}break;case wm_destroy:postquitmessage (0);//Drop a WM_QUIT message into the message queue function parameters will be//wm_ The wparam of quit is generally used as the return value of the WinMain function,//getmessage returns a 0 message loop ending program when it receives a WM_QUIT message. Break;default:return DefWindowProc (Hwnd,message,wparam,lparam);//This function calls the default window procedure, the message that the application does not process into the//line default processing. In the process of writing the window is generally placed in the default}}//main function int WINAPI WinMain (hinstance hinstance,//The handle of the current application instance hinstance preinstance,// The handle to the previous application instance,//in the Win32 environment, this parameter is always null LPSTR lpcmdline,//pointer to this program command line int ncmdshow)//The flag that determines how the application window is displayed {//Create a complete window requires the following four Step HWND hwnd;//define the window handle MSG msg;//defines a variable to store the message Char lpszclassname[]= "window"; Wndclass wc;//defines a window type variable//design a window class wc.style=0;//window style wc.lpfnwndproc=wndproc;//window function, callback function//message processing, A part of the wc.cbclsextra=0;//class that is written by the writer is attached to the memory, usually with the memory set to the 0wc.cbwndextra=0;//window, usually set to 0wc.hinstance=hinstance;//defines the application's instance handle Wc.hicon=loadicon (null,idi_application);//The window's icon wc.hcursor=loadcursor (NULL,IDC_ ARROW)///Window cursor wc.hbrbackground= (hbrush) getstockobject (White_brush);//window background color wc.lpszmenuname=null;// Window menu Resource Name wc.lpszclassname=lpszclassname;//window type name//Register window class registerclass (&AMP;WC);//Create Window Hwnd=createwindow (  lpszclassname,//window type name "Windows"///Window instance title ws_overlappedwindow,//window style 0,0,600,400,//(x,y,width,height) Location and size null,//the handle of the parent window null,//the handle of the main menu hinstance,//application instance handle NULL);//is generally set to null//Display and update window ShowWindow (hwnd,//window handle ncmdshow);// The window is displayed in UpdateWindow (HWND); The//updatewindow function refreshes the window by sending a wm_paint//message. UpdateWindow sends the WM_PAINT message directly to the window//procedure function for processing. This message is not placed in the message queue. The message loop//getmessage () Gets the message from the message queue while (GetMessage (&msg,null,0,0)) {translatemessage (&msg);//    Converts a keyboard message to a character message dispatchmessage (&msg);//Dispatches a message to the system}return the Msg.wparam;//winmain function ends, returns a value to the system by return.    Msg.wparam from a message indicating exit, returning the value to the system and exiting. Similar to return 0; This value is not necessarily 0, depending on what the message is. }</span>


Reference: http://blog.csdn.net/subleo/article/details/4440532


Windows application Run mechanism (note)

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.