Start the road of VC

zhaozj2021-02-08  464

The first lecture use crete () method to create a new form application

In general, most Windows applications are constructed by one or several forms. A rich class library is provided in VC to create a Windows Form application. We typically create a form through the CREAMEWND class to create a form, the definition of the Create () function is 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 1, the first parameter lpszclassName is a pointer to a form name string (a Wndclass 2 structure). Such names can be any predefined control class name that is registered by the global function AFXREGIsterWndClass. If it is empty, use the default properties of the CWND class. The second parameter lpszWindowname is a string pointer as a form header. The third parameter DWStyle is a macro defined form type, which is as follows:

WS_Border creates a form with a border.

WS_CAPTION Creates a form with a title bar (implies WS_BORDER). You can't be used with WS_DLGFrame.

WS_CHILD creates a child form. Can't be used with WS_POPUP.

WS_CLIPCHILDREN does not include the area occupied by the subscriber in the parent form. Used to create a parent form.

WS_CLIPSIBLINGS makes child forms, don't live in one another; it is when a specified sub-form receives a PAINT message, the WS_Clipsiblings type will be updated together with the portion of the overlapping subsystem exceeding the area, (if not using WS_CLIPSIBLINGS and the sub-window Body heavy stack, when you draw in a customer area of ​​a child, you may draw to the client area of ​​the neighboring subform.) Only use with WS_CHILD.

WS_DISABLED creates an initial uncomplex form.

WS_DLGFRAME creates a form with bilateral but no title.

WS_GROUP Specifies the first control of the control group that can be moved from one control from one control from one control. 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 horizontal scroll bar.

WS_MAXIMIZE creates a maximum size form.

WS_MAXIMizebox creates a form that maximizes the button.

WS_MINIMIZE creates an initial minimized form. Only use with WS_OVERLAPPED.

WS_MINIMIZEBOX creates a form that minimizes the button.

WS_OVERLAPPED creates an overlapping form. A overlapping form typically has title and border.

WS_OVERLAPPEDWINDOW Creates a overlap form used by WS_OVERLAPPED, WS_CAPPED, WS_SYSMENU, WS_THICKFRAME, WS_MINIMIZEBOX, and WS_MaximizeBox. WS_POPUP creates a pop-up form. Can't be used with WS_CHILD.

WS_POPUPWINDOW creates a pop-up form that is used with WS_Border, WS_POPUP, and WS_SYSMENU. WS_CAPTION must be combined with the WS_POPUPWINDOW to allow the control menu to be visible.

WS_SYSMENU Creates a form in the title bar with a control menu box. Can only be used with a form with a title bar.

WS_TABSTOP Specifies one of any number of controls that can be moved by the user using the TAB key. The Tab key moves the user to move to the next control specified by WS_TABSTOP.

WS_THICKFRAME creates a Window with a thick border that allows it to change the size.

WS_Visible creates an initial visible form.

WS_VScroll creates a form with a vertical scroll bar.

Since the constants in the above table can be combined, the bit or operation is used, so the value of the constant name is defined as a 32-bit 16-based number of 0x00c00000L. For example, the WS_SYSMENU | WS_MINIMIZEBOX is 0x00080000L | 0x00020000L. 0x in front of the number is 16-bit identifier, and L represents 32 bits, the context of the above formula can be represented as follows:

= 0000000000000000000000000000000000000000000000000000000000000000000000000000000 ------------------------------------------ 00000000000000000000000 = 0x000A0000L = 655360;

When you use a decimal number 655360, you will see the same result when you replace WS_SYSMENU | WS_MINIMIZEBOX as a stream.

The fourth parameter RECT is an object of a RECT structure for specifying the size and location of the form. The definition of the RECT structure is as follows:

TypedEf struct; long top; long right; long bottom;} Rect, * Prect, Near * npRect, Far * lpre;

We can initialize a RECT structure using the constructor of the CRECT class produced from the TagRect structure. A heavy duty constructor of CRECT is defined as follows:

// from Left, Top, Right, And BottomcRect (Int L, Int T, Int r, INT B);

The fifth parameter pParentWnd is used to specify a parent form, which is a pointer to the CWND class object. The sixth parameter NID is used to specify a form ID as a child form. The last parameter is about creating a pointer, which has been default to null, and it is not necessary.

Note:

1 Function of the virtual function: If you do not define a virtual function if the same name function you need to be overloaded in the derived class is not defined as a virtual function, when the pointer defined by the base class is pointed to the address of the derived class object (assignment compatibility rules), this pointer The same name function called is defined in the base class; if the defined virtual function is defined, the same name function called by the pointer is defined in the object pointing to by the pointer.

Second to talk about the application of CREATE () method

Below is an example of creating a form with a CREATE () function of the CFrameWnd class:

/ # include

