Start of Win32 SDK programming, create window, message processing, message loop

Source: Internet
Author: User

After learning a lot of windows SDK programming knowledge, it is proposed to use Win32 SDK (pure API, non-MFC) to write windowsProgramNow.
Preparation: C language (C ++ is better), VC ++ 6.0 (because it is simpler, you can also use vs2010 to wait ). because VC ++ 6.0 is too old, it does not support some new features of C language (such as the definition location of variables), so we will use it here (and later. preparation of CPP formatSource codeBut basically it is the C language syntax. My platform is Windows XP SP3, and win7 is also compiled.
As this is the first log, write a simple window. For more information about function functions, see online msdn.

The general steps for Windows SDK programming are as follows:
1. register the window class. The wndclassex structure is used. The registerclassex function is used.
2. Create a window, function createmediawex.
3. Message loop, structure MSG, function getmessage, translatemessage, dispatchmessage.
4. Infinite Loop and Message Processing

Below isCodeOf course, there are also analyses:

# Include <windows. h> //  Contains windows-related API functions

// This is the so-called message processing function.
// Callback indicates that the current function is a callback function, that is, a call convention.
// Hwnd: Window handle, umsg: Received message, wparam, lparam: parameters related to the message
Lresult callback wndproc (hwnd, uint umsg, wparam, lparam)
{
Switch (Umsg) // Message Selection
{
// Here I add a left-click message.
Case Wm_lbuttondown:
MessageBox (hwnd, " Left click " , " Prompt " , Mb_ OK );
Return 0 ;

Case Wm_destroy: // When the current window is destroyed
Postquitmessage ( 0 );// Send out program message
Return 0 ; // If the message is processed, 0 is returned to notify windows

Case Wm_close: // When you click Close in the upper-right corner of the window
Destroywindow (hwnd ); // Destruction window
Return 0 ;

Case Wm_paint: // When the window needs to be re-painted
{
Paintstruct PS = { 0 };
Beginpaint (hwnd, & PS ); // Start re-painting

Endpaint (hwnd, & PS ); // End re-painting
Return 0 ;
}
}
Return Defwindowproc (hwnd, umsg, wparam, lparam );
// For messages we do not want to process, such as messages sent when the mouse moves over the window
// We will pass the original message to the default window message processing function for processing, otherwise the application will lose response
}

Int Apientry winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline,Int Ncmdshow)
{
// Note that the windows program should use the winmain entry function instead of the main function.
// Of course, if you do not create a window, you can also use main.
Wndclassex WC = { 0 }; // Window class structure to prepare for registering window classes
WC. cbclsextra = 0 ; // Additional class information, no, set to 0
WC. cbsize = Sizeof (WC ); // Size of the wndclassex Structure
WC. cbwndextra = 0 ; // Extra window memory, no, set to 0
WC. hbrbackground = (hbrush) getstockobject (gray_brush ); // Window background. The gray background is used here.
WC. hcursor = loadcursor (null, makeintresource (idc_arrow )); // Mouse type used by the application
WC. hicon = loadicon (null, makeintresource (idi_application )); // Cursor type
WC. hiconsm = NULL; // Small cursor of the application, no matter, set to null
WC. hinstance = hinstance; // Application instance handle, passed by the winmain Function
WC. lpfnwndproc = wndproc; // This is very important, that is, the message processing function of the current window, passing the wndproc address
WC. lpszclassname = " Vbgk_class " ; // The class name used to create the class. It can be customized.
WC. lpszmenuname = NULL; // Menu. If no menu exists, null is used.
WC. Style = cs_hredraw | cs_vredraw; // Class style, vertical re-painting, horizontal re-painting

If (! Registerclassex (& WC )) // Registration window class
{
MessageBox (null, " Register class failed! " , Null, mb_ OK );
Return 1 ;
}

// The create window introduces the parameters of the createmediawex function.
Hwnd = createdomainwex (
0 , // Extended window style
" Vbgk_class " , // Here is the name of the window class we just created
" Girl does not cry " ,
Ws_overlappedwindow, // Window Style
Cw_usedefault, // X coordinate at Initialization
Cw_usedefault, // Y coordinate
320 , // Window width, which is set to 320
240 ,// Window height
Null, // Parent window handle, no
Null, // Menu, no
Hinstance, // Instance handle, from winmain
Null // The additional parameter used to send the wm_create message, which is generally zero.
);
If (! Hwnd)
{
MessageBox (null, " Createdomainwex failed! " , Null, mb_ OK );
Return 2 ;
}

Updatewindow (hwnd ); // Update window
Showwindow (hwnd, ncmdshow );// Display window

MSG;
Bool Bret;
// Enter message loop
While (Bret = getmessage (& MSG, null, 0 , 0 ))! =- 1 )
{
If (Bret = 0 )
{
Break ;
}

Translatemessage (& MSG ); // Translate messages
Dispatchmessage (& MSG ); // Send and distribute messages
}

Return MSG. wparam; // The winmain function ends and the entire program exits.
}

Illustration:

Girl does not cry (QQ: 191035066) @ cnblogs/nbsofer @

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.