2014-09-29 53 views
0

我得到了一些代碼了從互聯網上,使面板純粹透明,但在這裏,我需要做透明面板有點暗像下面如何創建的C#Windows應用程序中的部分透明的面板

圖片

enter image description here

透明面板的代碼是: -

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Drawing; 
using System.Drawing.Drawing2D; 
using System.Drawing.Imaging; 

namespace Test 
{ 
    public class TransparentPanel : Panel 
    { 
     protected override CreateParams CreateParams 
     { 
      get 
      { 
       CreateParams cp = base.CreateParams; 
       cp.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT 
       return cp; 
      } 
     } 
     protected override void OnPaintBackground(PaintEventArgs e) 
     { 
      //base.OnPaintBackground(e); 
     } 
    } 
} 

誰能幫助我在這裏。任何代碼將不勝感激。

+0

組形成混濁 – Trikaldarshi 2014-09-29 06:45:13

+0

你應該插入圖像*直接*到你的問題,並支持它與解釋性文字(對於那些不顯示圖片) – Sayse 2014-09-29 06:47:30

+0

@AnonymousMohit我想要一個面板不形式 – 2014-09-29 06:50:52

回答

0

您需要使用:Aero desktop experience,你可以找到的信息:Create Special Effects With The Desktop Window Manager

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace FixedFormWithAeroGlass 
{ 
    public class Form1 : Form 
    { 
     [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)] 
     public struct DWM_BLURBEHIND 
     { 
     public uint dwFlags; 
     [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)] 
     public bool fEnable; 
     public IntPtr hRegionBlur; 
     [System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)] 
     public bool fTransitionOnMaximized; 

     public const uint DWM_BB_ENABLE = 0x00000001; 
     public const uint DWM_BB_BLURREGION = 0x00000002; 
     public const uint DWM_BB_TRANSITIONONMAXIMIZED = 0x00000004; 
     } 

     [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)] 
     public struct RECT 
     { 
     public int left, top, right, bottom; 

     public RECT(int left, int top, int right, int bottom) 
     { 
      this.left = left; this.top = top; 
      this.right = right; this.bottom = bottom; 
     } 
     } 

     [System.Runtime.InteropServices.DllImport("dwmapi.dll", PreserveSig = false)] 
     public static extern int DwmEnableBlurBehindWindow(System.IntPtr hWnd, ref DWM_BLURBEHIND pBlurBehind); 

     public const int DWM_BB_ENABLE = 0x1; 
     public const int DWM_BB_BLURREGION = 0x2; 
     public const int DWM_BB_TRANSITIONONMAXIMIZED = 0x4; 

     private System.ComponentModel.IContainer components = null; 

     public Form1() 
     { 
     InitializeComponent(); 

     FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; 
     } 

     #region Windows Form Designer generated code 

     private void InitializeComponent() 
     { 
     this.SuspendLayout(); 
     // 
     // Form1 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.ClientSize = new System.Drawing.Size(470, 342); 
     this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; 
     this.Name = "Form1"; 
     this.ResumeLayout(false); 

     } 

     #endregion 

     protected override void OnPaintBackground(PaintEventArgs e) 
     { 
     base.OnPaintBackground(e); 

     e.Graphics.Clear(BackColor); 
     using (Brush b = new SolidBrush(Color.FromArgb(196, Color.Black))) 
      e.Graphics.FillRectangle(b, ClientRectangle); 
     } 

     protected override void OnLoad(EventArgs e) 
     { 
     base.OnLoad(e); 

     DWM_BLURBEHIND dbb; 
     dbb.fEnable = true; 
     dbb.dwFlags = DWM_BB_ENABLE | DWM_BB_BLURREGION; 

     using (Graphics g = CreateGraphics()) 
      dbb.hRegionBlur = new Region(new Rectangle(0, 0, Width, Height)).GetHrgn(g); 

     dbb.fTransitionOnMaximized = false; 

     DwmEnableBlurBehindWindow(this.Handle, ref dbb); 
     } 

     protected override void Dispose(bool disposing) 
     { 
     if (disposing && (components != null)) 
     { 
      components.Dispose(); 
     } 
     base.Dispose(disposing); 
     } 
    } 
} 

,並與對照窗口的例子

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace AddingControlsInAeroStyleForm 
{ 
    public class Form1 : Form 
    { 
     [System.Runtime.InteropServices.DllImport("dwmapi.dll", PreserveSig = false)] 
     public static extern void DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMargins); 

     [System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)] 
     public struct MARGINS 
     { 
     public int Left, Right, Top, Bottom; 

     public MARGINS(int left, int top, int right, int bottom) 
     { 
      Left = left; 
      Top = top; 
      Right = right; 
      Bottom = bottom; 
     } 

     public MARGINS(int margin) 
     { 
      Left = margin; 
      Top = margin; 
      Right = margin; 
      Bottom = margin; 
     } 
     } 

     private System.Windows.Forms.Panel panel1; 
     private System.Windows.Forms.Button button1; 

     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     public Form1() 
     { 
     InitializeComponent(); 

     MARGINS margins = new MARGINS(-1); 

     DwmExtendFrameIntoClientArea(this.Handle, ref margins); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
     this.TransparencyKey = Color.FromArgb(255, Color.Black); 
     panel1.BackColor = Color.FromArgb(255, Color.Black); 

     } 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) 
     { 
     if (disposing && (components != null)) 
     { 
      components.Dispose(); 
     } 
     base.Dispose(disposing); 
     } 

     #region Windows Form Designer generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() 
     { 
     this.panel1 = new System.Windows.Forms.Panel(); 
     this.button1 = new System.Windows.Forms.Button(); 
     this.panel1.SuspendLayout(); 
     this.SuspendLayout(); 
     // 
     // panel1 
     // 
     this.panel1.Controls.Add(this.button1); 
     this.panel1.Dock = System.Windows.Forms.DockStyle.Fill; 
     this.panel1.Location = new System.Drawing.Point(0, 0); 
     this.panel1.Name = "panel1"; 
     this.panel1.Size = new System.Drawing.Size(374, 303); 
     this.panel1.TabIndex = 0; 
     // 
     // button1 
     // 
     this.button1.FlatAppearance.BorderSize = 0; 
     this.button1.Location = new System.Drawing.Point(202, 23); 
     this.button1.Name = "button1"; 
     this.button1.Size = new System.Drawing.Size(125, 70); 
     this.button1.TabIndex = 0; 
     this.button1.UseVisualStyleBackColor = true; 
     // 
     // Form1 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.ClientSize = new System.Drawing.Size(374, 303); 
     this.Controls.Add(this.panel1); 
     this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; 
     this.MaximizeBox = false; 
     this.MinimizeBox = false; 
     this.Name = "Form1"; 
     this.Text = "Form1"; 
     this.Load += new System.EventHandler(this.Form1_Load); 
     this.panel1.ResumeLayout(false); 
     this.ResumeLayout(false); 

     } 

     #endregion 


    } 
} 
+0

我希望控制像文本框,組合框應該生效或用其他字代替顯示透明與桌面我希望透明與控制的形式 – 2014-09-29 10:34:32

+0

與航空,你可以做到這一點,你需要一個例子? – lupok 2014-09-29 10:45:39

+0

是的,如果你可以管理代碼或者一些例子,那將會很棒 – 2014-09-29 11:59:36

相關問題