MDI面向对象的方法,从MDICREATESTRUCT

时间:2019-04-06 09:19:09

标签: c++ winapi mdi

我正在尝试实现MDI Child窗口基类,并按照to this reference的详细说明说

  

当MDI客户端窗口通过调用创建MDI子窗口时   CreateWindow,系统发送WM_CREATE消息给创建的   窗口。 WM_CREATE消息的lParam成员包含一个指针   到CREATESTRUCT结构。的lpCreateParams成员   结构包含指向传递的MDICREATESTRUCT结构的指针   带有创建MDI子窗口的WM_MDICREATE消息。

我正在使用以下简单的基本模板类来创建MDI子窗口,实现上述语句以检索该指针。 (添加了一些评论)

basemdi.h

#pragma once
#include <Windows.h>

template <typename DERIVED_TYPE>
class BaseMDI
{
public:
    inline HWND GetHandle() const;
    BOOL Initialize(
        PCTSTR szWindowName,
        HWND hParent,
        DWORD dwExStyle = WS_EX_MDICHILD, // THIS IS MDI WINDOW
        DWORD dwStyle = 0,
        int x = CW_USEDEFAULT,
        int y = CW_USEDEFAULT,
        int width = CW_USEDEFAULT,
        int height = CW_USEDEFAULT,
        HMENU hMenu = nullptr,
        HINSTANCE hInstance = GetModuleHandle(nullptr),
        LPVOID lpCreate = nullptr
    );

protected:
    virtual PCTSTR ClassName() const = 0;
    static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) = 0;

    HWND m_hwnd = nullptr;
};

template<typename DERIVED_TYPE>
BOOL BaseMDI<DERIVED_TYPE>::Initialize(
    PCTSTR szWindowName,
    HWND hParent,
    DWORD dwExStyle,
    DWORD dwStyle,
    int x,
    int y,
    int width,
    int height,
    HMENU hMenu,
    HINSTANCE hInstance,
    LPVOID lpCreate)
{
    UNREFERENCED_PARAMETER(lpCreate);

    WNDCLASSEX wc = { };

    wc.cbClsExtra = 0;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.cbWndExtra = 0;
    wc.hbrBackground = reinterpret_cast<HBRUSH>(GetStockObject(WHITE_BRUSH));
    wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
    wc.hIcon = nullptr;
    wc.hInstance = hInstance;
    wc.lpfnWndProc = WindowProc;
    wc.lpszClassName = ClassName();
    wc.lpszMenuName = nullptr;
    wc.style = CS_VREDRAW | CS_HREDRAW;

    RegisterClassEx(&wc);

    MDICREATESTRUCT mdicreate;

    // ASSIGN POINTER TO THIS SO THAT WE LATER RETRIEVE IT
    mdicreate.lParam = (LPARAM) this;

    mdicreate.szClass = ClassName();
    mdicreate.szTitle = TEXT("Hello");
    mdicreate.hOwner = hInstance;
    mdicreate.x = CW_USEDEFAULT;
    mdicreate.y = CW_USEDEFAULT;
    mdicreate.cx = CW_USEDEFAULT;
    mdicreate.cy = CW_USEDEFAULT;
    mdicreate.style = dwStyle;

    m_hwnd = CreateWindowEx(
        dwExStyle,
        ClassName(),
        szWindowName,
        dwStyle,
        x, y,
        width,
        height,
        hParent,
        hMenu,
        hInstance,
        &mdicreate // PASS ADDRESS OF MDICREATESTRUCT
    );

    return m_hwnd ? TRUE : FALSE;
}
// following base class WndProc calls derived class procedure, 
// I retrive this pointer here to call correct procedure, but pThis is read acess vioalaiton
template<typename DERIVED_TYPE>
inline LRESULT BaseMDI<DERIVED_TYPE>::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    DERIVED_TYPE* pThis = nullptr;

    if (uMsg == WM_CREATE)
    {
        // RETRIEVE POINTER TO THIS
        CREATESTRUCT* pCreate = reinterpret_cast<CREATESTRUCT*>(lParam);
        MDICREATESTRUCT* pMdi = reinterpret_cast<MDICREATESTRUCT*>(pCreate->lpCreateParams);
        pThis = reinterpret_cast<DERIVED_TYPE*>(pMdi->lParam);
        SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pThis));

        pThis->m_hwnd = hWnd;
    }
    else
    {
        pThis = reinterpret_cast<DERIVED_TYPE*>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
    }

    if (pThis)
    {
        // EXCEPTION IS THROWN HERE
        return pThis->HandleMessage(uMsg, wParam, lParam);
    }
    else
    {
        return DefMDIChildProc(hWnd, uMsg, wParam, lParam);
    }
}

