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

如何在c#windows应用程序中创建部分透明的面板,c#,winforms,panel,transparent,C#,Winforms,Panel,Transparent,我从互联网上获得了一些代码,使面板完全透明,但这里我需要使透明面板有点暗,如下图所示 透明面板的代码为:- 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.Imagin

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

透明面板的代码为:-

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);
        }
    }
}
有人能帮我吗。任何代码都将不胜感激。

您需要使用:并且您可以在以下位置找到信息:

以及一个带有控件的窗口示例

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


   }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
命名空间AddingControlsInAeroStyleForm
{
公开课表格1:表格
{
[System.Runtime.InteropServices.DllImport(“dwmapi.dll”,PreserveSig=false)]
公共静态外部无效DwmExtendFrameIntoClientArea(IntPtr hWnd,参考页边距pMargins);
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
公共结构页边距
{
公共int左、右、上、下;
公共页边距(左整数、上整数、右整数、下整数)
{
左=左;
顶部=顶部;
右=右;
底部=底部;
}
公共保证金(国际保证金)
{
左=边距;
顶部=边距;
右=边距;
底部=边距;
}
}
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button按钮1;
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
公共表格1()
{
初始化组件();
保证金=新保证金(-1);
DwmExtendFrameIntoClientArea(this.Handle,ref-margins);
}
私有void Form1\u加载(对象发送方、事件参数e)
{
this.TransparencyKey=Color.FromArgb(255,Color.Black);
panel1.BackColor=Color.FromArgb(255,Color.Black);
}
/// 
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.panel1=new System.Windows.Forms.Panel();
this.button1=new System.Windows.Forms.Button();
这个.panel1.SuspendLayout();
这个.SuspendLayout();
// 
//小组1
// 
this.panel1.Controls.Add(this.button1);
this.panel1.Dock=System.Windows.Forms.DockStyle.Fill;
this.panel1.Location=新系统图纸点(0,0);
this.panel1.Name=“panel1”;
this.panel1.Size=新系统图纸尺寸(374303);
this.panel1.TabIndex=0;
// 
//按钮1
// 
this.button1.FlatAppearance.BorderSize=0;
this.button1.Location=新系统图纸点(202,23);
this.button1.Name=“button1”;
this.button1.Size=新系统图纸尺寸(125,70);
this.button1.TabIndex=0;
this.button1.UseVisualStyleBackColor=true;
// 
//表格1
// 
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统.Drawing.Size(374303);
this.Controls.Add(this.panel1);
this.FormBorderStyle=System.Windows.Forms.FormBorderStyle.FixedDialog;
this.ebox=false;
this.ebox=false;
this.Name=“Form1”;
this.Text=“Form1”;
this.Load+=new System.EventHandler(this.Form1\u Load);
此.panel1.ResumeLayout(错误);
此选项为.resume布局(false);
}
#端区
}
}

您应该直接在问题中插入图像,并用解释性文字支持(对于不显示图片的问题)@匿名Mohit我想要一个小组不是form@Grundy这不是真的,我想要透明的面板,但背景颜色也像图中所示。@Agent\u Spock你是在尝试这个问题的代码吗?是的,我需要一些链接或代码来实现这一点,因为我有透明的代码,只是不能使背景darki想要文本框之类的控件,combobox应该生效,或者换句话说,不是用桌面显示透明度,而是用formwith aero中的控件显示透明度。你可以这样做,你需要一个例子吗?是的,如果你能管理代码或一些例子,那就太好了。我添加了一个进一步的例子
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


   }
}