Start VC-create a form

Source: Internet
Author: User

First, create a form application using the CREATE () method.

Generally, most Windows application interfaces are composed of one or more forms.
VC ++ provides a variety of class libraries for creating Windows Forms applications.
In general, we can use the CREATE () method in the cfreamewnd class to create a form, create ()
The function is defined as follows:

Bool create (lpctstr lpszclassname,
Lpctstr lpszwindowname,
DWORD dwstyle = ws_overlappedwindow,
Const rect & rect = rectdefault,
Cwnd * pparentwnd = NULL ,//! = NULL for popups
Lpctstr lpszmenuname = NULL,
DWORD dwexstyle = 0,
Ccreatecontext * pcontext = NULL );

This is a virtual function ①. The first parameter lpszclassname is a pointer to a form class name string.
(A wndclass ② struct ). Such names can be arbitrary by the global function afxregisterwndclass
The name of the pre-defined control class that has been registered. If it is null, the default properties of the cwnd class are used. Second Parameter
Lpszwindowname is a string pointer to the form title.
The third parameter dwstyle is the form type defined by the macro. The specific definition is as follows:

Ws_border creates a form with a border.

Ws_caption: Creates a form with a title bar (ws_border is hidden). It cannot be used with ws_dlgframe.
Used together.

Ws_child creates a subform. It cannot be used with ws_popup.

Ws_clipchildren does not include the area occupied by the quilt form in the parent form. Creates a parent form.

Ws_clipsiblings isolates subforms from each other, that is, when a specified subform receives
When a painting message is sent, the ws_clipsiblings type does not hold all overlapping sub-Windows.
Updates the parts that exceed the region (if ws_clipsiblings is not used
And child forms overlap. When you draw in the customer area of a child form
It is drawn to the customer area of the adjacent subform .) It is only used with ws_child.

Ws_disabled creates an initial unavailable form.

Ws_dlgframe creates a form with bilateral content but no title.

Ws_group specifies the control that a user can use the direction key to move from one control to another.
The first widget of the widget group. All controls defined with the ws_group
Style false after the first control belong to the same
Group. The next control with the ws_group style starts
The next group (that is, one group ends where the next
Begins ).

Ws_hscroll creates a form with a horizontal scroll bar.

Ws_maximize creates a form of the maximum size.

Ws_maximizebox.

Ws_minimize creates an initial minimal form. It is only used with ws_overlapped.

Ws_minimizebox creates a form with a minimal button.

Ws_overlapped creates an overlapping form. An overlapping form generally has a title and a border.

Ws_overlappedwindow: Create one with ws_overlapped, ws_caption, ws_sysmenu,
Ws_thickframe, ws_minimizebox, and ws_maximizebox 1
Overlapping forms used.

Ws_popup creates a pop-up form. It cannot be used with ws_child.

Ws_popupwindow: Create one with ws_border, ws_popup, and ws_sysmenu
Pop-up form. Ws_caption must be combined with ws_popupwindow
To make the control menu visible.

Ws_sysmenu creates a form with a control menu box in the title bar. Only those with title bars
The form is used together.

Ws_tabstop specifies any number of controls that can be moved by the user using the tab key.
Control. The tab key allows you to move to the next control specified by ws_tabstop.

Ws_thickframe creates a window with a thick border so that it can be changed.

Ws_visible: Create an initial visible form.

Ws_vscroll creates a form with a vertical scroll bar.

Since constants in the preceding table can be combined with bitwise OR operations, the value of the constant name is defined as similar
0x00c00000l 32-bit hexadecimal number type. For example, when ws_sysmenu | ws_minimizebox is performed
0x00080000l | 0x00020000l. The 0x in front of the number is a 16-digit identifier, and l represents 32-bit.
The formula can be expressed as follows:

=
00000000000010000000000000000000
(|) 00000000000000100000000000000000
------------------------------------------
00000000000010100000000000000000 = 0x000a0000l = 655360;

When you replace ws_sysmenu | ws_minimizebox with a decimal number of 655360 as the real parameter, you will see
Same result.

The fourth parameter, rect, is the object of a rect struct, used to specify the size and position of the form. Rect knot
The structure is defined as follows:

Typedef struct tagrect
{
Long left;
Long top;
Long right;
Long bottom;
} Rect, * prect, near * nprect, far * lprect;

We can use the crect class constructor derived from the tagrect struct to initialize a rect structure.
Body. A reload constructor of crect is defined as follows:

// From left, top, right, and bottom
Crect (int l, int T, int R, int B );

The fifth parameter pparentwnd is used to specify the parent form, which is a pointer to the cwnd class object. Sixth
The NID parameter is used to specify the form ID as the child form. The last parameter is a pointer to the content to be created.
The default value is null. ignore this.

Note:

① Role of the virtual function: if the same name needs to be reloaded in the derived class is not
When the base class function is defined as a virtual function, when the pointer defined by the base class points to the address of the derived class object
Value Compatibility Rules). The function with the same name called through this pointer is defined in the base class. Otherwise
Virtual function, the function with the same name called by the pointer is defined in the object to which the Pointer Points.

 

