欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ObjectArx利用CAdUiPalette和CAdUiPaletteSet类创建交互界面

程序员文章站 2022-05-22 10:35:51
...

   经常在软件中看到如下图所示的交互窗口,使用频率也比较频繁,它其实是一个 CAdUiPaletteSet类控件中嵌套了一个CAdUiPalette类控件,然后在CAdUiPalette中添加控件或者绑定相应的对话框来进行内容的展示,所用到的类继承关系:

1)CAdUiPaletteSet ——CAdUiDockControlBar——CControlBar
2)CAdUiPalette——CWnd

ObjectArx利用CAdUiPalette和CAdUiPaletteSet类创建交互界面

下面用一个简单例子展示一下开发流程:

- 1. CAdUiPaletteSet类的使用

- 1. 1 TunnelSectionPaletteSet .h头文件

#pragma once
#include "TunnelSectionPalette.h"

class CTunnelSectionPaletteSet : public CAdUiPaletteSet
{
    DECLARE_DYNAMIC(CTunnelSectionPaletteSet)

public:
    virtual ~CTunnelSectionPaletteSet();

    static void Refresh();

        // 创建单实例对象
    static CTunnelSectionPaletteSet* Instance();

    static void Destroy();

        // 停靠位置
    static CRect DockRect();

        // 设置可见性
    void SetVisible(bool bVisible = true);


protected:
    CTunnelSectionPaletteSet();

        //计算停靠位置
    virtual CSize CalcFixedLayout (BOOL bStretch, BOOL bHorz);

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

        // 不允许改变大小 
    virtual BOOL CanBeResized() const{ return TRUE;}; 

    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);

    virtual void PaletteActivated(CAdUiPalette* pActivated, CAdUiPalette* pDeactivated);

    DECLARE_MESSAGE_MAP()

private:
    static CTunnelSectionPaletteSet* s_pPaletteSet;
};

- 1. TunnelSectionPaletteSet .cpp文件

#include "StdAfx.h"
#include "TunnelSectionPaletteSet.h"

static CLSID CLSID_TUNNLESECTION_PALETTESET = { 0x76e12808, 0x4eb1, 0x47a0, { 0xad, 0x38, 0xfd, 0x5c, 0xec, 0x70, 0x8a, 0x38 } };
CTunnelSectionPaletteSet* CTunnelSectionPaletteSet::s_pPaletteSet = NULL;

IMPLEMENT_DYNAMIC(CTunnelSectionPaletteSet, CAdUiPaletteSet)

    BEGIN_MESSAGE_MAP(CTunnelSectionPaletteSet, CAdUiPaletteSet)
        ON_WM_CREATE()
    END_MESSAGE_MAP()

CTunnelSectionPaletteSet::~CTunnelSectionPaletteSet()
{

}

CTunnelSectionPaletteSet::CTunnelSectionPaletteSet()
{

}

CSize CTunnelSectionPaletteSet::CalcFixedLayout(BOOL bStretch, BOOL bHorz)
{
    return DockRect().Size();
}

int CTunnelSectionPaletteSet::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CAdUiPaletteSet::OnCreate(lpCreateStruct) == -1)
    {
        return -1;
    }

    return 0;
}

void CTunnelSectionPaletteSet::PaletteActivated(CAdUiPalette* pActivated, CAdUiPalette* pDeactivated)
{
    CAdUiPaletteSet::PaletteActivated(pActivated, pDeactivated);

    CTunnelSectionPalette *pTunnelPalete = dynamic_cast<CTunnelSectionPalette*>(pActivated);
    if (pTunnelPalete)
    {
        pTunnelPalete->UpdateTree();
    }
}

CTunnelSectionPaletteSet* CTunnelSectionPaletteSet::Instance()
{
    if (s_pPaletteSet)
    {
        return s_pPaletteSet;
    }

    CAcModuleResourceOverride resOverride;
    CMDIFrameWnd* pAcadFrame = acedGetAcadFrame();  

    s_pPaletteSet = new CTunnelSectionPaletteSet;
    s_pPaletteSet->Create(_T("大样图管理器"), WS_OVERLAPPED | WS_DLGFRAME, DockRect(), pAcadFrame,  PSS_AUTO_ROLLUP | PSS_CLOSE_BUTTON);
    s_pPaletteSet->SetToolID(&CLSID_TUNNLESECTION_PALETTESET);
    s_pPaletteSet->EnableDocking(CBRS_ALIGN_ANY | CBRS_SIZE_FIXED);

    CTunnelSectionPalette* pPalette = new CTunnelSectionPalette;
    pPalette->Create(WS_CHILD | WS_VISIBLE, _T("大样图管理器"), s_pPaletteSet);

    s_pPaletteSet->AddPalette(pPalette);

    return s_pPaletteSet;
}

