最顶层的表单,点击“通过"可能的? [英] Topmost form, clicking "through" possible?

查看:32
本文介绍了最顶层的表单,点击“通过"可能的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您之前的回答,使我能够完成在鼠标坐标中显示大红叉的基本工具,以便更显眼.红十字是透明形式的具有透明背景的图像.问题是你不能点击,因为它的最顶部和窗体的中心实际上定位到鼠标 xy.有什么方法可以使这个可用,以便使十字仍然显示在光标上但可点击"通过?

Thank you for previous answers that enabled to me complete the basic tool that shows large red cross in the mouse coordinates in order to let be more visible. The red cross is an image with transparent background in the transparent form. The problem is that you cannot click through, since its topmost and the center of form is actually positioned to mouse xy. Is there any way how to make this usable in order to have the cross still displayed on the cursor but "clickable" through?

推荐答案

您可以使用 SetWindowLong 设置 WS_EX_TRANSPARENT 窗口样式:

You can use SetWindowLong to set the WS_EX_TRANSPARENT window style:

如果分层窗口具有 WS_EX_TRANSPARENT 扩展窗口样式,则分层窗口的形状将被忽略,并将鼠标事件传递给分层窗口下方的其他窗口.

If the layered window has the WS_EX_TRANSPARENT extended window style, the shape of the layered window will be ignored and the mouse events will be passed to the other windows underneath the layered window.

CodeProject 有这篇文章详细介绍了该技术.虽然它在 VB.NET 中,但应该很容易转换为 C#.

CodeProject has this article detailing the technique. Though it's in VB.NET it should be easy to convert to C#.

我过去使用过以下代码:

I have used the following code in the past:

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 方法中.虽然我不得不承认这一行

but it also was copied from somewhere else. The important lines here are in the OnShown method. Though I have to admit that the line

wl = wl | 0x80000 | 0x20;

有点神秘,设置 WS_EX_LAYERED 和 WS_EX_TRANSPARENT 扩展样式.

is a little cryptic, setting the WS_EX_LAYERED and WS_EX_TRANSPARENT extended styles.

你也可以这样设置

wl = wl | WS_EX.Layered | WS_EX.Transparent;

这篇关于最顶层的表单,点击“通过"可能的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