2. Application of the CREATE () method

The following is an example of using the CREATE () function of the cframewnd class to create a form:

//////////////////////////////////////// /////
# Include <afxwin. h>

Class cmywnd: Public cframewnd
{
Public:
Cmywnd ()
{
Create (cs_dblclks, 0, hbrush (color_windowframe), afxgetapp ()-> loadstandardicon (idi_application) ,__ T ("creamdog"), ws_sysmenu, crect (100,100,500,500 ), this, null );
Showwindow (sw_shownormal );
};
};

Class cmyapp: Public cwinapp
{
Public:
Virtual bool initinstance ()
{
M_pmainwnd = new cmywnd;
Return true;
};
};

Cmyapp MyApp;
/////////////////////////////////////////

First, the cmywnd class is generated from its class cframe and the constructor is defined to generate a new form.
In the constructor, The creat () function is used to create a form. The system global function afxregisterwndclass ()
Register a form class. The function is defined as follows:

Lpctstr afxapi afxregisterwndclass (uint nclassstyle, hcursor = 0, hbrush hbrbackground = 0, hicon = 0 );

The first parameter nclassstyle specifies the type of the Form class. If it is null, all parameters use
Default value. The default value is:

Nclassstyle = cs_dblclks; // responds to the double-click event
Hcursor = idc_arrow; // standard arrow
Hbrbackground = NULL; // do not update the background
Hicon = idi_application; // Windows logo (small window in WINXP)

Because the form is not updated when the default value is used, four parameters of the function must be specified manually. The first parameter
Set it to cs_dblclks. Cs_dblclks is a macro definition of the form type. The following table lists all forms.
Type macro definition.