void CTunnelSectionPaletteSet::Destroy()
{
    if (s_pPaletteSet)
    {
        s_pPaletteSet->DestroyWindow();

        delete s_pPaletteSet;
        s_pPaletteSet = NULL;
    }
}

CRect CTunnelSectionPaletteSet::DockRect()
{
    long nHight = 0;
    if (!s_pPaletteSet)
    {
        CRect rect;
        ::GetWindowRect(acedGetAcadFrame()->m_hWndMDIClient, &rect);
        acedGetAcadFrame()->ScreenToClient(&rect);

        nHight = rect.bottom - rect.top;
    }
    else
    {
        CRect rect;
        s_pPaletteSet->GetWindowRect(&rect);
        s_pPaletteSet->ScreenToClient(&rect);
        nHight = rect.bottom - rect.top;
        if (s_pPaletteSet->IsFloating())
        {
            ::GetWindowRect(acedGetAcadFrame()->m_hWndMDIClient, &rect);
            acedGetAcadFrame()->ScreenToClient(&rect);

            nHight = rect.bottom - rect.top;
            if (nHight < 600)
            {
                nHight = 600;
            }
        }
    }

    CRect dockRect(0, 0, 180, nHight);

    return dockRect;
}

void CTunnelSectionPaletteSet::SetVisible(bool bVisible /*= true*/)
{
    CMDIFrameWnd *pAcadFrame = acedGetAcadFrame();
    s_pPaletteSet->RestoreControlBar();
    pAcadFrame->ShowControlBar(this, bVisible, FALSE);
}

- 2. CAdUiPalette类的使用

- 2. 1 TunnelSectionPalette.h头文件

#pragma once
#include "ObjectsDialog.h"

class CTunnelSectionPalette : public CAdUiPalette
{
    DECLARE_SERIAL(CTunnelSectionPalette)

public:
    CTunnelSectionPalette();
    virtual ~CTunnelSectionPalette();

    virtual void OnSetActive();

    virtual bool CanFrameworkTakeFocus();
protected:
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg void OnSize(UINT nType, int cx, int cy);

    DECLARE_MESSAGE_MAP()
private:
    CObjectsDialog* m_pObjectsDlg;
};

- 2. 2 TunnelSectionPalette.cpp文件

#include "StdAfx.h"
#include "TunnelSectionPalette.h"


IMPLEMENT_SERIAL(CTunnelSectionPalette, CAdUiPalette, 1)

BEGIN_MESSAGE_MAP(CTunnelSectionPalette, CAdUiPalette)
    ON_WM_CREATE()
    ON_WM_SIZE()
END_MESSAGE_MAP()

CTunnelSectionPalette::CTunnelSectionPalette()
: CAdUiPalette()
{
    m_pObjectsDlg = NULL;
}

CTunnelSectionPalette::~CTunnelSectionPalette()
{
    if (m_pObjectsDlg)
    {
        delete m_pObjectsDlg;
        m_pObjectsDlg = NULL;
    }
}

int CTunnelSectionPalette::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CAdUiPalette::OnCreate(lpCreateStruct) == -1)
        return (-1);

    CAcModuleResourceOverride resOverride;
    CRect rect;

    GetClientRect(&rect);
    m_pObjectsDlg = new CObjectsDialog;
    m_pObjectsDlg->Create(CObjectsDialog::IDD, this);
    m_pObjectsDlg->MoveWindow(rect);
    m_pObjectsDlg->ShowWindow(SW_SHOW);

    return (0);
}

void CTunnelSectionPalette::OnSetActive()
{
    return (CAdUiPalette::OnSetActive());
}

bool CTunnelSectionPalette::CanFrameworkTakeFocus()
{
    return true;
}

void CTunnelSectionPalette::OnSize(UINT nType, int cx, int cy)
{
    CAdUiPalette::OnSize(nType, cx, cy);

    CRect rect;
    GetClientRect(rect);

    m_pObjectsDlg->MoveWindow(rect);
    m_pObjectsDlg->Invalidate();
}

- 3.效果展示:

ObjectArx利用CAdUiPalette和CAdUiPaletteSet类创建交互界面