开发者

How do I create and use a CFormView in an MFC regular DLL? (visual studio 2008)

开发者 https://www.devze.com 2022-12-14 21:09 出处:网络
I recently asked this question which got me started in the right direction - at least for loading the MFC DLL and trying to show a dlg box.

I recently asked this question which got me started in the right direction - at least for loading the MFC DLL and trying to show a dlg box.

The problem is, the typical dialog box is horrible as a main window for an APP. It is quite simple for me to create a new exe project to do what I want, but the problem is that I have a DLL and the tools just don't seem to allow me to hook up the classes to the windows forms in the resource editor. Thus I can't seem to handle the events that I need.

My questions:

  • How do I create and display a CFormView (based on an IDD_FORMVIEW I created in a resource editor) in an MFC DLL project?
  • How do I get the form to show and to process input?
  • How do I add event/message handlers for that window? (The menu item to do that from the .rc editor is greyed out)
  • How do I set a menu to the formview? (the properties for the resource in the editor do not let me associate it with a menu resource. (I can't figure out why)

The links I have been looking at are pretty light and ambiguous about how to do it. Most of them assume I can create a mainframe as an MFC single document app via the "wizard" - which is not the case.

Right now I call Create() on the window class I made and pass in the CWnd of the desktop as the parent.

I am not sure I have subclassed the CFormView Correctly. In fact, I am pretty sure I have done little of what I need to do, though I tried to follow the instructions I have seen.

I then call ShowWindow(SW_SHOW), but I still see nothing.

I think this SHOULD be simple. All I want to do is show the form I created in the form editor.

How do I do that and what is the simplest way?

Here is some code - the cpp code that calls it

MainForm *mf = new MainForm();

mf->Create(CWnd::GetDesktopWindow());

mf->ShowWindow(SW_SHOW);

Here is the .h file for the MainForm cl开发者_JAVA百科ass

#include "afxcmn.h"



// MainForm form view

class MainForm : public CFormView
{
    DECLARE_DYNCREATE(MainForm)

public:
    MainForm();
    virtual ~MainForm();


public: 
    virtual BOOL Create(CWnd* pParent);

public:
    enum { IDD = IDD_FORMVIEW_MAIN };
#ifdef _DEBUG
    virtual void AssertValid() const;
#ifndef _WIN32_WCE
    virtual void Dump(CDumpContext& dc) const;
#endif
#endif

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

    DECLARE_MESSAGE_MAP()
public:
    CListCtrl m_SymbolSetList;
};

and here is the cpp for MainForm

#include "stdafx.h"
#include "MainForm.h"


// MainForm

IMPLEMENT_DYNCREATE(MainForm, CFormView)

MainForm::MainForm()
    : CFormView(MainForm::IDD)
{

}

MainForm::~MainForm()
{
}

void MainForm::DoDataExchange(CDataExchange* pDX)
{
    CFormView::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_LIST_SYMBOLSETS, m_SymbolSetList);
}

BEGIN_MESSAGE_MAP(MainForm, CFormView)
END_MESSAGE_MAP()


// MainForm diagnostics

#ifdef _DEBUG
void MainForm::AssertValid() const
{
    CFormView::AssertValid();
}

#ifndef _WIN32_WCE
void MainForm::Dump(CDumpContext& dc) const
{
    CFormView::Dump(dc);
}
#endif
#endif //_DEBUG

BOOL MainForm::Create(CWnd* pParent)
{
    CRect rect; 
    //pParent->GetClientRect(rect);
    return CFormView::Create(NULL, NULL, WS_CHILD | WS_VISIBLE, rect, pParent, 0, NULL);
}


I would investigate creating and showing your window in a separate MFC UI thread. MFC has got its own mechanism for delivering Windows messages to the CWnd dervived objects called message pump and it needs to initialize its internal structures for it to work. I think you need to use framework function call to do it. Try this version of AfxBeginThread:

CWinThread* AfxBeginThread(

CRuntimeClass* pThreadClass,
int nPriority=HREAD_PRIORITY_NORMAL,
UINT nStackSize = 0,
DWORD dwCreateFlags = 0,
LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL 

);

and pass your form as a pThreadClass; there is RUNTIME_CLASS macro that will do it for you. You then end the thread from withing the frame code.

As for the message handling, try opening properties for your form in the resource editor and click the Messages button on the top. You can than add your handlers to the messages that you need to handle.

I was able to associate the Menu property with a resource ID of a menu. I am not sure why you are not able to do so. The resource editor might get confused sometimes if you select a different resource in the tree view but your main window displays a different resource.

Regards

0

精彩评论

暂无评论...
验证码 换一张
取 消