Cs_bytealignclient is aligned to the customer area of the form on the byte boundary (in the X direction. This type will
The width and horizontal position of the form when it is displayed.

Cs_bytealignwindow is aligned on the byte boundary (in the X direction. This type will affect
When the body is displayed, its width and its horizontal position are displayed.

Cs_classdc allocates a device environment and is shared by all forms in the class. Because the Form class
Special processing, which can be applied to several threads of an application
Create a form of the same class. It also applies to multiple threads trying to make
Use the same device environment. In this case, only one
The thread successfully performs its drawing operation.

Cs_dblclks
A double-click message is sent to the form program.

Cs_globalclass specifies that this form class is an application global class. The global application class is
A window by which EXE or DDL registration is effective to all modules in the process
Body class.

Cs_hredraw
All forms.

Cs_noclose: the button is disabled and unavailable.

Cs_owndc assigns a unique device environment for each form in this class.

Cs_parentdc sets the rectangle cut from the child form to the parent form so that the child window can be fully stored in the parent form
Draw on the form. A form slave device with cs_parentdc Property Control
The system cache of the environment receives a rule from the device environment. It does not set the parent window
The device environment or device environment settings of the sub-form. Specify cs_parentdc
To improve application performance.

Cs_savebits stores a part of the screen image that is pinned by a form of this class as a bitmap. When
The form is moved. The system uses the saved bitmap to restore the screen image, including
Other blocked forms. Therefore, if the memory used by bitmap is not released,
In addition, other screen actions do not invalidate the stored image. The system will not send
The wm_paint message is sent to the formed form.
This type of small form (such as dish) is temporarily displayed when other screen actions occur.
Single or dialog box) is very useful. This type adds the time required to display the form,
Because the system must first allocate memory to store bitmaps.
 
Cs_vredraw
All forms.

The macro value in the preceding table is similar to the hexadecimal number of 0x0080.
Or | symbol combination. The principle has been described earlier.

The second parameter hcursor is the handle of the mouse pointer, but the cursor
Will be repainted as an arrow, so it does not make much sense to set this when registering the Form class. Set it to 0. If you need
To define the cursor, you must first disable the number mark re-painting by reloading the onsetcursor () function of the Form class base class,
Let it return the true value, so that the onsetcursor () function does not work when re-painting is performed. You can use the following
Statement.

Setcursor (afxgetmainwnd ()-> loadstandardcursor (idc_ibeam ));

Specifically: setcursor () is a global function, which is used to set the cursor parameter of the entire routine. It is a macro-defined cursor sentence.
Handle. Afxgetmainwnd () is a system function that returns the handle of the current main form. Cwinapp
The loadstandardcursor () member function is used to read a system pointer.
Definition:

Idc_appstarting standard arrow with small hourglass
Standard arrow of idc_arrow
Idc_cross cross cursor (for positioning)
Idc_hand Windows 2000: Manual
Idc_help arrow with question mark
Idc_ibeam I-type logo
Idc_icon obsolete for applications marked version 4.0 or later.
Idc_no prohibition symbol
Idc_size obsolete for applications marked version 4.0 or later. Use idc_sizeall.
Idc_sizeall cross arrow
Idc_sizenesw: bidirectional arrow pointing to the northeast and Southwest China
Idc_sizens bidirectional arrow pointing to South and North
Idc_sizenwse: bidirectional arrows pointing to the northwest and southeast
Idc_sizewe: bidirectional arrow pointing to something
Up Arrow of idc_uparrow
Idc_wait hourglass

In the preceding table, the macro is defined as a function similar to makeintresource (32649 ).
Definition:

# Define makeintresourcew (I) (lpwstr) (DWORD) (Word) (I) // Unicode
# Define makeintresource makeintresourcew

Restore it to a C code that is easy to understand:

Char * (unsigned long (unsigned short (32649 ))
The reason why the system will reverse it is to replace it with the general integer Real-Time Parameter area in the heavy-load function.
No (I estimate ). In addition, if you want to use a custom icon or pointer file, the instance resources are involved.
The issue of allocation will be described later.

The third parameter, hbrbackground, specifies the paint resource of the form background. You must specify one. Otherwise
The background will not be updated. This parameter can use the system color, which is defined as follows:

Color_scrollbar 0
Color_background 1
Color_activecaption 2
Color_inactivecaption 3
Color_menu 4
Color_window 5
Color_windowframe 6
Color_menutext 7
Color_windowtext 8
Color_captiontext 9
Color_activeborder 10
Color_inactiveborder 11
Color_appworkspace 12
Color_highlight 13
Color_highlighttext 14
Color_btnface 15
Color_btnshadow 16
Color_graytext 17
Color_btntext 18
Color_inactivecaptiontext 19
Color_btnhighlight 20

The fourth parameter is the program icon identifier. If it is 0, it is the default Windows logo. Same as the cursor,
When you need to customize the icon, add the statement for setting the icon, for example:

Afxgetmainwnd ()-> seticon (afxgetapp ()-> loadstandardicon (idi_exclamation), false );

Unlike setting the cursor, the function for setting the cursor is a global function, and the function for setting the icon is
Member functions of the cwinapp class (because the icon is only valid in the form), you need to use the system before calling the function.
The main function afxgetmainwnd () is used to obtain the handle of the current main form, and then the cwinapp loadstandardicon ()
The member function reads the System icon and returns a handle to the icon. Finally, the cframewnd class seticon ()
The member function sets the icon of the form as the icon handle just returned.

Return to the CREATE () statement. The first parameter uses the registered class name. In the second parameter
Forced type conversion (_ T) is used. This data type does not make any changes to the string.
Standardized programming. The third parameter is the form type mentioned above, and ws_sysmenu is the most
Big, minimized, and closed buttons. The fourth parameter enables the form to be opened at the given position and size.
The fifth parameter "this" indicates that the form is the parent form. The sixth parameter indicates that there is no child form.
Showwindow () is a display form. Its Parameter ncmdshow is used to determine how the form is displayed.
It must be one of the following macro definitions:

Sw_hide 0 hides the form and activates other forms.

Sw_maximize 3 is activated and the form is displayed to maximize.

Sw_minimize 6 minimizes the specified form and activates the next Z-ordered Median
Form on the top.

Sw_restore 9 is activated and this form is displayed. If the form has been maximized or
To minimize, the system restores the form to the original size and position.
When an application restores a minimal form
Set this flag.

Sw_show 5 is activated on the current size and position and displays this form.

Sw_showmaximized 3 and sw_maximize

Sw_showminimized 2 is activated and the form is displayed as minimized.

Sw_showminnoactive 7 shows that this form is minimized. This value is similar to sw_showminimized,
Unless the form is not activated.
Sw_showna 8 displays this form in the last size and position. This value
Similar to sw_shownormal, unless the form is not activated.

Sw_shownoactivate 4 displays this form in the current size and position. This value is similar to sw_show,
Unless the form is not activated.

Sw_shownormal 1 is activated and a form is displayed. If the form has been maximized
Or minimize, the system will restore the form to the original size and original position.
An application should specify
This tag.

The definition of the form framework is over. The following is the cmyapp class derived from the cwinapp base class.
The virtual member function initinstance () in the base class is overloaded in class definition.
I have mentioned this before. When you begin to describe the internal statements of this function, you must first describe the data members of the cwinapp.
M_pmainwnd.
M_pmainwnd data member is used to store a pointer pointing to the main form direction in your thread. When involved
When the form m_pmainwnd is closed, the MFC Library will automatically terminate your thread. If this thread is your
The main thread in the application will also be terminated. If the data member is null
In this thread, the main form of the cwinapp object for the application is also often terminated. M_pmainwnd is
A public variable of the cwnd class pointer.
Generally, this variable is set when you overload the initinstance () function. In a work thread,
The value of this data member is inherited from its parent thread.
As mentioned above, this variable is assigned a value when the initinstance () function is overloaded
It points to a new framework form class object, which is instantiated by the derived class cmywnd.
. Returns a true value, indicating that the instance is successfully initialized.
Finally, an object is created using a cmyapp instance and its name is random. The system will
To create a simple form.

 

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.