【问题标题】:How to create a partially transparent panel in c# windows application [duplicate]如何在c#windows应用程序中创建部分透明面板[重复]
【发布时间】:2014-11-23 11:47:37
【问题描述】:

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

透明面板的代码是:-

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

谁能帮帮我。任何一段代码都会受到赞赏。

【问题讨论】:

  • 你应该将图片直接插入到你的问题中,并用说明文字支持它(对于那些不显示图片的人)
  • @AnonymousMohit 我想要一个面板而不是表单
  • @Grundy 这不是真的,我想要透明面板,但背景颜色也如图所示。
  • @Agent_Spock 你在尝试这个问题的代码吗?
  • 是的,期待一些链接或代码来实现这一点,因为我有透明代码,只是不能使背景变暗

标签: c# winforms panel transparent


【解决方案1】:

您需要使用: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


   }
}

【讨论】:

  • 我希望像文本框、组合框这样的控件应该生效,或者换句话说,而不是在桌面上显示透明度,我希望表单中的控件具有透明度
  • 用aero你可以做到,你需要一个例子吗?
  • 是的,如果您可以管理代码或一些示例,那就太好了
  • 我又加了一个例子
猜你喜欢
  • 2011-02-12
  • 1970-01-01
  • 2010-09-20
  • 1970-01-01
  • 2015-09-20
  • 1970-01-01
  • 2021-07-18
  • 2023-04-04
  • 1970-01-01
相关资源
最近更新 更多