如何在c#windows应用程序中创建部分透明的面板

时间:2014-09-29 06:43:30

标签: c# winforms panel transparent

我从互联网上获得了一些代码,使面板纯粹透明但在这里我需要使透明面板有点暗,如下图所示

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);
        }
    }
}

任何人都可以帮助我。任何一段代码都将受到赞赏。

1 个答案:

答案 0 :(得分: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


   }
}