0

我有很多CDialogEx派生类在做这样的事情OnInitDialog

CMeetingScheduleAssistantApp::InitialiseResizeIcon(m_bmpResize, m_lblResize, this);
CMeetingScheduleAssistantApp::RestoreWindowPosition(_T("PublisherDB"), this, true);

然后,我在每个派生对话框类中添加了以下内容:

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();
}

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

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

这可以做到吗?我是否过于复杂了?

问题

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

设置

结果

错误

我不知道是不是因为它需要一个对话框 ID ,如此所述。

需要对话 ID的类,如CDialogCFormView或。CPropertyPage

所以我无法找到正确的方法来创建一个用于我所有其他对话框类的基本 CDialogEx 类。

更新

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

#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();
}
4

1 回答 1

1

根据鼓励我尝试手动创建类的评论,我让它工作:

#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
}

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

这是ResizingDialog.h标题:

#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);
};

实际的功能SaveWindowPosition,这里没有显示RestoreWindowPositionInitialiseResizeIcon因为它们与问题没有直接关系。

于 2018-06-19T12:01:06.777 回答