Primary Windows API C + + language programming (3)

Source: Internet
Author: User
Tags bool class definition

Because of the previous chapter of the article I did not introduce to you the specific implementation of the initial, resulting in a lot of readers can not very good understanding, this is my mistake. I now make up, please forgive us.

Initialization of a Windows API program is mainly divided into four parts. 1, the definition of the Window Class 2, window class Registration 3, Create window instance 4, display window

① window class definition

In Windows applications, window classes define the form and functionality of a window. Its definition process is done by assigning values to the window class data structure, which contains the properties of the window class Wndclass. The definition of a window class is commonly used in the following functions, described here.

1. LoadIcon function

The role of the LoadIcon function is to load a window icon in the application. Its prototype is:

HICON LoadIcon
(
   HINSTANCE hInstance, //图标资源所在的模块句柄;
               //如果使用NULL,就会使用系统预定义的图标
   LPCTSTR lpIconName //图标资源名或系统预定义的图标标识名
)
2、LoadCursor函数
LoadCursor函数作用是在应用程序中加载一个窗口光标。它的原型为:
HCURSOR LoadCursor
(
   HINSTANCE hInstance,//光标资源所在的模块句柄;
              //如果使用NULL,它就会使用系统预定义的光标
   LPCTSTR lpCursorName//图标资源名或系统预定义的光标标识名
)

3. Getstockobject function

The application invokes the Getstockobject function to obtain the background brush provided by the system, which is a prototype:

Hbrush getstockobject (int nbrush);

Nbrush The background brush name provided for the system

② Registration window Class

Windows system itself provides some predefined window classes, we can also define our own window class, the window class must be registered before use. The function used by the window class registration is registerclass () implementation. Its prototype is:

RegisterClass (&wndclass);

WNDCLASS is the class structure of the window (the previous second article has already been introduced)

③ Create a window

After the window is registered, it will be instantiated, simply to create a window instance. It is implemented by the function CreateWindow (), and the prototype of the function is:

HWND CreateWindow
(
   LPCTSTR lpszClassName,//窗口实例的类的名字
   LPCTSTR lpszTitle,    //窗口标题名
   DWORD dwStyle,    //新创建的窗口的样式(后面将会详细介绍)
   int x,          //窗口左上角的横坐标
   int y,          //窗口左上角的纵坐标
   int nWidth,        //窗口的宽度
   int nHeight,       //窗口的高度
   HWND hwndParent,   //该窗口的父窗口的句柄
   HMENU hMenu,     //窗口主菜单句柄
   HINSTANCE hInstance, //创建窗口的应用程序当前句柄
   LPVOID lpParam     //指向一个传递给窗口的参数值的指针
)

So what are the window styles?

Identification description

Ws_border Create a general bounding window

Ws_caption Create a window with a title bar

Ws_hscroll Create a window with a horizontal scroll bar

Ws_maximizebox Create a window with a maximized button

Ws_minimizebox Create a window with a minimized button

ws_overlapped Create a window with a border and a title bar

Ws_overlappedwindow Create a window with a border, a title bar, a system menu, and a maximized, minimized button

Ws_popup Create a pop-up window

Ws_popupwindow Create a pop-up window with border and system menus

Ws_sysmenu Create a window of the system menu

Ws_vscroll Create a window with a vertical scroll bar

Of course, in order to achieve a variety of styles of Windows, WINAPI provides a "|" To express and (∪) the meaning of. For example, to implement a pop-up window that has a vertical scrollbar and a horizontal scroll bar, you can enter: ws_bscoll| ws_vscroll| Wm_popup

④ Display window

The above operations are implemented by the function, the display window is certainly no exception. The display of the window class is implemented by the ShowWindow and UpdateWindow functions. The application calls the ShowWindow function to display a window on the screen in the form of:

BOOL ShowWindow (hwnd,ncmdshow)

Where the HWND is the window handle, nCmdShow is the window display form identification

nCmdShow Total six optional values

Identification description

Sw_hide Hidden window

SW_SHOWNORMAL Display and activate Windows

Sw_showminimize Display and minimize windows

Sw_showmaximize Display and maximize windows

Sw_shownoactive displays but does not activate Windows

Sw_restore the original position and size of the recovery window

After the window is displayed, the application should use the UpdateWindow function to update and draw the user area and issue a WM_PAINT message. It is in the form of:

BOOL Updatawindow (HWND);

HWND as Window handle

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.