C ++ / CLI:将MFC嵌入到WinForm中

时间:2017-09-01 15:03:34

标签: winforms mfc focus c++-cli

您好,

几个星期以来,我们正在尝试将MFC对话框“转换”为“MFC格式”,可以嵌入到WinForm用户控件中。

我们已经成功地做到了:

  • 我们制作了一个WinForm用户控件,名为 Dlg_WU_MFC_Container
  • 创建时,UC会创建名为 CDlgEdgeType
  • 的MFC表单
  • 然后,每次调整UC大小或移动时,我们也会移动并调整MFC格式

这是代码(我试图删除许多不必要的东西..):

Dlg_WU_MFC_Container.h:

#pragma once

public ref class Dlg_WU_MFC_Container : public System::Windows::Forms::UserControl
{
private:
    CDlgEdgeType* _dialog;
    CWnd *_wnd;

    private: //---Local Controls
    System::ComponentModel::IContainer^  components;

public: 
    Dlg_WU_MFC_Container();
    ~Dlg_WU_MFC_Container();
    !Dlg_WU_MFC_Container();

    template<class T, class HP>
    void InitializeContainer() {
        CDlgEdgeType = 
    }

private:
    void RefreshEmbeddedSize();

#pragma region Windows Form Designer generated code 
         /// <summary>
         /// Required method for Designer support - do not modify
         /// the contents of this method with the code editor.
         /// </summary>
         void InitializeComponent(void)
         {
             this->SuspendLayout();
             // 
             // Dlg_WU_MFC_Container
             // 
             this->AutoScaleDimensions = System::Drawing::SizeF(96, 96);
             this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Dpi;
             this->ForeColor = System::Drawing::SystemColors::WindowText;
             this->Margin = System::Windows::Forms::Padding(0);
             this->Name = L"Dlg_WU_MFC_Container";
             this->Size = System::Drawing::Size(816, 480);
             this->SizeChanged += gcnew System::EventHandler(this, &Dlg_WU_MFC_Container::evSizeChanged);
             this->VisibleChanged += gcnew System::EventHandler(this, &Dlg_WU_MFC_Container::evVisibleChanged);
             this->ResumeLayout(false);

         }
#pragma endregion
private: System::Void evSizeChanged(System::Object^  sender, System::EventArgs^  e);
private: System::Void evVisibleChanged(System::Object^  sender, System::EventArgs^  e);
};

Dlg_WU_MFC_Container.cpp:

    #include "Dlg_WU_MFC_Container.h"
    #include "DlgEdgeType.h"

    Dlg_WU_MFC_Container::Dlg_WU_MFC_Container()
    {
        InitializeComponent();

        _wnd = NULL;
        _dialog = new CDlgEdgeType();
    }

    Dlg_WU_MFC_Container::~Dlg_WU_MFC_Container()
    {
        if (components)
        {
            delete components;
        }
        this->!Dlg_WU_MFC_Container();
    }

    Dlg_WU_MFC_Container::!Dlg_WU_MFC_Container()
    {
        // We need to detach the handle to free it for other usage
        if (_wnd) {
            _wnd->Detach();

            delete _wnd;

            _wnd = NULL;
        }

        if (_dialog) {
            delete _dialog;

            _dialog = NULL;
        }
    }

    System::Void Dlg_WU_MFC_Container::evSizeChanged(System::Object^  sender, System::EventArgs^  e) {  
        RefreshEmbeddedSize();
    }

    // Inform the embedded form to adapt to its new size
    void Dlg_WU_MFC_Container::RefreshEmbeddedSize() {
        if (_dialog && _isShown) {
            CRect containerWnd;

            containerWnd.left = this->Left;
            containerWnd.right = this->Right;
            containerWnd.top = this->Top;
            containerWnd.bottom = this->Bottom;

            _dialog->ReplaceEmbeddedForm(containerWnd);
        }
    }

    System::Void Dlg_WU_MFC_Container::evVisibleChanged(System::Object^  sender, System::EventArgs^  e) {
// _isShown basically useless.. !
        if (Visible != _isShown) {
            _isShown = Visible;

            if (_dialog) {
                if (Visible) {
                    void *handle = Handle.ToPointer();

                    if (handle) {
                        // We need to create a new CWnd which will contain
                        // the handle of the current WinForm control where
                        // the embedded MFC will be contained
                        _wnd = new CWnd();

                        _wnd->Attach((HWND)handle);

                        _dialog->Create(_wnd);

                        RefreshEmbeddedSize();
                    }
                } else {
                    // When the control is not shown anymore, we need to free
                    // the handle so another control can use it (the handle
                    // is stored in the MFC permanent map)
                    _wnd->Detach();

                    _dialog->DestroyWindow();

                    delete _wnd;

                    _wnd = NULL;
                }
            }
        }
    }

CDlgEdgeType.cpp:

void CDlgEdgeType::ReplaceEmbeddedForm(CRect &rect) {
    if (!GetSafeHwnd()) {
        return;
    }

    SetWindowPos(NULL, rect.left, rect.top, rect.Width(), rect.Height(), SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);
}

这个东西工作“很棒”: CDlgEdgeType 在我们的应用程序中得到很好的显示,当应用程序调整大小或移动时,一切顺利。

这是我的问题: CDlgEdgeType Dlg_WU_MFC_Container 作为父级,很棒。但是后者并不知道MFC表单是一个“孩子”..所以焦点有点丢失,箭头键和Tab键根本不起作用。

您应该知道的是, Dlg_WU_MFC_Container 会添加到自定义 TabControl TabPages 中。因此,如果用户尝试浏览MFC表单的控件或他尝试使用箭头键浏览TreeView,TabControl似乎接管焦点并将更改选项卡..这不方便D:

我不知道如何解决这个问题,我的同事也不知道。

也许我们整合MFC的方式是错误的,但是没有真正的主题(我们看到更多“将WinForm表单嵌入到MFC中”)。此外,由于我们的软件历史悠久,我们不能简单地重新创建CDlgEdgeType。这是一个很大的对话框,事实上,我们有7个对话框,代码实现了模板,但是为了清晰起见,我删除了它们。

谢谢!

Sbizz。

1 个答案:

答案 0 :(得分:1)

好吧,我们找到了一条出路...这可能不是最好的方法,但它正在发挥作用(或者至少它似乎有效!)。

首先,我设法将密钥发送到MFC表单:

bool Dlg_WU_MFC_Container::ProcessDialogKey(Keys keyData) {
    ::SendMessage(CWnd::GetFocus()->GetSafeHwnd(), WM_KEYDOWN, (WPARAM)keyData, (LPARAM)0);

    return true;
}

由于TabControl通过WM_ACTIVATE获取控件,我们尝试通过将WM_ACTIVATE也发送到MFC表单来“覆盖”它,结果如下:

bool Dlg_WU_MFC_Container::ProcessDialogKey(Keys keyData) {
    ::SendMessage(CWnd::GetFocus()->GetSafeHwnd(), WM_ACTIVATE, MAKEWPARAM(WA_ACTIVE, 0), (LPARAM)0);
    ::SendMessage(CWnd::GetFocus()->GetSafeHwnd(), WM_KEYDOWN, (WPARAM)keyData, (LPARAM)0);

    return true;
}

唯一的问题是“Tab”键似乎不起作用,但经过调查后,我们的用户不需要它,所以......:D但我认为它只与WM_ACTIVATE的第三个参数有关(以前的控制)。必须使用它来知道在按下Tab后必须聚焦哪个控件。

Sbizz