简体   繁体   English

有条件的点击表格

[英]Click-through form on condition

I have a form which has various buttons and panels. 我的表格有各种按钮和面板。 I have one button which when pressed runs a check against some values and if the check passes I need the mouse click to fall through the form and hit whatever it is beneath the application window. 我有一个按钮,当按下时会对一些值​​进行检查,如果检查通过,我需要点击鼠标才能通过表单并点击应用程序窗口下面的任何内容。

What I'm currently doing is after the button is pressed and the check has passed, I set the form to transparent using: 我目前正在做的是按下按钮并且检查通过后,我使用以下方法将表单设置为透明:

[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);

[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
private int oldWindowLong = 0;

public void SetFormTransparent(IntPtr Handle)
{
    oldWindowLong = GetWindowLong(Handle, -20);
    SetWindowLong(Handle, -20, Convert.ToInt32(oldWindowLong | 0x80000 | 0x20));
}

public void SetFormNormal(IntPtr Handle)
{
    SetWindowLong(Handle, -20, Convert.ToInt32(oldWindowLong | 0x80000));
}

Then I create a 1 millisecond timer, I simulate the mouse click using: 然后我创建一个1毫秒的计时器,我模拟鼠标点击使用:

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

And set the form back to normal. 并将表单设置恢复正​​常。 This results in a really inconsistent and sometimes slow/unresponsive behavior. 这导致了非常不一致且有时缓慢/无响应的行为。

What other options do I have if I want to simulate a mouse click as soon as the button's check has passed? 如果我想在按钮检查通过后立即模拟鼠标单击,我还有哪些其他选项?

The point is to use Color.Magenta as TransparencyKey and BackColor of your form. 关键是使用Color.Magenta作为表单的TransparencyKeyBackColor Then make button invisible, and simulate a click event, then make the button visible again. 然后使按钮不可见,并模拟单击事件,然后再次使按钮可见。

In this example, when you click on the button, it makes the form transparent and then simulates a click to pass through the form. 在此示例中,当您单击按钮时,它会使窗体透明,然后模拟单击以通过窗体。

[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

private const int MOUSEEVENTF_LEFTDOWN = 0x02;
private const int MOUSEEVENTF_LEFTUP = 0x04;

public void PerformClick()
{
    uint X = (uint)Cursor.Position.X;
    uint Y = (uint)Cursor.Position.Y;
    mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
}

private void button1_Click(object sender, EventArgs e)
{
    //Just to keep the form on top
    this.TopMost = true;

    //Make form transparent and click through
    this.TransparencyKey = Color.Magenta;
    this.BackColor = Color.Magenta;

    //Make the button invisible and perform a click
    //The click reaches behind the button
    //Then make button visible again to be able handle clicks again
    this.button4.Visible = false;
    PerformClick();
    this.button4.Visible = true;
}

Notes 笔记

Make Transparent and Click Through 透明和点击
To make a form Transparent and make clicks pass through the form, you can simply Set the TransparencyKey property and BackColor property of your form to the same color Color.Magenta . 要使表单透明并使点击通过表单,您只需将表单的TransparencyKey属性和BackColor属性设置为相同颜色Color.Magenta

Pay attention that the key point is using Magenta as TransparencyKey and BackColor . 注意关键点是使用Magenta作为TransparencyKeyBackColor For example, if you use Red, it makes the form transparent but doesn't make it click through. 例如,如果您使用红色,它会使表单透明但不会使其单击。

If you have some controls on your form, they will remain visible and will receive clicks. 如果您在表单上有一些控件,它们将保持可见状态并将获得点击。 If you need to make them invisible, you can simply set Visible property of them to false 如果需要使它们不可见,只需将它们的Visible属性设置为false

Make Normal 正常
To make that form normal, it's enough to set BackColor to another color different than TransparencyKey , for example SystemColors.Control 要使该表格正常,将BackColor设置为与TransparencyKey不同的另一种颜色就足够了,例如SystemColors.Control

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

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