vc6.0 动态工具栏按钮

头文件:

class CDynToolbarDlg : public CDialog
{
// Construction
public:
    BOOL OnToolTipNotify(UINT id, NMHDR *pNMHDR, LRESULT *pResult);//鼠标置于按钮上有文字提示
    void OnToolBtnClick(UINT nID);//
    void OnAddInfo();//
    CDynToolbarDlg(CWnd* pParent = NULL);    // standard constructor
    CToolBar    m_Toolbar;//工具栏
    CImageList    m_ImageList;//图像列表
    CString        m_szToolTip;//工具提示

源文件:

// DynToolbarDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DynToolbar.h"
#include "DynToolbarDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

//定义工具栏按钮命令ID
#define ID_ADDINFO        1001
#define ID_UPDATEINFO    1002
#define ID_DELINFO        1003
#define ID_QUERYINFO    1004
#define ID_SAVEINFO        1005

class CAboutDlg : public CDialog
{
public:
    CAboutDlg();

// Dialog Data
    //{{AFX_DATA(CAboutDlg)
    enum { IDD = IDD_ABOUTBOX };
    //}}AFX_DATA

    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CAboutDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    //}}AFX_VIRTUAL

// Implementation
protected:
    //{{AFX_MSG(CAboutDlg)
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
    //{{AFX_DATA_INIT(CAboutDlg)
    //}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CAboutDlg)
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    //{{AFX_MSG_MAP(CAboutDlg)
        // No message handlers
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDynToolbarDlg dialog

CDynToolbarDlg::CDynToolbarDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CDynToolbarDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CDynToolbarDlg)
        // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT
    // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDynToolbarDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CDynToolbarDlg)
        // NOTE: the ClassWizard will add DDX and DDV calls here
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDynToolbarDlg, CDialog)
    //{{AFX_MSG_MAP(CDynToolbarDlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    //}}AFX_MSG_MAP
    ON_COMMAND(ID_ADDINFO, OnAddInfo)
    ON_NOTIFY_EX(TTN_NEEDTEXT, 0, OnToolTipNotify)
//    ON_COMMAND_RANGE(ID_ADDINFO, ID_SAVEINFO, OnToolBtnClick)     
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDynToolbarDlg message handlers

BOOL CDynToolbarDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // Add "About..." menu item to system menu.

    // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000);

    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
        CString strAboutMenu;
        strAboutMenu.LoadString(IDS_ABOUTBOX);
        if (!strAboutMenu.IsEmpty())
        {
            pSysMenu->AppendMenu(MF_SEPARATOR);
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
        }
    }

    // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);            // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon
    
    
    //创建图像列表
    m_ImageList.Create(32, 32, ILC_COLOR24|ILC_MASK, 1, 1);    
    //向图像列表中添加图图像
    CBitmap bmp;
    for(int n=0; n<5; n++)
    {
        bmp.LoadBitmap(IDB_BITMAP1 + n);
        m_ImageList.Add(&bmp, RGB(255, 255, 255));
        bmp.DeleteObject();
    }
    //定义工具栏命令ID数组
    UINT nArray[5];
    for(int i=0; i<5; i++)
    {
        nArray[i] = ID_ADDINFO + i;
    }
    m_Toolbar.CreateEx(this);
    m_Toolbar.SetButtons(nArray, 5);
    //设置工具栏按钮和按钮图像大小
    m_Toolbar.SetSizes(CSize(60, 56), CSize(24, 24));
    //设置工具栏文本
    m_Toolbar.SetButtonText(0, "信息添加");
    m_Toolbar.SetButtonText(1, "信息修改");
    m_Toolbar.SetButtonText(2, "信息删除");
    m_Toolbar.SetButtonText(3, "信息查询");
    m_Toolbar.SetButtonText(4, "信息保存");
    //设置工具栏按钮显示图标
    m_Toolbar.GetToolBarCtrl().SetImageList(&m_ImageList);
    //显示工具栏
    RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
    // TODO: Add extra initialization here
    
    m_Toolbar.EnableToolTips(TRUE);
    return TRUE;  // return TRUE  unless you set the focus to a control
}


void CDynToolbarDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
        CAboutDlg dlgAbout;
        dlgAbout.DoModal();
    }
    else
    {
        CDialog::OnSysCommand(nID, lParam);
    }
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CDynToolbarDlg::OnPaint() 
{
    if (IsIconic())
    {
        CPaintDC dc(this); // device context for painting

        SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

        // Center icon in client rectangle
        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;

        // Draw the icon
        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialog::OnPaint();
    }
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CDynToolbarDlg::OnQueryDragIcon()
{
    return (HCURSOR) m_hIcon;
}

void CDynToolbarDlg::OnAddInfo()
{
    MessageBox("进入信息添加模块!");
}


void CDynToolbarDlg::OnToolBtnClick(UINT nID)
{
    switch(nID)
    {
    case ID_ADDINFO:
        MessageBox("进入信息添加模块");
        break;
    case ID_UPDATEINFO:
        MessageBox("进入信息修改模块");
        break;
    case ID_DELINFO:
        MessageBox("进入信息删除模块");
        break;
    case ID_QUERYINFO:
        MessageBox("进入信息查询模块");
        break;
    case ID_SAVEINFO:
        MessageBox("进入信息保存模块");
        break;
    default:
        break;
    }
}

BOOL CDynToolbarDlg::OnToolTipNotify(UINT id, NMHDR *pNMHDR, LRESULT *pResult)
{
    TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pNMHDR;
    UINT nID =pNMHDR->idFrom;                                         //获取工具栏按钮ID
    if(nID)                                                        //判断按钮ID是否存在
    {
        UINT nIndex = m_Toolbar.CommandToIndex(nID);                     //根据ID获取按钮索引
        if(nIndex!= -1)                                                //判断索引是否正确
        {
            m_Toolbar.GetButtonText(nIndex, m_szToolTip);                     //获取工具栏文本
            pTTT->lpszText = m_szToolTip.GetBuffer(m_szToolTip.GetLength());    //设置提示信息文本
            pTTT->hinst = AfxGetResourceHandle();                        //获得资源句柄
            return TRUE;                                            //返回TRUE值
        }
     }
     return FALSE;                                                    //返回FALSE值
}
发现自己的不足,善于利用找到的方法去扬长避短。行动起来。
原文地址:https://www.cnblogs.com/rechen/p/5132159.html