class CMyWnd: public CFrameWnd {public: CMyWnd () {Create (AfxRegisterWndClass (CS_DBLCLKS, 0, HBRUSH (COLOR_WINDOWFRAME), AfxGetApp () -> LoadStandardIcon (IDI_APPLICATION)), __ T ( "Creamdog"), WS_SYSMENU, CRect (100, 100 , 500, 500), null; showwindow (sw_shownormal);};}; class cmyapp: public cwinapp {public: virtual bool initInstance () {m_pmainwnd = new cmywnd; return

CMYAPP myApp; /

First, a CMYWND class is generated from its class CFrame, and the constructor is defined to generate a new form. In the constructor, create a form using the Creat () function, where the system global function AFXREGISTERWNDCLASS () is used to register a form class, the specific definition of the function is as follows:

LPCTSTR AFXAPI AFXREGISTERWNDCLASS (Uint NclassStyle, HCursor HCURSOR = 0, Hbrush Hbrbackground = 0, Hicon Hicon = 0);

The first parameter nclassStyle specifies the type of the form class. If all parameters use the default value if NULL. The specific default is:

NclassStyle = CS_DBLCLKS; // Response Double-click Event HCURSOR = IDC_ARROW; // Standard Arrow HBRBACKGROUND = NULL; // Do not update background Hicon = IDI_Application; // Windows logo (in WinXP is small window)

Since the form does not update when using the default value, the four parameters of the general function require artificial designation, and the first parameter is set to CS_DBLCLKs. CS_DBLCLKS is a macro definition of a form type, which lists macro definitions of all form types.

CS_BYTEALIGNCLIENT Aligns the customer area of ​​the form on byte boundary (in X direction). This type will affect its width and its horizontal position when the form is displayed.

CS_BYTEALIGNWINDOW Aligns the window on the byte boundary (in X direction). This type will affect its width and its horizontal position when displayed in the window.

CS_CLASSDC assigns a device environment and shared all forms in the class. Since the form class is detacted, it is a form that can be applied to several thread creation of an application. It also applies to multiple threads to try simultaneously using the same device environment. When this happens, the system only allows a thread to succeed in its drawing operation.

CS_DBLCLKS When the pointer is inside the form belonging to this class, and when the user doubles the mouse, a double-click message will be sent to the form program.

CS_GLOBALCLASS specifies that this form class is an application all class. Application Location is a window body class that is registered with EXE or DDL valid for all modules in the process.

CS_HREDRAW If the form is moved or the sized adjuster changes the size of the client area, re-painted all forms.

CS_NOCLOSE closes the twist.

CS_OWNDC Assigns a unique device environment for each form in this class.

CS_PARENTDC Sets the rectangles cut in the subform into the parent form to make the child window can draw on the parent form. A form of a rule is received from a system cache with a CS_ParentDC property control. It does not give a child form without the device environment or device environment of the parent window. Specify the CS_ParentDC to improve the performance of the application. CS_SAVEBITS saves a part of the screen image of such a form of such a form. When the form is moved, the system uses a saved bitmap to recover the screen image, including other residual forms. Therefore, if the memory is not released by the bitmap, and other screen operations do not make the stored image invalid. The system does not send a WM_PAINT message to the capped form. This type is useful for temporarily displaying a small form (such as a menu or dialog) when the other screen movements occur. This type adds the time required to display the form, as the system must first distribute the memory bitmap. CS_VREDRAW If the form is moved or the sized adjuster changes the height of the client, re-painted all forms.

The macro definition value in the above table is similar to the 16-bit 16-digit number of 0x0080, so they can be combined with a bitwise or | symbol, and the principle is introduced.

The second parameter hcursor is the handle of the mouse pointer, but since the cursor is redrawd as an arrow due to the formation of the form, it is not aware of this, and it is 0. . If you need to define the cursor, you should first turn off the digital scale, the method is: The ONSETCURSOR () function of the Form class class, allowing it to return to the true value, so when the oversetcursor () letter does not work when redrawing . You can use the following statement.

SetCursor (AFXGetMainWnd () -> LoadStandardCursor (IDC_IBEAM));

Where: setCursor () is a global function, used to set the cursor parameter of the entire routine is a macro definition cursor handle. AFXGetMainWnd () is a system function that returns the handle of the current main form. CWINAPP's LoadStandCursor () member function is used to read a system pointer, and each system pointer is defined as follows:

Standard Standard arrow arrow IDC_ARROW IDC_CROSS crosshairs IDC_APPSTARTING hourglass with a small (for positioning) IDC_HAND Windows 2000: IDC_HELP-hand arrow with a question mark IDC_IBEAM I-beam IDC_ICON Obsolete for applications marked version 4.0 or later IDC_NO prohibited character IDC_SIZE Obsolete for applications. marked version 4.0 or later. Use IDC_SIZEALL. IDC_SIZENESW on IDC_SIZEALL cross arrow pointing northeast and southwest of the two-way arrow IDC_SIZENS pointing double arrow pointing north and south IDC_SIZENWSE IDC_SIZEWE double arrow pointing northwest and southeast of things bidirectional arrow arrow IDC_WAIT hourglass IDC_UPARROW

The macro defined value similar to the MakeintResource (32649) is similar to the MakeintResource (32649).

#define makeintresourceW (i) (LPWSTR) ((DWORD) ((Word))) // Unicode # define makeintresource makeintresourceW

Restore it as an easy-to-understand C code:

Char * (unsigned long (unsigned long (32649)) is to fill it in the system, in order to distinguish between the overload function and the general shaping real parameters (I do, if you want to use itself Define icons or pointer files, which are involved in the instance resource allocation, will be described later. The third parameter hbrbackground, the brush resource for the specified form background, should specify one, otherwise the background will not be updated. This parameter can be used The system color is defined as follows:

COLOR_SCROLLBAR 0COLOR_BACKGROUND 1COLOR_ACTIVECAPTION 2COLOR_INACTIVECAPTION 3COLOR_MENU 4COLOR_WINDOW 5COLOR_WINDOWFRAME 6COLOR_MENUTEXT 7COLOR_WINDOWTEXT 8COLOR_CAPTIONTEXT 9COLOR_ACTIVEBORDER 10COLOR_INACTIVEBORDER 11COLOR_APPWORKSPACE 12COLOR_HIGHLIGHT 13COLOR_HIGHLIGHTTEXT 14COLOR_BTNFACE 15COLOR_BTNSHADOW 16COLOR_GRAYTEXT 17COLOR_BTNTEXT 18COLOR_INACTIVECAPTIONTEXT 19COLOR_BTNHIGHLIGHT 20

The fourth parameter is the identifier of the program icon, which is 0 is the default Windows logo. Similar to the cursor, add the statement of the setting icon when you need to customize the icon, for example:

AFXGETMAINWND () -> seticon (AFXGetApp () -> LoadStandardon (IDi_exclamation), false;

Unlike the setting cursor, the function of setting the cursor is a global function, and the function of setting the icon is a member function of the CWINAPP class (because the icon is active in the form), so the system function AFXGETMAINWND () is required before the function calls. Get the handle of the current main form, then use the CWINAPP's LoadStandardIn () member function to read the system icon and return a handle of an icon, and finally the CFRameWnd class seticon () member function sets the form icon set to the icon handle just returned. .

Back to the CREATE () statement, the first parameter is used by the class name just registered. In the second parameter, forced type conversion (__t), this data type does not change any changes to the string, just serving as a standardized programming. The third parameter is the type type of the previously entered, and the WS_SYSMENU is to maximize the form, minimize, close three press. The fourth parameter enables the form to open in the given position and size. The meaning of the fifth parameter this is that this form is a parent window. The sixth parameter indicates no subform. ShowWindow () The name of the name, that is, the form is displayed. Its parameter ncmdshow is how to determine how the form is displayed. It must be one of the following macros:

SW_HIDE 0 hides this form and activates other forms.

SW_MAXIMIZE 3 activates and displays this form to maximize.

SW_MINIMIZE 6 Minimizes the specified form and activates the next form in the top of the z.

SW_RESTORE 9 activates and displays this form. If this form has been maximized or minimized, the system will restore this form to the original size and original position. An application should specify this tag when a minimized form is restored.

SW_SHOW 5 activates and displays this form in the current dimensions and locations.

SW_SHOWMAXIMIZED 3 with SW_MAXIMIZE

SW_SHOWMINIMIZED 2 activates and displays this form as minimization.

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 location, this value is similar to sw_shownormal, unless the form is not activated. SW_SHOWNOACTIVATE 4 Displays this form in the current dimensions and locations. This value is similar to SW_SHOW, unless the form is not activated.

SW_SHOWNORMAL 1 activates and displays a form. If this form has been maximized or minimized, the system will restore this form to the original size and original position. An application should specify this tag when this form is opened.

The definition of the form frame is over. The following is class CMYAPP that is derived from the CWINAPP base class. The virtual member in the base class is overloaded in the class definition, and the concept of virtual functions is in front. It is mentioned. When starting to indicate the statement inside this function, you must first need to explain the data member m_pmainwnd of CWINAPP. M_PMainWnd data members are used to store a pointer to the main form pair of threads in your thread. When the form involves m_pmainwnd is turned off, the MFC library will automatically terminate your thread. If this thread is the main thread in your application, the application will also be terminated. If this data member is NULL, when terminating this thread, the main form of the CWINAPP object of the application will often terminate. m_pmainwnd is a public variable of the CWND pointer. Generally speaking, set this variable when you overreload the initInstance () function. In a working thread, the value of this data is inherited from its parent thread. As mentioned above, the variable is assigned in the reloaded InitInstance () function, allowing it to point to a new framework form class, and this object is instantiated by the derived CMYWnd that is just defined. Return to a true value, indicating that the initialization instance is successful. Finally, an object is instantiated with a CMYAPP class. The system will construct the previously defined classes and letters so that a simple form is created.

转载请注明原文地址:https://www.9cbs.com/read-509.html

New Post(0)