Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I created a dialog with ok and cancel buttons, when I open this dialog, button OK is selected..is there a way to make selected CANCEL button?

my code:

<pre>
#include "stdafx.h"
#include "Scarico.h"
#include "Stamp.h"
#include "afxdialogex.h"


// CStamp dialog

IMPLEMENT_DYNAMIC(CStamp, CDialogEx)

CStamp::CStamp(CWnd* pParent /*=NULL*/)
	: CDialogEx(IDD_DIALOG1, pParent)
{
	m_bStamp = true;
}

CStamp::~CStamp()
{
}

void CStamp::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDOK, m_okbutton);
	DDX_Control(pDX, IDCANCEL, m_cancelbutton);
}


BEGIN_MESSAGE_MAP(CStamp, CDialogEx)
	ON_BN_CLICKED(IDOK, &CStamp::OnBnClickedOk)
	ON_BN_CLICKED(IDCANCEL, &CStamp::OnBnClickedCancel)
	ON_WM_CLOSE()
END_MESSAGE_MAP()


// CStamp message handlers


BOOL CStamp::OnInitDialog()
{
	CDialog::OnInitDialog();
	
	CFont                           *pOldFont;
	CWnd                            *pWnd;
	LOGFONT							sLogFont;
	BOOL							bCreated;
	CFont                           font;

	pOldFont = GetFont();												// Informazioni sul font attuale
	pOldFont->GetLogFont(&sLogFont);
	 
	sLogFont.lfHeight = 28;												// Nuove caratteristiche del font
	sLogFont.lfWeight = FW_BOLD;
	sLogFont.lfOutPrecision = OUT_TT_ONLY_PRECIS;
	sLogFont.lfQuality = ANTIALIASED_QUALITY;
	bCreated = m_fntSize24_Bold.CreateFontIndirect(&sLogFont);			// Creazione del nuovo font e suo settaggio

	pWnd = (CWnd *)GetDlgItem(IDC_STATICST);
	pWnd->SetFont(&m_fntSize24_Bold);

	return TRUE;
}


void CStamp::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here
	m_bStamp = true;
	CDialogEx::OnOK();
}


void CStamp::OnBnClickedCancel()
{
	// TODO: Add your control notification handler code here
	m_bStamp = false;
	CDialogEx::OnCancel();
}


void CStamp::OnClose()
{
	m_bStamp = false;
}









What I have tried:

I tried to search on google but I can find nothing..thanks...now I set cancel button like default button, and I set false ok button like default button but when software is launched ok button is still defaul button..I don't know why
Posted
Updated 8-Jun-23 7:56am
v3
Comments
Richard MacCutchan 8-Jun-23 5:59am    
When I suggested you add more information, I meant specifc details of your code and what happens. Just saying that it does not work does not help us to understand the problem.
Richard MacCutchan 8-Jun-23 7:09am    
Please use an editor to look at your resource file and show us the actual dialog definition.

Yes, you can set the default button in the dialog editor. Select the button and then choose properties, and set Default in the Behaviour group as true. Remember to set the OK button's Default setting to false. You may also need to adjust the tab order.

[edit]
It seems that the order of the buttons in the dialog is also necessary for this to work. in the following Dialog, pressing the Enter key invokes the Cancel button:
C++
IDD_DIALOG1 DIALOGEX 0, 0, 310, 177
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
    DEFPUSHBUTTON   "Cancel",IDCANCEL,253,156,50,14
    PUSHBUTTON      "OK", IDOK, 199, 156, 50, 14
    END

However, in the following layout when the order is reversed it still invokes the OK button, even though it is not the default:
C++
IDD_DIALOG1 DIALOGEX 0, 0, 310, 177
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
    PUSHBUTTON      "OK", IDOK, 199, 156, 50, 14
    DEFPUSHBUTTON   "Cancel",IDCANCEL,253,156,50,14
    END

[/edit]
 
Share this answer
 
v2
Comments
Member 14594285 8-Jun-23 5:07am    
I did it but situation doesn't change..must I write something in the code?
Richard MacCutchan 8-Jun-23 5:21am    
I just tried it and it works fine; you need to provide more information. Please use the Improve question link above, and add complete details.
Member 14594285 8-Jun-23 8:47am    
ok..so where must I change?
Richard MacCutchan 8-Jun-23 9:01am    
You can do it in the dialog editor by removing both buttons and then adding the Cancel first (and setting it as default) and the OK second.
Member 14594285 8-Jun-23 9:17am    
yes but in the dialog directly or must I open a window with metalanguage?
Richard is correct - you can set the default button with the resource editor. You can also do it programmatically. MFC's CButton class has the method SetButtonStyle and you can call this with the argument BS_DEFPUSHBUTTON and that button object will be the new default.

There is a caveat: the previous default button must have the default property removed first and that is done by calling the same method but with the argument BS_PUSHBUTTON or whatever style is appropriate for it.
 
Share this answer
 

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