FormCloseQuery equivalent in MFC - mfc

In Delphi, there is FormCloseQuery event. What is equivalent in MFC?
I want to stop CMainFrame from closing

1) Add a handler to the WM_CLOSE message in your CMainFrame Message Map:
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
...
ON_WM_CLOSE()
...
END_MESSAGE_MAP()
2) Add afx_msg void OnClose(); to class definition in the header file
3) Add CMainFrame::OnClose() implementation
void CMainFrame::OnClose()
{
if (okToClose)
{
CFrameWnd::OnClose();
}
else
{
// Do nothing
}
}

Related

onTimer() not trigger [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have an issue with OnTimer() function of Modeless Dialog window (who is a child window of CDialogEx window) ,it refuse to trigger.
When i make the dialog Modal with CDialog::DoModal() function its working fine.
But i want Modeless Dialog.
Could you explain me what happen here .
I try to be accurate. The ClogoDlg Dialog is a SPLASH window of the application .It showing for 3 minutes, in the middle the program running Lengthy Operation. The SPLASH window displays a duration of time. Therefor i want the OnTimer() function.
Thanks .
my code is:
BOOL Clotto2Dlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
ClogoDlg* pdlg=new ClogoDlg();
pdlg->Create(GetDesktopWindow());
//pdlg->DoModal();
pdlg->ShowWindow(SW_SHOW);
}
My ClogoDlg.cpp
#include "stdafx.h"
#include "ClogoDlg.h"
IMPLEMENT_DYNAMIC(ClogoDlg, CDialog)
ClogoDlg::ClogoDlg(CWnd* pParent ) : CDialog(ClogoDlg::IDD, pParent)
{
}
ClogoDlg::~ClogoDlg()
{
KillTimer(m_nTimer);
}
BOOL ClogoDlg::Create(CWnd* pParentWnd)
{
// TODO: Add your specialized code here and/or call the base class
return CDialog::Create(ClogoDlg::IDD,pParentWnd);
}
BEGIN_MESSAGE_MAP(ClogoDlg, CDialog)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONDBLCLK()
ON_WM_PAINT()
ON_WM_TIMER()
END_MESSAGE_MAP()
void ClogoDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
Invalidate();
CDialog::OnLButtonDown(nFlags, point);
}
void ClogoDlg::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDialog::OnLButtonDblClk(nFlags, point);
}
BOOL ClogoDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
m_nTimer = SetTimer(2, 1000, NULL);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
BOOL ClogoDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
return CDialog::PreTranslateMessage(pMsg);
}
void ClogoDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// Do not call CDialog::OnPaint() for painting messages
CTime timeNow;
timeNow = CTime::GetCurrentTime();
CString str;
str.Format(L"%3d:%2d:%2d", timeNow.GetHour(), timeNow.GetMinute(), timeNow.GetSecond());
dc.TextOut(10,10, str);
}
void ClogoDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: Add your message handler code here and/or call default
Invalidate();
CDialog::OnTimer(nIDEvent);
}
And my ClogoDlg.h
#pragma once
#include "resource.h"
class ClogoDlg:public CDialog
{
DECLARE_DYNAMIC(ClogoDlg)
public:
enum{IDD=IDD_LOGO};
ClogoDlg();
~ClogoDlg();
BOOL Create(CWnd * pParent = NULL);
CTime timer;
int m_nTimer;
DECLARE_MESSAGE_MAP()
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
virtual BOOL OnInitDialog();
virtual BOOL PreTranslateMessage(MSG* pMsg);
afx_msg void OnPaint();
afx_msg void OnTimer(UINT_PTR nIDEvent);
};

m_hWnd member variable is null

I'm trying to get the handle of a dialog box with this code:
class CStatisticsDlg : public CDialogEx
{
public:
CStatisticsDlg();
// Dialog Data
enum { IDD = IDD_STATISTICS };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
public:
};
CStatisticsDlg::CStatisticsDlg() : CDialogEx(CStatisticsDlg::IDD)
{
}
void CStatisticsDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
// DDX_Control(pDX, IDC_FUCK, m_fuck);
}
BEGIN_MESSAGE_MAP(CStatisticsDlg, CDialogEx)
END_MESSAGE_MAP()
But when I create an instance of the class with this:
CStatisticsDlg statisticsDlg;
and try to get its handle via statisticsDlg.m_hWnd, the handle is null.
Why this is the case? What is the best way to get the handle for a dialog box?
The first point during the creation process where you can get the m_hWnd is the dialog's OnInitDialog function. The m_hWnd does not exist before the DoModal call and does not exist after the DoModal returns.
try modalless dialog model .Use create function instead of domodal.m_hWnd does not exist before create call begins.

OnSelectionChanged not getting called

