QQ登录

只需一步,快速开始

上位机MFC对话框实现工具条的使用

[ 复制链接 ]
在建立基于对话框的应用程序中,MFC AppWizard还没有提高工具条和工具提示的支持。
当前例程主要介绍如何在基于对话框的应用程序中添加工具条和工具提示。
效果如下图。

上位机MFC对话框实现工具条的使用

上位机MFC对话框实现工具条的使用

点击界面工具栏按钮可执行相应功能。
点击top 或Bottom可以将工具栏放置在界面上部或下部。
源代码下载地址:
请点击此处下载

请先注册会员后在进行下载

已注册会员,请先登录后下载

文件名称:TOOLBAR_AND_TOOLTIPS.rar 
文件大小:31.84 KB  售价:1金币
下载权限: 不限 以上或 VIP会员   [购买捐助会员]   [充值积分]   有问题联系我



例程源代码的使用,或以下载后打开工程学习。
大概过程为:

首先,定义工具条的按钮命令标识:
    #define ID_NEW  WM_USER + 100
    #define ID_LIST  WM_USER + 101
    #define ID_CUT  WM_USER + 102
    ..........

在消息映射表中加入下面内容:
         ON_NOTIFY_EX( TTN_NEEDTEXT, 0, NotifyFunction )

//ClassWizard does not handle ON_NOTIFY_EX or ON_NOTIFY_EX_RANGE; if you want to use either of them, you need to edit your message //map yourself.

        BEGIN_MESSAGE_MAP(CDlg_TTBarDlg, CDialog)
         //{{AFX_MSG_MAP(CDlg_TTBarDlg)
            ........................................
            .......................................
         //}}AFX_MSG_MAP
         ON_NOTIFY_EX( TTN_NEEDTEXT, 0, NotifyFunction )
       END_MESSAGE_MAP()

TTN_NEEDTEXT消息被送回程序,NotifyFunction被激活:

BOOL CDlg_TTBarDlg::NotifyFunction( UINT id, NMHDR * pTTTStruct, LRESULT * pResult )
{
pResult = NULL ; // Not Used
id = 0 ; // Not used
  TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pTTTStruct;
UINT nID = pTTTStruct->idFrom;
switch ( nID)
{
  case ID_NEW:
   pTTT->lpszText = MAKEINTRESOURCE((LPCSTR)(IDS_NEW));
   break ;
  case ID_LIST:
   pTTT->lpszText = MAKEINTRESOURCE((LPCSTR)(IDS_LIST));
   break ;
  case ID_CUT:
   pTTT->lpszText = MAKEINTRESOURCE((LPCSTR)(IDS_CUT));
   break ;
  case ID_PRINT:
   pTTT->lpszText = MAKEINTRESOURCE((LPCSTR)(IDS_PRINT));
   break ;
  case ID_EXIT:
   pTTT->lpszText = MAKEINTRESOURCE((LPCSTR)(IDS_EXIT));
   break ;
}
return(TRUE);
}

为了显示工具条和工具条按钮,添加下面代码到对话框:

  CToolBarCtrl *m_toolbarCtrl ;        // ToolBar Control
  TBBUTTON btn[5];                    // Array of toolbar Buttons , depending upon the number of buttons

每个按钮需要初始化如下:  

btn[0].iBitmap = 0 ;
btn[0].idCommand = ID_NEW ;
btn[0].fsState = TBSTATE_ENABLED  ;
btn[0].fsStyle = TBSTYLE_BUTTON  ;
btn[0].dwData = 0 ;
btn[0].iString = 0;

btn[1].iBitmap = 1 ;
btn[1].idCommand = ID_LIST ;
btn[1].fsState = TBSTATE_ENABLED  ;
btn[1].fsStyle = TBSTYLE_BUTTON ;
btn[1].dwData = 0 ;
btn[1].iString = 1;

......................
.......................

// Enable the Tool Tips with the following line

EnableToolTips(TRUE);

// Create the ToolBar
m_toolbarCtrl = new CToolBarCtrl() ;

// If you want to position the toolbar at the top  use the CCS_TOP option.
     m_toolbarCtrl->Create(TBSTYLE_TOOLTIPS |WS_CHILD | WS_VISIBLE | CCS_TOP | CCS_NODIVIDER, rc ,this , 0 );
else
// If you want to position the toolbar at the bottom  use the CCS_BOTTOM option.
     m_toolbarCtrl->Create(TBSTYLE_TOOLTIPS |WS_CHILD | WS_VISIBLE | CCS_BOTTOM | CCS_NODIVIDER, rc ,this , 0 );

// Add the number of buttons to be added to the Toobar Control
m_toolbarCtrl->AddButtons(5, btn);

// Select the Bitmap to be displayed
m_toolbarCtrl->AddBitmap(1, IDR_MAINFRAME);

// Set the button size and the Image Size

CSize sz_btn(30,30);
m_toolbarCtrl->SetButtonSize(sz_btn);

CSize sz_img(30,30);
m_toolbarCtrl->SetBitmapSize(sz_img);

// Call Autosize to set the button and bitmap size
m_toolbarCtrl->AutoSize();


回复

使用道具 举报

点击查看
快速回复 返回列表 客服中心 搜索