用VC++在工具列上添加平面下拉式方塊控制項

來源:互聯網
上載者:User

使用過OFFICE2000的人都知道,它的介面,尤其是菜單和工具條,可謂是讓人耳目一新。雖然Visual C++開發工具也提供了對工具條的支援,但通常只是按紐的集合,不能直接加入組合框等控制項,實現OFFICE2000風格的工具條。本執行個體針對其中的一個細節,講述了在Windows環境下用Visual C++6.0在工具條中加入平面下拉式方塊控制項方法,並實現了組合框的訊息響應函數,使得我們的程式看上去更加專業。程式編譯運行後的效果一所示:


圖一、工具列中的平面下拉式方塊控制項

  一、實現方法

  用應用程式嚮導(AppWizard)產生一個基於單文檔的工程(Project),首先開啟VC的工具條資源編輯器,在工具條要加入組合框的地方加一個空按紐,並將資源共用ID定義為ID_TOOL_ZOOM。

  其次,從物件導向的思想出發,一個工具條作為一個整體,應該封裝為一個類,下拉式方塊控制項應該作為這個類的一個成員變數。因此用Visual C++的類嚮導CLASSWIZARD產生一個以CToolBar為基類的的新類CMainToolBar,並加入成員變數CFlatComboBox m_combobox。

  在向工具條添加控制項的過程中,調用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;
 }
 // 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_H__76CF28F4_005F_11D7_8F58_00E04C0BECE6__INCLUDED_)
#define AFX_MAINTOOLBAR_H__76CF28F4_005F_11D7_8F58_00E04C0BECE6__INCLUDED_
#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(LPCREATESTRUCT lpCreateStruct)
{
 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 rect;
 //找到指定的工具項
 while(m_wndToolBar.GetItemID(index)!=ID_TOOL_ZOOM)
  index++;
  //設定指定工具項的寬度並擷取新的地區 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);
  return 0;
 }

 void CMainFrame::OnSelectZoomed()
 {
  CString strContent;
  m_wndToolBar.m_wndZoom.GetWindowText(strContent);
  AfxMessageBox(strContent);
 }

  四、小結

  為了實現OFFICE2000風格的工具條,本執行個體介了一種比較巧妙的方法,利用Visual C++6.0已有的開發環境支援,在工具條中加入了平面下拉式方塊控制項,並實現了組合框的訊息響應,使用者選擇組合框中的某一項後,會彈出一個對話方塊,提示使用者所選擇的資訊。

共2頁。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.