CMainFrame 和 CInPlaceFrame 公共接口的封装


背景

在 Windows 编程中,对于要实现支持自动化技术的工程,需要存在两个框架类CMainFrameCInPlaceFrame。当处于非嵌入状态时,通过AfxGetMainWnd()theApp.GetMainWnd()获取到的主框架为CMainFrame的对象,反之即为CInPlaceFrame的对象(虽然非嵌入状态下,应用程序也会产生CMainFrame对象)。一般情况下,嵌入状态和非嵌入状态应用程序所实现的功能基本相同,只是非嵌入状态下功能多通过对应用程序界面的操作实现,而嵌入状态多为对应用程序接口函数的调用。

问题


在为嵌入状态封装对外接口时,每次对需要用到主框架的地方在获取主框架指针后都需要判断应用程序是否处于嵌入状态,然后将主框架指针转换为相应类型再调用其方法。

解决思路


  • 将使用到主框架的功能进行封装

如获取某个面板,我们可以将该功能封装为其他类的一个成员函数,在该成员函数中做是否为嵌入状态的判断,然后获取对应框架下的面板,这样我们就屏蔽了对框架的操作。此方法的问题也很明显,工程庞大时我们可能需要封装大量这样的函数来满足需求,并且这也不符合面向对象的基本要求。

  • 封装纯虚基类

创建一个基类MainFrameInterface使CMainFrameCInPlaceFrame 都从此类继承,将需要CMainFrameCInPlaceFrame同时实现的功能均添加到此基类中并声明为纯虚成员函数:

// MainFrameInterface.h
#pragma once
class MainFrameInterface
{
public:
      MainFrameInterface(){}
      virtual ~MainFrameInterface(){}
protected:
      MyPanel* getMyPanel() = 0;
}
// MainFrm.h
#pragma once
class CMainFrame : public CBCGPFrameWnd, public MainFrameInterface
{
protected:
      virtual MyPanel* getMyPanel();
...
}
// IpFrame.h
#pragma once
class CInPlaceFrame : public CBCGPOleDocIPFrameWnd, public MainFrameInterface
{
protected:
      virtual MyPanel* getMyPanel();
...
}

然后给应用程序类添加一个获取主框架接口对象指针的成员函数:

#pragma once
class CMyApp : public CBCGPWinApp
{
public:
      MainFrameInterFace* getMainFrameInterface()
      {
            MainFrameInterface* pMainFrameInterface = 0;
            CFrameWnd* pMainFrame = this->GetMainWnd();
            if(pMainFrame)
            {
                  // 注意:若直接使用
                  // pMainFrameInterface = dynamic_cast<MainFrameInterface*>(pMainFrame);
                  // pMainFrameInterface 将永远为空
                  if(pMainFrame->IsKindOf(RUNTIME_CLASS(CInPlaceFrame)))
                  {
                        pMainFrameInterface = dynamic_cast<CInPlaceFrame*>(pMainFrame);
                  }
                  else
                  {
                        pMainFrameInterface = dynamic_cast<CMainFrame*>(pMainFrame);
                  }
            }
           return pMainFrameInterface;
      }
...
}

使用时:

MainFrameInterface* pMainFrameInterface = theApp.getMainFrameInterface();
if(pMainFrameInterface)
{
      MyPanel* pMyPanel = pMainFrameInterface->getMyPanel();
}

这样我们就可以通过getMainFrameInterface()获取主框架的接口对象,直接调用虚拟接口的成员函数即可通过多态的方式访问到正确的主框架对应的功能。

总结


该方法我已经在实际的项目中使用过了,可以正确实现预期功能。但由于我自己的开发经验有限,如果其中存在任何不妥的地方,还希望大家能够给予指正。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容