CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2014
    Posts
    3

    MFC- receiving Button-Click-Message failed

    I've created a new dialog in my MFC dialog based application. the new dialog contains 5 control buttons.

    the following happens and I don't understand why?

    1. click on buttonX. (result ok, OnBnClicked message is sent)
    2. click on on any place of the application, but not on the dialog.(removing focus from dialog)
    3. click again on buttonX (FAILED, OnBnClicked message is NOT sent). but if instead I click on any other button in the dialog (result ok, OnBnClicked message is sent).
    and when I do:

    1. ...
    2. ...
    3. click on the dialog area just to set focus on the dialog again
    4. click again on buttonX. (result ok, OnBnClicked message is sent)
    **I need to do step 3 only if I want to click again on the buttonX! why?? I think it related to SetFocus() but I m not sure how.

    I've tried different style like, tool windows, overlapped, popup. it happens in all the cases.
    Thanks for the help.

    Code:
    class CToolsDlg : public CBDialog
    {
        DECLARE_DYNAMIC(CToolsDlg)
    
    public:
        CToolsDlg(CWnd* pParent = NULL);   // standard constructor
        virtual ~CToolsDlg();
        CToolTipCtrl m_ToolsTips;
    
    // Dialog Data
        enum { IDD = IDD_TOOLS_DIALOG };
    
    protected:
        virtual void OnCancel();
        virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    
        DECLARE_MESSAGE_MAP()
    public:
        CMFCButton m_HoodButton;
        CMFCButton m_MediaForewardButton;
        CMFCButton m_MediaBackwardButton;
        CMFCButton m_LeftRightButton;
        virtual BOOL OnInitDialog();
        virtual BOOL PreTranslateMessage(MSG* pMsg);
    
        afx_msg void OnTimer(UINT_PTR nIDEvent);
        afx_msg void OnBnClickedCancel();
        afx_msg void OnBnClickedToolsHoodButton();
        afx_msg void OnBnClickedMediaForewardButton();
        afx_msg void OnBnClickedMediaBackwardButton();
        afx_msg void OnBnClickedLeftRightButton();
        afx_msg void OnBnClickedBackMediaPressButton();
    };
    .cpp

    Code:
    IMPLEMENT_DYNAMIC(CToolsDlg, CBDialog)
    
    CToolsDlg::CToolsDlg(CWnd* pParent /*=NULL*/)
    : CBDialog(CToolsDlg::IDD, pParent)
    {
    
    }
    CToolsDlg::~CToolsDlg()
    {
    }
    void CToolsDlg::DoDataExchange(CDataExchange* pDX)
    {
        CBDialog::DoDataExchange(pDX);
        DDX_Control(pDX, IDC_TOOLS_HOOD_BUTTON,     m_HoodButton);
        DDX_Control(pDX, IDC_MEDIA_FOREWARD_BUTTON, m_MediaForewardButton);
        DDX_Control(pDX, IDC_MEDIA_BACKWARD_BUTTON, m_MediaBackwardButton);
        DDX_Control(pDX, IDC_TOOLS_LEFT_RIGHT,      m_LeftRightButton);
    }
    
    
    BEGIN_MESSAGE_MAP(CToolsDlg, CBDialog)
        ON_WM_CLOSE()
        ON_WM_DESTROY()
        ON_WM_TIMER()
        ON_BN_CLICKED(IDC_TOOLS_HOOD_BUTTON, &CToolsDlg::OnBnClickedToolsHoodButton)
        ON_BN_CLICKED(IDC_MEDIA_FOREWARD_BUTTON, &CToolsDlg::OnBnClickedMediaForewardButton)
        ON_BN_CLICKED(IDC_MEDIA_BACKWARD_BUTTON, &CToolsDlg::OnBnClickedMediaBackwardButton)
        ON_BN_CLICKED(IDC_TOOLS_LEFT_RIGHT, &CToolsDlg::OnBnClickedLeftRightButton)
        ON_BN_CLICKED(IDC_BACK_MEDIA_PRESS_BUTTON, &CToolsDlg::OnBnClickedBackMediaPressButton)
    END_MESSAGE_MAP()
    
    
    // CToolsDlg message handlers
    
    BOOL CToolsDlg::OnInitDialog()
    {
        CBDialog::OnInitDialog();
    
        // Window position
        //////////////////////////////////////////////////////////////////////////
        CMainFrame* mf =  (CMainFrame*)AfxGetMainWnd();
        RECT MFwinRect;
        RECT ThiswinRect;
        CWnd* fv = mf->m_wndSplitter.GetView( mf->m_wndSplitter.GetCurrentViewIndex(0,0) );
        fv->GetWindowRect(&MFwinRect);
        GetWindowRect(&ThiswinRect);
        MoveWindow(
            MFwinRect.right - (ThiswinRect.right - ThiswinRect.left) - 14,  // X
            MFwinRect.top + 14,                                             // Y
            (ThiswinRect.right - ThiswinRect.left),                         // nWidth
            (ThiswinRect.bottom - ThiswinRect.top) );                       // nHeight
    
        // Set controls state
        //////////////////////////////////////////////////////////////////////////
        m_ToolsTips.Create(this);
        m_ToolsTips.AddTool(&m_HoodButton,          TOOLTIP_HOOD_BUTTON);
        m_ToolsTips.AddTool(&m_MediaForewardButton, TOOLTIP_MEDIA_FOREWARD_BUTTON);
        m_ToolsTips.AddTool(&m_MediaBackwardButton, TOOLTIP_MEDIA_BACKWARD_BUTTON);
        m_ToolsTips.AddTool(&m_LeftRightButton,     TOOLTIP_LEFT_RIGHT_BUTTON);
        m_ToolsTips.SetDelayTime(1000);
        m_ToolsTips.Activate(BARAK_PREFS->m_Params.m_bShowToolTips);
    
        // Main timer loop (no need for now)
        // SetTimer( 1, 1000, NULL );
        return TRUE;
    }
    
    BOOL CToolsDlg::PreTranslateMessage(MSG* pMsg)
    {
        m_ToolsTips.RelayEvent(pMsg);
    
        return CBDialog::PreTranslateMessage(pMsg);
    }
    
    void CToolsDlg::OnCancel()
    {
        // When closing the window, destroy it and not only hide (its a floating window).
        DestroyWindow();
    }
    
    void CToolsDlg::OnTimer(UINT_PTR nIDEvent)
    {
        CBDialog::OnTimer(nIDEvent);
    }
    
    void CToolsDlg::OnBnClickedToolsHoodButton()
    {
        ...
    }
    
    void CToolsDlg::OnBnClickedMediaForewardButton()
    {
        ...
    }
    
    void CToolsDlg::OnBnClickedMediaBackwardButton()
    {   
        ...
    }
    
    void CToolsDlg::OnBnClickedLeftRightButton()
    {
        ...
    }
    
    void CToolsDlg::OnBnClickedBackMediaPressButton()
    {
        ...
    }
    Last edited by VictorN; October 2nd, 2014 at 09:29 AM. Reason: Added Code tags

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: MFC- receiving Button-Click-Message failed

    1. What is CBDialog class?
    2. What is the style of your buttons?
    Victor Nijegorodov

  3. #3
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: MFC- receiving Button-Click-Message failed

    The PreTranslateMessage function is a strong suspect. Try removing it and see what happens.

    Another source of problems could be the splitter window.
    Nobody cares how it works as long as it works

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: MFC- receiving Button-Click-Message failed

    Quote Originally Posted by yarivatias
    3. click on the dialog area just to set focus on the dialog again
    4. click again on buttonX. (result ok, OnBnClicked message is sent)
    **I need to do step 3 only if I want to click again on the buttonX! why?? I think it related to SetFocus() but I m not sure how.
    Where and how do you use SetFocus()?
    And how does your "OnBnClicked" message handler body look like?
    Victor Nijegorodov

  5. #5
    Join Date
    Oct 2014
    Posts
    3

    Re: MFC- receiving Button-Click-Message failed

    Quote Originally Posted by VictorN View Post
    Where and how do you use SetFocus()?
    And how does your "OnBnClicked" message handler body look like?
    Hello Victor,
    I m not using SetFocus() (the entire code is posted). Do you think I should add SetFocus() in some place?

    I set the content of "OnBnClicked" message handler as follow ( it's helping me to detect when OnBnClicked message is sent ):

    void CToolsDlg::OnBnClickedToolsHoodButton() {AfxMessageBox("Hood button");}
    void CToolsDlg::OnBnClickedMediaForewardButton() {AfxMessageBox("MediaForeward button");}
    void CToolsDlg::OnBnClickedMediaBackwardButton() {AfxMessageBox("MediaBackward button");}
    void CToolsDlg::OnBnClickedLeftRightButton() {AfxMessageBox("LeftRight button");}
    void CToolsDlg::OnBnClickedBackMediaPressButton() {AfxMessageBox("BackMediaPressbutton");}

    So I dont think it related to the content of "OnBnClicked"message handler.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: MFC- receiving Button-Click-Message failed

    Quote Originally Posted by yarivatias View Post
    Hello Victor,
    I m not using SetFocus() (the entire code is posted). Do you think I should add SetFocus() in some place?
    No.
    But for test purposes I would rather used TRACE(...) within the message handlers, not MessageBox. Or just set the breakpoints and debug the application.

    BTW, is the dialog a modal one or a modeless? And could you post a very small project reproducing the "strange" behavior of your buttons message handlers?
    Victor Nijegorodov

  7. #7
    Join Date
    Oct 2014
    Posts
    3

    Re: MFC- receiving Button-Click-Message failed

    Quote Originally Posted by VictorN View Post
    No.
    But for test purposes I would rather used TRACE(...) within the message handlers, not MessageBox. Or just set the breakpoints and debug the application.

    BTW, is the dialog a modal one or a modeless? And could you post a very small project reproducing the "strange" behavior of your buttons message handlers?
    Thank you for the help,
    Today is an holiday, I will answer tommorrow or the day after.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured