MFC基础知识(七)——停靠窗口添加工具条

先创建工具条,设ID为IDR_Object,修改程序如下:
ObjectWnd.h修改:

#pragma once
#include "stdafx.h"
#include "afxdockablepane.h"
#include "ObjectDlg.h"
#include "afxwin.h"

//添加继承类
class CObjectToolBar : public CMFCToolBar
{
public:
    virtual void OnUpdateCmdUI(CFrameWnd* /*pTarget*/, BOOL bDisableIfNoHndler)
    {
        CMFCToolBar::OnUpdateCmdUI((CFrameWnd*) GetOwner(), bDisableIfNoHndler);
    }

    virtual BOOL AllowShowOnList() const { return FALSE; }
};


class CObjectWnd :
    public CDockablePane
{
public:
    CObjectWnd(void);
    ~CObjectWnd(void);
    DECLARE_MESSAGE_MAP()
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg void OnSize(UINT nType, int cx, int cy);
    afx_msg void OnDestroy();


    CObjectToolBar m_wndToolBar;//工具条对象
    CObjectDlg m_objectDlg; //对话框类

    void AdjustLayout();

};

ObjectWnd.cpp修改:

#include "stdafx.h"
#include "ObjectWnd.h"
#include "resource.h"
#include "MainFrm.h"
#include "CSDNtest.h"//添加此头文件(工程名.h)

CObjectWnd::CObjectWnd(void)
{
}


CObjectWnd::~CObjectWnd(void)
{
}

BEGIN_MESSAGE_MAP(CObjectWnd, CDockablePane)
    ON_WM_CREATE()
    ON_WM_SIZE()
    ON_WM_DESTROY()
END_MESSAGE_MAP()


void CObjectWnd::AdjustLayout()
{
    if (GetSafeHwnd () == NULL || (AfxGetMainWnd() != NULL && AfxGetMainWnd()->IsIconic())) { return; } CRect rectClient; GetClientRect(rectClient); //设置工具条位置 int cyTlb = m_wndToolBar.CalcFixedLayout(FALSE, TRUE).cy; m_wndToolBar.SetWindowPos(NULL, rectClient.left, rectClient.top, rectClient.Width(),cyTlb, SWP_NOACTIVATE | SWP_NOZORDER); //控件在窗口中所占空间大小 m_objectDlg.SetWindowPos(this,rectClient.left,rectClient.top+cyTlb,rectClient.Width(),rectClient.Height()-cyTlb,SWP_NOACTIVATE | SWP_NOZORDER); } int CObjectWnd::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CDockablePane::OnCreate(lpCreateStruct) == -1) return -1; CRect rectDummy; rectDummy.SetRectEmpty(); // 创建组合: const DWORD dwViewStyle = WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | WS_BORDER | CBS_SORT | WS_CLIPSIBLINGS | WS_CLIPCHILDREN; //创建工具条 m_wndToolBar.Create(this, AFX_DEFAULT_TOOLBAR_STYLE, IDR_Object); m_wndToolBar.LoadToolBar(IDR_Object, 0, 0, TRUE /* 已锁定*/); m_wndToolBar.CleanUpLockedImages(); m_wndToolBar.LoadBitmap(theApp.m_bHiColorIcons ? IDR_Object : IDR_Object, 0, 0, TRUE /* 锁定*/); m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() | CBRS_TOOLTIPS | CBRS_FLYBY); m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() & ~(CBRS_GRIPPER | CBRS_SIZE_DYNAMIC | CBRS_BORDER_TOP | CBRS_BORDER_BOTTOM | CBRS_BORDER_LEFT | CBRS_BORDER_RIGHT)); m_wndToolBar.SetOwner(this); // 所有命令将通过此控件路由,而不是通过主框架路由: m_wndToolBar.SetRouteCommandsViaFrame(FALSE); // 创建对话框窗口: if (!m_objectDlg.Create(IDD_ObjectDlg,this)) { TRACE0("未能创建对话框窗口\n"); return -1; // 未能创建 } m_objectDlg.ShowWindow(SW_SHOW); AdjustLayout(); return 0; } void CObjectWnd::OnSize(UINT nType, int cx, int cy) { CDockablePane::OnSize(nType, cx, cy); AdjustLayout(); } void CObjectWnd::OnDestroy() { CDockablePane::OnDestroy(); m_objectDlg.DestroyWindow(); // TODO: 在此处添加消息处理程序代码 } 

运行结果:

你可能感兴趣的:(mfc,工具条,停靠窗口,停靠视图)