【问题标题】:Draw semi transparent overlay image all over the windows form having some controls在具有一些控件的 Windows 窗体上绘制半透明覆盖图像
【发布时间】:2011-05-29 00:59:39
【问题描述】:

在具有一些控件的窗口窗体上绘制半透明覆盖图像,这样它的所有子控件都应该可见,但您不能单击它们。它应该就像我们通过一些半透明的黑镜看到一些东西一样。

我尝试过使用透明控件。这是子类 Panel 控件并在该控件上绘制图像,但是所有控件都是完全可见的。

【问题讨论】:

    标签: c# overlay transparent


    【解决方案1】:

    这将需要您在现有表单之上显示的另一种表单。它的 Opacity 属性可以创建预期的效果。向您的项目添加一个新类并粘贴如下所示的代码。再次调用 Close() 方法移除效果。

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    class Plexiglass : Form {
        public Plexiglass(Form tocover) {
            this.BackColor = Color.DarkGray;
            this.Opacity = 0.30;      // Tweak as desired
            this.FormBorderStyle = FormBorderStyle.None;
            this.ControlBox = false;
            this.ShowInTaskbar = false;
            this.StartPosition = FormStartPosition.Manual;
            this.AutoScaleMode = AutoScaleMode.None;
            this.Location = tocover.PointToScreen(Point.Empty);
            this.ClientSize = tocover.ClientSize;
            tocover.LocationChanged += Cover_LocationChanged;
            tocover.ClientSizeChanged += Cover_ClientSizeChanged;
            this.Show(tocover);
            tocover.Focus();
            // Disable Aero transitions, the plexiglass gets too visible
            if (Environment.OSVersion.Version.Major >= 6) {
                int value = 1;
                DwmSetWindowAttribute(tocover.Handle, DWMWA_TRANSITIONS_FORCEDISABLED, ref value, 4);
            }
        }
        private void Cover_LocationChanged(object sender, EventArgs e) {
            // Ensure the plexiglass follows the owner
            this.Location = this.Owner.PointToScreen(Point.Empty);
        }
        private void Cover_ClientSizeChanged(object sender, EventArgs e) {
            // Ensure the plexiglass keeps the owner covered
            this.ClientSize = this.Owner.ClientSize;
        }
        protected override void OnFormClosing(FormClosingEventArgs e) {
            // Restore owner
            this.Owner.LocationChanged -= Cover_LocationChanged;
            this.Owner.ClientSizeChanged -= Cover_ClientSizeChanged;
            if (!this.Owner.IsDisposed && Environment.OSVersion.Version.Major >= 6) {
                int value = 1;
                DwmSetWindowAttribute(this.Owner.Handle, DWMWA_TRANSITIONS_FORCEDISABLED, ref value, 4);
            }
            base.OnFormClosing(e);
        }
        protected override void OnActivated(EventArgs e) {
            // Always keep the owner activated instead
            this.BeginInvoke(new Action(() => this.Owner.Activate()));
        }
        private const int DWMWA_TRANSITIONS_FORCEDISABLED = 3;
        [DllImport("dwmapi.dll")]
        private static extern int DwmSetWindowAttribute(IntPtr hWnd, int attr, ref int value, int attrLen);
    }
    

    【讨论】:

    • 这个类到底是怎么命名的?
    • 看起来是Utils.Plexiglass(this); ... ALT+F4 将关闭它。在此窗口顶部获取黑色文本的任何方式,例如“请稍等”? (我只是将不透明度提高了三倍,它有助于我添加到此叠加层的动态标签)。
    • 嘿 PeterX,想知道您是如何在有机玻璃上显示标签的?任何指针? :)
    • @HansPassant,这个答案非常有帮助,但它似乎也使子控件(即标签文本)半透明。有没有办法让表单半透明,而它的孩子是完全不透明的?谢谢。
    • 当然,这就是 Opacity 的作用。此类仅对覆盖现有窗口有用,按照OP的要求“应该可见但您不能单击它们”,显示任何UI都没有用。
    【解决方案2】:

    创建一个layered window,该layered window 保留在您的主要表单之上并与其位置同步。您可以使用 32 位 RGBA 图像更改分层窗口的 alpha 以获得您想要的效果。

    有一篇不错的代码项目文章向您展示了如何做到这一点here

    【讨论】:

      【解决方案3】:

      我认为更简单的方法是在您设置其不透明度的位置放置一个透明的 Label 控件,同时禁用其 AutoSize 功能并将标签的大小调整为您想要覆盖的表面的大小。

      然后,当您想要覆盖标签时,将其发送到前面(以编程方式)并使其可见。当您想要禁用覆盖时,请将其发送到后面并使其不可见。

      我已经使用覆盖整个表单的文本标签来完成此操作。我认为如果不是设置 Label 控件的 Text 属性,而是设置一个半透明 (PNG) 图像,它的工作原理是一样的。

      【讨论】:

        猜你喜欢
        • 2017-09-22
        • 1970-01-01
        • 2018-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多