Visual c ++ programming experience (I)

Source: Internet
Author: User
Visual c ++ 5.0 programming experience (I)
Another method to change the window title

In my article titled visual C ++ September 1, 1997 programming experience, published in 4.0, I mentioned a method to change the window title, modify the value of the member variable m_strtitle of the cframewnd class in the precreatewindow () member function of the cmainframe class (Note: it is a derived class of the cframewnd class) of the application. The disadvantage of this method is that you can only set the content of the window title at a time and cannot modify it at any time during the program running. For example, when you need to display the current coordinates of the mouse on the window title, we need to use the full function bool setwindowtext (hwnd, lpctstr lpstring) provided by Visual C ++ ). This function is actually a Win32 function. The first parameter must be the handle of a window framework, and the second parameter must be a 32-bit pointer pointing to a constant string, it is a variable of the lpctstr type. Normally, the content of the window title is dynamically changed in the document class or view class of the application, so there is no ready-made handle pointing to the window framework for us to use, therefore, we also need to call another Win32 function afxgetmainwnd () before using the setwindowtext () function to obtain a pointer to the framework class of the application, for example, using the statement:

Cwnd * m_pcwnd = afxgetmainwnd ();

Then, call the setwindowtext () function as follows:

Setwindowtext (* m_pcwnd, (lpctstr) m_windowtext );

// M_windowtext can be a cstring variable

 

Add bitmap with more than 256 colors as resources to the application

In Visual C ++ 5.0 or earlier versions, the self-contained bitmap editor cannot browse or edit bitmaps of more than 256 colors, and cannot be embedded in resource files) bitmaps of more than 256 colors are used as resources (otherwise, an error is reported when the application is running ). This feature enables us to use other methods to enhance the aesthetics of interface pictures when developing applications using Visual C ++, therefore, you can use visual c ++ to develop the application kernel and the Visual Basic development interface. This problem has been improved in Visual C ++ 5.0. First, you can create and edit a 256-color bitmap in the bitmap editor. Secondly, Visual C ++ 5.0 allows programmers to embed bitmaps of more than 256 colors into resources, but it still cannot be viewed in the Visual C ++ bitmap editor, you must also select Win32 release as the compilation method to generate executable applications. Another condition is that bitmap with more than 256 colors of resources cannot be automatically enabled or disabled by the application kernel. For example, a method for adding Bitmap Buttons to a dialog box is mentioned in the above article titled visual C ++ 4.0 programming experience, that is, the program developer creates four bitmaps for each button to indicate the button's up, down, and focus respectively) and disable, and the combination of the Title name of the button and one of the above four states must be used as the identification of the bitmap, so that when the application draws the bitmap button, automatically find the corresponding resource (that is, bitmap ). However, this automatic ing is only limited to the bitmap that can be opened by the visual C ++ bitmap editor. Therefore, if you select a bitmap of more than 256 colors as the resource of the bitmap button and want to switch between the above four states, you must use the following function and programming reference model.

* S sets four constants: button_up, button_down, button_focus, and button_disable, respectively, to identify the current status of each button.

* S sets a crect Class Object for each bitmap button in the corresponding dialog box class of the application (for convenience described below, we may assume two): m_rect1 and m_rect2, to record the coordinate rectangles occupied by buttons in the dialog box. In the dialog box class, set an integer variable for each button: buton1_status and button2_status to record the current status of each button. Then, initialize the crect class objects and integer variables in the constructor of the dialog box.

* S creates message processing functions in the dialog box class that respond to various statuses of the mouse, such as onmousemove (), onlbuttonup (), and onlbuttondown.

* S press Ctrl and W at the same time or click the classwizard button on the toolbar to open the classwizard dialog box. Select the dialog box class in the class name list box, select the Class Name of the class in the object IDs list box, select the wm_paint message in the message list box, and double-click it, in this case, classwizard adds an onpaint () function to the dialog box class. Then, select the name of the new toolbar button in the object IDs list box and double-click the command message in the message list box. The classwizard will add the corresponding message processing function to the dialog box class. Close the classwizard dialog box.

This article provides some program code for your reference only.

Editor's note: the source program is published inHttp://www.computerworld.com.cn/98/skill/default.htm. Welcome!

 

Do not load menus, toolbar, and status bars in the application.

In the application (SDI and MDI) with window framework structure generated through Appwizard, the MFC class library has loaded menus (including a system menu), tool bars, and status bars for us. But sometimes due to special needs, we may want to not load menus, tool bars, and status bars in our own applications. In this case, we need to manually delete and modify statements in some classes. The procedure is as follows:

1. Open the mainfrm. cpp file and use the function drop-down list box on the toolbar to find the oncreate () function. Follow the procedure below to comment out the statements for creating the toolbar and status bar.

Int cmainframe: oncreate (maid)

{

If (cframewnd: oncreate (lpcreatestruct) =-1)

Return-1;

// Add the annotator here

/* If (! M_wndtoolbar.create (this) |! M_wnd toolbar. loadtoolbar (idr_mainframe ))

{

Trace0 ("failed to create toolbar/N ");

Return-1; // fail to create

}

If (! M_wndstatusbar.create (this) |! M_wndstatus bar. setindicators (indicators, sizeof (indicators)/sizeof (uint )))

{

Trace0 ("failed to create status bar/N ");

Return-1; // fail to create

}

// Todo: Remove this if you don't want Tool tips or a resizeable Toolbar

M_wndtoolbar.setbarstyle (m_wndtoolbar.getbarstyle () | cbrs_tooltips | cbrs_flyby | cbrs_size_dynamic );

// Todo: delete these three lines if you don't want the toolbar to be dockable

M_wndtoolbar.enabledocking (cbrs_align_any );

Enabledocking (cbrs_align_any );

Dockcontrolbar (& m_wndtoolbar );

// End the comment here

*/

Return 0;

}

2. In the mainfrm. cpp file, use the function drop-down list box on the toolbar to find the precreatewindow () function. Set CS. style to the following format, that is, do not load the system menu.

// Create a window without min/MAX buttons, system menu, or sizable border

CS. Style = ws_overlapped | ws_border;

3. In the main application, which contains the. cpp file that defines the full-Process Variables of theapp, use the function drop-down list box on the toolbar to find the initinstance () function. In the sentence "pdoctemplate = new csingledoctemplate", replace idr_mainframe with null. As shown in the following program.

Bool cyourmainapp: initinstance ()

{

//... Skip some irrelevant statements here

Csingledoctemplate * pdoctemplate;

Pdoctemplate = new csingledoctemplate (

Null,

// Replace idr_mainframe with null

Runtime_class (cnobardoc ),

Runtime_class (cmainframe ),

// Main SDI frame window

Runtime_class (cnobarview ));

Adddoctemplate (pdoctemplate );

//... Skip some irrelevant statements here

}

4. Find the compilation method drop-down list box on the toolbar of the Visual C ++ editor and select Win32 release to generate the release version application.

So far, we have an application that does not contain the menu, toolbar, and status bar structure. (

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.