flx413
这个好玩吗
2016-02-23 09:21
采纳率: 25%
浏览 4.5k

Windows编程如何监听鼠标是否被一直按下

 //这个函数用来处理 鼠标和键盘的事件
    void OnWindowMessage( HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam ) {
        switch (message)
        {
        case WM_LBUTTONDOWN:
            MessageBox(NULL, "检测到鼠标事件", "这个是标题", 0);
            bIsMouseDown = true;
            break;
        case WM_LBUTTONUP:
            bIsMouseDown = false;
            break;
        case WM_KEYDOWN:
            break;
        }
    }

我想按下鼠标左键不松开一直弹出MessageBox,松开左键不再弹出。上面代码的效果是点击左键不松开只弹出一次对话框。这个该怎么实现呢?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • u010551319
    小小歌的梦 2016-02-23 09:42

    void OnWindowMessage( HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam ) {
    switch (message)
    {
    case WM_LBUTTONDOWN:
    bIsMouseDown = true;
    whilebIsMouseDown bLBTNDown)
    {
    MessageBox(NULL, "检测到鼠标事件", "这个是标题", 0);
    }
    break;
    case WM_LBUTTONUP:
    bIsMouseDown = false;
    break;
    case WM_KEYDOWN:
    break;
    }
    }

    点赞 评论
  • u010551319
    小小歌的梦 2016-02-23 09:45
    1. void OnWindowMessage( HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam ) { switch (message) { case WM_LBUTTONDOWN: bIsMouseDown = true; while(bIsMouseDown) { MessageBox(NULL, "检测到鼠标事件", "这个是标题", 0); } break; case WM_LBUTTONUP: bIsMouseDown = false; break; case WM_KEYDOWN: break; } }
    点赞 评论
  • u010551319
    小小歌的梦 2016-02-23 14:55

    // LeftButtonDownDlg.cpp : 实现文件
    //

    #include "stdafx.h"
    #include "LeftButtonDown.h"
    #include "LeftButtonDownDlg.h"

    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif

    static BOOL bIsMouseDown = FALSE;
    HWND m_hWnd;
    HHOOK hhookMs = NULL;//钩子句柄
    LRESULT CALLBACK LowLevelMouseProc (INT nCode, WPARAM wParam, LPARAM lParam);//钩子回调函数

    BOOL UninstallKbHook();//注销钩子
    BOOL InstallKbHook();//注册钩子

    // 用于应用程序“关于”菜单项的 CAboutDlg 对话框

    class CAboutDlg : public CDialog
    {
    public:
    CAboutDlg();

    // 对话框数据
    enum { IDD = IDD_ABOUTBOX };

    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持
    

    // 实现
    protected:
    DECLARE_MESSAGE_MAP()
    };

    CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
    {
    }

    void CAboutDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    }

    BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    END_MESSAGE_MAP()

    // CLeftButtonDownDlg 对话框

    CLeftButtonDownDlg::CLeftButtonDownDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CLeftButtonDownDlg::IDD, pParent)
    {
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    InstallKbHook( );
    }

    void CLeftButtonDownDlg::DoDataExchange(CDataExchange* pDX)
    {
    CDialog::DoDataExchange(pDX);
    }

    BEGIN_MESSAGE_MAP(CLeftButtonDownDlg, CDialog)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    //}}AFX_MSG_MAP
    // ON_WM_LBUTTONUP()
    END_MESSAGE_MAP()

    // CLeftButtonDownDlg 消息处理程序

    BOOL CLeftButtonDownDlg::OnInitDialog()
    {
    CDialog::OnInitDialog();

    // 将“关于...”菜单项添加到系统菜单中。
    
    // IDM_ABOUTBOX 必须在系统命令范围内。
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000);
    
    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
        BOOL bNameValid;
        CString strAboutMenu;
        bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
        ASSERT(bNameValid);
        if (!strAboutMenu.IsEmpty())
        {
            pSysMenu->AppendMenu(MF_SEPARATOR);
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
        }
    }
    
    // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
    //  执行此操作
    SetIcon(m_hIcon, TRUE);         // 设置大图标
    SetIcon(m_hIcon, FALSE);        // 设置小图标
    
    // TODO: 在此添加额外的初始化代码
    return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
    

    }

    void CLeftButtonDownDlg::OnSysCommand(UINT nID, LPARAM lParam)
    {
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
    CAboutDlg dlgAbout;
    dlgAbout.DoModal();
    }
    else
    {
    CDialog::OnSysCommand(nID, lParam);
    }
    }

    // 如果向对话框添加最小化按钮,则需要下面的代码
    // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
    // 这将由框架自动完成。

    void CLeftButtonDownDlg::OnPaint()
    {
    if (IsIconic())
    {
    CPaintDC dc(this); // 用于绘制的设备上下文

        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
    
        // 使图标在工作区矩形中居中
        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;
    
        // 绘制图标
        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialog::OnPaint();
    }
    

    }

    //当用户拖动最小化窗口时系统调用此函数取得光标
    //显示。
    HCURSOR CLeftButtonDownDlg::OnQueryDragIcon()
    {
    return static_cast(m_hIcon);
    }

    LRESULT CALLBACK LowLevelMouseProc (INT nCode, WPARAM wParam, LPARAM lParam)

    {

    MSLLHOOKSTRUCT *pkbhs = (MSLLHOOKSTRUCT *)lParam;

    char strMsg[100] = {0};

    switch (nCode)

    {

    case HC_ACTION:

    {

    if((wParam == WM_LBUTTONUP))

    {

    bIsMouseDown = FALSE;
    }

    }

    default:

    break;

    }

    return CallNextHookEx (NULL, nCode, wParam, lParam);

    }

    BOOL InstallKbHook( )

    {

    if (hhookMs )

    UninstallKbHook();

    hhookMs = SetWindowsHookEx(WH_MOUSE_LL,

    (HOOKPROC)LowLevelMouseProc, AfxGetApp()->m_hInstance, NULL);

    return(hhookMs != NULL);

    }

    BOOL UninstallKbHook()

    {

    BOOL fOk = FALSE;

    if (hhookMs ) {

    fOk = UnhookWindowsHookEx(hhookMs );

    hhookMs = NULL;

    }

    return(fOk);

    }

    UINT PostMessagBox(LPVOID lpData)
    {
    HWND tmp = (HWND)lpData;
    while(bIsMouseDown)
    {
    PostMessage(tmp, 0x1234, 0, 0);
    Sleep(1000);
    }
    return 0;
    }
    static int i = 0;
    LRESULT CLeftButtonDownDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
    {
    // TODO: 在此添加专用代码和/或调用基类
    switch (message)
    {
    case WM_LBUTTONDOWN:
    bIsMouseDown = TRUE;
    m_hWnd = GetSafeHwnd();
    AfxBeginThread(PostMessagBox, m_hWnd);
    break;
    case 0x1234:
    CString csInfo;
    i++;
    csInfo.Format("i = %d", i);
    MessageBox(csInfo);
    return TRUE;
    }
    return CDialog::WindowProc(message, wParam, lParam);
    }

    点赞 评论
  • lx624909677
    lx624909677 2016-03-30 11:16

    可以使用HOOK来监听。

    点赞 评论

相关推荐