2010-12-21 64 views
8

在窗體窗體上繪製具有一些控件的半透明疊加圖像,以便它的所有子控件都應該可見,但不能單擊它們。它應該就像我們通過一些半透明的黑色鏡子看到一些東西。在窗體窗體上繪製具有一些控件的半透明疊加圖像

我嘗試過使用透明控件。這是對該控件進行子類別面板控制和繪製圖像的分類,但是所有控件都是完全可見的。

+1

wpf或winforms。 – Will 2010-12-21 19:45:35

回答

3

創建一個layered window,它保留在主窗體的頂部並與其位置同步。您可以使用32位RGBA圖像改變分層窗口的alpha值,以獲得您想要的效果。

有一個體面的codeproject文章,告訴你如何做到這一點here

26

這將需要另一種形式,您在現有的頂部顯示。其不透明屬性可以產生預期的效果。爲您的項目添加一個新類並粘貼下面顯示的代碼。調用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); 
} 
+0

其工作。謝謝Mohammed – MLS 2010-12-23 20:02:18

0

我相信一個簡單的方法是把你設置其不透明度的透明標籤控件,並禁用其自動調整大小功能,並調整標籤要覆蓋面的大小。

然後,當您要覆蓋標籤時,將其發送到前面(以編程方式)並使其可見。當你想禁用覆蓋時,將它發送回去並使其不可見。

我已經用覆蓋整個表單的文本標籤來完成此操作。我認爲,如果不是設置Label控件的Text屬性,而是設置一個半透明(PNG)圖像,它會起到同樣的作用。