按钮类头文件
/*
*  @file   MyBitmapBtn.h
*  @note   ...
*  @brief  能修改图片的自定义按钮类
*  @author ...
*  @date   2013/09/20
*/
#pragma once#define WM_CONNECTWIFI WM_USER+700class CMyBitmapBtn : public CBitmapButton
{
DECLARE_DYNAMIC(CMyBitmapBtn)public:
CMyBitmapBtn();
virtual ~CMyBitmapBtn();protected:
DECLARE_MESSAGE_MAP()
public:
    COLORREF TextColor;
    CString m_sSsid;//如果这个按钮被用于WIFI列表中的按钮,这个成员变量必须要赋值成ssid,否则的话是普通按钮.
    void SetTextColor(COLORREF crColor);
    void SetSsidString(CString ssid);
    virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
    afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
};-------------------------------------------------------------------------------
// MyBitmapBtn.cpp : 实现文件#include "stdafx.h"
#include "MyBitmapBtn.h"IMPLEMENT_DYNAMIC(CMyBitmapBtn, CBitmapButton)CMyBitmapBtn::CMyBitmapBtn():m_sSsid("")
{
    
}CMyBitmapBtn::~CMyBitmapBtn()
{
}
BEGIN_MESSAGE_MAP(CMyBitmapBtn, CBitmapButton)
    ON_WM_LBUTTONUP()
END_MESSAGE_MAP()
void CMyBitmapBtn::SetTextColor(COLORREF crColor) 
{
    TextColor = crColor;
}
void CMyBitmapBtn::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
    // TODO:  Add your code to draw the specified item
    CRect rect = lpDrawItemStruct->rcItem;
    CDC *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
    int nSaveDC = pDC->SaveDC();
    UINT state = lpDrawItemStruct->itemState;
    TCHAR strText[MAX_PATH + 1];
    ::GetWindowText(m_hWnd, strText, MAX_PATH);
    CBitmapButton::DrawItem(lpDrawItemStruct);
    pDC->SetTextColor(TextColor);
    if (strText!=NULL)
    {
        CFont *hFont = GetFont();
        CFont *hOldFont = pDC->SelectObject(hFont);
        CSize szExtent = pDC->GetTextExtent(strText, lstrlen(strText));
        CPoint pt(rect.CenterPoint().x - szExtent.cx / 2, rect.CenterPoint().y - szExtent.cy / 2);
        if (state & ODS_SELECTED)
        {
            pt.Offset(1,1);
        }
        int nMode = pDC->SetBkMode(TRANSPARENT);
        if (state & ODS_DISABLED)
        {
            pDC->DrawState(pt, szExtent, strText, DSS_DISABLED, TRUE, 0, (HBRUSH)NULL);
        }
        else
        {
            pDC->DrawState(pt, szExtent, strText, DSS_NORMAL, TRUE, 0, (HBRUSH)NULL);
        }
        pDC->SelectObject(hOldFont);
        pDC->SetBkMode(nMode);
    }
    pDC->RestoreDC(nSaveDC);
}void CMyBitmapBtn::SetSsidString(CString ssid)
{
    m_sSsid = ssid;
}void CMyBitmapBtn::OnLButtonUp(UINT nFlags, CPoint point)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    if (!m_sSsid.IsEmpty())
    {
        CString* str = new CString(m_sSsid);
        assert(str);
        ::PostMessage(this->GetParent()->m_hWnd,WM_CONNECTWIFI,(WPARAM)str,1);
        OutputDebugString("-----------------右键消息\n");
    }
    else
    {
        CBitmapButton::OnLButtonUp(nFlags, point);
    }
}

解决方案 »

  1.   

    排除掉鼠标本身有问题的可能性.再有, PreTranslateMessage中拦截, 看是否真的鼠标弹起消息有多个.
      

  2.   

    我看了一下消息调度里也是两次
    if (pMsg->message == WM_LBUTTONUP)
        {
            int a = 0;
            OutputDebugString("!!!!!!!!!!!!!!!!!!消息调度\n");
        }
      

  3.   

    要么是你输出错了, 要么是你的环境有问题.你对每个OutputDebugString都加上特殊的标识, 比如是里面的串中加上数字, 每个地方用不同数字, 排除有多个地方输出相同日志的问题.
    正常情况下, 鼠标消息是不可能自动增加的. 按一下是一下.或者新建一个工程, 看在其它MFC标准对话框中鼠标事件是否会变多.
      

  4.   

    下边是输出
    !!!!!!!!!!!!!!!!!!按下--消息调度捕获
    !!!!!!!!!!!!!!!!!!抬起--消息调度捕获
    -----------------右键消息
    ****************创建了连接WIFI线程
    Call send message cmd 3
    Call send message cmd 4
    !!!!!!!!!!!!!!!!!!按下--消息调度捕获
    !!!!!!!!!!!!!!!!!!抬起--消息调度捕获
    -----------------右键消息
    ****************创建了连接WIFI线程
      

  5.   

    还有这个按钮类在当前项目中的其他地方也有用到,在其他地方按一下只进一次OnLButtonUp()的哎!
      

  6.   

    Call send message cmd 3
    Call send message cmd 4
    这里是做了什么呢?
      

  7.   

    试试“::SendMessage(this->GetParent()->m_hWnd,WM_CONNECTWIFI,(WPARAM)str,1);”
      

  8.   

    有没有在程序的某个地方调用了OnLButtonup?
      

  9.   

    你这样试一下呢void CMyBitmapBtn::OnLButtonUp(UINT nFlags, CPoint point)
    {
        // TODO: 在此添加消息处理程序代码和/或调用默认值
        if (!m_sSsid.IsEmpty())
        {
            /*
            CString* str = new CString(m_sSsid);
            assert(str);
            ::PostMessage(this->GetParent()->m_hWnd,WM_CONNECTWIFI,(WPARAM)str,1);
            */
            OutputDebugString("-----------------右键消息\n");
        }
        else
        {
            CBitmapButton::OnLButtonUp(nFlags, point);
        }
    }