Chapter 2 Study Notes

Source: Internet
Author: User
Diyinside Community academy course (Beta)

Windows API self-study course (3)-Windows message queue mechanism

Learning task:

1. Explain the Windows message event model;

2. Read the hellowin. C code in detail and explain the code;

3. Detailed steps for running Windows API applications

4. Compile A Win32 API application with Windows message events

Learning Win32 API homework by citypw (Shawn Chang)
 
Learning content:

Read <programming windows, Fifth Edition> Chapter 3
1. Explain the Windows message event model;
Windows development is different from Dos development models. Windows development is based on message event-driven design. When a user needs to complete a function, it calls the API of the operating system, then, the operating system packs user needs into messages and puts them into the queue. The queue is a FIFO, finally, the application removes the message from the message queue and responds to it.

2. Read the hellowin. C code in detail and explain the code;

3. Detailed steps for running Windows API applications
Create window: 1. Design a window class 2. Register window class 3. Create Window 4. Display and update window
The user starts the operation and starts the message loop until the application is closed.

4. Compile A Win32 API application with Windows message events
# Pragma comment (Lib, "winmm. lib") // use a file required by multimedia.
# Include <windows. h>

// Message Processing Function
// Parameter: Window handle, message, Message Parameter, Message Parameter
Lresult callback wndproc (hwnd, uint message, wparam, lparam)
{
HDC;
Rect;
Paintstruct pS;
// Process messages of interest
Switch (Message)
{
Case wm_create:
// Self-recorded
Playsound (text ("hello.wav"), null, snd_filename | snd_async );
Return 0;

Case wm_paint:
// Draw a "hello, quake III Arena"
HDC = beginpaint (hwnd, & PS );

Getclientrect (hwnd, & rect );

Drawtext (HDC, text ("Hello, quake III Arena"),-1, & rect,
Dt_singleline | dt_center | dt_vcenter );
Endpaint (hwnd, & PS );
Return 0;
Case wm_keydown:
// A message is displayed when you press the letter or number on the keyboard.
MessageBox (hwnd, text ("shit"), text ("yeah"), null );
Return 0;

Case wm_destroy:
// When the user closes the window, the window is destroyed, the program needs to end, and an exit message is sent to exit the message loop.
Postquitmessage (0 );
Return 0;
}
// Default processing functions provided by the system for other messages
Return: defwindowproc (hwnd, message, wparam, lparam );
}

// Main function of the application
// Parameters: instance handle, the handle of the previous instance, command line parameters, and window display mode
Int winapi winmain (hinstance, hinstance hprevinstance,
Pstr szcmdline, int icmdshow)
{
// 1. register the window class
Static tchar szappname [] = text ("hellowin"); // window class name

Wndclass; // custom "window class" Structure
Wndclass. Style = cs_hredraw | cs_vredraw;
Wndclass. lpfnwndproc = wndproc; // correlation Message Processing Function
Wndclass. cbclsextra = 0;
Wndclass. cbwndextra = 0;
Wndclass. hinstance = hinstance; // instance handle
Wndclass. hicon = loadicon (null, idi_application); // icon
Wndclass. hcursor = loadcursor (null, idc_arrow); // cursor
Wndclass. hbrbackground = (hbrush) getstockobject (white_brush); // painter
Wndclass. lpszmenuname = NULL;
Wndclass. lpszclassname = szappname; // Class Name
// Register
If (! Registerclass (& wndclass ))
{
MessageBox (null, text ("registerclass fail! "),
Szappname, mb_iconerror );
Return 0;
}

// Create a window
Hwnd;
Hwnd = createwindow (szappname, // window class name
Text ("the hello program"), // window title
Ws_overlappedwindow, // window style
100, // X coordinate at window startup
300, // y coordinate at window startup
500, // The initial window width
300, // The initial window height
Null,
Null,
Hinstance, // instance handle
Null );

Showwindow (hwnd, icmdshow );
Updatewindow (hwnd );

// Message loop
MSG;
While (getmessage (& MSG, null, 0, 0) // retrieve the message from the Message Queue
{
Translatemessage (& MSG); // convert the message
Dispatchmessage (& MSG); // resend the message
}
Return msg. wparam;
}

ID: citypw
Shawn Chang
Http://blog.csdn.net/raiden56

See Chapter 1st of programming windows and sunxin video.

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.