在窗体上绘制半透明的覆盖图像,并有一些控件

在窗体窗体上绘制半透明的叠加图像,使其所有子控件都可见,但不能单击它们。 它应该就像我们通过一些半透明的黑色镜子看到一些东西。

我曾尝试使用透明控制。 这是子控制面板控制和绘图图像,但所有的控件都是完全可见的。

这将需要另一种forms,你现在的顶部显示。 其不透明属性可以产生预期的效果。 为您的项目添加一个新类,并粘贴下面显示的代码。 调用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); } 

创build一个保存在主窗体顶部并与其位置同步的分层窗口 。 您可以使用32位RGBA图像改变分层窗口的alpha值,以获得您想要的效果。

有一个体面的codeproject文章,告诉你如何在这里做到这一点 。

我相信一个更简单的方法是将透明Label控件放置在设置其不透明度的位置,并禁用其AutoSizefunction,并将标签大小调整为要覆盖的曲面大小。

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

我用一个覆盖整个表单的文本标签来完成这个工作。 我认为这将是相同的,而不是设置Label控件的Text属性,您设置一个半透明(PNG)图像。