C# 按条件点击表格
我有一个有各种按钮和面板的表单。我有一个按钮,当按下该按钮时,它会对一些值进行检查,如果检查通过,我需要鼠标单击以穿过表单并点击应用程序窗口下方的任何内容 我目前正在做的是在按下按钮并通过检查后,我使用以下命令将表单设置为透明:C# 按条件点击表格,c#,winforms,C#,Winforms,我有一个有各种按钮和面板的表单。我有一个按钮,当按下该按钮时,它会对一些值进行检查,如果检查通过,我需要鼠标单击以穿过表单并点击应用程序窗口下方的任何内容 我目前正在做的是在按下按钮并通过检查后,我使用以下命令将表单设置为透明: [DllImport("user32.dll", SetLastError = true)] static extern int GetWindowLong(IntPtr hWnd, int nIndex); [DllImport("user32.dll")] sta
[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);
并将表单设置回正常状态。这会导致非常不一致的行为,有时会导致缓慢/无响应的行为
如果我想在按钮检查通过后立即模拟鼠标单击,我还有哪些其他选项?关键是使用
颜色。洋红
作为表单的透明键
和背景色
。
然后使按钮不可见,并模拟单击事件,然后使按钮再次可见
在本例中,当您单击按钮时,它会使窗体透明,然后模拟单击以通过窗体
[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
请注意,关键点是使用洋红色作为透明键
和背景色
。例如,如果使用红色,则会使窗体透明,但不会使其单击
如果窗体上有一些控件,它们将保持可见,并将收到单击。如果需要使它们不可见,只需将它们的Visible
属性设置为false
正常化要使该窗体正常,只需将
BackColor
设置为与TransparencyKey
不同的另一种颜色即可,例如SystemColors.Control
,只要该窗口有父窗口,或者是拥有的窗口,就可以了。您好,感谢链接,了解这一点对我很有用。然而,我的窗户不是别人的。它是根目录,我需要点击它并点击它下面的其他应用程序或桌面。谢谢。我已经使用了另一种颜色的透明键来使窗体透明,但这不会使我的单击从窗口中消失。在按钮上执行单击不会模拟应用程序窗口下的单击,因此我也不能使用它。关键点在于使用洋红作为颜色,例如,红色使窗体透明,但不能通过,但洋红使窗体透明,并单击通过。@Footch如果您选中了解决方案,请告诉我:)是,我试过了,你是对的,品红色确实使表单点击通过,但它不适用于按钮(我不需要按钮是透明的,只有在满足条件时才点击通过)。我需要的是当我点击按钮时,点击按钮、表单和整个窗口,允许我点击应用程序窗口下方的任何内容。@kres0345这是一个老错误/问题。该技巧适用于某些颜色,例如,如果使用Color.Red,它将是透明的,但可以处理单击,但使用洋红,窗体也将是鼠标透明的。