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

上传人:b****8 文档编号:9256268 上传时间:2023-02-03 格式:DOCX 页数:10 大小:17.72KB
下载 相关 举报
VC++实现工具栏上添加平面组合框控件.docx_第1页
第1页 / 共10页
VC++实现工具栏上添加平面组合框控件.docx_第2页
第2页 / 共10页
VC++实现工具栏上添加平面组合框控件.docx_第3页
第3页 / 共10页
VC++实现工具栏上添加平面组合框控件.docx_第4页
第4页 / 共10页
VC++实现工具栏上添加平面组合框控件.docx_第5页
第5页 / 共10页
点击查看更多>>
下载资源
资源描述

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

《VC++实现工具栏上添加平面组合框控件.docx》由会员分享,可在线阅读,更多相关《VC++实现工具栏上添加平面组合框控件.docx(10页珍藏版)》请在冰豆网上搜索。

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

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

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

网站建设-找友创互联开网店-找友创互联0元招收代理【CN域名0.5元】

   2008年11月13日   社区交流

关键字:

xcopyAppBuilderCRecordSetFileMappingFileMapping

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

  但是仅仅产生平面组合框是不够的,必须实现组合框的消息响应函数,才能方便地运用组合框。

在VsiaulC++中,消息响应函数通常都是用类向导来实现,但是此处由于组合框是用函数创建的,所以必须亲自动手来写代码,也并不麻烦,与类向导生成的代码格式是一样的,可以参照来写。

下面代码定义了组合框的选择变化消息响应函数:

///////////////////////////////////////////////////////////////////////////////////////////////

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

  二、编程步骤

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

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

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

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

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

  三、程序代码

////////////////////////////////////////////////////

//FlatComboBox.h:

headerfile

#if!

defined(FLATCOMBOBOX_H_INCLUDED)

#defineFLATCOMBOBOX_H_INCLUDED

#if_MSC_VER>1000

#pragmaonce

#endif//_MSC_VER>1000

#defineFC_DRAWNORMAL0x00000001

#defineFC_DRAWRAISED0x00000002

#defineFC_DRAWPRESSD0x00000004

//CFlatComboBoxwindow

classCFlatComboBox:

publicCComboBox

{

 //Construction

 public:

  CFlatComboBox();

  //Attributes

 public:

  boolm_bLBtnDown;

  COLORREFm_clrHilite;

  COLORREFm_clrShadow;

  COLORREFm_clrDkShad;

  COLORREFm_clrButton;

  //Operations

 public:

  voidDrawCombo(DWORDdwStyle,COLORREFclrTopLeft,

COLORREFclrBottomRight);

  intOffset();

  //Overrides

  //ClassWizardgeneratedvirtualfunctionoverrides

  //{{AFX_VIRTUAL(CFlatComboBox)

  //}}AFX_VIRTUAL

  //Implementation

 public:

  virtual~CFlatComboBox();

  //Generatedmessagemapfunctions

 protected:

  //{{AFX_MSG(CFlatComboBox)

   afx_msgvoidOnMouseMove(UINTnFlags,CPointpoint);

   afx_msgvoidOnLButtonDown(UINTnFlags,CPointpoint);

   afx_msgvoidOnLButtonUp(UINTnFlags,CPointpoint);

   afx_msgvoidOnTimer(UINTnIDEvent);

   afx_msgvoidOnPaint();

  //}}AFX_MSG

 DECLARE_MESSAGE_MAP()

};

#endif//!

defined(FLATCOMBOBOX_H_INCLUDED)

///////////////////////////////////////////

#include"stdafx.h"

#include"FlatComboBox.h"

#ifdef_DEBUG

#definenewDEBUG_NEW

#undefTHIS_FILE

staticcharTHIS_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()

/////////////////////////////////CFlatComboBoxmessagehandlers

voidCFlatComboBox:

:

OnMouseMove(UINTnFlags,CPointpoint)

{

 SetTimer(1,10,NULL);

 CComboBox:

:

OnMouseMove(nFlags,point);

}

voidCFlatComboBox:

:

OnLButtonDown(UINTnFlags,CPointpoint)

{

 m_bLBtnDown=true;

 CComboBox:

:

OnLButtonDown(nFlags,point);

}

voidCFlatComboBox:

:

OnLButtonUp(UINTnFlags,CPointpoint)

{

 m_bLBtnDown=false;

 Invalidate();

 CComboBox:

:

OnLButtonUp(nFlags,point);

}

voidCFlatComboBox:

:

OnTimer(UINTnIDEvent)

{

 POINTpt;

 GetCursorPos(&pt);

 CRectrcItem;

 GetWindowRect(&rcItem);

 staticboolbPainted=false;

 //OnLButtonDown,showpressed.

 if(m_bLBtnDown==true){

  KillTimer

(1);

  if(bPainted==true){

   DrawCombo(FC_DRAWPRESSD,:

:

GetSysColor(COLOR_BTNSHADOW),:

:

GetSysColor(COLOR_BTNHIGHLIGHT));

   bPainted=false;

  }

  return;

 }

//Ifmouseleaves,showflat.

 if(!

rcItem.PtInRect(pt)){

  KillTimer

(1);

  if(bPainted==true){

   DrawCombo(FC_DRAWNORMAL,:

:

GetSysColor(COLOR_BTNFACE),:

:

GetSysColor(COLOR_BTNFACE));

   bPainted=false;

  }

  return;

 }

 //Onmouseover,showraised.

 else{

  if(bPainted==true)

   return;

  else{

   bPainted=true;

   DrawCombo(FC_DRAWRAISED,:

:

GetSysColor(COLOR_BTNSHADOW),:

:

GetSysColor(COLOR_BTNHIGHLIGHT));

  }

 }

 CComboBox:

:

OnTimer(nIDEvent);

}

