Re: using a modeless WinForms "application" from MFC
// CMFCWinFormHybridApp initialization
extern void ManagedInit();
bool MFCMessageFilter(HWND hwnd,UINT message,LONG x,LONG y,DWORD time,
WPARAM wparam, LPARAM lparam)
_AFX_THREAD_STATE *pState = AfxGetThreadState();
ref class CMFCMessageFilter:public System::Windows::Forms::IMessageFilter
virtual bool PreFilterMessage(System::Windows::Forms::Message % m)
if (m_pMainWnd == NULL && AfxOleGetUserCtrl())
// Not launched /Embedding or /Automation, but has no main window!
TRACE(traceAppMsg, 0, "Warning: m_pMainWnd is NULL in CWinApp::Run -
Microsoft MVP in VC++
"Sheng Jiang[MVP]" <email@example.com> wrote in message
I remember a while ago I have a demo app in which I have my CHTMLView
app work on a windows form message pump... basically override CWinApp::Run
and call Application::Start instead of calling CWinThread::Run . To make
PretranslateMessage work I added my own IMessageFilter implementation to
application and call MFC's CWinApp::PreTranslateMessage in it. Haven't
tested it extensively, though.
Microsoft MVP in VC++
"Dan Smith" <Dan Smith@nospam.nospam> wrote in message
I'm in the process of adding a bunch of new .NET functionality to a
behaved) legacy MFC application. One of the things I'm adding is a
code editor" (ala VBA): the window should minimize/restore separately
the main application, have its own menu/toolbar, etc.
The first approach was straight-forward enough: create the "application"
a Form and use it as a modeless dialog. However, things didn't work
with menu keyboard accelerators and the like (Ctrl-C, Alt-X, F1),
a result of the main MFC application grabbing them.
So, the approach that I've been using for the past several weeks is to
create my own Application in a separate thread. See
This has been working reasonably well, also having to Invoke() back to
main thread at times is a pain.
However, I'm now seeing some crashes with very strange call stacks such
System.OutOfMemoryException: Out of memory.
at System.Drawing.Graphics.FromHdcInternal(IntPtr hdc)
at System.Windows.Forms.Control.PaintException(PaintEventArgs e)
e, Int16 layer, Boolean disposeEventArgs)
at System.Windows.Forms.Control.WmEraseBkgnd(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.UserControl.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32
IntPtr wparam, IntPtr lparam)
this seems to occur when the user tries to move my "application" window
immediately after I've Invoke()d to the main thread. Also, I'd like a
little bit better window management integration with the legacy MFC
application; for example, I don't want one of its modal (error) dialogs
get hidden behind this editor application window.
With that extensive background; the most recent thing I've tried is to
this top-level Form from MFC via the CWinFormsDialog<> class. After
TopLevel=false on my Form, the form is displayed--although the results
quirky as I see two sets of non-client area items (close, minimize,
maximize, etc.). But the bigger problem is that I can't get the
accelerators working. I've overridden PreTranslateMessage() and tried
calling Control.PreProcessMessage() (and the like), but it doesn't seem
At this point, I'm trying to figure out where to go next: sticking with
separate .NET Application in it's own thread is fine, but I've got no
what could be causing the crash above. And even then, there are still
glitches with getting windows "owned" correctly (and the legacy MFC
application has it's own ideas too).
Switching to MFC for at least some of the "application" shell (although
absolutely little as possible in MFC!) might help, but not if I can't
the keyboard accelerators. Is what I'm trying to do in MFC possible?
supported? If not, what are the alternatives? My .NET UI has docking
windows and MDI; I *really* don't want to have to do all that "shell" in
MFC--even if the content are WinForms controls.
Thanks for any tips/advice/pointers/etc.