请问如何给Dialog based工程添加Toolbar?

demo001 2004-04-11 10:00:15
在下通过Classwizard建立一个Dialog based程序
添加了Toolbar资源,并且在对话框类的::OnCreate(LPCREATESTRUCT lpCreateStruct)中添加if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
m_ToolBar.Create(this,WS_CHILD|WS_VISIBLE|CBRS_TOP,AFX_IDW_TOOLBAR);
m_ToolBar.LoadToolBar(IDR_DRAWTOOLBAR);
return 0;

结果并不能创建Toolbar

而如果是一个SDI工程,在CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)中添加相同的语句,就可以建立Toolbar了

请教各位,这是为什么呢?如何给Dialog based工程添加Toolbar呢?小生谢过!
...全文
480 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
badguy2002 2004-04-11
  • 打赏
  • 举报
回复
因为vc中默认不支持对话框中拥有工具栏,所以你那样做是不行的,需要自己做,就按照上面说的方法
_foo 2004-04-11
  • 打赏
  • 举报
回复
Q:如何在对话框中加入工具条

在 OnInitDialog 中加入下面代码: BOOL CYourDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Create the toolbar. To understand the meaning of the styles used, you
// can take a look at the MSDN for the Create function of the CToolBar class.

ToolBar.Create(this, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_TOOLTIPS |CBRS_FLYBY | CBRS_BORDER_BOTTOM);

// I have assumed that you have named your toolbar''s resource as IDR_TOOLBAR1.
// If you have given it a different name, change the line below to accomodate
// that by changing the parameter for the LoadToolBar function.

ToolBar.LoadToolBar(IDR_TOOLBAR1);

CRect rcClientStart;
CRect rcClientNow;
GetClientRect(rcClientStart);


// To reposition and resize the control bar

RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST,0, reposQuery, rcClientNow);
CPoint ptOffset(rcClientNow.left - rcClientStart.left,rcClientNow.top-rcClientStart.top);

CRect rcChild;
CWnd* pwndChild = GetWindow(GW_CHILD);

while (pwndChild)
{
pwndChild->GetWindowRect(rcChild);
ScreenToClient(rcChild);
rcChild.OffsetRect(ptOffset);
pwndChild->MoveWindow(rcChild, FALSE);
pwndChild = pwndChild->GetNextWindow();
}
CRect rcWindow;
GetWindowRect(rcWindow);
rcWindow.right += rcClientStart.Width() - rcClientNow.Width();
rcWindow.bottom += rcClientStart.Height() - rcClientNow.Height();
MoveWindow(rcWindow, FALSE);

// And position the control bars
RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);

return TRUE; // return TRUE unless you set the focus to a control
}

badguy2002 2004-04-11
  • 打赏
  • 举报