voidCFlatComboBox:

:

OnPaint()

{

 Default();

 DrawCombo(FC_DRAWNORMAL,:

:

GetSysColor(COLOR_BTNFACE),:

:

GetSysColor(COLOR_BTNFACE));

}

voidCFlatComboBox:

:

DrawCombo(DWORDdwStyle,COLORREFclrTopLeft,

COLORREFclrBottomRight)

{

 CRectrcItem;

 GetClientRect(&rcItem);

 CDC*pDC=GetDC();

 //Coverupdark3Dshadow.

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

 }

 //Coverupdark3Dshadowondroparrow.

 rcItem.DeflateRect(1,1);

 rcItem.left=rcItem.right-Offset();

 pDC->Draw3dRect(rcItem,:

:

GetSysColor(COLOR_BTNFACE),

:

:

GetSysColor(COLOR_BTNFACE));

 //Coverupnormal3Dshadowondroparrow.

 rcItem.DeflateRect(1,1);

 pDC->Draw3dRect(rcItem,:

:

GetSysColor(COLOR_BTNFACE),

:

:

GetSysColor(COLOR_BTNFACE));

 if(!

IsWindowEnabled()){

  return;

 }

 switch(dwStyle)

 {

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

  caseFC_DRAWRAISED:

   rcItem.top-=1;

   rcItem.bottom+=1;

   pDC->Draw3dRect(rcItem,:

:

GetSysColor(COLOR_BTNHIGHLIGHT),:

:

GetSysColor(COLOR_BTNSHADOW));

   break;

  caseFC_DRAWPRESSD:

   rcItem.top-=1;

   rcItem.bottom+=1;

   rcItem.OffsetRect(1,1);

   pDC->Draw3dRect(rcItem,:

:

GetSysColor(COLOR_BTNSHADOW),:

:

GetSysColor(COLOR_BTNHIGHLIGHT));

   break;

  }

  ReleaseDC(pDC);

 }

 intCFlatComboBox:

:

Offset()

 {

  //ThankstoToddBrannamforthissuggestion...

  return:

:

GetSystemMetrics(SM_CXHTHUMB);

 }

 ///////////////////////////MainToolBar.h:

interfacefortheCMainToolBarclass.

 #if!

defined(AFX_MAINTOOLBAR_H__76CF28F4_005F_11D7_8F58_00E04C0BECE6__INCLUDED_)

 #defineAFX_MAINTOOLBAR_H__76CF28F4_005F_11D7_8F58_00E04C0BECE6__INCLUDED_

 #if_MSC_VER>1000

 #pragmaonce

 #endif//_MSC_VER>1000

 #include"FlatComboBox.h"

 classCMainToolBar:

publicCToolBar

 {

  public:

   CMainToolBar();

   virtual~CMainToolBar();

  public:

   CFlatComboBoxm_wndZoom;

 };

 #endif

 //////////////MainToolBar.cpp:

implementationoftheCMainToolBarclass.

 #include"stdafx.h"

 #include"ToolBar.h"

 #include"MainToolBar.h"

 #ifdef_DEBUG

 #undefTHIS_FILE

 staticcharTHIS_FILE[]=__FILE__;

 #definenewDEBUG_NEW

 #endif

 CMainToolBar:

:

CMainToolBar()

 {}

 CMainToolBar:

:

~CMainToolBar()

 {}

 ////////////////////////////////////////////////////////////

 intCMainFrame:

:

OnCreate(LPCREATESTRUCTlpCreateStruct)

 {

  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("Failedtocreatetoolbarn");

   return-1;//failtocreate

  }

  if(!

m_wndStatusBar.Create(this)||!

m_wndStatusBar.SetIndicators(indicators,

sizeof(indicators)/sizeof(UINT)))

  {

   TRACE0("Failedtocreatestatusbarn");

   return-1;//failtocreate

  }

  //TODO:

Deletethesethreelinesifyoudon'twantthetoolbartobedockable

  m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);

  EnableDocking(CBRS_ALIGN_ANY);

  DockControlBar(&m_wndToolBar);

  intindex=0;

  RECTrect;

  //找到指定的工具项

  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("Failedtocreatecombo-boxn");

    returnFALSE;

   }

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

   return0;

  }

  voidCMainFrame:

:

OnSelectZoomed()

  {

   CStringstrContent;

   m_wndToolBar.m_wndZoom.GetWindowText(strContent);

   AfxMessageBox(strContent);

  }

  四、小结

  为了实现OFFICE2000风格的工具条,本实例介了一种比较巧妙的方法,利用VisualC++6.0已有的开发环境支持,在工具条中加入了平面组合框控件,并实现了组合框的消息响应,用户选择组合框中的某一项后,会弹出一个对话框,提示用户所选择的信息。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 解决方案 > 学习计划

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1