简体   繁体   English

创建一个新的基础CDialogEx派生类

[英]Creating a new base CDialogEx derived class

I have a lot of CDialogEx derived classes that do something like this in OnInitDialog : 我有很多CDialogEx派生类,它们在OnInitDialog

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

Then, I have the following added to each derived dialog 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();
}

The only thing that is different for each dialog is the phrase that is used for saving the window position. 每个对话框唯一不同的是用于保存窗口位置的短语。

I want to have a based CDialogEx class that I can inherit from that will perform the above actions. 我想要一个可以从其继承的基于CDialogEx类来执行上述操作。 I have looked on SO and seem some questions and creating a CDialog class and inheriting from another CDialog class. 我已经看过SO了,似乎有些问题,创建了一个CDialog类并从另一个CDialog类继承。 But this class I want to create is more generic. 但是我要创建的此类更通用。 Effectively to be used as a base instead of CDialogEx . 有效地代替CDialogEx用作基础。

Can this be done? 能做到吗? Am I over-complicating this? 我是否使这个复杂化了?

Problems 问题

Why I try to create a new class, derived from CDialogEx : 为什么我尝试创建一个从CDialogEx派生的新类:

设定

结果

错误

I don't know if it is because it requires a dialog ID as stated here . 我不知道是否是因为它需要一个此处所述的对话框ID

Classes such as CDialog , CFormView , or CPropertyPage , which require a dialog ID . 诸如CDialogCFormViewCPropertyPage类的类需要对话框ID

So I can't work out the correct way to create a base CDialogEx class for use in all my other dialog classes. 因此,我无法找出正确的方法来创建用于所有其他对话框类的基本CDialogEx类。

Update 更新

I created this code and it tells me that CResizingDialog is not a class or a namespace: 我创建了以下代码,它告诉我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();
}

Based on the comments encouraging me to try to create the class manually, I have it working: 基于鼓励我尝试手动创建类的注释,我可以正常工作:

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

I decided to duplicate the methods that were in the app class into this new dialog class instead. 我决定将应用程序类中的方法复制到此新对话框类中。 Eventually they can be removed from the app class. 最终,它们可以从应用程序类中删除。 The only thing I also had to do was #include my resource file because the image needs to know the value of the resource ID. 我唯一要做的就是#include我的资源文件,因为图像需要知道资源ID的值。

This is the ResizingDialog.h header: 这是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);
};

The actual functions SaveWindowPosition , RestoreWindowPosition and InitialiseResizeIcon are not shown here as they don't directly relate to the issue. 实际功能SaveWindowPositionRestoreWindowPositionInitialiseResizeIcon不在此处显示,因为它们与问题没有直接关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM