Windows: principles of window development

Keywords: Windows C Programming SDK

Window development principle

1: Some concepts

  • API: Application Programming Interface, just like the library function of C language, there are packaged functions and interfaces in windows for operating windows applications and operating systems, namely Windows API. The header file is: windows. H (there are about 10000 functions in VS2019). We can check these APIs in offline or online MSDN. Download offline MSDN: MSDNLibraryVisualStudio6.0
  • SDK: software development kit a collection of resources including API function libraries, help manuals, and tools
  • Window: for example, our notepad has title bar, menu, system menu, scroll bar, maximize and minimize windows to form a menu:


In addition, the menu can be divided into customer area and non customer area:
The relationship between them is that the customer area covers the non customer area, and the non customer area is larger than the customer area

  • Handle: a handle can be simply understood as an ID, just like an ID card, which uniquely identifies a resource. For example, common windows have window handles, icons have icon handles, menus have menu handles, cursors have cursor handles, and so on.

2: The simplest Win32 program

(1) : include header file: include < windows. H >

(2) : entry function:

Different from the entry function of C/C + + console program, the entry function here is Winmain function.

The main function is the CUI (Console Uers Interface) application entry function
The Winmain function is a GUI (Graphical User Interface) application entry function

//Simplest Win32 program:
//1. Header file:
#include<Windows.h>
//2. Entry function:
int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR lpCmdLine, int nCmdShow)
{
//3. Function body:
	return 0;
}

Calling convention: the calling convention defines the way to stack the function. We know that the parameters and local variables of the function are stored in the stack. The input sequence is that the parameters are input first, and then the local variables are input according to the declared sequence. So there are two conventions about whether to stack parameters from right to left or from left to right when they are pushed: the function parameters defined by "stdcall" are pushed from right to left, and the windows API functions are pushed by "stdcall", which must be explicitly expressed, that is, there must be "stdcall" before WinMain. By default, C/C + + functions use "cdecl" instead of explicit expression.
Function parameters, local variables stored in the stack
Generally, we use the macro of "stdcall" to replace it, especially WINAPI
WINAPI
CALLBACK
APIENTRY

Parameter list resolution:

  • HINSTANCE: application instance handle type
  • hInstance: the handle of the current application instance (identifying the EXE and managing all resources of the exe)
  • hPreInstance: represents the handle of the previous application instance. 16 is a parameter above exe, which has been discarded by 32-bit and 64 bit. The default is now 0.
  • Lpstr: char * type, pointing to a C language string.
  • lpCmdLine: command line arguments
  • nCmdShow: how windows are displayed (maximize, minimize, hide, etc.)

Explore HINSTANCE:
We go from HINSTANCE to definition:
It can be seen that HINSTANCE is the parameter of the macro function declare Ou handle:

Then, go to the definition of the macro function "declare" handle:
Get: where ාාis the connection symbol

We replace name with HINSTANCE:

Then remove the connector and arrange:

From this we can see that HINSTANCE is a pointer to a structure with only one integer data member. In Windows, pointer types are encapsulated, so in Windows, pointer types are rarely seen, usually replaced by handles.

3: Message prompt box: MessageBox function

#include<Windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR lpCmdLine, int nCmdShow)
{
	MessageBox(NULL, L"My first Win32 application program", L"Tianjin University of science and technology", MB_OK);
	return 0;
}

Effect:

Message box function resolution:
Parameters:

  • The first parameter: the handle of the parent window, NULL if there is no parent window.
  • Second parameter: prompt information of prompt box. 50: Tell the compiler the advantages of using Unicode for string encoding: support languages all over the world, and the operating systems of languages all over the world can also be replaced by TEXT().
  • Third parameter: prompt box title
  • Fourth parameter: combination of key and icon. Combine with scrofula. Note: buttons and icons are combined, not buttons and buttons, and icons and icons are combined

MSDN:
Button:
Icon:

  • Return value: return the value of the clicked button IDOK (OK), idyes (yes), IDCANCEL (cancel), IDNO (no)
