C Language Call WIN32 API Tutorial 1 Create window

Source: Internet
Author: User
Tags getmessage message queue win32

This learning note is based on the vc++6.0 development environment and calls the Win32 API for Windows System application development through the C language programming language.

1, open vc++6.0, click File-New Project->WIN32 Application project name fill example1, click OK, select an empty project, click Finish.

2, click the "New File" button, create a new blank file, click File-Save as input filename example1.c Select the workspace corresponding folder, save.

3, click FileView, right click on the source file, click Add Files to directory, select example1.c, click OK.

4, add code

#include <windows.h>
#include <Shlwapi.h>


LRESULT CALLBACK Winsunproc (HWND hwnd,uint umsg,wparam wparam,lparam LPARAM); Declaring a message callback function

int WINAPI WinMain (hinstance hinstance,hinstance hpreinstance,ptstr szcmdline,int icmdshow)//win32 API Main program function entry
{
Wndclass wndcls; Defining form Classes
HWND hwnd; Defines the handle that is used to save a successfully created window after a handle is returned
MSG msg; Define message structure body variables
wndcls.cbclsextra=0; The number of bytes appended to the struct, typically a total of 0
wndcls.cbwndextra=0; The number of bytes attached to the form instance, typically a total of 0
Wndcls.hbrbackground= (Hbrush) getstockobject (Black_brush); Background color
Wndcls.hcursor=loadcursor (Null,idc_arrow); Cursor handle
Wndcls.hicon=loadicon (null,idi_application); Icon handle icon displayed on taskbar
Wndcls.hinstance=hinstance; Module handle
Wndcls.lpfnwndproc=winsunproc; function pointer, which points to the function entry for processing window messages
Wndcls.lpszclassname= "Windows"; Custom class name, do not repeat with other class names
Wndcls.lpszmenuname=null; A string of menu names
Wndcls.style=cs_hredraw |                            Cs_vredraw; Specify the window style see parameter Note 1
if (!                                        RegisterClass (&AMP;WNDCLS)) return 0; Register the form class if the failure directly returns 0 end program
Hwnd=createwindow ("Windows", "Create a new Window", ws_overlappedwindow,0,0,300,150,null,null,hinstance,null);//Create a form

Apiws_overlappedwindow for Window Styles//See parameter Note 2
ShowWindow (Hwnd,sw_shownormal)///Display the form's API into the form handle and display it needs to be displayed
UpdateWindow (HWND);//Refresh the form's API

while (GetMessage (&msg,null,0,0))//Enter message loop
{
TranslateMessage (&msg);//Converts a virtual key message to a character message. A character message is sent to the calling thread's message queue, which is read when the next thread calls the function GetMessage or PeekMessage
DispatchMessage (&MSG); The function dispatches a message to the window program. Messages obtained from GetMessage are usually dispatched. The window program to which the message is dispatched is the Mainproc () function
}
return 0;
}
LRESULT CALLBACK Winsunproc (HWND hwnd,uint umsg,wparam wparam,lparam LPARAM)//callback function definition
{
Switch (umsg)
{
Case wm_destroy://Closed window is a message sent by the system
PostQuitMessage (0);//Send exit message getmessage return 0 when message is received, main function exits message loop
Break
Default
return DefWindowProc (Hwnd,umsg,wparam,lparam);//The message that is not processed is given to the system for processing.
}
return 0;
}

5, Parameter detailed analysis

Parameter Note 1

window class Style parameters

1,cs_vredraw: Redraw the entire window when the vertical length changes or moves the window

2,cs_hredraw: Redraw the entire window when the horizontal length changes or moves the window

3,cs_dblclks: Allows a message to be sent to the window by double-clicking the mouse button

When you double-click a form

If the window does not have the CS_dblclks flag, the system sends the following message to the window:

1 wm_lbuttondown 2 wm_lbuttonup 3 wm_lbuttondown 4 wm_lbuttonup

If the window has the CS_dblclks flag, the system sends the following message to the window:

1 wm_lbuttondown 2 wm_lbuttonup 3 wm_lbuttondblclk 4 wm_lbuttonup

4,cs_globalclass: When the CreateWindow or CreateWindowEx function is called to allow its hinstance parameters and to register the window class when the window is created, it is passed to the

The hinstance parameters of the registerclass are different. If this style is not specified, the two hinstance must be the same.

