2015-11-03 58 views
2

我有一个窗体,其中有各种按钮和面板。我有一个按钮,当按下按钮时会对某些值执行检查,如果检查通过,我需要鼠标单击以通过窗体下落并打到应用程序窗口下面的任何按钮。点击通过表单上的条件

被按下后,按钮,检查已通过什么我目前做的是,我的表格设置为透明使用:

[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); 

并将窗体设置恢复正​​常。这导致了一个非常不一致,有时缓慢/无反应的行为。

如果我想在按钮检查通过后模拟鼠标点击,还有其他选择吗?

+0

只要窗口有一个父窗口,或者是一个拥有的窗口,那么你可以[使用这个](http://stackoverflow.com/a/7692496/17034)。 –

+0

你好,感谢您的链接,这对我来说很有用。不过,我的窗户不属于另一个窗户。这是根源,我需要点击才能通过它并击中其他应用程序或其下的桌面。 – Footch

回答

3

重点是使用Color.Magenta作为TrancparecyKeyBackColor您的窗体。 然后使按钮不可见,并模拟点击事件,然后再次使按钮可见。

这里是一个示例,当您单击按钮时,它将使表单透明,然后模拟单击以通过表单。

[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; 
} 

注意

制造透明和点击
为了使形式透明,并点击通过的形式,你可以简单地设置您的形式向TrancparecyKey财产和BackColor财产Color.Magenta

请注意,关键是使用洋红色作为TrancparecyKeyBackColor。例如,如果您使用红色,它会使表单透明,但不会使其点击。

如果您的表单上有一些控件,它们将保持可见状态,并且会收到点击。如果你需要让他们看不见,你可以将它们的Visible属性只是设置为false

进行正常
要作出这样的形式正常的,这是不够的设定BackColorTrancparecyKey不同的另一种颜色,例如SystemColors.Control

+0

谢谢。我已经使用另一种颜色的TransparencyKey来使表单透明,但是这不会让我的点击穿过窗口。执行按钮点击不会模拟应用程序窗口下方的点击,因此我也无法使用它。 – Footch

+0

关键在于将洋红色作为颜色使用,例如,红色使您的窗体透明但不通过,但洋红色使其透明且点击通过。 –

+0

我测试解决方案,它在这里正常工作:) –