How to add a plane drop-down control on the toolbar

Source: Internet
Author: User

 

 

How to add a plane drop-down control on the toolbar
Author: Li jinfan

Download the sample code in this article

As follows:

Anyone who has used WordXP knows that her interface is refreshing. Especially her menus and toolbar are cool. There are also many people on the Internet who are imitating her, including the products of many software companies. Her appearance caused the software industry to experience the fashion style of Chen XP interface. I will tell you the implementation steps step by step.
Step 1: create a single document Project
Step 2: Go to the resource editor and add a new tool item. If there is no content in it, name the resource ID_TOOL_ZOOM. What is the relationship between a new tool item and my drop-down control? Of course it is related, because the pull-down control will be displayed at this position.
Step 3: derive a new class, derived from CToolBar. The class name is CMainToolBar. How to derive, I will only prompt you, menu Insert-> New Class .... Select Generic Class in Class Type. If not, select... Try it.
Step 4: because we need a drop-down box, we have to declare a CComboBox object in the CMainToolBar. The declaration is as follows:

public:CComboBox m_wndZoom; 

Step 5: replace the old class with the new class. Before replacement, you must include the Declaration file of the new class. In the MainFrm. h file, add this sentence # include "MainToolBar. h ". Find the CToolBar m_wndToolBar, and replace the CToolBar with the CMainToolBar.
Step 6: Add the following code at the end of the OnCreate function of CMainFrm (before return 0 of course:

Int index = 0; RECT 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 the new region 80 as the width m_wndToolBar.SetButtonInfo (index, ID_TOOL_ZOOM, TBBS_SEPARATOR, 80); m_wndToolBar.GetItemRect, & rect); // set the location rect. top + = 2; rect. bottom + = 200; // create and display the control if (! Export (WS_CHILD | WS_VISIBLE | items | 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%"); Round ("50%"); Round ("75%"); Round ("100% "); upper ("125%"); m_wndToolBar.m_wndZoom.AddString ("150%"); m_wndToolBar.m_wndZoom.AddString ("175%"); lower ("200%"); // select the default value lower (3 );

After adding it, run it again. Oh, really. However, three problems still need to be solved. 1. The tool bar is not high enough. 2. The following boxes are not flat controls. 3. How to process messages. I will solve it one by one:
Problem 1: The tool bar is not high enough.
Easy to solve. Go to the resource manager and open the tool. Find a tool item and set its Height attribute to 20. This increases the tool bar.

Problem 2: The following boxes are not flat controls, which are easy to solve. Just use others' classes. Here I am using the CFlatComboBox of Kirk Stowell. The sample program already contains this class. Use Project to Add to Project to File... Add the FlatComboBox. h and FlatComboBox. Cpp files to the project. Then, include the CFlatComboBox class in MainToolBar. h. Of course, use # include "FlatComboBox. h ". Finally, CFlatComboBox class is used to replace CComboBox class. Re-compile and run it again to see if it is flat?

Question 3: How to handle messages. This control can only be viewed and cannot be used. What is the significance. So we have to process his messages so that he can work. We can't expect ClassWizard to help us, Because ClassWizard can't see this message, so we have to rely on ourselves. In the CMainFrm class, we need to add the afx_msg void OnSelectZoomed (); function in protected. Go to the MainFrm. cpp file, find BEGIN_MESSAGE_MAP, and add ON_CBN_SELENDOK (ID_TOOL_ZOOM, OnSelectZoomed ). Then, of course, we need to implement its processing function. The processing function is as follows:

Void CMainFrame: OnSelectZoomed () {// get the content and MSGBOX shows the CString strContent; m_wndToolBar.m_wndZoom.GetWindowText (strContent); AfxMessageBox (strContent); // pop up the selected content}

Complete the work, and then compile and run it again. Look at the interface, it is indeed a flat, controlled toolbar.
Note: I haven't written the essay for a long time. This is the first essay I wrote after I got out of the society. Please forgive me if it is not easy to write!
My Email is a lijf971@21cn.com. What do you want to send me?

 

 

Code:

MainToolBar. h

#pragma once


// CMainToolBar

class CMainToolBar : public CToolBar
{
DECLARE_DYNAMIC(CMainToolBar)

public:
CMainToolBar();
virtual ~CMainToolBar();

protected:
DECLARE_MESSAGE_MAP()

public:
CComboBox m_combox;
};

MainToolBar. cpp

// MainToolBar.cpp : implementation file
//

#include "stdafx.h"
#include "SingleDoc.h"
#include "MainToolBar.h"


// CMainToolBar

IMPLEMENT_DYNAMIC(CMainToolBar, CToolBar)

CMainToolBar::CMainToolBar()
{

}

CMainToolBar::~CMainToolBar()
{
}


BEGIN_MESSAGE_MAP(CMainToolBar, CToolBar)
END_MESSAGE_MAP()



// CMainToolBar message handlers

 

MainFram. h

// CToolBar m_wndToolBar;
CMainToolBar m_wndToolBar;

 

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 );

// Maintoolbar
Int index = 0;
RECT rect;
// Find the specified tool item
While (m_wndToolBar.GetItemID (index )! = IDD_HKX)
Index ++;
// Set the width of the specified tool item and obtain that the new area 80 is the width.
M_wndToolBar.SetButtonInfo (index, IDD_HKX, TBBS_SEPARATOR, 80 );
M_wndToolBar.GetItemRect (index, & rect );

// Set the location
Rect. top + = 2;
Rect. bottom + = 200;

// Create and display controls

If (! M_wndToolBar.m_combox.Create (WS_CHILD | WS_VISIBLE | CBS_AUTOHSCROLL | CBS_DROPDOWNLIST | CBS_HASSTRINGS, rect,
& M_wndToolBar, IDD_HKX ))
{
TRACE0 ("Failed to create combo-box \ n ");
Return FALSE;
}
M_wndToolBar.m_combox.ShowWindow (SW_SHOW );

// Fill in the content

M_wndToolBar.m_combox.AddString (TEXT ("25% "));
M_wndToolBar.m_combox.AddString (TEXT ("50% "));
M_wndToolBar.m_combox.AddString (TEXT ("75% "));
// M_wndToolBar.m_combox.AddString ("100% ");
// M_wndToolBar.m_combox.AddString ("125% ");
// M_wndToolBar.m_combox.AddString ("150% ");
// M_wndToolBar.m_combox.AddString ("175% ");
// M_wndToolBar.m_combox.AddString ("200% ");

// Select the default item
M_wndToolBar.m_combox.SetCurSel (3 );

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.