如何从应用程序中捕获鼠标Windows消息

如何从应用程序中捕获鼠标Windows消息

本文介绍了如何从应用程序中捕获鼠标Windows消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先很抱歉,因为对于C#来说,这可能是一个过于本地化的问题,但是无论如何,我都没有找到有关VB.NET的信息,而且我对C#的理解也不是很好.

First of all sorry 'cause maybe this could be a too localized question for C# but anyways I didn't find info about this for VB.NET and I don't understand in a good level C# code.

基本上,我需要从应用程序中捕获/处理一些鼠标消息.

Basically I need to capture/process some mouse messages out of the application.

我该怎么做?

这只是我需要在应用程序外部进行复制的代码示例:

This is just a code example of what I need to reproduce but outside the application:

Protected Overrides Sub WndProc(ByRef m As Message)

    Select Case m.Msg

        Case &H200 ' WM_MOUSEMOVE
            MsgBox("Mouse Move")

        Case &H201 ' WM_LBUTTONDOWN
            MsgBox("Left Button Down")

        Case &H202 ' WM_LBUTTONUP
            MsgBox("Left Button Up")

        Case Else
            MyBase.WndProc(m)

    End Select

End Sub

主要思想是按下一个按钮,该按钮将隐藏应用程序,以使用户可以在屏幕上选择一个区域,在这里我需要捕获那些Windows消息,以了解何时需要开始/结束区域选择.

The main idea is to press a button that will hide the application to let the user selects a region over the screen and there is where I need to capture those windows messages to know when I need to start/end the region selection.

推荐答案

有两种方法可以做到这一点.

There are two ways to do this.

  1. 使用全局挂钩(在Google上使用)我不推荐.

  1. Using global hooks (Google it)I do not recommend.

使您的winform透明.

Making ur winform transparent.

您需要将winform的透明颜色设置为背景控件颜色.要使表单无法定位,您需要将此代码添加到您的消息处理程序中

U need set transparent color of ur winform as the background control color. To make the form unfocusable u need add this code to ur message handler

if (m.Msg == WM_MOUSEACTIVATE)
{
    m.Result = new IntPtr(MA_NOACTIVATE);
    return;
}

如果您想接收左击消息,只需在MouseActivate消息之前添加此代码即可.

if u want to receive left-click message just add this code before MouseActivate massage.

if (m.Msg == WM_LBUTTONDOWN)
{
}

完整示例

namespace
{
    public partial class XXXXXForm : Form
    {
        private const int WM_MOUSEACTIVATE = 0x21;
        private const int WM_LBUTTONDOWN = 0x0201;
        private const int MK_LBUTTON = 0x0001;
        private const int MA_NOACTIVATE = 3;

        public ClockForm()
        {
            InitializeComponent();

            this.DoubleBuffered = true;
            this.TopMost = true;
            this.FormBorderStyle = FormBorderStyle.None;
            this.ShowIcon = false;
            this.ShowInTaskbar = false;
            this.TransparencyKey = this.BackColor;

        }


        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private extern static int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        private const int WM_SYSCOMMAND = 0x112;
        private const int SC_MOVE = 0xF010;
        private const int HTCAPTION = 0x0002;


        protected override void WndProc(ref Message m)
        {

            if (m.Msg == WM_LBUTTONDOWN)
            {
...
            }
            else if (m.Msg == WM_MOUSEACTIVATE)
            {
                m.Result = new IntPtr(MA_NOACTIVATE);
                return;
            }
            base.WndProc(ref m);

        }

        protected override bool ShowWithoutActivation
        {
            get
            {
                return false;
            }
        }

        private void Form_Paint(object sender, PaintEventArgs e)
        {

        }

    }
}

这篇关于如何从应用程序中捕获鼠标Windows消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 06:33