MFC-vc++点击一个按钮触发另外一个按钮的事件方法

83 篇文章 3 订阅

void CPCSoftwareDlg::OnBnClickedButRs485test()
{
    // TODO:  在此添加控件通知处理程序代码
    OnBnClickedButtonSendmsg(); //调用另外一个按钮的点击事件
    m_RS485TestDlg.ShowWindow(TRUE);


}

void CPCSoftwareDlg::OnBnClickedButtonSendmsg()
{
    // TODO: 在此添加控件通知处理程序代码
    // TODO: 在此添加控件通知处理程序代码
    if (pSocketServer == NULL)
    {
        WriteLog("Socket没有初始化");
        return;
    }
    if (pSocketServer->m_clientList.GetCount() <= 0)
    {
        WriteLog("没有客户端链接");
        return;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用 MFC 中的 WM_LBUTTONDOWN 和 WM_LBUTTONUP 消息来实现这个功能。首先,在你的按钮类中添加一个变量来记录按钮点击的次数。然后,重载按钮类的 PreTranslateMessage 函数,以便在每次用户点击按钮时处理消息。 在 PreTranslateMessage 函数中,你可以捕获 WM_LBUTTONDOWN 和 WM_LBUTTONUP 消息,并在这些消息中检查鼠标是否在按钮上单击。如果鼠标在按钮上按下并松开,你可以增加按钮点击次数并触发相应的事件。 以下是一个示例代码,演示了如何实现这个功能: ``` class CMyButton : public CButton { public: CMyButton() : m_nClickCount(0) {} afx_msg void OnLButtonDown(UINT nFlags, CPoint point); afx_msg void OnLButtonUp(UINT nFlags, CPoint point); DECLARE_MESSAGE_MAP() private: int m_nClickCount; }; BEGIN_MESSAGE_MAP(CMyButton, CButton) ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() END_MESSAGE_MAP() void CMyButton::OnLButtonDown(UINT nFlags, CPoint point) { CButton::OnLButtonDown(nFlags, point); // Check if the mouse is over the button CRect rect; GetWindowRect(&rect); if (rect.PtInRect(point)) { // Mouse was clicked on the button SetCapture(); } } void CMyButton::OnLButtonUp(UINT nFlags, CPoint point) { CButton::OnLButtonUp(nFlags, point); // Check if the mouse is over the button CRect rect; GetWindowRect(&rect); if (rect.PtInRect(point)) { // Mouse was released on the button ReleaseCapture(); // Increment click count and trigger event m_nClickCount++; switch (m_nClickCount) { case 1: // Trigger first click event OnFirstClick(); break; case 2: // Trigger double click event OnDoubleClick(); break; default: // Trigger multiple click event OnMultipleClick(); break; } } } BOOL CMyButton::PreTranslateMessage(MSG* pMsg) { // Check for WM_LBUTTONDOWN and WM_LBUTTONUP messages if (pMsg->message == WM_LBUTTONDOWN || pMsg->message == WM_LBUTTONUP) { // Check if the mouse is over the button CRect rect; GetWindowRect(&rect); if (rect.PtInRect(pMsg->pt)) { // Mouse was clicked on the button SetCapture(); } else if (GetCapture() == this) { // Mouse was released outside the button ReleaseCapture(); // Reset click count m_nClickCount = 0; } } return CButton::PreTranslateMessage(pMsg); } ``` 在上面的代码中,我们在 CMyButton 类中添加了一个变量 m_nClickCount 来记录按钮点击的次数。在 OnLButtonDown 和 OnLButtonUp 函数中,我们捕获了鼠标按下和松开的消息,并检查鼠标是否在按钮上单击。如果鼠标在按钮上按下,我们使用 SetCapture 函数捕获鼠标消息,以便在鼠标移动时可以继续处理消息。如果鼠标在按钮上松开,我们使用 ReleaseCapture 函数释放鼠标捕获,并根据点击次数触发相应的事件。 在 PreTranslateMessage 函数中,我们检查 WM_LBUTTONDOWN 和 WM_LBUTTONUP 消息,并在这些消息中检查鼠标是否在按钮上单击。如果鼠标在按钮上按下,我们使用 SetCapture 函数捕获鼠标消息。如果鼠标在按钮上松开,我们使用 ReleaseCapture 函数释放鼠标捕获,并重置按钮点击次数。 希望这个示例代码可以帮助你实现你的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

txwtech笛克特科

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值