template <typename DERIVED_TYPE>
HWND BaseMDI<DERIVED_TYPE>::GetHandle() const
{
    return m_hwnd;
}

这是我创建MDI子窗口对象的实际实例的方法,该类继承了模板化的基类

mdiwindow.h

#pragma once
#include "mdibase.h"

class MDI : public BaseMDI<MDI>
{
public:
    virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) override;

private:
    inline virtual PCTSTR ClassName() const override;
};

PCTSTR MDI::ClassName() const
{
    return TEXT("MDIWindow");
}

mdiwindow.cpp

 #include "mdiwindow.h"


LRESULT MDI::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    default:
        return DefMDIChildProc(m_hwnd, uMsg, wParam, lParam);
    }
}

在创建MDI子级时,我在basemdi.h中遇到了例外,它对pThis指针说读取访问冲突

我正在遵循有关如何检索传递给MDICREATESTRUCT的指向CreateWindowEx的指针的msdn指令,该指针在this中保留了指向lpCreateParams的指针,但是由于某种原因,检索到的指针不起作用。

您知道这可能是什么原因吗?

2 个答案:

答案 0 :(得分:0)

我设法解决了这个问题。

问题在于,MSDN在谈论使用CreateWindowCreateWindowEx的可能性,这不适用于子MDI窗口,而需要使用CreateMDIWindow

这是上面示例中正在工作的Initialize函数,上面的其余代码很好:

template<typename DERIVED_TYPE>
BOOL BaseMDI<DERIVED_TYPE>::Initialize(
    PCTSTR szWindowName,
    HWND hParent,
    DWORD dwExStyle,
    DWORD dwStyle,
    int x,
    int y,
    int width,
    int height,
    HMENU hMenu,
    HINSTANCE hInstance,
    LPVOID lpCreate)
{
    UNREFERENCED_PARAMETER(lpCreate);

    WNDCLASSEX wc = { };

    wc.cbClsExtra = 0;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.cbWndExtra = 0;
    wc.hbrBackground = reinterpret_cast<HBRUSH>(GetStockObject(WHITE_BRUSH));
    wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
    wc.hIcon = nullptr;
    wc.hInstance = hInstance;
    wc.lpfnWndProc = WindowProc;
    wc.lpszClassName = ClassName();
    wc.lpszMenuName = nullptr;
    wc.style = CS_VREDRAW | CS_HREDRAW;

    if (!RegisterClassEx(&wc)) abort();

    m_hwnd = CreateMDIWindow(
        ClassName(),
        szWindowName,
        dwStyle,
        x, y,
        width,
        height,
        hParent,
        hInstance,
        (LPARAM)this);

    if (!m_hwnd) abort();

    return m_hwnd ? TRUE : FALSE;
}

答案 1 :(得分:0)

我最近将MDI与OOP方法结合使用很有趣,这对我有用:

    MDICREATESTRUCT mcs;
    mcs.szTitle = WindowText();
    mcs.szClass = ClassName();
    mcs.hOwner = GetModuleHandle( nullptr );
    mcs.x = x;
    mcs.y = y;
    mcs.cx = width;
    mcs.cy = height;
    mcs.style = dwStyle;
    mcs.lParam = reinterpret_cast< LPARAM >( this );

    mSubWnd = reinterpret_cast< HWND >( SendMessage( hParent, WM_MDICREATE, 0, reinterpret_cast< LONG >( &mcs ) ) );