Windows program design Reading Notes 1 -- create a window, Program Design Reading Notes

Source: Internet
Author: User

Windows program design Reading Notes 1 -- create a window, Program Design Reading Notes

The first win32 program, simple creation window:

#include <windows.h> LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,                    PSTR szCmdLine, int iCmdShow){     static TCHAR szAppName[] = TEXT ("HelloWin") ;     HWND         hwnd ;     MSG          msg ;     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 (WHITE_BRUSH) ;     wndclass.lpszMenuName  = NULL ;     wndclass.lpszClassName = szAppName ;     if (!RegisterClass (&wndclass))     {          MessageBox (NULL, TEXT ("This program requires Windows NT!"),                       szAppName, MB_ICONERROR) ;          return 0 ;     }          hwnd = CreateWindow (szAppName,                  // window class name                          TEXT ("The Hello Program"), // window caption                          WS_OVERLAPPEDWINDOW,        // window style                          CW_USEDEFAULT,              // initial x position                          CW_USEDEFAULT,              // initial y position                          CW_USEDEFAULT,              // initial x size                          CW_USEDEFAULT,              // initial y size                          NULL,                       // parent window handle                          NULL,                       // window menu handle                          hInstance,                  // program instance handle                          NULL) ;                     // creation parameters          ShowWindow (hwnd, iCmdShow) ;     UpdateWindow (hwnd) ;          while (GetMessage (&msg, NULL, 0, 0))     {          TranslateMessage (&msg) ;          DispatchMessage (&msg) ;     }     return msg.wParam ;}LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){     HDC         hdc ;     PAINTSTRUCT ps ;     RECT        rect ;          switch (message)     {     case WM_CREATE:return 0 ;               case WM_PAINT:          hdc = BeginPaint (hwnd, &ps) ;                    GetClientRect (hwnd, &rect) ;                    DrawText (hdc, TEXT ("Hello, Windows Program!"), -1, &rect,                    DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;                    EndPaint (hwnd, &ps) ;          return 0 ;               case WM_DESTROY:          PostQuitMessage (0) ;          return 0 ;     }     return DefWindowProc (hwnd, message, wParam, lParam) ;}

A window is a WNDCLASS created by the window class. The window class determines the Window Process for processing window messages.

This is defined in MSDN as follows:

typedef struct {  UINT      style;  WNDPROC   lpfnWndProc;  int       cbClsExtra;  int       cbWndExtra;  HINSTANCE hInstance;  HICON     hIcon;  HCURSOR   hCursor;  HBRUSH    hbrBackground;  LPCTSTR   lpszMenuName;  LPCTSTR   lpszClassName;} WNDCLASS, *PWNDCLASS;

In the program, we generally define and initialize

  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 (WHITE_BRUSH) ;     wndclass.lpszMenuName  = NULL ;     wndclass.lpszClassName = szAppName ;

The wndclass. style field indicates the class style. For details, refer to MSDN:

Style Action
CS_BYTEALIGNCLIENT

Aligns the window's client area on a byte boundary (in the x direction ).

This style affects the width of the window and its horizontal placement on the display.

CS_BYTEALIGNWINDOW

Aligns the window on a byte boundary (in the x direction ).

This style affects the width of the window and its horizontal placement on the display.

CS_CLASSDC Allocates one device context to be shared by all windows in the class.
CS_DBLCLKS

Sends a double-click message to the window procedure when the user double-clicks the mouse

While the cursor is within a window belonging to the class.

CS_DROPSHADOW

Windows XP:Enables the drop shadow effect on a window.

The effect is turned on and off through SPI_SETDROPSHADOW.

CS_GLOBALCLASS

Specifies that the window class is an application global class.

For more information, see Application Global Classes.

CS_HREDRAW Redraws the entire window if a movement or size adjustment changes the width of the client area.
CS_NOCLOSE DisablesCloseOn the window menu.
CS_OWNDC Allocates a unique device context for each window in the class.
CS_PARENTDC

Sets the clipping rectangle of the child window to that of the parent window so that the child can draw on the parent.

A window with the CS_PARENTDC style bit parameters es a regular device context from the system's cache of device contexts.

CS_SAVEBITS Saves, as a bitmap, the portion of the screen image obscured by a window of this class.
CS_VREDRAW Redraws the entire window if a movement or size adjustment changes the height of the client area.

Here we select CS_HREDRAW | CS_VREDRAW, indicating that the window will be repainted regardless of the vertical or horizontal size change.

Wndclass. lpfnWndProc indicates the window procedure settings, pointing to a function WndProc.

Wndclass. cbClsExtra and cbWndExtra are used to maintain some reserved space in the internal window structure and can be used as needed.

Wndclass. hInstance indicates that the instance handle of the application is the first parameter of WinMain.

