Dave's Brain

Browse - programming tips - mfc dialog set default button

Date: 2008jan21
Platform: MFC
Language: C/C++

Q.  How do I set the default button in a dialog when using MFC?

A.  Usually its the "OK" button.

This function does the trick:

int CMyDialogClass::SetDefaultButton(const DWORD nNewId) 
{
	DWORD nPrevId;

	nPrevId  = GetDefID();
	// Its not automatic - we have to remove the border from the old button
	SendDlgItemMessage(nPrevId, BM_SETSTYLE, BS_PUSHBUTTON, (LPARAM)TRUE);

	SetDefID(nNewId);
	// Likewise, add a border to the new one
	SendDlgItemMessage(nNewId, BM_SETSTYLE, BS_DEFPUSHBUTTON, (LPARAM)TRUE);

	return nPrevId;
}
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.
Advertisements: