Use VC ++ to add a plane combo control on the toolbar

Source: Internet
Author: User

Anyone who has used Office2000 knows that its interface, especially menus and toolbar, is refreshing. Although visual c ++ development tools also provide support for tool bar, it is usually only a set of buttons and cannot be directly added to the control such as the combo box to implement the Office2000 style tool bar. This example describes how to use visual c ++ 6.0 in a Windows environment to add a plane combo box control method to the toolbar and implement the message response function of the combo box, make ourProgramLooks more professional. After the program is compiled and run, the result 1 is as follows:


Figure 1. Plane combo control in the toolbar

I. Implementation Method

Use the Application Wizard (Appwizard) to generate a project based on a single document. First open the VC toolbar resource editor and add an empty button to the toolbar to be added to the combo box, define the resource sharing ID as id_tool_zoom.

Secondly, starting from the object-oriented idea, a toolbar as a whole should be encapsulated into a class, and the combox control should be used as a member variable of the class. Therefore, use the Class Wizard classwizard of Visual C ++ to generate a new class cmaintoolbar Based on the ctoolbar and add the member variable cflatcombobox m_combobox.

when adding a control to a toolbar, call the ctoolbar: getitemid () function to obtain the ID of each button until the "null" button is found. The prototype of the ctoolbar: getitemid () function is uint getitemid (INT nindex) Const. The nindex parameter is the index number of the current button in the toolbar, the reference value of this index number is "0 ". After the "null" button is found, call the ctoolbar: setbuttoninfo () function to set the width of the button. Finally, call functions such as ccombox: Create () and ccombox: addstring () to dynamically create a plane combo control, the following Code allows you to dynamically create a plane combo control:

// set the width of the specified tool item and obtain the width of the new area 80
m_wndtoolbar.setbuttoninfo (index, id_tool_zoom, tbbs_separator, 80);
m_wndtoolbar.getitemrect (Index, & rect);
// set the location
rect. top + = 2;
rect. bottom + = 200;
// create and display
If (! M_wndtoolbar.m_wndzoom.create (ws_child | ws_visible |
cbs_autohscroll | cbs_dropdownlist |
cbs_hasstrings, rect, & m_wndtoolbar, id_tool_zoom ))
{< br> trace0 ("failed to create combo-Box \ n");
return false;
}< br> m_wndtoolbar.m_wndzoom.showwindow (sw_show );
// fill in the content
m_wndtoolbar.m_wndzoom.addstring ("25%");
m_wndtoolbar.m_wndzoom.addstring ("50%");
Fill ("75% ");
Merge ("100%");
m_wndtoolbar.m_wndzoom.addstring ("125%");
Merge ("150%");
m_wndtoolbar.m_wndzoom.addstring ("175% ");
m_wndtoolbar.m_wndzoom.addstring ("200%");
m_wndtoolbar.m_wndzoom.setcursel (3);

However, it is not enough to generate a plane combo box. The message response function of the combo box must be implemented to facilitate the use of the combo box. In vsiaul C ++, message response functions are usually implemented using Class Wizard. However, because the combo box is created using the function, you must write the code by yourself, the code format is the same as that generated by the Class Wizard. You can refer to it for writing. The following code defines the message response function for selecting and changing a combo box:

//////////////////////////////////////// //////////////////////////////////////// ///////////////
Begin_message_map (cmainframe, cframewnd)
// {Afx_msg_map (cmainframe)
On_wm_create ()
On_cbn_selendok (id_tool_zoom, onselectzoomed)
//} Afx_msg_map
End_message_map ()
//////////////////////////////////////// //////////////////////////////////////// //////////////
Afx_msg void onselectzoomed ();

Ii. programming steps

1. Start visual c ++ 6.0, generate a single document project, and name this project "toolbar ";

2. Add a tool button in the resource editor, set "caption" to null, and name the ID Resource Identifier id_tool_zoom;

3. Start Class Wizard to derive a new class cmaintoolbar from ctoolbar;

4. Add the # include "maintoolbar. H" Statement to the mainfrm. h file, find the ctoolbar m_wndtoolbar statement, and replace the ctoolbar with the cmaintoolbar;

5. Add code and compile and run the program.
3. program code

//////////////////////////////////////// //////////////////////////////////////
// Flatcombobox. h: header file
# If! Defined (flatcombobox_h_included)
# Define flatcombobox_h_included
# If _ msc_ver> 1000
# Pragma once
# Endif // _ msc_ver> 1000
# Define fc_drawnormal 0x00000001
# Define fc_drawraised 0x00000002
# Define fc_drawpressd 0x00000004