回复
url:http://cpc.3322.net/technology/index.html
在对话框中加入工具栏
苏水荣

  工具栏(ToolBar)是一种非常方便的控件,能大大增加用户操作的效率,但是基于对话框的程序,却不能像使用编辑框(Edit Box)和列表框(List Box)一样,方便地增加工具栏控件。本文将介绍一种在对话框中加入工具栏的方法。


  一、 技术要点分析


  所有的Windows控件(包括工具栏、编辑框等)都派生自CWnd类,这就意味着,我们可以用窗口类的Create()函数把它们“创建”并显示到另一个窗口(例如对话框)上。把工具栏加入到对话框中正是使用了这样的一种方法。

  通常,我们使用CToolBarCtrl类(派生自CWnd类)来创建并管理工具栏控件。使用这个类创建一条工具栏的一般步骤如下:

  1.派生一个CToolBarCtrl的对象;

  2.调用CToolBarCtrl::Create函数创建工具栏对象;

  3.调用CToolBarCtrl::AddBitmap()和CToolBarCtrl::AddString()为工具栏对象加入位图和提示信息;

  4.派生一个TBUTTON数组对象进行工具栏中各按钮的具体设置;

  5.修改主窗口的OnNotify()函数,以显示工具栏上的提示信息。

  以上步骤在下面的范例代码中会有具体体现。


  二、 范例程序的建立与主要代码分析


  利用Visual C++ 的向导生成一个基于对话框的程序,命名为ToolBarInDial。修改主对话框样式如图1。绘出一条工具栏的位图并建立一选单,设置几个子选单项,然后建立一组工具栏的提示信息串(String Table),一旦鼠标在工具栏某项上停留,就会显示提示信息。下面给出程序中的主要代码。

  在主对话框CToolBarInDialDlg的类定义中有如下的变量说明:

  CToolBarCtrl ToolBar;

  int ButtonCount;

  int ButtonBitmap;

  BOOL DoFlag;

  TBUTTON m_Button[5];

  //设置工具栏上具体信息的变量数组

  //主对话框的初始化函数

  BOOL CToolBarInDialDlg::OnInitDialog()

  {

  RECT rect;

  //设置工具栏的显示范围

  rect.top=0; rect.left=0; rect.right=48; rect.bottom=16;

  ToolBar.Create(WS_CHILD|WS_VISIBLE|CCS_TOP|TBSTYLE_TOOLTIPS|CCS_ADJUSTABLE,rect,this,0);

  //建立工具栏并设置工具栏的样式

  ButtonBitmap=ToolBar.AddBitmap(5,IDB_PLAY); //加入工具栏的位图

  ButtonString=ToolBar.AddString(IDS_FIRST);//加入工具栏的提示信息

  //以下代码开始设置各具体的按钮

  m_Buttons[ButtonCount].iBitmap=

  ButtonBitmap+ButtonCount; //ButtonCount初值为0

  m_Buttons[ButtonCount].idCommand=ID_PLAY; //工具栏与选单上某子项对应

  m_Buttons[ButtonCount].fsState=TBSTATE_ENABLED;

  //设置工具栏按钮为可选

  m_Buttons[ButtonCount].fsStyle=TBSTYLE_BUTTON;

  //设置工具栏按钮为普通按钮

  m_Buttons[ButtonCount].dwData=0;

  m_Buttons[ButtonCount].iString=IDS_LAST;

   ++ButtonCount;

  //类似地设置第二个按钮

  m_Buttons[ButtonCount].iBitmap=ButtonBitmap+ButtonCount;

  m_Buttons[ButtonCount].idCommand=ID_STOP;

  m_Buttons[ButtonCount].fsState=TBSTATE_ENABLED;

  m_Buttons[ButtonCount].fsStyle=TBSTYLE_BUTTON;

  m_Buttons[ButtonCount].dwData=0;

  m_Buttons[ButtonCount].iString=IDS_NEXT;

  ++ButtonCount;

  ……//省略设置剩下的按钮的代码

   ToolBar.AddButtons(ButtonCount,m_Buttons);

  //为工具栏加入按钮并显示在对话框中

   return TRUE;

  }

  //当鼠标在工具栏上停留时,调用这个函数来显示提示信息

  BOOL CToolBarInDialDlg::OnNotify(WPARAM wParam, LPARAM lParam, LRESULTpResult)

  {

  TOOLTIPTEXTtt;

  tt=(TOOLTIPTEXT)lParam;

  CString Tip;

  switch(tt->hdr.code)

  {

  case TTN_NEEDTEXT:

  //该信息表明要求显示工具栏上的提示

  switch(tt->hdr.idFrom)

  {

  case ID_PLAY:


  图1

  Tip.LoadString(IDS_FIRST); //设置对应于工具栏上ID_PLAY的按钮的提示信息

  break;

  case ID_STOP:

  Tip.LoadString(IDS_NEXT);

  //IDS_FIRST,IDS_NEXT等为一系列CString串

  break;

  ……//类似地设置剩下按钮的提示信息

  }

   strcpy(tt->szText,(LPCSTR)Tip);

   //显示提示信息

  break;

  }

  return CDialog::OnNotify(wParam, lParam, pResult);

  }

  //该演示程序的工具栏能由用户定制,随时增加或删除工具栏中的某一项

  void CToolBarInDialDlg::OnApply()

  {

   switch(DoFlag) //用户选择了增加或删除工具栏中的“退出”按钮

  {

  case TRUE: //增加工具栏上的“退出”按钮

  m_Buttons[ButtonCount].iBitmap=ButtonBitmap+ButtonCount;

  m_Buttons[ButtonCount].idCommand=ID_QUIT;

  m_Buttons[ButtonCount].fsState=TBSTATE_ENABLED;

  m_Buttons[ButtonCount].fsStyle=TBSTYLE_BUTTON;

  m_Buttons[ButtonCount].dwData=0;

  m_Buttons[ButtonCount].iString=IDS_FIRST;

  ToolBar.InsertButton(ButtonCount,&&m_Buttons[ButtonCount]);

  //根据m_Buttons的信息在工具栏的尾部加上一个按钮

  break;

  case FALSE:

  if(ToolBar.GetButtonCount()==4) //删除工具栏上某一特定位置的按钮

  {

  ToolBar.DeleteButton(3);

  //删除工具栏上某一按钮

  }

  break;

  }

  }

  void CToolBarInDialDlg::OnPlay() //响应函数举例

  {

  ……

  //对应选单项的响应函数

  }

  以上程序在中/英文Windows 98、VC++ 6.0环境下编译通过,运行正常。图2为运行中的有工具栏的对话框程序。


  图2

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