CMFCButton.SetToolTip() crash - c++

I'm trying to display a tooltip on a CMfcButton.
When my code run the SetToolTip(), the application crash.
BOOL CGenerationDlg::OnInitDialog()
{
BOOL bret = CPropertyPage::OnInitDialog();
m_pButtonExport = (CMFCButton *)GetDlgItem(IDC_BTN_EXPORTE_BILAN);
m_pButtonExport->EnableFullTextTooltip();
m_pButtonExport->SetTooltip(L"my tooltip");
return bret;
}
void CKenoDlg::DoDataExchange(CDataExchange* pDX)
{
CPropertyPage::DoDataExchange(pDX);
}
Here is my Header file :
// KenoDlg.h : fichier d'en-tête
//
#pragma once
#include "keno.h"
#include "AboutDlg.h"
// boîte de dialogue CKenoDlg
class CKenoDlg : public CPropertyPage
{
// Construction
public:
CKenoDlg(CWnd* pParent = NULL); // constructeur standard
CAboutDlg* myDialog;
// Données de boîte de dialogue
enum { IDD = IDD_KENO_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // Prise en charge de DDX/DDV
CMFCButton * m_pButtonExport;
// Implémentation
protected:
HICON m_hIcon;
// Fonctions générées de la table des messages
virtual BOOL OnInitDialog();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedBtnGenerate();
afx_msg void OnBnClickedBtnExport();
afx_msg void OnStnClickedStaticAbout();
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
};
Here is my error :
Any idea please ?
Thanks a lot :)
Best regards,

Try this:
Change your header to define a CMFCButton instance rather than a pointer:
class CKenoDlg : public CPropertyPage
{
// ... existing code ...
protected:
virtual void DoDataExchange(CDataExchange* pDX); // Prise en charge de DDX/DDV
CMFCButton m_pButtonExport;
// ... existing code ...
};
Then change your DoDataExchange function as follows:
void CKenoDlg::DoDataExchange(CDataExchange* pDX)
{
CPropertyPage::DoDataExchange(pDX);
DDX_Control(pDX, IDC_BTN_EXPORTE_BILAN, m_pButtonExport);
}
Finally, in OnInitDialog, do something like this:
BOOL CGenerationDlg::OnInitDialog()
{
BOOL bret = CPropertyPage::OnInitDialog();
m_pButtonExport.EnableFullTextTooltip();
m_pButtonExport.SetTooltip(L"my tooltip");
return bret;
}
The reason why you need to do it like this rather than with a pointer is because MFC has to subclass the control to a CMFCButton rather than the default CButton. When you use the DDX macro in DoDataExchange, this is done behind the scenes when the default implementation of OnInitDialog calls UpdateData, which in turn calls DoDataExchange and - if I recall correctly - on the first time through, the dialog controls are sub-classed to the correct types.
As you used a pointer, and had no link between the button control and the type you were using it as, there was a mismatch between the actual and expected types and that was the reason for the crash.
If you use the VS2012 wizard to add a variable from the dialog designer (right-click on a dialog control and choose Add Variable), it will create the member variable declaration in the header file and will add the DDX macro to the DoDataExchange function for you. You can then choose to change the type of the member variable, e.g. from CButton to one of your own CButton derived classes.

You use the term pointer to button and name the variable 'm_pButtonExport'. That is incorrect. The code creates an instance of a CMFCButton object, not a pointer to an instance of a button object. The variable should be named 'm_ButtonExport' and referred to as an instance of a CMFCButton object NOT a pointer to one.

Related

c++ Public member variable is inaccessible

There's an error message "member ... is inaccessible" in Visual Studio when I try to access the value of a member variable.
But, the member variable is declared public. It's a derived class, but it's not a member variable of the base class.
From the compiler, there's an error message "cannot access protected member".
The lines that cause the error:
CKaltestDlg dlg;
fprintf(debugout, "Reminders get input focus %s \n", dlg.m_ReminderInputFocus ? "true" : "false");
The header file. The member variable in question is near the end, under a public: heading.
// KaltestDlg.h : header file
//
#pragma once
// CKaltestDlg dialog
class CKaltestDlg : public CDialogEx
{
// Construction
public:
CKaltestDlg(CWnd* pParent = nullptr); // standard constructor
// Dialog Data
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_KALTEST_DIALOG };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
public:
DECLARE_MESSAGE_MAP()
afx_msg void OnBnClickedRemindersinputfocus();
CButton m_EnableDisableInputFocus;
BOOL m_ReminderInputFocus;
CButton m_EnableDisableRemindersOntop;
BOOL m_RemindersOnTop;
afx_msg void OnBnClickedRemindersalwaysontop();
CButton m_EnableDisableFlash;
BOOL m_FlashTaskbarButton;
afx_msg void OnBnClickedReminderflash();
};
From the documentation of the DECLARE_MESSAGE_MAP() macro
Note
If you declare any member after DECLARE_MESSAGE_MAP, you must specify a new access type (public, private, or protected) for them.
So the macro expansion may result in the visibility being changed. Either move the macro to the end of the class or add public: right after it. I'd recommend leaving a comment about this in your code to remind you of this fact if you modify the class in the future.