Wndclass. hIcon sets the icon for the window class. To use the local icon, the first parameter must be the hInstance instance handle, the second parameter is the icon, and the predefined icon must start with IDI.

Wndclass. hCursor is similar to the above. Load the mouse pointer.

Wndclass. hbrBackground specifies the background color. Here we use a white brush.

Wndclass. lpszMenuName is the window menu.

Wndclass. lpszClassName specifies the name of the window class.

In this way, the window class is initialized and RegisterClass is used to register the class. below is the window creation,

Using the createWindow function, MSDN is defined as follows:

HWND CreateWindow (_ in LPCTSTR lpClassName, // window name _ in LPCTSTR lpWindowName, // window title _ in DWORD dwStyle, // window style _ in int x, // x coordinate _ in int y, // y coordinate _ in int nWidth, // length _ in int nHeight, // height _ in HWND hWndParent, // parent window handle _ in HMENU hMenu, // Window menu handle _ in HINSTANCE hInstance, // program instance handle _ in LPVOID lpParam // create parameters );

CreateWindow only creates a window internally, that is, windows allocates a piece of memory. to display the window, you must use the ShowWindow function.

The first parameter is the window handle created by CreateWindow, and the second parameter is the iCmdShow value accepted by WinMain. This parameter determines the initial display format of the window.

UpdateWindow is a re-painting window, which is completed by sending a WM_PAINT message.

The last step is the message loop, that is, the message is continuously accepted. When a message exists, the WndProc function is called for processing.

 


Windows Programming creation window

It doesn't matter. It's just a few small errors.
For example, in win32 programming, the NULL of all uppercase letters is used instead of Null. Without such writing, the null of all lowercase letters is used in C.
We also use hInstance instead of hinstance.
Case WM_CLOSE instead of WM_DESTORY
VREDRAW spelling is also incorrect
The following programmers successfully compile the program. You can compare it to find it helpful to you :)

// WNDCLASSEX. cpp: Defines the entry point for the application.
//
# Define WIN_32_LEAN_AND_MEAN
// # Include "stdafx. h"
# Include <windows. h>
// # Include <xwindows. h>

Lresult callback WindowProc (HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
PAINTSTRUCT ps;
HDC hdc;

Switch (msg ){
Case WM_CREATE: {return 0;} break;
Case WM_PAINT: {hdc = BeginPaint (hwnd, & ps); EndPaint (hwnd, & ps); return 0;} break;

// WM_DESTORY
Case WM_CLOSE: {PostQuitMessage (0); return 0;} break;
Default: break ;}

Return (DefWindowProc (hwnd, msg, wparam, lparam ));}

Int APIENTRY WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
Int nCmdShow)
{
HWND hwnd;
MSG msg;

WNDCLASSEX winclass;
Winclass. cbSize = sizeof (WNDCLASSEX );
Winclass. style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC | CS_DBLCLKS;
Winclass. lpfnWndProc = WindowProc;
Winclass. cbClsExtra = 0;
Winclass. cbWndExtra = 0;
Winclass. hInstance = hInstance;
Winclass. hIcon = LoadIcon (NULL, IDI_WINLOGO );
Winclass. hCursor = LoadCursor (NULL, IDC_ARROW );
Winclass. hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH );
Winclass. lpszMenuName = NULL;
Winclass. lpszClassName = "HenryTank1 ";
Winclass. hIconSm = LoadIcon (NULL, ID ...... full text>

Windows Programming

Use the DialogBoxParam (hInstance, MAKEINTRESOURCE (IDD_DIALOG) function in winmain, NULL, dlgproc, NULL );
Use this callback function (name, but it must be consistent with the parameter in DialogBoxParam) to process the message loop in the dialog box.
A processing function I wrote for your reference.
INT_PTR CALLBACK dlgproc (
HWND hwndDlg,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
Switch (uMsg)
{
Case WM_COMMAND:
If (LOWORD (wParam) = ID_Open)
{
OpenPeFile (hwndDlg );
Return (INT_PTR) TRUE;
}
If (LOWORD (wParam) = ID_Close)
{
EndDialog (hwndDlg, NULL );
Return (INT_PTR) TRUE;
}
If (LOWORD (wParam) = ID_About)
{
MessageBox (NULL, "PEView1.0 \ nWrite by iehx! "," Auout ", MB_ OK );
Return (INT_PTR) TRUE;
}
Break;

Case WM_INITDIALOG:
Init (hwndDlg );

Return (INT_PTR) TRUE;
Break;

Case WM_CLOSE:
EndDialog (hwndDlg, NULL );
Break;
}
Return (INT_PTR) FALSE;
}
Note that it is different from the window message loop.
I like writing simple windows using pure APIs, so I don't know how to send messages to me.

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.