当前位置导航:炫浪网>>网络学院>>编程开发>>C++教程>>Visual C++教程

VC++实现工具栏上添加平面组合框控件

使用过OFFICE2000的人都知道,它的界面,尤其是菜单和工具条,可谓是让人耳目一新。虽然Visual C++开发工具也提供了对工具条的支持,但通常只是按纽的集合,不能直接加入组合框等控件,实现OFFICE2000风格的工具条。本实例针对其中的一个细节,讲述了在Windows环境下用Visual C++6.0在工具条中加入平面组合框控件方法,并实现了组合框的消息响应函数,使得我们的程序看上去更加专业。程序编译运行后的效果如图一所示:


图一、工具栏中的平面组合框控件

  一、实现方法

  用应用程序向导(AppWizard)生成一个基于单文档的工程(Project),首先打开VC的工具条资源编辑器,在工具条要加入组合框的地方加一个空按纽,并将资源共享ID定义为ID_TOOL_ZOOM。

  其次,从面向对象的思想出发,一个工具条作为一个整体,应该封装为一个类,组合框控件应该作为这个类的一个成员变量。因此用Visual C++的类向导CLASSWIZARD生成一个以CToolBar为基类的的新类CMainToolBar,并加入成员变量CFlatComboBox m_combobox(CflatComboBox为平面组合框类。

  在向工具条添加控件的过程中,调用CToolBar::GetItemID()函数来获取每个按钮的ID,直到搜索到"空"按钮。CToolBar::GetItemID()函数的原型为:UINT GetItemID( int nIndex ) const,参数nIndex为当前按钮在工具条中的索引号,该索引号的基准值为"0"。找到"空"按钮后,调用CToolBar::SetButtonInfo()函数设置按钮的宽度信息。最后调用CComBox::Create()、CcomBox::AddString()等函数动态创建平面组合框控件,下面的代码实现了平面组合框控件的动态创建:

//设置指定工具项的宽度并获取新的区域 80是宽度
m_wndToolBar.SetButtonInfo(index, ID_TOOL_ZOOM, TBBS_SEPARATOR, 80);
m_wndToolBar.GetItemRect(index, &rect);
//设置位置
rect.top+=2;
rect.bottom += 200;
// 创建并显示
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);
//填充内容
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);

  但是仅仅产生平面组合框是不够的,必须实现组合框的消息响应函数,才能方便地运用组合框。在Vsiaul C++中,消息响应函数通常都是用类向导来实现,但是此处由于组合框是用函数创建的,所以必须亲自动手来写代码,也并不麻烦,与类向导生成的代码格式是一样的,可以参照来写。下面代码定义了组合框的选择变化消息响应函数:

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

  二、编程步骤

  1、启动Visual C++6.0,生成一个单文档项目,将该项目命名为"ToolBar";

  2、通过资源编辑器新增一个工具按钮,"Caption"设置为空,ID资源标志符命名为ID_TOOL_ZOOM;

  3、启动Class Wizard从CToolBar派生一个新类CMainToolBar;

  4、在MainFrm.h文件中添加#include "MainToolBar.h"语句,然后找到 CToolBar m_wndToolBar语句,用CMainToolBar代替CToolBar;

  5、添加代码,编译运行程序。

  三、程序代码

////////////////////////////////////////////////////
// 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;
 }

共2页 首页 上一页 1 2 下一页 尾页 跳转到
相关内容
赞助商链接