CMFCButton导致PropertySheet窗口关闭

google keyword: CMFCButton breaks MFC applications with CPropertySheet

背景:

在CPropertySheet中使用了CMFCButton或者其他MFCxx系列的控件

现象:

窗口一闪而过,立刻关闭

问题:

目前认定这是 vs2010 sp1 的一个bug

  • 对于其他MFCx系列的控件,存在同样的问题
  • 无论模态窗口还是非模态窗口都存在该问题。

解决:

  • 使用 vs2012 进行构建
  • 不要在PropertSheet中使用MFCButton,改用CButton

http://social.msdn.microsoft.com/Forums/vstudio/en-US/9b45fd05-bfe2-4215-a1fb-8c16b7c5b33d/cmfcbutton-breaks-mfc-applications-with-cpropertysheet?forum=vclanguage

Q

I am pretty new to MFC, but I am having a problem which I can reproduce on all samples I've tried so far. For instance, a perfect sample is the one located in C:\Program Files (x86)\Microsoft Visual Studio 10.0\Samples\1033\VC2010Samples.zip/C++/MFC/controls/cmnctrl

If I just start that up and try to add a CMFCButton control to any of the sheets in the tab by simply dragging one from the toolbox and dropping it on any surface of for instance the IDD_COMMAND_LINK_CONTROL dialog resource window, and then try to run the application, it immediately shuts down. It compiles just fine.

When debugging I can see that the problem lies in DoModal() of the CPropertySheet called allcontrolssheet:

BOOL CCmnCtrl3App::InitInstance()
{
	AfxEnableControlContainer();

	// Standard initialization

	CAllControlsSheet   allcontrolssheet(_T("Common Controls Sample"));
	m_pMainWnd = &allcontrolssheet;
	allcontrolssheet.DoModal();
	return FALSE;
}

More specifically, if I step into the DoModal() code, ending up in the dlgprop.cpp file, I can see that the code execution never enters the following if-clause on line 948, since the ContinueModal() function returns 0 or false:

if (ContinueModal())
{
	// enter modal loop
	DWORD dwFlags = MLF_SHOWONIDLE;
	if (GetStyle() & DS_NOIDLEMSG)
		dwFlags |= MLF_NOIDLEMSG;
	nResult = RunModalLoop(dwFlags);
}

However, as soon as the CMFCButton is removed, the application runs perfectly again. Also, it seems like if I do not use tabs (CPropertySheet), but instead just put a CMFCButton directly on a dialog window, everythiong works fine...

Can anyone try this and see if you also get this error, or if you have any ideas or know something from experience that I do not know yet? Would be very thankful for help.

(Using VS2010)

UPDATE:

However, I noticed that if I create a CButton instead in the designer and then create a control variable for it and finally programmatically change the type from CButton to CMFCButton the program does not close immediately(or if having multiple tabs, the tab with the button does not close in this case)

A

If I just start that up and try to add a CMFCButton control to any of the sheets in the tab by simply dragging one from the toolbox and dropping it on any surface of for instance the IDD_COMMAND_LINK_CONTROL dialog resource window, and then try to run the application, it immediately shuts down. It compiles just fine.

I don't see the same problem you mention.

When built with VS2010 SP1 the property sheet shows up, but as soon as I try to show the page that I've added the CMFCButton to (the IDD_COMMAND_LINK_CONTROL dialog as per your description), that page disappears.

Rebuilding the same sources with VS2012 and all is OK, so it's likely to be something MS are aware of and have subsequently fixed.

Confirm

Thanks for trying it out and helping! Yep, I am using VS2010 SP1 (haven't tried 2012). It seems that you get the same problem in VS2010. Good, then at least it is "confirmed".

Thanks for your time and effort!

posted @ 2014-08-07 10:43  kevinzhwl  阅读(578)  评论(0编辑  收藏  举报