创建一个新的基 CDialogEx 派生 class

Creating a new base CDialogEx derived class

我有很多 CDialogEx 派生的 classes 在 OnInitDialog:

中做这样的事情
CMeetingScheduleAssistantApp::InitialiseResizeIcon(m_bmpResize, m_lblResize, this);
CMeetingScheduleAssistantApp::RestoreWindowPosition(_T("PublisherDB"), this, true);

然后,我将以下内容添加到每个派生对话框中 class:

int CPublishersDatabaseDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CDialogEx::OnCreate(lpCreateStruct) == -1)
        return -1;

    // Save Initial window size to m_rcInit
    GetWindowRect(&m_rcInit);

    return 0;
}

void CPublishersDatabaseDlg::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
{
    // Set the minimum window size to initial size.
    lpMMI->ptMinTrackSize.x = m_rcInit.Width();
    lpMMI->ptMinTrackSize.y = m_rcInit.Height();

    CDialogEx::OnGetMinMaxInfo(lpMMI);
}

void CPublishersDatabaseDlg::OnClose()
{
    CMeetingScheduleAssistantApp::SaveWindowPosition(_T("PublisherDB"), this);
    CDialogEx::OnClose();
}

每个对话框唯一不同的是用于保存 window 位置的短语。

我想要一个可以从中继承的基础 CDialogEx class 来执行上述操作。我查看了 SO,似乎有些问题并创建了一个 CDialog class 并继承了另一个 CDialog class。但是我要创建的这个 class 更通用。有效地用作基础而不是 CDialogEx.

这能做到吗?我是不是太复杂了?

问题

为什么我尝试创建一个新的 class,派生自 CDialogEx:

我不知道是不是因为它需要 对话 ID,如 here.

所述

Classes such as CDialog, CFormView, or CPropertyPage, which require a dialog ID.

所以我想不出创建基础 CDialogEx class 以用于我所有其他对话框 classes.

的正确方法

更新

我创建了这段代码,它告诉我 CResizingDialog 不是 class 或命名空间:

#include "ResizingDialog.h"
#include "resource.h"
#include "stdafx.h"

IMPLEMENT_DYNAMIC(CResizingDialog, CDialogEx)

CResizingDialog::CResizingDialog(const CString& strWindowID, UINT nIDTemplate, CWnd* pParent = nullptr)
    : m_strWindowID(strWindowID), CDialogEx(nIDTemplate, pParent)
{

}

CResizingDialog::~CResizingDialog()
{
}

void CResizingDialog::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CResizingDialog, CDialogEx)
    ON_WM_CREATE()
    ON_WM_GETMINMAXINFO()
    ON_WM_CLOSE()
END_MESSAGE_MAP()


int CResizingDialog::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CDialogEx::OnCreate(lpCreateStruct) == -1)
        return -1;

    // Save Initial window size to m_rcInit
    GetWindowRect(&m_rcInit);

    return 0;
}


void CResizingDialog::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
{
    // Set the minimum window size to initial size.
    lpMMI->ptMinTrackSize.x = m_rcInit.Width();
    lpMMI->ptMinTrackSize.y = m_rcInit.Height();

    CDialogEx::OnGetMinMaxInfo(lpMMI);
}


void CResizingDialog::OnClose()
{
    SaveWindowPosition(m_strWindowID, this);

    CDialogEx::OnClose();
}

根据鼓励我尝试手动创建 class 的评论,我成功了:

#include "stdafx.h"
#include "resource.h"
#include "ResizingDialog.h"

IMPLEMENT_DYNAMIC(CResizingDialog, CDialogEx)

CResizingDialog::CResizingDialog(const CString& strWindowID, UINT nIDTemplate, CWnd* pParent /* nullptr */, bool bOnlyStorePosition /* false */)
    : m_strWindowID(strWindowID),
      m_bOnlyStorePosition(bOnlyStorePosition), CDialogEx(nIDTemplate, pParent)
{

}

CResizingDialog::~CResizingDialog()
{
}

void CResizingDialog::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CResizingDialog, CDialogEx)
    ON_WM_CREATE()
    ON_WM_GETMINMAXINFO()
    ON_WM_CLOSE()
END_MESSAGE_MAP()

int CResizingDialog::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CDialogEx::OnCreate(lpCreateStruct) == -1)
        return -1;

    // Save Initial window size to m_rcInit
    GetWindowRect(&m_rcInit);

    return 0;
}

void CResizingDialog::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
{
    // Set the minimum window size to initial size.
    lpMMI->ptMinTrackSize.x = m_rcInit.Width();
    lpMMI->ptMinTrackSize.y = m_rcInit.Height();

    CDialogEx::OnGetMinMaxInfo(lpMMI);
}

void CResizingDialog::OnClose()
{
    SaveWindowPosition(m_strWindowID, this);

    CDialogEx::OnClose();
}

void CResizingDialog::OnOK()
{
    SaveWindowPosition();
    CDialogEx::OnOK();
}

BOOL CResizingDialog::OnInitDialog()
{
    CDialogEx::OnInitDialog();

    if(!m_bOnlyStorePosition)
        InitialiseResizeIcon(m_bmpResize, m_lblResize, this);

    RestoreWindowPosition(m_strWindowID, this, true);

    return TRUE;  // return TRUE unless you set the focus to a control
                  // EXCEPTION: OCX Property Pages should return FALSE
}

我决定将应用 class 中的方法复制到这个新对话框 class 中。最终它们可以从应用程序中删除 class。我还必须做的唯一一件事是 #include 我的资源文件,因为图像需要知道资源 ID 的值。

这是ResizingDialog.h header:

#pragma once
#include <afxwin.h>

class CResizingDialog : public CDialogEx
{
    DECLARE_DYNAMIC(CResizingDialog)

public:
    CResizingDialog(const CString& phrase, UINT nIDTemplate, CWnd* pParent = nullptr, bool bOnlyStorePosition = false); // Constructor
    virtual ~CResizingDialog(); // Destructor

protected:
    void OnOK() override;
    virtual void DoDataExchange(CDataExchange* pDX) override;    // DDX/DDV support
    void SaveWindowPosition(void) { SaveWindowPosition(m_strWindowID, this); }

public:
    BOOL OnInitDialog() override;
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg void OnGetMinMaxInfo(MINMAXINFO* lpMMI);
    afx_msg void OnClose();
    DECLARE_MESSAGE_MAP()

private:
    CBitmap m_bmpResize;
    CStatic m_lblResize;
    CRect m_rcInit;
    CString m_strWindowID;
    bool m_bOnlyStorePosition;

    void RestoreWindowPosition(CString strWindow, CWnd* pWindow, bool bOverrideState = false);
    void SaveWindowPosition(CString strWindow, CWnd* pWindow);
    void InitialiseResizeIcon(CBitmap& rBmpResize, CStatic& rLblResize, CWnd* pDialog);
};

此处未显示实际函数 SaveWindowPositionRestoreWindowPositionInitialiseResizeIcon,因为它们与问题没有直接关系。