Click here to Skip to main content
15,509,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
MFC C++ VC 2010.

I have a dialog problem that is driving me nuts. I have a dialog that is sort of a version of AfxMessageBox with some added features. The default button needs to be settable and I can't seem to get that to work.

My .rc file looks like this

IDD_DLG_GENMSG DIALOGEX 0, 0, 240, 95
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
    LTEXT           "Static",IDC_GM_TXTMSG,41,7,192,62
    ICON            "",IDC_GM_ICOMSG,18,10,20,20,0,WS_EX_TRANSPARENT
    PUSHBUTTON      "OK",IDC_GM_BTNLEFT,68,74,50,14
    PUSHBUTTON      "Cancel",IDC_GM_BTNRIGHT,122,74,50,14
    PUSHBUTTON      "Abort",IDC_GM_BTNABORT,41,74,50,14
    PUSHBUTTON      "Retry",IDC_GM_BTNRETRY,95,74,50,14
    PUSHBUTTON      "Ignore",IDC_GM_BTNIGNORE,149,74,50,14
END


At start I enable of disable buttons as needed and the text on the button is changed programmatically as needed. No matter what I do, the first button listed in the resource file is always the default.

I tried a couple of methods described in these links
https://forums.codeguru.com/showthread.php?197179-Switching-Default-Buttons-MFC

https://www.betaarchive.com/wiki/index.php/Microsoft_KB_Archive/67655

As I said, not this always had the first listed button as the default when I hit Return. As a work around I implemented a PreTranslateMessage override, but it never received the WM_KEYDOWN message and WK_RETURN key.


C++
BOOL CDlgGenMsg::PreTranslateMessage(MSG* pMsg) 
{
	ASSERT(pMsg != NULL);
	ASSERT_VALID(this);
	ASSERT(m_hWnd != NULL);
 
	// Intercept Return key
	if((pMsg->message == WM_KEYDOWN))
	{
		if(pMsg->wParam == VK_RETURN)
		{
			// Process return
		}
	}
 
	return(CDialog::PreTranslateMessage(pMsg));
}


What I have tried:

Detailed above in the body.

I'm probably missing something simple, but it's got me stumped.
Posted
Updated 28-Jun-22 16:04pm
Comments
CPallini 23-Jun-22 5:40am    
I don't know if this
https://jeffpar.github.io/kbarchive/kb/126/Q126874/
it is relevant to your problem. It is a while since I used MFC.

I am sure you would need to modify CButton style by appending
BS_DEFPUSHBUTTON

Check ModifyStyle() method. It is in CWnd class
 
Share this answer
 
Comments
wdolson 23-Jun-22 22:04pm    
That was essentially what was recommended in both of the solutions above. I tried ModifyStyle and got the same result. It always sets the first button declared in the resource file.
I finally solved it. Weird problem.

I use the interface library Prof-UIS. I've been using it for 15 years and never run into anything like this. Their derived classes for controls have always used the standard MFC style variables, but I dug into the source code and found that they did something different for their version of CButton.

Had to modify the library a bit, but got it working.

Thanks for the help and sorry I left out it was Prof-UIS, I didn't imagine that it was relevant.
 
Share this answer
 
I need to change the default button programatically. I tried the techniques in the two links given and neither worked.
 
Share this answer
 
Comments
Richard Deeming 23-Jun-22 4:39am    
If you want to reply to a solution, click the "Have a Question or Comment?" button under that solution and post a comment.

Do not post your comment as another "solution" to your question.
Richard MacCutchan 23-Jun-22 4:45am    
Disregard comment.
wdolson 23-Jun-22 22:03pm    
I was responding to the first solution offered, which has been deleted. I guess I used the wrong way to respond. Sorry.
Richard MacCutchan 24-Jun-22 3:05am    
I deleted it because I misread the question, and essentially duplicated what you had already tried. My apologies.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900