简体   繁体   English

没有使用TransparencyKey触发MouseMove事件

[英]MouseMove event doesn't fired with TransparencyKey

I'm creating a simple application that draws a horizontal and a vertical line following the mouse. 我正在创建一个简单的应用程序,在鼠标之后绘制一条水平线和一条垂直线。

The form is transparent using TransparencyKey, and the lines are drawn using the Paint event: 该窗体使用TransparencyKey是透明的,并且使用Paint事件绘制线条:

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Pen pen = new Pen(Color.Lime);
            e.Graphics.DrawLine(pen, 0, py, this.Size.Width, py);
            e.Graphics.DrawLine(pen, px, 0, px, this.Size.Height);
        }

private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            px = e.X; // get cursor X pos
            py = e.Y; // get cursor Y pos
            Invalidate(); // fire Paint event
        }

But the MouseMove event only is fired when the mouse is over the lines drawn. 但是,只有当鼠标悬停在绘制的线条上方时,才会触发MouseMove事件。 How to make the form catch mouse events when transparent? 透明时如何使表单捕获鼠标事件? (Only the mouse move, I want the form still click-through) (只有鼠标移动,我希望表单仍然可以点击)

As you can read here: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.transparencykey%28v=vs.110%29.aspx - "Any mouse actions, such as the click of the mouse, that are performed on the transparent areas of the form will be transferred to the windows below the transparent area". 您可以在此处阅读: http : //msdn.microsoft.com/zh-cn/library/system.windows.forms.form.transparencykey%28v=vs.110%29.aspx- “任何鼠标操作,例如单击鼠标,在窗体的透明区域上执行的操作将转移到透明区域下方的窗口中。” One way to accomplish your goal is to write your own method, that will check the cursor relative position to the form in a loop, here's what I mean (it worked for me with TransparencyKey, resizing and moving the form): 实现目标的一种方法是编写自己的方法,该方法将在循环中检查光标相对于表单的相对位置,这就是我的意思(它与TransparencyKey一起工作,对表单进行了大小调整和移动):

private void MouseMovedWithTransparency()
{
   Point lastCursorPos = new Point(-1, -1); // Starting point.
   while (this.Visible)
   {
        Point currentCursorPos = Cursor.Position;
        if (this.ContainsFocus && currentCursorPos.X > this.Left && currentCursorPos.X < this.Left + this.Width &&
            currentCursorPos.Y > this.Top && currentCursorPos.Y < this.Top + this.Height)
        {
            if ((currentCursorPos.X != lastCursorPos.X) || (currentCursorPos.Y != lastCursorPos.Y))
            {
                // Do actions as in MouseMoved event.

                // Save the new position, so it won't be triggered, when user doesn't move the cursor.
                lastCursorPos = currentCursorPos;
            }
        }

        Application.DoEvents(); // UI must be kept responsive.
        Thread.Sleep(1);
    }
}

All you have to do now is to invoke this method from the Shown event - this.Visible has to be true, so it's the easiest way to make it work. 现在您要做的就是从Shown事件中调用此方法-this.Visible必须为true,因此这是使其最简单的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM