[英]Click-through form on condition
我的表格有各种按钮和面板。 我有一个按钮,当按下时会对一些值进行检查,如果检查通过,我需要点击鼠标才能通过表单并点击应用程序窗口下面的任何内容。
我目前正在做的是按下按钮并且检查通过后,我使用以下方法将表单设置为透明:
[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));
}
然后我创建一个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);
并将表单设置恢复正常。 这导致了非常不一致且有时缓慢/无响应的行为。
如果我想在按钮检查通过后立即模拟鼠标单击,我还有哪些其他选项?
关键是使用Color.Magenta
作为表单的TransparencyKey
和BackColor
。 然后使按钮不可见,并模拟单击事件,然后再次使按钮可见。
在此示例中,当您单击按钮时,它会使窗体透明,然后模拟单击以通过窗体。
[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;
}
笔记
透明和点击
要使表单透明并使点击通过表单,您只需将表单的TransparencyKey
属性和BackColor
属性设置为相同颜色Color.Magenta
。
注意关键点是使用Magenta作为TransparencyKey
和BackColor
。 例如,如果您使用红色,它会使表单透明但不会使其单击。
如果您在表单上有一些控件,它们将保持可见状态并将获得点击。 如果需要使它们不可见,只需将它们的Visible
属性设置为false
正常
要使该表格正常,将BackColor
设置为与TransparencyKey
不同的另一种颜色就足够了,例如SystemColors.Control
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.