Header:
#pragma once
class AlarmsList : public CVSListBox
{
DECLARE_DYNAMIC(AlarmsList)
public:
AlarmsList();
virtual ~AlarmsList();
void OnAfterAddItem(int index);
void OnSelectionChanged(NMHDR *pNMHDR, LRESULT *pResult);
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnDtnDatetimechangeDatetimepicker1(NMHDR *pNMHDR, LRESULT *pResult);
};
void AlarmsList::OnAfterAddItem(int index)
{
GetParent()->GetDlgItem(IDC_TIMEPICK)->EnableWindow(true);
LOGIC->addAlarm();
LOGIC->changeSelection(index);
}
void AlarmsList::OnSelectionChanged(NMHDR *pNMHDR, LRESULT *pResult)
{
}
OnAfterAddItem gets called when i add a new item but OnSelectionChanged NEVER gets called how much i even try.
Linking it trough a message map neither dosnt work:
IMPLEMENT_DYNAMIC(AlarmsList, CVSListBox)
BEGIN_MESSAGE_MAP(AlarmsList, CVSListBox)
ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST, OnSelectionChanged)
END_MESSAGE_MAP()
I create the AlarmsList object using the create function.
Source code and project: http://www.filedropper.com/clockmaster
Generally, I think the LVN_ITEMCHANGED notification is sent to the parent window. Put the handler and the message map entry int your dialog/window that is the parent of the list box.
Didnt help :/.
Tried both parent property page and that property pages dialog.
Overloading dosnt work either :/, it does for OnAfterAddItem tough.
And yes I'm then using the same parameters as the virtual function.
You can try overriding the functions in the CVSListBoxBase class.In this class, the signature of OnSelectionChanged function requires no arguments.
You can find the declaration of the CVSListBoxBase class in afxvslistbox.h.
Just had a look at some of my own MFC code that uses list boxes, and the following works;
CMyListBox : public CListBox
{
}
class CMyDialog : public CDialog
{
// Construction
public:
CMyDialog(CFeatureDoc* pFeatureDoc,BOOL SheetLayout = FALSE,CWnd* pParent = NULL); // standard constructor
//{{AFX_DATA(CMyDialog)
enum { IDD = IDD_MY_DIALOG };
CMyListBox m_MyListBox;
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMyDialog)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(CMyDialog)
afx_msg void OnSelChangeListBox();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
void CMyDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyDialog)
DDX_Control(pDX, IDC_MY_LIST_BOX, m_MyListBox);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyDialog, CDialog)
//{{AFX_MSG_MAP(CMyDialog)
ON_LBN_SELCHANGE(IDC_MY_LIST_BOX, OnSelChangeListBox)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyDialog message handlers
void CMyDialog::OnSelChangeListBox()
{
}
If you want to have your own control process messages from a dialog, you may want to subclass it. See this related question What's the correct way to create a subclass of a MFC control?

Can't get OnContextMenu to work for custom CListCtl class

I am trying to get a context menu to work for a CListCtrl derived class. I just created a method OnContextMenu, but it isn't being called. What am I missing? I am using Visual Studio 2008, to create a CDialog based MFC application.
CustomList.h
class tcCustomListCtl : public CListCtl
{
DECLARE_DYNAMIC(tcCustomListCtl)
public:
tcCustomListCtl();
virtual ~tcCustomListCtl();
protected:
DECLARE_MESSAGE_MAP()
afx_msg void OnContextMenu(CWnd* pWnd,CPoint pos );
};
CustomList.cpp
// tcFaultListCtl
IMPLEMENT_DYNAMIC(tcCustomListCtl, CListCtrl)
tcCustomListCtl::tcCustomListCtl()
{
}
tcCustomListCtl::~tcCustomListCtl()
{
}
BEGIN_MESSAGE_MAP(tcCustomListCtl, CListCtrl)
END_MESSAGE_MAP()
// tcCustomListCtl message handlers
afx_msg void tcCustomListCtl::OnContextMenu(CWnd* pWnd,CPoint pos )
{
TRACE("tcCustomListCtl::OnContextMenu\n");
}
I found out I had to add ON_WM_CONTEXTMENU() to message map.

I can't seem to add a column header to a list box in an inherited MFC dialog. What's wrong?