//Example:
#include<Windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR lpCmdLine, int nCmdShow)
{
	//Message prompt box
	int nResult = MessageBox(NULL, TEXT("My first win32 application program"), TEXT("Tianjin University of science and technology"), MB_ICONINFORMATION | MB_YESNOCANCEL);
	if (IDYES == nResult)
	{
		MessageBox(NULL, L"Click "yes( Y)"Button", NULL, MB_OK);
	}
	else if (IDNO == nResult)
	{
		MessageBox(NULL, L"Click "no"( N)"Button", NULL, MB_OK);
	}
	else if (IDCANCEL == nResult)
	{
		MessageBox(NULL, L"Click the Cancel button", NULL, MB_OK);
	}
	return 0;
}

4: First window program

Design window class

  1. First define a window:
//Define window wc:
	WNDCLASS wc;   

Let's go to definition:
See that WNDCLASS is a structure type that contains the properties of a window.

  1. Assign values to properties of window wc
  • WC. Style = CS | hredraw | CS | vredraw / / the style of the window class, redraw horizontally and redraw vertically.
  • wc.lpfnwndProc = WindowProc; / / window processing function (window callback function)
  • wc.cbClsExtra = 0; / / additional memory of window class
  • wc.cbWndExtra = 0; / / additional memory of the window
  • Wc.heinstance = heinstance; / / the instance handle of the current application
  • wc.hIcon = LoadIcon (heinstance, makeintresource (idi_logo)); / / if there is no icon, it will be NULL. You can use the system icon LoadIcon. The first parameter is NULL, and the icon handle is NULL
  • wc.hCursor = loadcursor (heinstance, makeintresource (idc_cursor)); / / load cursor, cursor handle
  • wc.hbrBackground = CreateSolidBrush(RGB(0, 255, 0)); / / red, green and blue 0-255 / / background brush color
  • Wc.lpszmenuaname = NULL; / / menu name
  • wc.lpszClassName = szAppClassName;

(2) : register window class

//Register window class:
	if (0 == RegisterClass(&wc))
	{
		MessageBox(NULL, L"This program cannot run at Windows NT upper", L"Reminder", MB_OK | MB_ICONERROR);
		return 0;
	}

(3) : create window

	//Create window:
	HWND hwnd = CreateWindow(
		szAppClassName,                                       //Window type name
		L"Tianjin University of science and technology",                                       //Title of the window
		WS_BORDER | WS_CAPTION |WS_SYSMENU| WS_MAXIMIZEBOX | WS_MINIMIZEBOX,   //WS: the style of Windows Style window
		300, 200,											  //Coordinates of the upper left corner of the window
		800, 600,											  //Width and height of window
		NULL,												  //Parent window handle
		NULL,												  //Menu handle
		hInstance,                                            //Application Instance Handle 
		NULL												  //Additional parameters, information passed by WM create 
	);

(4) : display and update windows

	//4. Display window
	ShowWindow(hwnd, SW_SHOW);
	//SW show: display
	//SW? Showmaxsize: maximize display
	//SW ABCD minimize: minimize display

(5) : message loop

	UpdateWindow(hwnd);

	MSG msg;
	while (GetMessage(&msg,NULL, 0,0))//WM_QUIT: exit message, GetMessage returns 0, cycle ends
	{
		//Convert virtual key messages to character messages
		TranslateMessage(&msg);
		//Distribute messages to window handlers
		DispatchMessage(&msg);
	}

Window processing function:

//Window processing function
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_CLOSE://Window close message
		DestroyWindow(hwnd);//Window destroy function
		break;
	case WM_DESTROY://Window destroy message
		PostQuitMessage(0);//Send the exit message and exit the program directly: WM ﹣ quit
		break;
	}
	return DefWindowProc(hwnd, uMsg, wParam, lParam);//Operating system processing
}

Full code:

#include<Windows.h>
#include"resource.h"
//First window program
//1. Design window class
//2. Register window class
//3. Create window
//4. Display and update window
//5. Message loop

