在窗体上绘制半透明覆盖图像,同时保留一些控件,使其所有子控件可见但不能被点击。它应该像我们透过半透明黑镜看到一些东西。
我尝试使用透明控件。这是子类化Panel控件并在该控件上绘制图像,但是所有控件都是完全可见的。
在窗体上绘制半透明覆盖图像,同时保留一些控件,使其所有子控件可见但不能被点击。它应该像我们透过半透明黑镜看到一些东西。
我尝试使用透明控件。这是子类化Panel控件并在该控件上绘制图像,但是所有控件都是完全可见的。
这将需要另一个表单,您需要在现有表单之上显示它。使用其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