I have a CStdDlg thats inherits from a CDialog class. In the CStdDlg dialog, I have a list box (m_lcList1), edit box (m_ceEdit1), a radio button(m_rbButton2) and buttons OK, Cancel and Button1.
I am trying to create another class named CDerivedDlg that inherits from CStdDlg. I want to use everything in CStdDlg but from the CDerivedDlg.
This is a silly test application here, but I need something exactly like this is a real application.
I'll show all the code below. PROBLEM: The problem keeps bombing whenever I try to add a column header to the list box. m_hWnd = NULL
Can anyone tell me what's wrong? I would truly appreciate it. Thank you.
// CStdDlg.cpp file
#include "stdafx.h"
#include "testdlg.h"
#include "StdDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CStdDlg dialog
CStdDlg::CStdDlg(UINT nIDTemplate, CWnd* pParent /*=NULL*/)
: CDialog(CStdDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CStdDlg)
//}}AFX_DATA_INIT
}
void CStdDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CStdDlg)
DDX_Control(pDX, IDC_EDIT1, m_ceEdit1);
DDX_Control(pDX, IDC_RADIO2, m_rbButton2);
DDX_Control(pDX, IDC_LIST1, m_lcList1);
//}}AFX_DATA_MAP
}
IMPLEMENT_DYNAMIC(CStdDlg, CDialog)
BEGIN_MESSAGE_MAP(CStdDlg, CDialog)
//{{AFX_MSG_MAP(CStdDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CStdDlg message handlers
BOOL CStdDlg::OnInitDialog()
{
CDialog::OnInitDialog();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
===================================================================================
//CStdDlg.h file
#if !defined(AFX_STDDLG_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_)
#define AFX_STDDLG_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// StdDlg.h : header file
//
#include <afxwin.h>
/////////////////////////////////////////////////////////////////////////////
// CStdDlg dialog
class CStdDlg : public CDialog
{
DECLARE_DYNAMIC(CStdDlg)
// Construction
public:
CStdDlg(UINT nIDTemplate,CWnd* pParent = NULL); // standard constructor
CString GetTitle() {
CString csTTL;
GetWindowText(csTTL);
return csTTL;
}
// Dialog Data
//{{AFX_DATA(CStdDlg)
enum { IDD = IDD_STDDLG };
CEdit m_ceEdit1;
CButton m_rbButton2;
CListCtrl m_lcList1;
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CStdDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
void ShowMsg() { AfxMessageBox("ShowMsg from StdDlg"); }
// Generated message map functions
//{{AFX_MSG(CStdDlg)
virtual BOOL OnInitDialog();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STDDLG_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_)
===================================================================================
//CDerivedDlg.cpp file
// StdDlg.cpp : implementation file
//
#include "stdafx.h"
#include "testdlg.h"
#include "CDerivedDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDerivedDlg dialog
CDerivedDlg::CDerivedDlg(CWnd* pParent /*=NULL*/)
: CStdDlg(CDerivedDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDerivedDlg)
//}}AFX_DATA_INIT
}
void CDerivedDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDerivedDlg)
//}}AFX_DATA_MAP
}
IMPLEMENT_DYNAMIC(CDerivedDlg, CStdDlg)
BEGIN_MESSAGE_MAP(CDerivedDlg, CStdDlg)
//{{AFX_MSG_MAP(CDerivedDlg)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDerivedDlg message handlers
//void CDerivedDlg::OnOK()
//{
// // TODO: Add extra validation here
// AfxMessageBox("CDerived Class OK button pressed");
// CDialog::OnOK();
//}
BOOL CDerivedDlg::OnInitDialog()
{
CStdDlg::OnInitDialog();
SetWindowText("Derived Test Window");
m_lcList1.InsertColumn(0,"This is a test"); *******THIS IS WHERE IT CRASHES*****
m_rbButton2.ShowWindow(SW_HIDE);
m_ceEdit1.ShowWindow(SW_HIDE);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CDerivedDlg::OnButton1()
{
// // TODO: Add extra validation here
AfxMessageBox("CDerived Button1 pressed");
AfxMessageBox(GetTitle());
}
void CDerivedDlg::OnOK()
{
AfxMessageBox("CDerived Class OK button pressed");
}
===================================================================================
// CDerivedDlg.h file
#if !defined(AFX_CDerivedDlg_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_)
#define AFX_CDerivedDlg_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// CDerivedDlg.h : header file
//
#include "StdDlg.h"
/////////////////////////////////////////////////////////////////////////////
// CDerivedDlg dialog
class CDerivedDlg : public CStdDlg
{
DECLARE_DYNAMIC(CDerivedDlg)
// Construction
public:
CDerivedDlg(CWnd* pParent = NULL); // standard constructor
virtual void SetTTL(CString csTitle) { this->SetWindowText(csTitle); }
// Dialog Data
//{{AFX_DATA(CDerivedDlg)
enum { IDD = IDD_STDDLG };
//}}AFX_DATA
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CDerivedDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(CDerivedDlg)
virtual BOOL OnInitDialog();
afx_msg void OnButton1();
virtual void OnOK();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_CDerivedDlg_H__CDAFF61F_91AB_4B47_9970_6B8BB5DE0003__INCLUDED_)
===================================================================================
I'm guessing that since the DDX_Control() are on the base class, the derived class will not link the resource controls to their respective classes. You might want to try to change this:
void CDerivedDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
into this:
void CDerivedDlg::DoDataExchange(CDataExchange* pDX)
{
CStdDlg::DoDataExchange(pDX);
m_lcList1.InsertColumn(0,"This is a test"); *******THIS IS WHERE IT CRASHES*****
You have to create the control before you'll be able to use it.
See CListCtrl::Create and/or CListCtrl::CreateEx methods.
You must choose the LVS_REPORT style since you want to use the list as a report view control.