5,cs_bytealignclient: Position the user area of a window on a byte boundary (in the X direction)

6,cs_bytealignwindow: Position the window on a byte boundary (in the X direction)

7,CS_CLASSDC: All window instances of the window class share a window class DC

8,CS_OWNDC: Give each window instance its own DC. Note that although this is convenient, it must be used cautiously, since each DC accounts for approximately 800 bytes of memory.

9,CS_PARENTDC: Sets the clipping region of the child window to the DC of the parent window, so that the window can draw itself on the parent window. Note whether this is a child window or a DC from the system cache instead of the parent window. Use this style to improve system performance.

10,cs_noclose: Disable the System menu off option

11,cs_savebits: Saves the portion of the screen that is obscured by the window as a bitmap, so that when the window is moved, the system can use the saved bitmap to restore the corresponding portion of the screen movement so that the system does not send the WM_PAINT message to the window that is obscured by the window. This feature is appropriate for a window of the menu type because it is usually a short display and then disappears. Setting this attribute increases the time that the window is displayed because it typically allocates the memory that holds the bitmap first.

Parameter Note 2

1,ws_overlapped

Overlapping Windows (overlapped window) with all the features of the main application window. Its non-client area includes a retractable frame, a menu bar, a title block, and a minimized, maximized button.

2,ws_popup
Popup window, with all the features of a message box or dialog box. Its non-client area includes a fixed-size frame and a title bar.

3,ws_child

child window, with all the features of a button-like control. It does not have a non-client area, and the window's processing process is responsible for drawing each part of the window.

4,ws_minimize

Indicates that the window is minimized

5,ws_maximize

Indicates that the window is maximized

6,ws_visible

Controls whether the form is displayed
7,ws_disabled

Control whether a form is available
8,ws_clipsiblings

The meaning of this attribute needs special attention, it means the editing of the Brothers window, that is, the Brothers window is not drawn, why draw a brother window? Because two of Windows may overlap!!! If the Father window is an overlapping window of overlapped, then the sibling window will inevitably involve overwriting the clipping problem. If a child window uses the Ws_clipsiblings property, then the sibling window area covered by the window is not drawn, that is, the window is not drawn under it and the window of the sibling window, the effect is in its following sibling window even if the z-order is smaller than the window " In the window below the sibling window ", if this window is set to clipsublings then the following sibling window can be displayed (not drawing this section so that the following occlusion part of the Brothers show up)
9,ws_clipchildren

The implication is that the parent window is not drawn to the child window area. By default, the parent window is drawn against the background of the child window, but if the parent window has the Ws_clipchildren property set, the Father window is not drawn on the child window background.

10,ws_caption

Create the window with a title bar (meaning WS_BORDER style). cannot be used with the Ws_dlgframe style
11,ws_border

Create a window with a border
12,ws_dlgframe

Creates a window with the border style of the dialog box (bilateral box). This style of window cannot be with the title bar
13,ws_vscroll

Vertical scroll bar appears

14,ws_hscroll

Horizontal scroll bar appears
15,ws_sysmenu

Controls the top right corner of the window (maximized, minimized, off)
16,ws_thickframe

The window can be resized, and the mouse becomes a two-way arrow at the edge of the window
17,ws_group

Specifies a set of controls in which the user can move from one control to the next by the arrow key. The first control that uses the Ws_group style false for all controls defined is after the same group. Use the next control Ws_group style to start the next group (that is, the end of a group at the beginning)
18,ws_tabstop

The control can be specified by one of its users by using any number of the TAB keys that it moves. The TAB key moves the user to the next control ws_tabstop style specified

19,ws_minimizebox

Controls whether the Minimize button is displayed in the upper-right corner of the window
20,ws_maximizebox

Controls whether the Maximize button is displayed in the upper-right corner of the window

Commonly used combinations of definitions

Ws_tiled is the same as ws_overlapped

Ws_iconic is the same as Ws_minimize

Ws_sizebox is the same as Ws_thickframe

Ws_tiledwindow is the same as Ws_overlappedwindow

Ws_overlappedwindow Equivalent (ws_overlapped | ws_caption | Ws_sysmenu | Ws_thickframe | Ws_minimizebox | Ws_maximizebox)

Ws_popupwindow Equivalent (Ws_popup | Ws_border | Ws_sysmenu)

Ws_childwindow equivalent (ws_child)

C Language Call WIN32 API Tutorial 1 Create window

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.