C# 表單(WinForm)半透明(背景有顏色),元件不透明

C# 表單(WinForm)半透明(背景有顏色),元件不透明

C# 表單(WinForm)半透明(背景有顏色),元件不透明


資料來源: https://www.twblogs.net/a/5d03a528bd9eee487be9938c


GITHUB: https://github.com/jash-git/CS_OnlyWinFormTranslucent_project


main_FrmBackgound.Designer.cs

namespace CS_OnlyWinFormTranslucent
{
    partial class main_FrmBackgound
    {
        /// <summary>
        ///  Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <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.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(189, 108);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(229, 136);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // main_FrmBackgound
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 19F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.button1);
            this.Name = "main_FrmBackgound";
            this.Text = "main_FrmBackgound";
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.ResumeLayout(false);

        }

        #endregion

        private Button button1;
    }
}


main_FrmBackgound.cs

namespace CS_OnlyWinFormTranslucent
{
    public partial class main_FrmBackgound : Form
    {
        public static Form frmBack;
        sub_FrmForeground sub_FrmForeground;//
        public main_FrmBackgound()
        {
            InitializeComponent();
        }

        public static void Show(Form frmTop, Form frmBackOwner, Color frmBackColor, double frmBackOpacity)
        {
            // ­I´ºµ¡Åé³]¸m          
            frmBack.FormBorderStyle = FormBorderStyle.None;
            frmBack.StartPosition = FormStartPosition.Manual;
            frmBack.ShowIcon = false;
            frmBack.ShowInTaskbar = false;
            frmBack.Opacity = frmBackOpacity;
            frmBack.BackColor = frmBackColor;
            frmBack.Owner = frmBackOwner;
            frmBack.Size = frmTop.Size;
            frmBack.Location = frmTop.Location;

            // ³»³¡µ¡Åé³]¸m
            frmTop.Owner = frmBack;
            frmTop.StartPosition = FormStartPosition.Manual;
            frmTop.LocationChanged += (o, args) => { frmBack.Location = frmTop.Location; };

            // Åã¥Üµ¡Åé
            frmTop.Show();
            frmBack.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(frmBack!=null)
            {
                frmBack.Close();
                frmBack = null;
            }
            frmBack = new Form();
            sub_FrmForeground = new sub_FrmForeground();
            Show(sub_FrmForeground, this, Color.Yellow, 0.8);

        }
    }
}


sub_FrmForeground.Designer.cs

namespace CS_OnlyWinFormTranslucent
{
    partial class sub_FrmForeground
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <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.label1 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(41, 31);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(287, 38);
            this.label1.TabIndex = 0;
            this.label1.Text = "我是在半透明窗體上";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(86, 108);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(624, 141);
            this.button1.TabIndex = 1;
            this.button1.Text = "我是在半透明窗體上";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // sub_FrmForeground
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(18F, 38F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(128)))));
            this.ClientSize = new System.Drawing.Size(874, 552);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.label1);
            this.Font = new System.Drawing.Font("Microsoft JhengHei UI", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            this.Margin = new System.Windows.Forms.Padding(6);
            this.Name = "sub_FrmForeground";
            this.Text = "sub_FrmForeground";
            this.Load += new System.EventHandler(this.sub_FrmForeground_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private Label label1;
        private Button button1;
    }
}


sub_FrmForeground.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CS_OnlyWinFormTranslucent
{
    public partial class sub_FrmForeground : Form
    {
        #region win32 api
        private const uint WS_EX_LAYERED = 0x80000;
        private const int WS_EX_TRANSPARENT = 0x20;
        private const int GWL_STYLE = (-16);
        private const int GWL_EXSTYLE = (-20);
        private const int LWA_ALPHA = 0;

        [DllImport("user32", EntryPoint = "SetWindowLong")]
        private static extern uint SetWindowLong(
            IntPtr hwnd,
            int nIndex,
            uint dwNewLong
        );

        [DllImport("user32", EntryPoint = "GetWindowLong")]
        private static extern uint GetWindowLong(
            IntPtr hwnd,
            int nIndex
        );

        [DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")]
        private static extern int SetLayeredWindowAttributes(
            IntPtr hwnd,
            int crKey,
            int bAlpha,
            int dwFlags
        );
        #endregion

        public void SetPenetrate()
        {
            GetWindowLong(this.Handle, GWL_EXSTYLE);
            SetWindowLong(this.Handle, GWL_EXSTYLE, WS_EX_TRANSPARENT | WS_EX_LAYERED);
        }

        public sub_FrmForeground()
        {
            InitializeComponent();

        }

        private void sub_FrmForeground_Load(object sender, EventArgs e)
        {
            // 設置背景全透明
            this.BackColor = Color.AliceBlue;
            this.TransparencyKey = Color.AliceBlue;

            // 設置窗體鼠標穿透
            //SetPenetrate();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}


結果圖:

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *