简体   繁体   中英

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:

[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. 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 .

Pay attention that the key point is using Magenta as TransparencyKey and BackColor . 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

Make Normal
To make that form normal, it's enough to set BackColor to another color different than TransparencyKey , for example SystemColors.Control

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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