//New types of understanding:
//Lresult long result
//HWND window handle
//Uint used int message type (number)
//WParam usigned int additional information
//Lpstr long additional information
//ATOM    WORD          unsigned short 
//WORD   unsigned short 
//LPCTSTR const wchar_t*
//LPCTSTR =>CONST WCHAR *
//CONST =>const
//WCHAR =>wchar_t 


//Window processing function
LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam);


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR lpCmdLine, int cCmdShow)
{
	//1. Design window class
		//Char szappclassname [] = "trust"; string without L is called narrow string narrow character: one character is one byte, wide character: one character occupies two bytes
		//Wide character: wchar UUT ch = l'a '; wide string: wchar UUT ch = L "a"; wchar UUT ch = L "ABC";
	wchar_t szAppClassName[] = L"tust";                      //Window type name. Any window has a type name. You can select it by yourself
	WNDCLASS wc;
	wc.style			=	CS_HREDRAW | CS_VREDRAW;							 //Style of window class
	wc.lpfnWndProc		=	WindowProc;											 //Window processing function (window callback function)
	wc.cbClsExtra		=	0;													 //Additional memory of window class
	wc.cbWndExtra		=	0;													 //Additional memory for windows
	wc.hInstance		=	hInstance;								             //Instance handle for the current application
	wc.hIcon			=	LoadIcon(hInstance, MAKEINTRESOURCE(IDI_LOGO));		 //If there is no icon, it will be NULL. You can use the system icon LoadIcon. The first parameter is NULL. The icon handle
	wc.hCursor			=	LoadCursor(hInstance, MAKEINTRESOURCE(IDC_CURSOR));  //Load cursor, cursor handle
	wc.hbrBackground	=	CreateSolidBrush(RGB(0, 255, 0));				     //Red, green and blue 0-255 / / background brush color
	wc.lpszMenuName		=	NULL;									             //menu name
	wc.lpszClassName	=	szAppClassName;
	
	//2. Register window class
	if (0 == RegisterClass(&wc))
	{
		MessageBox(NULL, L"This program cannot run at Windows NT upper", L"Reminder", MB_OK | MB_ICONERROR);
		return 0;
	}

	//3. Create window
	HWND hwnd = CreateWindow(
		szAppClassName,                                       //Window type name
		L"Tianjin University of science and technology",                                       //Title of the window
		WS_BORDER | WS_CAPTION |WS_SYSMENU| WS_MAXIMIZEBOX | WS_MINIMIZEBOX,   //WS: the style of Windows Style window
		300, 200,											  //Coordinates of the upper left corner of the window
		800, 600,											  //Width and height of window
		NULL,												  //Parent window handle
		NULL,												  //Menu handle
		hInstance,                                            //Application Instance Handle 
		NULL												  //Additional parameters, information passed by WM create 
	);

	//4. Display window
	ShowWindow(hwnd, SW_SHOW);
	//SW show: display
	//SW? Showmaxsize: maximize display
	//SW ABCD minimize: minimize display


	//Message loop, update window, redraw
	UpdateWindow(hwnd);

	MSG msg;
	while (GetMessage(&msg,NULL, 0,0))//WM_QUIT: exit message, GetMessage returns 0, cycle ends
	{
		//Convert virtual key messages to character messages
		TranslateMessage(&msg);
		//Distribute messages to window handlers
		DispatchMessage(&msg);
	}
	return 0;
}

//Window processing function
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_CLOSE://Window close message
		DestroyWindow(hwnd);//Window destroy function
		break;
	case WM_DESTROY://Window destroy message
		PostQuitMessage(0);//Send the exit message and exit the program directly: WM ﹣ quit
		break;
	}
	return DefWindowProc(hwnd, uMsg, wParam, lParam);//Operating system processing
}

Published 44 original articles, won praise 11, visited 2530
Private letter follow

Posted by dzekic on Sat, 08 Feb 2020 07:26:20 -0800