How to get the value of a text box in MFC when the text box contains a float value

I am doing a program in MFC and I have come up with an issue. I have a text box created by using the toolbars in Visual Studio.
My text box ID is IDC_TEXT1 and I need to get the value from the text box (float value not string). How do I do it?
You can get the value of by calling GetDlgItemText and std::stod like in this example:
class CAboutDlg : public CDialogEx
{
CString m_txt;
double m_num;
public:
CAboutDlg() noexcept;
// Dialog Data
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedOk();
};
CAboutDlg::CAboutDlg() noexcept : CDialogEx(IDD_ABOUTBOX)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
ON_BN_CLICKED(IDOK, &CAboutDlg::OnBnClickedOk)
END_MESSAGE_MAP()
// App command to run the dialog
void CSO55114372App::OnAppAbout()
{
CAboutDlg aboutDlg;
aboutDlg.DoModal();
}
// CSO55114372App customization load/save methods
void CSO55114372App::PreLoadState()
{
BOOL bNameValid;
CString strName;
bNameValid = strName.LoadString(IDS_EDIT_MENU);
ASSERT(bNameValid);
GetContextMenuManager()->AddMenu(strName, IDR_POPUP_EDIT);
bNameValid = strName.LoadString(IDS_EXPLORER);
ASSERT(bNameValid);
GetContextMenuManager()->AddMenu(strName, IDR_POPUP_EXPLORER);
}
void CSO55114372App::LoadCustomState()
{
}
void CSO55114372App::SaveCustomState()
{
}
// CSO55114372App message handlers
void CAboutDlg::OnBnClickedOk()
{
GetDlgItemText(IDC_EDIT2, m_txt);
std::wstring s((LPCTSTR)m_txt);
m_num = std::stod(s);
CDialogEx::OnOK();
}
I just wanted to show you a second way of mapping a edit control to a float value. This is by using the ClassWizard.
Right-click the control and choose Add Variable...:
Set the variable category as value, the access to private, specify the name and finally set the variable type to float.
Click Next (if required) and set the numeric range for input.
Click Finish
Look at the DoDataExchange method. It will look something like this:
void CMFCApplication2Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, m_fValue);
DDV_MinMaxFloat(pDX, m_fValue, 1, 100);
}
Whenever you need to work with the current value of the edit control, you type UpdataData(TRUE); This synchronises the variable with the contents of the edit control. An example with a break-point in debug mode:

How to get handler(HWND) for dialog box

Hi I have created a dialog box and it woks.
My question is: how do you retreive the handle for it?
Also, if you get the handle, how would you change the static text control text inside it?
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);
}
BEGIN_MESSAGE_MAP(CStatisticsDlg, CDialogEx)
END_MESSAGE_MAP()
Assuming you're using MFC (as indicated by the tag), then presumably you have a CDialog class instance. CDialog is a subclass of CWnd, so you can retrieve the window handle by one of 3 ways:
Directly accessing its m_hWnd member
Casting it to an HWND with operator HWND()
Calling GetSafeHwnd() on it
Here is how to do it.
First create a member function to the main application class.
Then use the following code (Assuming the class name is CGenericApp, and your Dialog class is CGenericDlg.
CWnd* CGenericApp::GetDlg()
{
return m_pMainWnd;
}
Then when you want to get a handler to the main Dialog box, use:
CGenericApp* app = (CGenericApp*)AfxGetApp();
CGenericDlg* pDlg = (CGenericDlg*)(app->GetDlg());
HWND win = pDlg->GetSafeHwnd();
'win' will hold the HWND you are looking for.

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?