[英]Topmost form, clicking “through” possible?
感謝您之前的答案,這些答案使我能夠完成在鼠標坐標中顯示大紅十字的基本工具,以便更加明顯。 紅十字是透明背景的透明圖像。 問題是你不能點擊,因為它的最頂層和窗體的中心實際上是鼠標xy。 有沒有什么方法可以使這個可用,以便交叉仍然顯示在光標上但“可點擊”通過?
您可以使用SetWindowLong設置WS_EX_TRANSPARENT窗口樣式:
如果分層窗口具有WS_EX_TRANSPARENT擴展窗口樣式,則將忽略分層窗口的形狀,並將鼠標事件傳遞到分層窗口下的其他窗口。
CodeProject有這篇文章詳細介紹了這項技術。 雖然它在VB.NET中,但轉換為C#應該很容易。
我過去使用過以下代碼:
public enum GWL
{
ExStyle = -20
}
public enum WS_EX
{
Transparent = 0x20,
Layered = 0x80000
}
public enum LWA
{
ColorKey = 0x1,
Alpha = 0x2
}
[DllImport("user32.dll", EntryPoint = "GetWindowLong")]
public static extern int GetWindowLong(IntPtr hWnd, GWL nIndex);
[DllImport("user32.dll", EntryPoint = "SetWindowLong")]
public static extern int SetWindowLong(IntPtr hWnd, GWL nIndex, int dwNewLong);
[DllImport("user32.dll", EntryPoint = "SetLayeredWindowAttributes")]
public static extern bool SetLayeredWindowAttributes(IntPtr hWnd, int crKey, byte alpha, LWA dwFlags);
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
int wl = GetWindowLong(this.Handle, GWL.ExStyle);
wl = wl | 0x80000 | 0x20;
SetWindowLong(this.Handle, GWL.ExStyle, wl);
SetLayeredWindowAttributes(this.Handle, 0, 128, LWA.Alpha);
}
但它也是從其他地方復制的。 這里的重要部分是在OnShown
方法中。 雖然我不得不承認這一行
wl = wl | 0x80000 | 0x20;
有點神秘,設置WS_EX_LAYERED和WS_EX_TRANSPARENT擴展樣式。
你也可以設置它
wl = wl | WS_EX.Layered | WS_EX.Transparent;
提供更詳細/評論的版本,它也使用TransparencyKey作為透明度鍵(不是上面的版本的黑色),並且可以根據需要設置_alpha。
/// <summary>
/// 0: the window is completely transparent ... 255: the window is opaque
/// </summary>
private byte _alpha;
private enum GetWindowLong
{
/// <summary>
/// Sets a new extended window style.
/// </summary>
GWL_EXSTYLE = -20
}
private enum ExtendedWindowStyles
{
/// <summary>
/// Transparent window.
/// </summary>
WS_EX_TRANSPARENT = 0x20,
/// <summary>
/// Layered window. http://msdn.microsoft.com/en-us/library/windows/desktop/ms632599%28v=vs.85%29.aspx#layered
/// </summary>
WS_EX_LAYERED = 0x80000
}
private enum LayeredWindowAttributes
{
/// <summary>
/// Use bAlpha to determine the opacity of the layered window.
/// </summary>
LWA_COLORKEY = 0x1,
/// <summary>
/// Use crKey as the transparency color.
/// </summary>
LWA_ALPHA = 0x2
}
[DllImport("user32.dll", EntryPoint = "GetWindowLong")]
private static extern int User32_GetWindowLong(IntPtr hWnd, GetWindowLong nIndex);
[DllImport("user32.dll", EntryPoint = "SetWindowLong")]
private static extern int User32_SetWindowLong(IntPtr hWnd, GetWindowLong nIndex, int dwNewLong);
[DllImport("user32.dll", EntryPoint = "SetLayeredWindowAttributes")]
private static extern bool User32_SetLayeredWindowAttributes(IntPtr hWnd, int crKey, byte bAlpha, LayeredWindowAttributes dwFlags);
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
//Click through
int wl = User32_GetWindowLong(this.Handle, GetWindowLong.GWL_EXSTYLE);
User32_SetWindowLong(this.Handle, GetWindowLong.GWL_EXSTYLE, wl | (int)ExtendedWindowStyles.WS_EX_LAYERED | (int)ExtendedWindowStyles.WS_EX_TRANSPARENT);
//Change alpha
User32_SetLayeredWindowAttributes(this.Handle, (TransparencyKey.B << 16) + (TransparencyKey.G << 8) + TransparencyKey.R, _alpha, LayeredWindowAttributes.LWA_COLORKEY | LayeredWindowAttributes.LWA_ALPHA);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.