以透明形式点击进入桌面

发布于 2024-10-09 13:53:15 字数 311 浏览 1 评论 0原文

我有一个屏幕捕获实用程序,可以用橡皮筋在桌面上的某个区域进行操作。我已经以相当简单的方式做到了这一点,我有一个与屏幕大小相同的表单,我在其上绘制了转换为灰度的桌面屏幕截图。当用户按住鼠标左键时,他/她可以选择表单上的区域。用户绘制的矩形用透明颜色填充。一旦用户抬起鼠标,透明矩形就会保留在原处,并且实际的桌面可见。我的问题来了: 在我的开发 PC 上,我实际上可以单击这个透明矩形并进行导航等。而在我的另一台 PC 上,表单会通过鼠标单击实际的透明矩形进行响应。

我在 C# 中使用 .NET 4.0,关于如何让它在所有情况下实际点击到桌面有什么想法吗?

谢谢你,非常感谢:)

I have a screen capturing utility on which I can rubber band an area on the desktop. I've done this is a fairly easy manner, I have a form which is the same size as the screen on which I draw a screenshot of the desktop transformed into grayscale. When the user holds down the left mouse button he/she can select an area on the form. The rectangle which the user draws is filled with TransparentColor. Once the users lifts up his/her mouse the transparent rectangle is left in place and the actual desktop is visible. Here comes my problem:
On my dev PC I can actually click through this transparent rectangle and navigate around etc. while on my other PC the form responds on mouse clicks on the actual transparent rectangle.

I'm using .NET 4.0 in C#, any ideas on how I can make it actually click through to the desktop on all cases??

Thank you and much appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时间海 2024-10-16 13:53:15

经过深入研究后,我设法找到了解决此问题的正确解决方案。事实证明,通过正确的 Win32 API 调用,可以将表单设置为“不可见”以进行鼠标单击。这可以通过以下方式实现:

public const int GWL_EXSTYLE = -20;
public const uint WS_EX_LAYERED = 0x00080000;
public const uint WS_EX_TRANSPARENT = 0x00000020;

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

[DllImport("user32.dll")]
static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);

public void SetFormTransparent(IntPtr Handle) {
    oldWindowLong = GetWindowLong(Handle, GWL_EXSTYLE);
    SetWindowLong(Handle, GWL_EXSTYLE, Convert.ToInt32(oldWindowLong | WS_EX_LAYERED | WS_EX_TRANSPARENT));
}

public void SetFormNormal(IntPtr Handle) {
    SetWindowLong(Handle, GWL_EXSTYLE, Convert.ToInt32(oldWindowLong | WS_EX_LAYERED));
}

但万事皆有窍门。您需要注意的是,在论坛上进行的所有点击都将通过表单并发送到表单下方的任何内容。为了确保如果我单击表单(例如按钮)并且我希望单击该按钮,我做了一个简单的技巧。我在后台有一个计时器,每 N 毫秒运行一次并分析光标的位置。如果它位于我想要的区域之上,它将通过 SetFormNormal() 将表单设置为正常,否则它将是透明的。

希望这段代码有帮助并且人们会使用它。

I managed to find a proper solution for this problem after looking very deeply into this. It turns out with the proper Win32 API calls it is possible to set a form "Invisible" to mouse clicks. This can be achieved by:

public const int GWL_EXSTYLE = -20;
public const uint WS_EX_LAYERED = 0x00080000;
public const uint WS_EX_TRANSPARENT = 0x00000020;

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

[DllImport("user32.dll")]
static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);

public void SetFormTransparent(IntPtr Handle) {
    oldWindowLong = GetWindowLong(Handle, GWL_EXSTYLE);
    SetWindowLong(Handle, GWL_EXSTYLE, Convert.ToInt32(oldWindowLong | WS_EX_LAYERED | WS_EX_TRANSPARENT));
}

public void SetFormNormal(IntPtr Handle) {
    SetWindowLong(Handle, GWL_EXSTYLE, Convert.ToInt32(oldWindowLong | WS_EX_LAYERED));
}

But there is a trick to everything. You need to be careful that all clicks made on the forum will fall through the form and be sent to anything below the form. To ensure that if I click on my form e.g. on a button and I want the button clicked I did a simple trick. I have a timer in the background running every N milliseconds and analyzing the position of the Cursor. If it's above the area I want it to be, it'll set the Form to Normal via SetFormNormal() otherwise it'll be transparent.

Hope this code bit helps and people will use it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文