Basic Structure of Win32 application, basic structure of win32

Source: Internet
Author: User

Basic Structure of Win32 application, basic structure of win32
0 Introduction

Win32 API is an application programming interface used to create Windows applications. Generally, a Win32 application contains the following parts:

1) application portal;

2) Registration window class;

3) create a window;

4) display window;

5) Update window

6) message loop;

7) send messages;

1 application portal

The Win32 application uses the WinMain function as the program entry. The function prototype is as follows:

Int WinMain (

HINSTANCE hInstance,

HINSTANCE hPrevInstance,

LPTSTR lpCmdLine,

Int nCmdShow );

2. Registration window class

To register a window class, you must provide a custom window class name. In the window created by this window class name, the operating system delivers the message to the window process function specified when the window class is registered.

The Win32 application uses RegisterClassEx to register the window class. The function prototype is as follows:

ATOM RegisterClassEx (const wndclassex * pWndClass );

When Visual Studio is used to create a win32 project, a function of the registration window is automatically generated. The function implementation is as follows:

ATOM MyRegisterClass (HINSTANCE hInstance)

{

WNDCLASSEX wcex;

Wcex. cbSize = sizeof (WNDCLASSEX );

Wcex. style = CS_HREDRAW | CS_VREDRAW;

Wcex. lpfnWndProc = WndProc;

Wcex. cbClsExtra = 0;

Wcex. cbWndExtra = 0;

Wcex. hInstance = hInstance;

Wcex. hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_SKINUI ));

Wcex. hCursor = LoadCursor (NULL, IDC_ARROW );

Wcex. hbrBackground = (HBRUSH) (COLOR_WINDOW + 1 );

Wcex. lpszMenuName = MAKEINTRESOURCE (IDC_SKINUI );

Wcex. lpszClassName = szWindowClass;

Wcex. hIconSm = LoadIcon (wcex. hInstance, MAKEINTRESOURCE (IDI_SMALL ));

Return RegisterClassEx (& wcex );

}

3. Create window

The Win32 application uses createmediawex to create a window. The function prototype is as follows:

HWND createmediawexw (

_ In DWORD dwExStyle,

_ In_opt maid,

_ In_opt LPCWSTR lpWindowName,

_ In DWORD dwStyle,

_ In int X,

_ In int Y,

_ In int nWidth,

_ In int nHeight,

_ In_opt HWND hWndParent,

_ In_opt HMENU hMenu,

_ In_opt HINSTANCE hInstance,

_ In_opt LPVOID lpParam );

4. display window

The Win32 application uses ShowWindow to display the window. The function prototype is as follows:

BOOL ShowWindow (_ in HWND hWnd, _ in int nCmdShow );

5. Update window

The Win32 application uses UpdateWindow to update the window. The function prototype is as follows:

BOOL UpdateWindow (_ in HWND hWnd );

6. Message Loop

Each Win32 application requires at least one message loop with the following structure:

MSG msg;

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

{

If (! TranslateAccelerator (msg. hwnd, hAccelTable, & msg ))

{

TranslateMessage (& msg );

DispatchMessage (& msg );

}

}

Function prototypes are not listed here. For details, refer to MSDN.

7. Send messages

The window procedure function is used to receive messages delivered by the system. Win32 applications send messages here. A typical window procedure function is as follows:

Lresult callback WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

Int wmId, wmEvent;

PAINTSTRUCT ps;

HDC hdc;

Switch (message)

{

Case WM_COMMAND:

WmId = LOWORD (wParam );

WmEvent = HIWORD (wParam );

// Parse the menu selections:

Switch (wmId)

{

Case IDM_ABOUT:

DialogBox (hInst, MAKEINTRESOURCE (IDD_ABOUTBOX), hWnd, About );

Break;

Case IDM_EXIT:

DestroyWindow (hWnd );

Break;

Default:

Return DefWindowProc (hWnd, message, wParam, lParam );

}

Break;

Case WM_PAINT:

Hdc = BeginPaint (hWnd, & ps );

// TODO: Add any drawing code here...

EndPaint (hWnd, & ps );

Break;

Case WM_DESTROY:

PostQuitMessage (0 );

Break;

Default:

Return DefWindowProc (hWnd, message, wParam, lParam );

}

Return 0;

}

8 conclusion

Through the combination of the above parts, a simple Win32 application is complete. The Code is as follows:

Int WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)

{

UNREFERENCED_PARAMETER (hPrevInstance );

UNREFERENCED_PARAMETER (lpCmdLine );

MyRegisterClass (hInstance );

HWND hWnd = CreateWindow (szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL );

If (! HWnd)

{

Return FALSE;

}

ShowWindow (hWnd, nCmdShow );

UpdateWindow (hWnd );

MSG msg;

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

{

If (! TranslateAccelerator (msg. hwnd, hAccelTable, & msg ))

{

TranslateMessage (& msg );

DispatchMessage (& msg );

}

}

Return (int) msg. wParam;

}

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.