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