Windows application composition and programming steps

Source: Internet
Author: User

Windows application composition and programming steps:

1, the composition of the application:
A complete Windows application typically consists of five types of files
1.C Language source Program Files
2. Header files
3. module definition File
4. Resource Description File
5. Project documents

2, the source program composition structure:
1. Entry for all applications, similar to the main function
2. Complete a series of definitions and initialization, and generate a message loop

Windows applications, Portal Functions WinMain, window functions wndproc-> form the basic framework
Contains a variety of data types, data structures and functions, and so on.

/*winmain and WndProc are the main body of Windows applications */
(1) WinMain function
1. Basic components: Function description, initialization, and message loop

2. Function: Register window class, set up window and perform necessary initialization
Into the message loop, with an accepted message called the corresponding process
Terminating a program when a message loop is retrieved to Wm_quit

3.WinMain function Description

1 intWINAPI WinMain2 (3HInstance Hthisinst,//application Current Instance handle4HInstance Hprevinst,//application Other instance handle5LPSTR lpszCmdLine,//pointers to program command-line arguments6 intnCmdShow//integer value Identifying how the window is displayed when the application begins execution7 )8 {9 return 0;Ten}


Attention! Windows is multi-tasking, and multiple windows for the same application may exist at the same time,
Windows system execution of each window is called an instance and is uniquely identified with an instance handle
(2) initialization
Definition of window class: Define the form and function of window class->loadicon/loadcursor/getstockobject
Window class Registration: The window class must first be registered after using->registerclass
Create window instance: Creatwindow
Display window: Showwindow,updatewindow
(a) window class definition
By assigning a wndclass to the window class data structure, the data structure contains various
Property. The window class definition uses the following functions:
The role of LoadIcon is to load a window icon in the application. The prototype is:
Hicon LoadIcon (hinstance hinstance,lpctstr ipiconname)
First parameter: The module handle where the icon resource resides, and Null uses the system pre-defined icon
Second parameter: Icon resource Name or system pre-defined icon identification name

//window class definitionWndclass.style =0;//window type is the default typeWndclass. Ipfnwndproc = WndProc;//Defining window procedure FunctionsWndclass.cbclsextra =0;//No expansion of window classWndclass.cbwndextra =0;//No expansion of window instancesWndclass.hinstance = hinstance;//Current instance handleWndclass.hicon = Locadicon (null,idi_application);//the minimized icon for the window is the default iconWndclass.hcursor = Locadcursor (Null,idc_arrow);//window with arrow cursorWndclass.hbrbackground = Getstockobject (White_brush);//window background is whiteWndclass. Ipszmenuname = NULL;//no menu in WindowWndclass. Ipszclassname = Ipszclassname;//window class is named "Window"

The role of LoadCursor is to load a window cursor in the application
Hcursorloadcursor (hinstance hinstance,lpctstr lpcursorname)
First parameter: The module handle where the cursor resource resides, and Null uses the system pre-defined icon
Second parameter: Cursor resource name or system pre-defined icon identification name

Application Call function Getstockobject get system-supplied background brush
Hbrush getstockobject (int nbrush);

(b) Registration window class
The Windows system itself provides some predefined window classes, and programmers can customize the window classes,
The window class must be registered before use. The window was registered by the function
RegisterClass () implementation.

RegisterClass (&wndclass);//wndclass for window class structure
The RegisterClass function returns a Boolean value, and a successful registration returns the True

(c) Creating a window instance
Creating an instance of a window class is implemented by the function CreateWindow ()

The function prototypes are as follows:

HWND CreateWindow (LPCTSTR lpszclassname,//window class nameLPCTSTR Lpsztitle,//Window Title nameDWORD Dwstyle,//Create a style for a windowintX, Y,//top left corner coordinates of the windowintNwidth,nheight,//window width and heightHWND hWndParent,//the parent window handle of the windowHMENU HMENU,//window main dish handleHinstacnce Hinstcane,//the current handle of the application that created the windowLPVOID Lpparam//pointer to a parameter value passed to the window)

(d) Display window
The display of the window class is implemented by the ShowWindow and UpdateWindow functions. Application calls
The ShowWindow function displays a window on the screen
ShowWindow (hwnd,ncmdshow);//ncmdshow identifies the window display form
Sw_hide Hidden window
Sw_shownormal Displaying and activating the window
Sw_showminimize Display and minimize windows
Sw_showmaxmize Display and maximize windows
Sw_shownoactive display but do not activate the window
Sw_restore the original position and size of the recovery window
After the window is displayed, the application calls UpdateWindow to update and draw the user area,
and issue WM_PAINT messages.
UpdateWindow (HWND);

(3) message loop
Windows passes the resulting message to the corresponding process of the window function in the WinMain function
Reads a message from the message queue and places the message in the MSG struct

The common format for message loops is as follows:

1 msg msg; 2 ... .. 3  while (GetMessage (&msg,null,0,0)) 4  5 translatemessage (&msg); // converts the virtual key of a message to character information 6 // to deliver a message to a specified window function 7 }


Where the function getmessage in the form:
(return 0 value, that is, retrieve the Wm_quit message, the program ends the loop and exits)

1 GetMessage 2 (lpmsg,    // pointer to MSG structure 3hwnd,4//  Minimum message number value for message filtering 5// maximum message number value for message filtering 6 )

Windows application composition and programming steps

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.