Win32 window Program (C ++)

Source: Internet
Author: User

Editor's note: MFC has done a lot of work for us and it is necessary to learn the detailed process.

:

// Win32 dialog box. cpp: Defines the entry point for the application.

//

 

# Include "stdafx. h"

 

// Define the window message Response Function

Lresult callback WndProc (HWND hwnd,

UINT uMsg,

WPARAM wParam,

LPARAM lParam)

{

HDC hdc;

RECT rect;

PAINTSTRUCT ps; // receives the customer plot information returned by BeginPaint.

Switch (uMsg) // receives the client Region coordinates returned by GetClientRect

{

Case WM_PAINT:

{

// The pointer parameter generally indicates the return value of the receiving function, that is, _ out

Hdc = BeginPaint (hwnd, & ps );

GetClientRect (hwnd, & rect );

DrawText (hdc, "http://hi.baidu.com/darks00n",-1, & rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER );

EndPaint (hwnd, & ps );

Break;

}

Case WM_DESTROY: // terminate the corresponding thread after the window is closed

{

PostQuitMessage (0 );

}

Default:

Break;

}

 

Return DefWindowProc (hwnd, uMsg, wParam, lParam); // The default message processing function is used to send unprocessed messages.

}

 

Int APIENTRY WinMain (HINSTANCE hInstance,

HINSTANCE hPrevInstance,

LPSTR lpCmdLine,

Int nCmdShow)

{

// Step 1. Fill in the WNDCLASS struct

WNDCLASS wndclass;

Wndclass. style = CS_HREDRAW | CS_VREDRAW;

Wndclass. lpfnWndProc = WndProc;

Wndclass. cbClsExtra = 0;

Wndclass. cbWndExtra = 0;

Wndclass. hInstance = hInstance;

Wndclass. hIcon = LoadIcon (NULL, IDI_APPLICATION );

Wndclass. hCursor = LoadCursor (NULL, IDC_ARROW );

Wndclass. hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH );

Wndclass. lpszMenuName = NULL;

Wndclass. lpszClassName = "WND ";

 

// Step 2. register the window class

RegisterClass (& wndclass );

 

// Step 3. Create a window

HWND hwnd = CreateWindow ("WND", "Win32 dialog box", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL );

 

// Step 4. Specify the display window status

ShowWindow (hwnd, nCmdShow );

 

// Step 5. display window (send WM_PAINT message)

UpdateWindow (hwnd );

 

// Step 6. Message loop (continuously retrieve and process messages from the window thread Message Queue)

MSG msg;

// The value 0 indicates that WM_QUIT exits the program, and-1 indicates that the Message failed to be obtained.

While (GetMessage (& msg, NULL, 0, 0)> 0)

{

// Translate messages (convert a virtual key message to a character message, and then return it to the window Message Queue)

TranslateMessage (& msg); // differentiate the PreTranslateMessage function in MFC

// Send the message to the Message response function (in this example, WndProc)

DispatchMessage (& msg );

}

 

Return msg. wParam;

}

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.