Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMFCButton.SetToolTip() crash

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 :

enter image description here

Any idea please ?

Thanks a lot :)

Best regards,

like image 778
Walter Fabio Simoni Avatar asked May 12 '26 15:05

Walter Fabio Simoni


1 Answers

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.

like image 159
Roger Rowland Avatar answered May 14 '26 05:05

Roger Rowland