windows 捕获鼠标事件时的透明背景?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/882706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:31:13  来源:igfitidea点击:

Transparent background whilst capturing mouse events?

c#windowswinformstransparency

提问by HalliHax

There seems to be a similar question to this on here but with the 'opposite' problem (He didn't want mouse events captured).

这里似乎有一个与此类似的问题,但存在“相反”问题(他不想捕获鼠标事件)。

I have a form with a panel. The window is borderless and set to the exact size of the panel (for all intents and purposes, it is as if the panel is 'free floating'). I can set the panel's BackColor to SystemColors.Control, and then set the window's TransparencyKey to the same. This works in that it achieves the desired effect (transparency), but the panel can no longer capture mouse events (which is vital to the functionality)!

我有一个带面板的表格。窗口是无边框的,并设置为面板的确切大小(出于所有意图和目的,就好像面板是“自由浮动”的一样)。我可以将面板的 BackColor 设置为 SystemColors.Control,然后将窗口的 TransparencyKey 设置为相同。这是因为它达到了预期的效果(透明度),但面板无法再捕获鼠标事件(这对功能至关重要)!

Is there another way around this, or a way to re-enable mouse capture?

有没有其他方法可以解决这个问题,或者重新启用鼠标捕获的方法?

I have tried overriding the OnPaintBackground and doing a noop, but this didn't achieve real transparency because it doesn't update the background after every tick (so whatever is behind the panel at the initial draw remains there regardless of whether you move the panel or otherwise update it). It did, however, allow the panel to capture mouse events.

我曾尝试覆盖 OnPaintBackground 并执行 noop,但这并没有实现真正的透明度,因为它不会在每次滴答后更新背景(因此无论您是否移动面板,初始绘制时面板后面的任何内容都会保留在那里或以其他方式更新它)。但是,它确实允许面板捕获鼠标事件。

This isn't all that troublesome at this stage in the project but I stumbled across the problem during a quick prototype and it's starting to annoy me now. If anyone has any pointers they'd be much appreciated.

在项目的这个阶段,这并不是那么麻烦,但我在快速原型中偶然发现了这个问题,现在它开始让我烦恼。如果有人有任何指示,他们将不胜感激。

采纳答案by ChrisBD

If you were using VC++ I would say that you needed a message pump to process WM_ mouse event messages.

如果您使用的是 VC++,我会说您需要一个消息泵来处理 WM_ 鼠标事件消息。

A quick search reveals this thread which may be of assistance to you:

快速搜索显示此线程可能对您有所帮助:

Capturing ALL mouse events

捕获所有鼠标事件

I expect that you've already tried using the following:

我希望您已经尝试过使用以下内容:

/// <summary>
/// A transparent control.
/// </summary>
public class TransparentPanel : Panel
{
    public TransparentPanel()
    {
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams createParams = base.CreateParams;
            createParams.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT
            return createParams;
        }
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        // Do not paint background.
    }
}

回答by BFree

I don't really have an answer for you, but I do have another (maybe a tad "hacky") way for you to accomplish what you're trying to do.

我真的没有答案给你,但我确实有另一种(可能有点“hacky”)方式让你完成你想要做的事情。

Set the Forms Opacity property to 1% (don't mess with the transparency key) and now it'll capture the events. The form will not be visible (at least on my machine at 1% I couldn't see it at all) and you'll still be able to capture all mouse clicks.

将 Forms Opacity 属性设置为 1%(不要弄乱透明度键),现在它将捕获事件。该表单将不可见(至少在我的机器上 1% 我根本看不到它)并且您仍然可以捕获所有鼠标点击。