dedecms织梦内容管理系统      
首页 | 51单片机 | 休闲娱乐 | 手工乐园 | 免费软件 | 理财专区 | 串口通信 | UNIX/LINUX | TurboC | 专题 | 会员中心 | 支持论坛
  当前位置:主页>MFC/C++>文章内容
对话框中使用ToolBar最简方法
来源:http://www.codeproject.com/dialog/dialog_tips.asp 作者:温柔老胡 发布时间:2007-06-28  

Introduction

Hi friends this is my first article on Code Project, so sorry for the lack of presentation skills. There already one article is present on this site on this topic: "Tips for dialog based applications". (http://www.codeproject.com/dialog/dialog_tips.asp) But I am trying to put together the useful information form various sources regarding this topic.

Coming to the point my first suggestion: If you want to use a toolbar dont go for a dialog box. But for the applications already developed with dialog box and then need a toolbar to be added, this article is useful.

Lets start it from scratch. Stet by Step.

1) Right click Solution Explorer -> Add -> Resource -> Tool Bar -> New. This will open into Tool Bar Editor.

2)You can Create/Paint Buttons here. Make sure you change the default Ids of these buttons to some proper usable ids. (Use Property Explorer for this), this will create a .bmp image in resources. (Lets say, The Tool Bar is ‘TDR_TOOLBAR1’ and the two buttons are ‘IDC_TBUTTON1’ and ‘IDC_TBUTTON2’ )

3)Now go to the .h for dialog(You can add a class to a dialog) on which toolbar need to be placed. Declare a ‘CToolBar’ Object

CToolBar m_FirstToolBar

4)Now on the .cpp file of the dialog, in ‘OnInitDialog()’ add following code;(You Can add OnInitDialog event to the directly by declaring it in .h file added and using it in .cpp file)

if(!m_FirstToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || !m_FirstToolBar.LoadToolBar(IDR_TOOLBAR1))

{

   EndDialog(IDCANCEL);

}

 

RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST,0);

 

4.1. Here you can see the various attributes set for the toolbar in ‘CreateEx(...)’ like ‘TBSTYLE_FLAT’ for flat look of toolbar buttons;

4.2 In ‘LoadToolBar(...)’ the Resource created for the toolbar is used. This maps our toolbar object ‘m_FirstToolBar’ with resource ‘IDR_TOOLBAR1’

4.3 ‘RepositionBars()’ is used to place the toolbar on the dialog.

5) ‘MoveWindow()’ can be used to place the toolbar on the desired position.

m_FirstToolBar.MoveWindow( 50, 0,100, 100, 1 );

Now if you have finalized the place where you want to place the tool bar you can put it there using MoveWindow(...), my suggestion is to put a label in the design of dialog at place you want to put you toolbar; Then at run time get the position of this label and place the toolbar there using MoveWindow(...)

If you want to place this toolbar on Top Left and dynamically move all the controls accordingly and increase the dialog dimensions refer the article on this site I suggested above.

6)This is how the buttons of Tool Bar can be made visible/invisible

m_FirstToolBar.GetToolBarCtrl().HideButton(IDC_TBUTTON1, TRUE);

Notice that we have used the ‘IDC_TBUTTON1’ which we declared at the time of creating toolbar resource.

7) Similarly

m_FirstToolBar.GetToolBarCtrl().EnableButton(IDC_TBUTTON1,false)

can be used to disable a button.

8) To handle Click events: 8.1 Add following line to BEGIN_MESSAGE_MAP()

ON_COMMAND(IDC_TBUTTON1,OnToolBarButton1)

8.2 Write the desired code in .cpp file

FirstToolBarDialog:: OnToolBarButton1()

 

{

 

- - -

 

      - - -

 

}

 


(阅读次数:
上一篇:怀孕实用宝典30条   下一篇:对话框中使用ToolBar并显示ToolsTip
[收藏] [推荐] [评论(0条)] [返回顶部] [打印本页] [关闭窗口]  
用户名: 新注册) 密码: 匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
 §最新评论
  热点文章
·使用C++编写的 双向链表C++类
·使用C++编写的 CString类
·RichEditCtrl 使用方法
·CRichEdit 动态加入带有格式的文
·对话框中使用ToolBar并显示Tools
  相关文章
·对话框中使用ToolBar并显示Tools
·CRichEdit 动态加入带有格式的文
·RichEditCtrl 使用方法
·使用C++编写的 CString类
·使用C++编写的 双向链表C++类

 Powered by 温柔老胡 System by www.dedecms.com