MFC-接收按钮单击消息失败

MFC- receiving Button-Click-Message failed

本文关键字:消息 失败 单击 按钮 MFC-      更新时间:2023-10-16

我在基于MFC对话框的应用程序中创建了一个新对话框。新对话框包含5个控制按钮。

下面的事情发生了,我不明白为什么?

  1. 单击按钮X。(结果ok,发送了OnBnClicked消息)
  2. 单击应用程序的任何位置,但不要单击对话框。(从对话框中删除焦点)
  3. 再次单击按钮X(失败,未发送OnBnClicked消息)。但如果我点击对话框中的任何其他按钮(结果ok,发送OnBnClicked消息)

当我这样做的时候:

  1. 单击对话框区域可以再次设置对话框的焦点
  2. 再次单击按钮X。(结果ok,发送OnBnClicked消息)

**只有当我想再次点击按钮X时,我才需要执行步骤3!为什么?我认为它与SetFocus()有关,但我不确定是怎么回事。

按钮IDC是:IDC_BACK_MEDIA_PRESS_BUTTON 1180IDC_TOOLS_LEFT_RIGHT 1024IDC_MEDIA_FOREWARD_BUTTON 1103IDC_MEDIA_BACKWARD_BUTTON 1104IDC_TOOLS_HOOD_BUTTON 2346

对话框属性为:

IDD_TOOLS_DIALOG对话框0、0、51、218样式DS_SETFONT|DS_MODALFRAME|DS_FIXEDSYS|WS_POPUP|WS_VISIBLE|WS_ception|WS_SYSMENU标题"工具"FONT 8,"MS Shell Dlg",400,0,0x1开始PUSHBUTTON"媒体和向前",IDC_Media_Foreward_BUTTON,7,79,37,36,BS_MULTILINEPUSHBUTTON"&Media&BackWard",IDC_Media_BackWard_BUTTON,7,43,37,36,BS_MULTILINEPUSHBUTTON"后退媒体按压",IDC_Back_Media_Press_BUTTON,7127,37,36,BS_MULTILINE | NOT WS_VISIBLEPUSHBUTTON"发动机罩",IDC_TOOLS_OOD_BUTTON,7,7,37,36按钮"左-右",IDC_TOOLS_Left_Right,7175,37,36结束

我尝试过不同的风格,比如,工具窗口,重叠,弹出。这种情况在所有情况下都会发生。

谢谢你的帮助。

.h

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

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()
{
    ...
}

我看到您正在用对话框内容填充视图。你把注意力放在对话上了吗?我认为这种神秘的行为只发生在对话框中的第一次鼠标点击时(然后对话框获得焦点)。我只是猜测:)