// Cflatcombobox window
Class cflatcombobox: Public ccombobox
{
// Construction
Public:
Cflatcombobox ();
// Attributes
Public:
Bool m_blbtndown;
Colorref m_clrhilite;
Colorref m_clrshadow;
Colorref m_clrdkshad;
Colorref m_clrbutton;
// Operations
Public:
Void drawcombo (DWORD dwstyle, colorref clrtopleft, colorref clrbottomright );
Int offset ();
// Overrides
// Classwizard generated virtual function overrides
// {Afx_virtual (cflatcombobox)
//} Afx_virtual
// Implementation
Public:
Virtual ~ Cflatcombobox ();
// Generated message map Functions
Protected:
// {Afx_msg (cflatcombobox)
Afx_msg void onmousemove (uint nflags, cpoint point );
Afx_msg void onlbuttondown (uint nflags, cpoint point );
Afx_msg void onlbuttonup (uint nflags, cpoint point );
Afx_msg void ontimer (uint nidevent );
Afx_msg void onpaint ();
//} Afx_msg
Declare_message_map ()
};
# Endif //! Defined (flatcombobox_h_included)

//////////////////////////////////////// ///////////////////////////////
# Include "stdafx. H"
# Include "flatcombobox. H"
# Ifdef _ debug
# Define new debug_new
# UNDEF this_file
Static char this_file [] = _ file __;
# Endif

//// // Cflatcombobox

Cflatcombobox: cflatcombobox ()
{
M_blbtndown = false;
}

Cflatcombobox ::~ Cflatcombobox ()
{
}

Begin_message_map (cflatcombobox, ccombobox)
// {Afx_msg_map (cflatcombobox)
On_wm_mousemove ()
On_wm_lbuttondown ()
On_wm_lbuttonup ()
On_wm_timer ()
On_wm_paint ()
//} Afx_msg_map
End_message_map ()

//////////////////////////////////////// /////////// Cflatcombobox message handlers
Void cflatcombobox: onmousemove (uint nflags, cpoint point)
{
Settimer (1, 10, null );
Ccombobox: onmousemove (nflags, point );
}

Void cflatcombobox: onlbuttondown (uint nflags, cpoint point)
{
M_blbtndown = true;
Ccombobox: onlbuttondown (nflags, point );
}

Void cflatcombobox: onlbuttonup (uint nflags, cpoint point)
{
M_blbtndown = false;
Invalidate ();
Ccombobox: onlbuttonup (nflags, point );
}

Void cflatcombobox: ontimer (uint nidevent)
{
Point pt;
Getcursorpos (& pt );
Crect rcitem;
Getwindowrect (& rcitem );
Static bool bpainted = false;
// Onlbuttondown, show pressed.
If (m_blbtndown = true ){
Killtimer (1 );
If (bpainted = true ){
Drawcombo (fc_drawpressd,: getsyscolor (color_btnshadow ),
: Getsyscolor (color_btnhighlight ));
Bpainted = false;
}
Return;
}
// If mouse leaves, show flat.
If (! Rcitem. ptinrect (PT )){
Killtimer (1 );
If (bpainted = true ){
Drawcombo (fc_drawnormal,: getsyscolor (color_btnface),: getsyscolor (color_btnface ));
Bpainted = false;
}
Return;
}
// On mouse over, show raised.
Else {
If (bpainted = true)
Return;
Else {
Bpainted = true;
Drawcombo (fc_drawraised,: getsyscolor (color_btnshadow ),
: Getsyscolor (color_btnhighlight ));
}
}
Ccombobox: ontimer (nidevent );
}

Void cflatcombobox: onpaint ()
{
Default ();
Drawcombo (fc_drawnormal,: getsyscolor (color_btnface),: getsyscolor (color_btnface ));
}

Void cflatcombobox: drawcombo (DWORD dwstyle, colorref clrtopleft, colorref clrbottomright)
{
Crect rcitem;
Getclientrect (& rcitem );
CDC * PDC = getdc ();
// Cover up dark 3D shadow.
PDC-> draw3drect (rcitem, clrtopleft, clrbottomright );
Rcitem. deflaterect (1, 1 );
If (! Iswindowenabled ()){
PDC-> draw3drect (rcitem,: getsyscolor (color_btnhighlight),: getsyscolor (color_btnhighlight ));
}
Else {
PDC-> draw3drect (rcitem,: getsyscolor (color_btnface),: getsyscolor (color_btnface ));
}
// Cover up dark 3D shadow on drop arrow.
Rcitem. deflaterect (1, 1 );
Rcitem. Left = rcitem. Right-offset ();
PDC-> draw3drect (rcitem,: getsyscolor (color_btnface),: getsyscolor (color_btnface ));
// Cover up normal 3D shadow on drop arrow.
Rcitem. deflaterect (1, 1 );
PDC-> draw3drect (rcitem,: getsyscolor (color_btnface),: getsyscolor (color_btnface ));
If (! Iswindowenabled ()){
Return;
}
Switch (dwstyle)
{
Case fc_drawnormal:
Rcitem. Top-= 1;
Rcitem. Bottom + = 1;
PDC-> draw3drect (rcitem,: getsyscolor (color_btnhighlight),: getsyscolor (color_btnhighlight ));
Rcitem. Left-= 1;
PDC-> draw3drect (rcitem,: getsyscolor (color_btnhighlight),: getsyscolor (color_btnhighlight ));
Break;
Case fc_drawraised:
Rcitem. Top-= 1;
Rcitem. Bottom + = 1;
PDC-> draw3drect (rcitem,: getsyscolor (color_btnhighlight),: getsyscolor (color_btnshadow ));
Break;
Case fc_drawpressd:
Rcitem. Top-= 1;
Rcitem. Bottom + = 1;
Rcitem. offsetrect (1, 1 );
PDC-> draw3drect (rcitem,: getsyscolor (color_btnshadow),: getsyscolor (color_btnhighlight ));
Break;
}
Releasedc (PDC );
}

Int cflatcombobox: offset ()
{
// Thanks to Todd brannam for this suggestion...
Return: getsystemmetrics (sm_cxhthumb );
}

/// // Maintoolbar. h: interface for the cmaintoolbar class.

# If! Defined (afx_maintoolbar_h000076cf28f4_005f_11d7_8f58_00e04c0bece600000000ded _)
# Define afx_maintoolbar_h000076cf28f4_005f_11d7_8f58_00e04c0bece600000000ded _
# If _ msc_ver> 1000
# Pragma once
# Endif // _ msc_ver> 1000
# Include "flatcombobox. H"

Class cmaintoolbar: Public ctoolbar
{
Public:
Cmaintoolbar ();
Virtual ~ Cmaintoolbar ();

Public:
Cflatcombobox m_wndzoom;
};
# Endif

/// // Maintoolbar. cpp: Implementation of the cmaintoolbar class.
# Include "stdafx. H"
# Include "toolbar. H"
# Include "maintoolbar. H"
# Ifdef _ debug
# UNDEF this_file
Static char this_file [] =__ file __;
# Define new debug_new
# Endif

Cmaintoolbar: cmaintoolbar ()
{
}

Cmaintoolbar ::~ Cmaintoolbar ()
{
}

//////////////////////////////////////// //////////////////////////////////////// //////////////
Int cmainframe: oncreate (maid)
{
If (cframewnd: oncreate (lpcreatestruct) =-1)
Return-1;
If (! M_wndtoolbar.createex (this, tbstyle_flat, ws_child | ws_visible |
Cbrs_top | cbrs_gripper | cbrs_tooltips | cbrs_flyby |
Cbrs_size_dynamic) |! 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: 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 );
Int Index = 0;
Rect;
// Find the specified tool item
While (m_wndtoolbar.getitemid (INDEX )! = Id_tool_zoom)
Index ++;
// Set the width of the specified tool item and obtain that the new area 80 is the width.
M_wndtoolbar.setbuttoninfo (index, id_tool_zoom, tbbs_separator, 80 );
M_wndtoolbar.getitemrect (index, & rect );
// Set the location
Rect. Top + = 2;
Rect. Bottom + = 200;
// Create and display
If (! M_wndtoolbar.m_wndzoom.create (ws_child | ws_visible |
Cbs_autohscroll | cbs_dropdownlist |
Cbs_hasstrings, rect, & m_wndtoolbar, id_tool_zoom ))
{
Trace0 ("failed to create combo-Box \ n ");
Return false;
}
M_wndtoolbar.m_wndzoom.showwindow (sw_show );
// Fill in the content
M_wndtoolbar.m_wndzoom.addstring ("25% ");
M_wndtoolbar.m_wndzoom.addstring ("50% ");
M_wndtoolbar.m_wndzoom.addstring ("75% ");
M_wndtoolbar.m_wndzoom.addstring ("100% ");
M_wndtoolbar.m_wndzoom.addstring ("125% ");
M_wndtoolbar.m_wndzoom.addstring ("150% ");
M_wndtoolbar.m_wndzoom.addstring ("175% ");
M_wndtoolbar.m_wndzoom.addstring ("200% ");
M_wndtoolbar.m_wndzoom.setcursel (3 );
Return 0;
}

Void cmainframe: onselectzoomed ()
{
Cstring strcontent;
M_wndtoolbar.m_wndzoom.getwindowtext (strcontent );
Afxmessagebox (strcontent );
}

Iv. Summary

In order to implement the Office2000 style toolbar, this example introduces a clever method. Using the existing development environment support of Visual C ++ 6.0, a plane combo control is added to the toolbar, the message response of the combo box is implemented. After you select an item in the combo box, a dialog box is displayed, prompting you to select the information.

2 pages in total.

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.