简体   繁体   English

如何在C ++中从控制台应用程序显示MFC对话框?

[英]How to display a MFC dialog from a console application in C++?

I have a simple MFC dialog. 我有一个简单的MFC对话框。

class CMessageBoxWithCustomTextDlg : public CDialogEx
{
// Construction
public:
    CMessageBoxWithCustomTextDlg(CWnd* pParent = NULL); // standard constructor

    __declspec(dllexport) void SetData(std::string& data);

// Dialog Data
    enum { IDD = IDD_MESSAGEBOXWITHCUSTOMTEXT_DIALOG };

    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support


// Implementation
protected:
    HICON m_hIcon;

    // Generated message map functions
    virtual BOOL OnInitDialog();
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();
    DECLARE_MESSAGE_MAP()
public:
  afx_msg void OnBnClickedShowMessagebox();
};

I would like to export it as dll and call it from a simple console application. 我想将其导出为dll并从一个简单的控制台应用程序中调用它。 Is it possible? 可能吗?

It is possible; 有可能的; here is how I did it: For your console application have it be simply this: 这是我的操作方式:对于您的控制台应用程序,它就是这样:

#include <Windows.h>

typedef void (*EntryFunc)();
int main()
{
   HMODULE hMod = LoadLibrary(L"MFCDll.dll");

   EntryFunc func = (EntryFunc)GetProcAddress(hMod, "entrypoint");
   func();
}

The name of the DLL is MFCDll.dll and there is an exported function called entrypoint in that DLL. 该DLL的名称为MFCDll.dll,并且该DLL中有一个称为入口点的导出函数。

For the DLL I created a New MFC DLL project. 对于DLL,我创建了一个新的MFC DLL项目。 And other than the dialog code and the dialog in the resources add this code: 除了对话框代码和资源中的对话框之外,还添加以下代码:

extern "C" __declspec(dllexport) void entrypoint()
{
   AFX_MANAGE_STATE(AfxGetStaticModuleState());
   CMessageBoxWithCustomTextDlg dlg;
   dlg.DoModal();
}

And the console program will load the DLL, call into the DLL and the dialog shows. 控制台程序将加载DLL,调用DLL,并显示对话框。

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

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