Time added: 01-1-8 09:10:32 pm
New Features of tool bar in Visual C ++ 6
Author: Dave Schmitt
Microsoft has been pushing visual c ++ 6.0 for several months at www.microsoft.com/visualc. The official version is expected to be released by the end of this year. At the same time, preview version 6 will contain a lot of improvements and improvements, including support for Internet controls, such as flat toolbar. Although the improved control package has nothing to do with the Internet, it first appears in Internet Explorer, so it is named. In fact, the official pre-release is titled "Visual C ++ 4.0 technology preview for Internet Explorer 5.0 ".
In my previous discussion about the MFC toolbar class, I promised to provide a demo of the toolbar in version 6. There is a good message that all the work you have done with ctoolbar is valid in the new version, including some of the extended functions described in previous sections. Therefore, you can easily modify existingProgramTo obtain the "cool" interface like in Internet Explorer and Visual Studio. In addition, there is no bad news.
New Features of tool bar
As early as version 4, ctoolbar has been fully implemented by the MFC library. Once the public control dynamic link library (named comctl32.dll) becomes ubiquitous, ctoolbar becomes synonymous with the toolbar control that is already included in the operating system. However, ctoolbar does not reveal all the capabilities of the Public Toolbar Control. Now, through the createex () function, it is successful.
The public control dynamic link library now contains at least three types of styles: Initially, added to Internet Explorer 3.0, and added to Internet Explorer 4.0. Although these versions are theoretically backward compatible, some professionals have written some applications that cannot be normally run in later versions. This may be because they use some undisclosed features, these functions are not included in all versions.
Visual c ++ programmers do not have this experience, because in Visual C ++ 4.0 or 5.0, comctl32.dll is not a redistribution component and is updated when installing Internet Explorer, therefore, MFC programmers cannot rely on some features of the latest version for their programs. This is why ctoolbar only has limited functions of the original DLL. The ability of ctoolbar to achieve the latest features means that Microsoft will include the latest DLL in Visual C ++ 6.0 and use it as a redistributing component.
The vast majority of new features are determined by the new style flag specified when createex () and other ctoolbar member functions are called. The following is part of commctrl. H, which defines the tbstyle Class Identifier:
# Define tbstyle_button 0x0000
# Define tbstyle_sep 0x0001
# Define tbstyle_check 0x0002
# Define tbstyle_group 0x0004
# Define tbstyle_checkgroup (tbstyle_group | tbstyle_check)
# If (_ win32_ie> = 0x0300)
# Define tbstyle_dropdown 0x0008
# Endif
# If (_ win32_ie> = 0x0400)
# Define tbstyle_autosize 0x0010
# Define tbstyle_noprefix 0x0020
# Endif
# Define tbstyle_tooltips 0x0100
# Define tbstyle_wrapable 0x0200
# Define tbstyle_altdrag 0x0400
# If (_ win32_ie> = 0x0300)
# Define tbstyle_flat 0x0800
# Define tbstyle_list 0x1000
# Define tbstyle_customerase 0x2000
# Endif
# If (_ win32_ie> = 0x0400)
# Define tbstyle_registerdrop 0X4000
# Define tbstyle_transparent 0x8000
# Define tbstyle_ex_drawddarrows 0x00000001
# Endif
You will notice that some of them adopt Conditional compilation and depend on the value of _ win32_ie. By default, it refers to Internet Explorer 4.0 (that is, the value is 0x0400 ). For versions earlier than Internet Explorer 3.0 (with a value of 0x0300), most tbstyle identifiers refer to buttons or a group of buttons. Internet Explorer 3.0 introduces flat buttons, text labels, drop-down lists, and custom painting. Internet Explorer 4.0 enhances the drop-down list and custom drawing functions, and adds support for OLE drag targets to a toolbar.
Flat buttons and handles
In the past 18 months, I have been asked how to get a cool hand like a toolbar in Internet Explorer and Visual Studio instead of a embossed button but with a flat button and a hand that is easy to move and position.. These features are not supported by MFC, so the simplest way to obtain them is to buy an extension library. Visual c ++ 6.0 does not need to do this because it enables the ctoolbar class to support flat buttons, handles, and other new visual effects.
In the preview version, Appwizard does not automatically include these new features, but they are easy to add. Table 1 shows the oncreate () function of the main framework window created by Appwizard. Table 2 shows the modifications required to obtain the toolbar with flat buttons and handles. Figure 1 shows the toolbar created in table 1, while Figure 2 shows the toolbar implemented in table 2.
Table 1: cmainframe: oncreate as generated by Appwizard
Int cmainframe: oncreate (maid)
{
If (cmdiframewnd: oncreate (lpcreatestruct) =-1)
Return-1;
If (! M_wndtoolbar.create (this) |! M_wndtoolbar.loadtoolbar (idr_mainframe ))
{
Trace0 ("failed to create Toolbar \ n ");
Return-1; // fail to create
}
If (! M_wndstatusbar.create (this) |
! M_wndstatusbar.setindicators (indicators,
Sizeof (indicators
Figure 1
Table 2: Adding flat buttons and the gripper
Int cmainframe: oncreate (maid)
{
If (cmdiframewnd: oncreate (lpcreatestruct) =-1)
Return-1;
If (! M_wndtoolbar.createex (this) |! M_wndtoolbar.loadtoolbar (idr_mainframe ))
{
Trace0 ("failed to create Toolbar \ n ");
Return-1; // fail to create
}
If (! M_wndstatusbar.create (this) |
! M_wndstatusbar.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_gripper | cbrs_border_3d | cbrs_tooltips | cbrs_flyby | cbrs_size_dynamic );
// Todo: delete these three lines if you don't want the toolbar
// Be dockable
M_wndtoolbar.enabledocking (cbrs_align_any );
Enabledocking (cbrs_align_any );
Dockcontrolbar (& m_wndtoolbar );
Return 0;
}
Figure 2
To make a flat button, I must use createex () instead of create (). This new function is declared in afxext. h:
Bool createex
(
Cwnd * pparentwnd, // parent window
DWORD dwctrlstyle = tbstyle_flat, // extended Style
DWORD dwstyle = // Style
Ws_child | ws_visible | cbrs_align_top,
Crect rcborders = crect (0, 0, 0), // border rectangle
Uint nid = afx_idw_toolbar // identifier
);
Because the default extension style refers to tbstyle_flat, to get a flat button, you just need to simply form the AppwizardCodeChange "CREATE ()" to "createex. I will implement other extension styles later.
To obtain the handle, I must include the cbrs_gripper flag when calling the setbarstyle () function. See table 2. This is a new style of the ccontrolbar class, And the ctoolbar class inherits from it. Note that I have also added the cbrs_border_3d flag to correct an unknown draw problem, which will draw unnecessary points at the edge of the toolbar. This may mean that the preview version does have this problem, because once I add the 3D logo, it will be resolved immediately and it does not seem to affect anything else.
The two simple changes above are the most effort-saving way for a stored program to get a cool interface. When a program has a flat button and a handle, it will not change anything else.
Text tag
Internet Explorer enables normal toolbar to have large buttons and text labels that replace text prompts. The MFC programmer can use the setbuttontext () function to set a text string for each button to achieve this effect. Although this function has been included in Visual C ++ 5.0, it will not achieve satisfactory results if the flat button style is not used.
Table 3 shows how to use existing text prompts as button labels, and figure 3 and figure 4 show the effects of locating toolbar on the top and right, respectively. I still make the text prompt valid, but you can disable it by removing the cbrs_tooltips style when calling setbarstyle.
Table 3: adding text labels
Int cmainframe: oncreate (maid)
{
If (cmdiframewnd: oncreate (lpcreatestruct) =-1)
Return-1;
If (! M_wndtoolbar.createex (this) |
! M_wndtoolbar.loadtoolbar (idr_mainframe ))
{
Trace0 ("failed to create Toolbar \ n ");
Return-1; // fail to create
}
If (! M_wndstatusbar.create (this) |
! M_wndstatusbar.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_gripper | cbrs_border_3d | cbrs_tooltips | cbrs_flyby | cbrs_size_dynamic );
// Add text to each button
For (INT I = 0; I <m_wndtoolbar.getcount (); I ++)
{Uint id = m_wndtoolbar.getitemid (I );
Cstring S;
If (! S. loadstring (ID) continue;
Int J = S. Find (_ T ('\ n '));
If (j <0) continue;
S = S. Right (S. getlength ()-J-1 );
M_wndtoolbar.setbuttontext (I, S);} // adjust sizes to include text
Crect rect;
M_wndtoolbar.getitemrect (0, & rect );
M_wndtoolbar.setsizes (rect. Size (), csize (16, 15); // todo: delete these three lines if you don't want the toolbar
// Be dockable
M_wndtoolbar.enabledocking (cbrs_align_any );
Enabledocking (cbrs_align_any );
Dockcontrolbar (& m_wndtoolbar );
Return 0 ;}
Figure 4
To generate button labels, a simple for loop sweeps all the buttons and extracts the prompt text from the help string connected to them. After setting the labels for each button, call the setsizes () function of the toolbar to recalculate the appearance of the toolbar to make the labels visible. There may be better ways to implement the last step, but I still borrowed this process from Microsoft's mfcie sample program. It is effective only for nothing else.
In many aspects, mfcie is worth learning carefully. Essentially, it is actually a micro version of Internet Explorer, and it also describes how to use some new features of version 6. You will be surprised that it is so easy to add the HTML browsing function to your MFC application.
In most cases, you want text labels to be displayed under the button (which is the only choice in the original public control library ). Internet Explorer 3.0 adds the tbstyle_list style, which causes text labels to be displayed on the right of the button. This style is useful when a drop-down list or button is overwritten by a subwindow is displayed next to a tag.
Drop-down list
In the article "tool bar deformation", I demonstrated how to add a drop-down list to the tool bar by using a combo box as a subwindow. This method is still valid in version 6 and is still useful when you want to display the current selectable items on the toolbar. However, sometimes you want the toolbar to contain a button that displays the selection list or menu when you click the mouse. This function can now be implemented through the extended style tbstyle_dropdown and tbstyle_ex_drawddarrows.
For example, if I want the "new" button corresponding to the "file" menu to display a list Of all file types that my application knows how to create. Table 4 shows how to add this feature in the previous example, and Figure 5 shows the toolbar. I captured the screen when the mouse stays on the button, it shows the separation line between the button and the drop-down arrow.
To add a drop-down arrow, I must first call the setextendedstyle () function on the basic Toolbar Control and specify the tbstyle_ex_drawddarrows style. Then I must specify the tbstyle_dropdown style on each button that I want to display the drop-down arrow.
Table 4: converting the File menu new command to a drop-down button
Int cmainframe: oncreate (maid)
{
If (cmdiframewnd: oncreate (lpcreatestruct) =-1)
Return-1;
If (! M_wndtoolbar.createex (this) |
! M_wndtoolbar.loadtoolbar (idr_mainframe ))
{
Trace0 ("failed to create Toolbar \ n ");
Return-1; // fail to create
}
If (! M_wndstatusbar.create (this) |
! M_wndstatusbar.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_gripper | cbrs_border_3d | cbrs_tooltips | cbrs_flyby | cbrs_size_dynamic );
// Add text to each button
For (INT I = 0; I
Figure 5
When you click the new button, as shown in figure 5, the basic button operation occurs. In this example, the command message causes cwinapp: onfilenew () to be called (). However, when you click the drop-down arrow, the toolbar sends a tbn_dropdown notification message to the parent window of the toolbar. I can get this message by reloading the onnotify () function or adding on_notify to the Message ing.
Table 5 shows the implementation technology of the latter. I usually display a pop-up menu or a list box to respond to the notification message. In most cases, there is no need to provide a result value because the default value (tbddret_default) shows that I have obtained control over this event. Other possible result values are tbddret_nodefault and tbddret_treatpressed. The former indicates that the event is not controlled. When the latter occurs, the toolbar appears as if the button was clicked.
Table 5: handling drop-down notifications
Begin_message_map (cmainframe, cmdiframewnd)
// {Afx_msg_map (cmainframe)
On_wm_create ()
//} Afx_msg_map
On_notify (tbn_dropdown, afx_idw_toolbar, ondropdown)
End_message_map ()
Void cmainframe: ondropdown (nmhdr * pnotifystruct, lresult * result)
{
Nmtoolbar * pinfo = (nmtoolbar *) ppolicystruct;
Switch (pinfo-> iItem)
{
Case id_file_new:
Trace0 ("id_file_new drop down \ n ");
// Todo: Display popup menu or list box
Break;
// Todo: Add cases for other drop-down buttons
}
}
Hotspot Images
Run Internet Explorer 4.0 and hover the mouse over the toolbar. We can see that every button is a dull color in normal times. When the mouse clicks the button, it is highlighted and highlighted. This visual effect uses the tool bar control called "Hotspot image" function.
The general Appwizard Code uses a toolbar resource to specify the appearance of the buttons on it and a command ID connected to each button. The toolbar actually contains a composite bitmap. when calling the loadtoolbar () function, the bitmap is converted into an image list (see table 4 ). To activate the hotspot image function, I must use the sethotimagelist () function to provide the second image list, as shown in table 6.
Table 6: Using Hot Images
Int cmainframe: oncreate (maid)
{
If (cmdiframewnd: oncreate (lpcreatestruct) =-1)
Return-1;
If (! M_wndtoolbar.createex (this) |
! M_wndtoolbar.loadtoolbar (idr_mainframe ))
{
Trace0 ("failed to create Toolbar \ n ");
Return-1; // fail to create
}
Cimagelist IMG;
If (! IMG. Create (idb_mainframe, 128,128,128, RGB )))
{
Trace0 ("failed to load hot images \ n ");
Return-1;
}
M_wndtoolbar.gettoolbarctrl (). sethotimagelist (& IMG );
IMG. Detach ();
If (! M_wndstatusbar.create (this) |
! M_wndstatusbar.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_gripper | cbrs_border_3d | cbrs_tooltips | cbrs_flyby | cbrs_size_dynamic );
// Add text to each button
For (INT I = 0; I
Figure 6
Figure 6 shows how hotspot images are activated. When the mouse stays on the new button, it is displayed as an empty document image. Other buttons are flat and pale. To generate this tool bar, I will use toolbar.bmp IN THE ressub directory as toolbar1.bmp, insert it into the resource as a bitmap resource, and set its ID to idr_mainframe. Next, I edit the idr_mainframe toolbar and use a light color (for example, light gray) to replace its original color. Finally, I added the call to the sethotimagelist () function as shown in table 6.
This example demonstrates how to add a hotspot image to a toolbar, but it does not improve the toolbar's appearance. If you want to achieve better results at this point, the mfcie example contains a very good etching bitmap, it demonstrates how much visual it can achieve when you use a very artistic image.
Other features
The previous explains how to easily change the existing MFC program to obtain flat buttons, handles, drop-down lists, and hotspot images. Visual c ++ 6.0 also provides several other interesting toolbar features, including custom plotting, Ole dragging to the target, and using rebar (sorry, I don't know what the word is) containers. These features will be the topic of future topics.
Reference announcement address: http://blog.csdn.net/silver/services/trackbacks/5453.aspx