使用“区域"后,我无法收到WM_NCMOUSELEAVE [英] i can't receive WM_NCMOUSELEAVE after using "Region"

查看:85
本文介绍了使用“区域"后,我无法收到WM_NCMOUSELEAVE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现如果我使用"this.Region = new Region(this.DisplayRectangle)",那么我将无法收到WM_NCMOUSELEAVE,为什么?

Hi,i found that if i used "this.Region=new Region(this.DisplayRectangle)",then i can''t receive WM_NCMOUSELEAVE,why?

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [DllImport("user32")]
        public static extern bool TrackMouseEvent(ref TRACKMOUSEEVENT lpEventTrack);
        public struct TRACKMOUSEEVENT
        {

            public int cbSize;

            public uint dwFlags;

            public IntPtr hwndTrack;

            public uint dwHoverTime;

        }
        private const int WM_MOUSEHOVER = 0x2a1;
        private const int WM_MOUSELEAVE = 0x2a3;
        private const int WM_MOUSEMOVE = 0x200;
        private const uint TME_HOVER = 0x00000001;
        private const uint TME_LEAVE = 0x00000002;
        private const uint HOVER_DEFAULT = 0xFFFFFFFF;
        private const int TME_NONCLIENT = 0x00000010;
        private const int WM_LBUTTONUP = 0x202;
        private const int WM_LBUTTONDOWN = 0x201;
        public const int WM_NCMOUSEMOVE = 0x00A0;
        public const int WM_NCMOUSEHOVER = 0x02A0;
        public const int WM_NCMOUSELEAVE = 0x02A2;

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_NCMOUSEMOVE)
            {
                TRACKMOUSEEVENT msevnt = new TRACKMOUSEEVENT();

                msevnt.cbSize = Marshal.SizeOf(msevnt);

                msevnt.dwFlags = TME_NONCLIENT | TME_HOVER;

                msevnt.hwndTrack = this.Handle;

                msevnt.dwHoverTime = 400;

                TrackMouseEvent(ref msevnt);
            }
            else if (m.Msg == WM_NCMOUSEHOVER)
            {
                Text = new Random().Next().ToString();
            }
            else if (m.Msg == WM_NCMOUSELEAVE)
            {
                MessageBox.Show("");
            }
            base.WndProc(ref m);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Region = new Region(this.DisplayRectangle); //i can't receive WM_NCMOUSELEAVE after using it
        }
    }
}

推荐答案

一种可能性是:您的鼠标永远不会进入非客户区域(并且不会收到WM_NCMOUSEENTER,但这不是您的问题).如果使用Region属性修改Windows区域,则很容易发生.

哦,等一下!这正是您在做什么.属性DisplayRectange可以为您提供确切的客户区,严格切出非客户区.

可能您正在做一些不缩进的事情.通常,属性Region用于创建非矩形窗口.我无法建议您该怎么做,因为您没有解释自己正在做的目标.另外,使用P/Invoke是不好的.看起来您的代码很简单,并且可以完成纯.NET库可以执行的操作.您的表单具有所需的所有鼠标事件.请了解,在Mono( http://en.wikipedia.org/wiki/Mono_%28software%29 [ ^ ], http://www.mono-project.com/ [ ^ ]),您将终止此功能.最好不要这样做.

您的问题看起来非常简单,使用纯管理代码即可解决.

—SA
One possibility is: your mouse never enters the non-client region (and do not receive WM_NCMOUSEENTER, but this is not your problem). It can easily happen if you use Region property to modify the windows region.

Oh, wait a minute! This is exactly what you are doing. The property DisplayRectange gives you exactly the client area, strictly cutting out non-client area.

Probably you are doing something which you don''t indent. Normally, the property Region is used to create non-rectangular windows. I cannot advise you what to do as you did not explain the goal of what you are doing. Also, using P/Invoke is bad. It looks like your code is very simple and doing something that pure .NET libraries can do. You form has all the mouse event needed. Please understand that Forms application can run without recompilation on many platforms (Linux, Mac OS X and a lot more) under Mono (http://en.wikipedia.org/wiki/Mono_%28software%29[^], http://www.mono-project.com/[^]), and you are killing this feature. Better don''t do it.

You problem looks really simple, quite solvable using pure manages code.

—SA


这篇关于使用“区域"后,我无法收到WM_NCMOUSELEAVE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