C#自定义窗体更换皮肤的方法:创建特殊窗体

目录

1.窗体更换皮肤

2.实例

(1)图片资源管理器Resources.Designer.cs设计

(2)Form1.Designer.cs设计

(3)Form1.cs设计

(4) 生成效果

(5)一个遗憾


1.窗体更换皮肤

        要在Windows窗体应用程序中更换皮肤,通常需要修改窗体的外观,包括其颜色、字体、鼠标光标和非客户区(如窗口边框、最小化和最大化按钮等)。这可以通过创建一个自定义窗体或使用第三方库来实现。

2.实例

        在本实例的自定义窗体中单击鼠标右键,将弹出一个用于更换窗体皮肤的快捷菜单,选择“换皮肤”菜单下的任意子菜单:紫色、蓝色或绿色,程序将为当前窗体更换皮肤。

(1)图片资源管理器Resources.Designer.cs设计

        项目之初,先为项目准备好皮肤所需要的图片资源,并把图片设计到Resources.Designer.cs中,具体的设计方法参考本文作者写的其他文章:C#手动改变自制窗体的大小-CSDN博客 https://wenchm.blog.csdn.net/article/details/137027140

//------------------------------------------------------------------------------
// <auto-generated>
//     此代码由工具生成。
//     运行时版本:4.0.30319.42000
//
//     对此文件的更改可能会导致不正确的行为,并且如果
//     重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------

namespace _187.Properties {
    using System;
    
    
    /// <summary>
    ///   一个强类型的资源类,用于查找本地化的字符串等。
    /// </summary>
    // 此类是由 StronglyTypedResourceBuilder
    // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。
    // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen
    // (以 /str 作为命令选项),或重新生成 VS 项目。
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    internal class Resources {
        
        private static global::System.Resources.ResourceManager resourceMan;
        
        private static global::System.Globalization.CultureInfo resourceCulture;
        
        [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        internal Resources() {
        }
        
        /// <summary>
        ///   返回此类使用的缓存的 ResourceManager 实例。
        /// </summary>
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        internal static global::System.Resources.ResourceManager ResourceManager {
            get {
                if (object.ReferenceEquals(resourceMan, null)) {
                    global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("_187.Properties.Resources", typeof(Resources).Assembly);
                    resourceMan = temp;
                }
                return resourceMan;
            }
        }
        
        /// <summary>
        ///   重写当前线程的 CurrentUICulture 属性,对
        ///   使用此强类型资源类的所有资源查找执行重写。
        /// </summary>
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        internal static global::System.Globalization.CultureInfo Culture {
            get {
                return resourceCulture;
            }
            set {
                resourceCulture = value;
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap background {
            get {
                object obj = ResourceManager.GetObject("background", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap background1 {
            get {
                object obj = ResourceManager.GetObject("background1", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap background2 {
            get {
                object obj = ResourceManager.GetObject("background2", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap bottom {
            get {
                object obj = ResourceManager.GetObject("bottom", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap bottom1 {
            get {
                object obj = ResourceManager.GetObject("bottom1", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap bottom2 {
            get {
                object obj = ResourceManager.GetObject("bottom2", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap close {
            get {
                object obj = ResourceManager.GetObject("close", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap close1 {
            get {
                object obj = ResourceManager.GetObject("close1", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap close2 {
            get {
                object obj = ResourceManager.GetObject("close2", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap left {
            get {
                object obj = ResourceManager.GetObject("left", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap left1 {
            get {
                object obj = ResourceManager.GetObject("left1", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap left2 {
            get {
                object obj = ResourceManager.GetObject("left2", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap max {
            get {
                object obj = ResourceManager.GetObject("max", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap max_Normal {
            get {
                object obj = ResourceManager.GetObject("max_Normal", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap max_normal1 {
            get {
                object obj = ResourceManager.GetObject("max_normal1", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap max_normal2 {
            get {
                object obj = ResourceManager.GetObject("max_normal2", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap max1 {
            get {
                object obj = ResourceManager.GetObject("max1", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap max2 {
            get {
                object obj = ResourceManager.GetObject("max2", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap menu {
            get {
                object obj = ResourceManager.GetObject("menu", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap menu1 {
            get {
                object obj = ResourceManager.GetObject("menu1", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap menu2 {
            get {
                object obj = ResourceManager.GetObject("menu2", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap min {
            get {
                object obj = ResourceManager.GetObject("min", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap min1 {
            get {
                object obj = ResourceManager.GetObject("min1", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap min2 {
            get {
                object obj = ResourceManager.GetObject("min2", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap right {
            get {
                object obj = ResourceManager.GetObject("right", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap right1 {
            get {
                object obj = ResourceManager.GetObject("right1", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap right2 {
            get {
                object obj = ResourceManager.GetObject("right2", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap top {
            get {
                object obj = ResourceManager.GetObject("top", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap top1 {
            get {
                object obj = ResourceManager.GetObject("top1", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap top2 {
            get {
                object obj = ResourceManager.GetObject("top2", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
    }
}

(2)Form1.Designer.cs设计

         开始窗体设计,自定义窗体承载皮肤图片的panel控件设计,最小化、最大化、关闭,menuStrip1菜单设计,最重要的用于更换皮肤的ContextMenuStrip控件。

        在C#中,ContextMenuStrip控件是用于显示右键菜单的,而ToolStripMenuItem是该菜单中的一个项目。 对于ContextMenuStrip的DropDownItems,它们实际上是ToolStripItem的子类。设计三个ToolStripMenuItem菜单,用于切换不同的皮肤。

namespace _187
{
    partial class Form1
    {
        /// <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()
        {
            components = new System.ComponentModel.Container();
            panel1_Title = new Panel();
            pictureBox3_Min = new PictureBox();
            pictureBox2_Max = new PictureBox();
            pictureBox1_Close = new PictureBox();
            panel4_Bottom = new Panel();
            panel5_Left = new Panel();
            panel2_LCorner = new Panel();
            panel6_Right = new Panel();
            panel3_RCorner = new Panel();
            menuStrip1 = new MenuStrip();
            toolStripMenuItem2 = new ToolStripMenuItem();
            toolStripMenuItem3 = new ToolStripMenuItem();
            toolStripMenuItem4 = new ToolStripMenuItem();
            toolStripMenuItem5 = new ToolStripMenuItem();
            toolStripMenuItem6 = new ToolStripMenuItem();
            toolStripMenuItem1 = new ToolStripMenuItem();
            contextMenuStrip1 = new ContextMenuStrip(components);
            toolStripMenuItem7 = new ToolStripMenuItem();
            toolStripMenuItem9 = new ToolStripMenuItem();
            toolStripMenuItem10 = new ToolStripMenuItem();
            toolStripMenuItem11 = new ToolStripMenuItem();
            toolStripMenuItem8 = new ToolStripMenuItem();
            panel1_Title.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)pictureBox3_Min).BeginInit();
            ((System.ComponentModel.ISupportInitialize)pictureBox2_Max).BeginInit();
            ((System.ComponentModel.ISupportInitialize)pictureBox1_Close).BeginInit();
            panel5_Left.SuspendLayout();
            panel6_Right.SuspendLayout();
            menuStrip1.SuspendLayout();
            contextMenuStrip1.SuspendLayout();
            SuspendLayout();
            // 
            // panel1_Title
            // 
            panel1_Title.BackgroundImageLayout = ImageLayout.Stretch;
            panel1_Title.Controls.Add(pictureBox3_Min);
            panel1_Title.Controls.Add(pictureBox2_Max);
            panel1_Title.Controls.Add(pictureBox1_Close);
            panel1_Title.Dock = DockStyle.Top;
            panel1_Title.Location = new Point(0, 0);
            panel1_Title.Name = "panel1_Title";
            panel1_Title.Size = new Size(530, 31);
            panel1_Title.TabIndex = 0;
            panel1_Title.DoubleClick += Panel1_Title_DoubleClick;
            panel1_Title.MouseDown += Panel1_Title_MouseDown;
            panel1_Title.MouseMove += Panel1_Title_MouseMove;
            panel1_Title.MouseUp += Panel1_Title_MouseUp;
            // 
            // pictureBox3_Min
            // 
            pictureBox3_Min.Anchor = AnchorStyles.Top | AnchorStyles.Right;
            pictureBox3_Min.BackColor = Color.Transparent;
            pictureBox3_Min.Location = new Point(443, 1);
            pictureBox3_Min.Name = "pictureBox3_Min";
            pictureBox3_Min.Size = new Size(29, 29);
            pictureBox3_Min.TabIndex = 2;
            pictureBox3_Min.TabStop = false;
            pictureBox3_Min.Click += PictureBox3_Min_Click;
            // 
            // pictureBox2_Max
            // 
            pictureBox2_Max.Anchor = AnchorStyles.Top | AnchorStyles.Right;
            pictureBox2_Max.BackColor = Color.Transparent;
            pictureBox2_Max.Location = new Point(472, 1);
            pictureBox2_Max.Name = "pictureBox2_Max";
            pictureBox2_Max.Size = new Size(29, 29);
            pictureBox2_Max.TabIndex = 1;
            pictureBox2_Max.TabStop = false;
            pictureBox2_Max.Click += PictureBox2_Max_Click;
            // 
            // pictureBox1_Close
            // 
            pictureBox1_Close.Anchor = AnchorStyles.Top | AnchorStyles.Right;
            pictureBox1_Close.BackColor = Color.Transparent;
            pictureBox1_Close.Location = new Point(501, 1);
            pictureBox1_Close.Name = "pictureBox1_Close";
            pictureBox1_Close.Size = new Size(29, 29);
            pictureBox1_Close.TabIndex = 0;
            pictureBox1_Close.TabStop = false;
            pictureBox1_Close.Click += PictureBox1_Close_Click;
            // 
            // panel4_Bottom
            // 
            panel4_Bottom.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
            panel4_Bottom.Location = new Point(8, 402);
            panel4_Bottom.Name = "panel4_Bottom";
            panel4_Bottom.Size = new Size(514, 8);
            panel4_Bottom.TabIndex = 3;
            panel4_Bottom.MouseDown += Panel4_Bottom_MouseDown;
            panel4_Bottom.MouseMove += Panel4_Bottom_MouseMove;
            panel4_Bottom.MouseUp += Panel4_Bottom_MouseUp;
            // 
            // panel5_Left
            // 
            panel5_Left.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left;
            panel5_Left.Controls.Add(panel2_LCorner);
            panel5_Left.Location = new Point(0, 31);
            panel5_Left.Name = "panel5_Left";
            panel5_Left.Size = new Size(8, 379);
            panel5_Left.TabIndex = 4;
            panel5_Left.MouseDown += Panel5_Left_MouseDown;
            panel5_Left.MouseMove += Panel5_Left_MouseMove;
            panel5_Left.MouseUp += Panel5_Left_MouseUp;
            // 
            // panel2_LCorner
            // 
            panel2_LCorner.Anchor = AnchorStyles.Bottom | AnchorStyles.Left;
            panel2_LCorner.BackColor = Color.Transparent;
            panel2_LCorner.Location = new Point(0, 371);
            panel2_LCorner.Name = "panel2_LCorner";
            panel2_LCorner.Size = new Size(8, 8);
            panel2_LCorner.TabIndex = 7;
            panel2_LCorner.MouseDown += Panel2_LCorner_MouseDown;
            panel2_LCorner.MouseMove += Panel2_LCorner_MouseMove;
            panel2_LCorner.MouseUp += Panel2_LCorner_MouseUp;
            // 
            // panel6_Right
            // 
            panel6_Right.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right;
            panel6_Right.Controls.Add(panel3_RCorner);
            panel6_Right.Location = new Point(522, 31);
            panel6_Right.Name = "panel6_Right";
            panel6_Right.Size = new Size(8, 379);
            panel6_Right.TabIndex = 5;
            panel6_Right.MouseDown += Panel6_Right_MouseDown;
            panel6_Right.MouseMove += Panel6_Right_MouseMove;
            panel6_Right.MouseUp += Panel6_Right_MouseUp;
            // 
            // panel3_RCorner
            // 
            panel3_RCorner.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            panel3_RCorner.BackColor = Color.Transparent;
            panel3_RCorner.Location = new Point(0, 371);
            panel3_RCorner.Name = "panel3_RCorner";
            panel3_RCorner.Size = new Size(8, 8);
            panel3_RCorner.TabIndex = 7;
            panel3_RCorner.MouseDown += Panel3_RCorner_MouseDown;
            panel3_RCorner.MouseMove += Panel3_RCorner_MouseMove;
            panel3_RCorner.MouseUp += Panel3_RCorner_MouseUp;
            // 
            // menuStrip1
            // 
            menuStrip1.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
            menuStrip1.Dock = DockStyle.None;
            menuStrip1.Items.AddRange(new ToolStripItem[] { toolStripMenuItem2, toolStripMenuItem3, toolStripMenuItem4, toolStripMenuItem5, toolStripMenuItem6, toolStripMenuItem1 });
            menuStrip1.Location = new Point(8, 31);
            menuStrip1.Name = "menuStrip1";
            menuStrip1.Size = new Size(272, 25);
            menuStrip1.TabIndex = 6;
            menuStrip1.Text = "menuStrip1";
            // 
            // toolStripMenuItem2
            // 
            toolStripMenuItem2.Name = "toolStripMenuItem2";
            toolStripMenuItem2.Size = new Size(44, 21);
            toolStripMenuItem2.Text = "编辑";
            // 
            // toolStripMenuItem3
            // 
            toolStripMenuItem3.Name = "toolStripMenuItem3";
            toolStripMenuItem3.Size = new Size(44, 21);
            toolStripMenuItem3.Text = "视图";
            // 
            // toolStripMenuItem4
            // 
            toolStripMenuItem4.Name = "toolStripMenuItem4";
            toolStripMenuItem4.Size = new Size(44, 21);
            toolStripMenuItem4.Text = "工具";
            // 
            // toolStripMenuItem5
            // 
            toolStripMenuItem5.Name = "toolStripMenuItem5";
            toolStripMenuItem5.Size = new Size(44, 21);
            toolStripMenuItem5.Text = "窗口";
            // 
            // toolStripMenuItem6
            // 
            toolStripMenuItem6.Name = "toolStripMenuItem6";
            toolStripMenuItem6.Size = new Size(44, 21);
            toolStripMenuItem6.Text = "帮助";
            // 
            // toolStripMenuItem1
            // 
            toolStripMenuItem1.Name = "toolStripMenuItem1";
            toolStripMenuItem1.Size = new Size(44, 21);
            toolStripMenuItem1.Text = "文件";
            // 
            // contextMenuStrip1
            // 
            contextMenuStrip1.Items.AddRange(new ToolStripItem[] { toolStripMenuItem7, toolStripMenuItem8 });
            contextMenuStrip1.Name = "contextMenuStrip1";
            contextMenuStrip1.Size = new Size(181, 70);
            // 
            // toolStripMenuItem7
            // 
            toolStripMenuItem7.AutoSize = false;
            toolStripMenuItem7.DropDownItems.AddRange(new ToolStripItem[] { toolStripMenuItem9, toolStripMenuItem10, toolStripMenuItem11 });
            toolStripMenuItem7.Name = "toolStripMenuItem7";
            toolStripMenuItem7.Size = new Size(180, 22);
            toolStripMenuItem7.Text = "换皮肤";
            // 
            // toolStripMenuItem9
            // 
            toolStripMenuItem9.AutoSize = false;
            toolStripMenuItem9.BackgroundImageLayout = ImageLayout.None;
            toolStripMenuItem9.Name = "toolStripMenuItem9";
            toolStripMenuItem9.Size = new Size(180, 22);
            toolStripMenuItem9.Text = "蓝色经典";
            toolStripMenuItem9.Click += ToolStripMenuItem9_Click;
            // 
            // toolStripMenuItem10
            // 
            toolStripMenuItem10.AutoSize = false;
            toolStripMenuItem10.Name = "toolStripMenuItem10";
            toolStripMenuItem10.Size = new Size(180, 22);
            toolStripMenuItem10.Text = "绿色家园";
            toolStripMenuItem10.Click += ToolStripMenuItem10_Click;
            // 
            // toolStripMenuItem11
            // 
            toolStripMenuItem11.AutoSize = false;
            toolStripMenuItem11.Name = "toolStripMenuItem11";
            toolStripMenuItem11.Size = new Size(180, 22);
            toolStripMenuItem11.Text = "紫色小花";
            toolStripMenuItem11.Click += ToolStripMenuItem11_Click;
            // 
            // toolStripMenuItem8
            // 
            toolStripMenuItem8.Name = "toolStripMenuItem8";
            toolStripMenuItem8.Size = new Size(180, 22);
            toolStripMenuItem8.Text = "退出";
            toolStripMenuItem8.Click += ToolStripMenuItem8_Click;
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(530, 410);
            ContextMenuStrip = contextMenuStrip1;
            Controls.Add(panel6_Right);
            Controls.Add(panel5_Left);
            Controls.Add(panel4_Bottom);
            Controls.Add(panel1_Title);
            Controls.Add(menuStrip1);
            FormBorderStyle = FormBorderStyle.None;
            MainMenuStrip = menuStrip1;
            Name = "Form1";
            StartPosition = FormStartPosition.CenterScreen;
            Text = "窗体换肤";
            Activated += Form1_Activated;
            Load += Form1_Load;
            panel1_Title.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)pictureBox3_Min).EndInit();
            ((System.ComponentModel.ISupportInitialize)pictureBox2_Max).EndInit();
            ((System.ComponentModel.ISupportInitialize)pictureBox1_Close).EndInit();
            panel5_Left.ResumeLayout(false);
            panel6_Right.ResumeLayout(false);
            menuStrip1.ResumeLayout(false);
            menuStrip1.PerformLayout();
            contextMenuStrip1.ResumeLayout(false);
            ResumeLayout(false);
            PerformLayout();
        }

        #endregion

        private Panel panel1_Title;
        private PictureBox pictureBox1_Close;
        private PictureBox pictureBox3_Min;
        private PictureBox pictureBox2_Max;
        private Panel panel4_Bottom;
        private Panel panel5_Left;
        private Panel panel6_Right;
        private MenuStrip menuStrip1;
        private ToolStripMenuItem toolStripMenuItem1;
        private ToolStripMenuItem toolStripMenuItem2;
        private ToolStripMenuItem toolStripMenuItem3;
        private ToolStripMenuItem toolStripMenuItem4;
        private ToolStripMenuItem toolStripMenuItem5;
        private ToolStripMenuItem toolStripMenuItem6;
        private ContextMenuStrip contextMenuStrip1;
        private ToolStripMenuItem toolStripMenuItem7;
        private ToolStripMenuItem toolStripMenuItem9;
        private ToolStripMenuItem toolStripMenuItem10;
        private ToolStripMenuItem toolStripMenuItem11;
        private ToolStripMenuItem toolStripMenuItem8;
        private Panel panel2_LCorner;
        private Panel panel3_RCorner;
    }
}

(3)Form1.cs设计

        所有控件的方法,都在这里设计。这个实例不仅仅能换肤,还能实现窗体最小化、最大化、关闭、鼠标拖动各个边框改变窗体大小;鼠标拖动左下角、右下角同时改变窗体的长和宽;

namespace _187
{
    public partial class Form1 : Form
    {
        int top, left, height, width;
        bool bol = false, bo = false, bolTop = false, bolLeft = false, bolRight = false, bolBottom = false, bolLeftCornu = false, bolRightCornu = false;
        int x = 0, y = 0;

        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 默认肤色绿色家园
        /// </summary>
        private void Form1_Load(object sender, EventArgs e)
        {
            ToolStripMenuItem10_Click(sender, e);
            contextMenuStrip1.AutoSize = false; // 禁用自动调整大小
            toolStripMenuItem9.AutoSize = false;
            toolStripMenuItem10.AutoSize = false;
            toolStripMenuItem11.AutoSize = false;

        }
        /// <summary>
        /// 关闭窗体
        /// </summary>
        private void PictureBox1_Close_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        /// <summary>
        /// 最大化窗体及设置其图标
        /// </summary>
        private void PictureBox2_Max_Click(object sender, EventArgs e)
        {
            if (!bol)
            {
                top = Top;
                left = Left;
                height = Height;
                width = Width;
                Top = 0;
                Left = 0;
                int hg = SystemInformation.MaxWindowTrackSize.Height;
                int wh = SystemInformation.MaxWindowTrackSize.Width;
                Height = hg;
                Width = wh;
                bol = true;
                if (toolStripMenuItem9.Checked)
                    pictureBox2_Max.Image = Properties.Resources.max;
                if (toolStripMenuItem10.Checked)
                    pictureBox2_Max.Image = Properties.Resources.max1;
                if (toolStripMenuItem11.Checked)
                    pictureBox2_Max.Image = Properties.Resources.max2;
            }
            else
            {
                Top = top;
                Left = left;
                Height = height;
                Width = width;
                bol = false;
                if (toolStripMenuItem9.Checked)
                    pictureBox2_Max.Image = Properties.Resources.max_Normal;
                if (toolStripMenuItem10.Checked)
                    pictureBox2_Max.Image = Properties.Resources.max_normal1;
                if (toolStripMenuItem11.Checked)
                    pictureBox2_Max.Image = Properties.Resources.max_normal2;
            }
        }
        /// <summary>
        /// 最小化窗体
        /// </summary>
        private void PictureBox3_Min_Click(object sender, EventArgs e)
        {
            top = Top;
            left = Left;
            height = Height;
            width = Width;
            Height = 0;
            Width = 0;
            bo = true;
        }
        /// <summary>
        /// 窗体被再次激活
        /// </summary>
        private void Form1_Activated(object sender, EventArgs e)
        {
            if (bo)
            {
                Top = top;
                Left = left;
                Height = height;
                Width = width;
                bo = false;
            }
        }
        /// <summary>
        /// 上边框鼠标按下
        /// </summary>
        private void Panel1_Title_MouseDown(object sender, MouseEventArgs e)
        {
            x = e.X;
            y = e.Y;
            bolTop = true;
        }
        /// <summary>
        /// 上边框鼠标移动
        /// </summary>
        private void Panel1_Title_MouseMove(object sender, MouseEventArgs e)
        {
            if (bolTop)
            {
                Top += e.Y - y;
                Left += e.X - x;
            }
        }
        /// <summary>
        /// 上边框鼠标释放
        /// </summary>
        private void Panel1_Title_MouseUp(object sender, MouseEventArgs e)
        {
            bolTop = false;
        }
        /// <summary>
        /// 左边框鼠标按下
        /// </summary>
        private void Panel5_Left_MouseDown(object sender, MouseEventArgs e)
        {
            x = e.X;
            bolLeft = true;
        }
        /// <summary>
        /// 左边框鼠标移动
        /// </summary>
        private void Panel5_Left_MouseMove(object sender, MouseEventArgs e)
        {
            if (bolLeft)
            {
                Width += x - e.X;
                Left += e.X - x;
            }
        }
        /// <summary>
        /// 左边框鼠标释放
        /// </summary>
        private void Panel5_Left_MouseUp(object sender, MouseEventArgs e)
        {
            bolLeft = false;
        }
        /// <summary>
        /// 右边框鼠标按下
        /// </summary>
        private void Panel6_Right_MouseDown(object sender, MouseEventArgs e)
        {
            x = e.X;
            bolRight = true;
        }
        /// <summary>
        /// 右边框鼠标移动
        /// </summary>
        private void Panel6_Right_MouseMove(object sender, MouseEventArgs e)
        {
            if (bolRight)
            {
                Width += e.X - x;
            }
        }
        /// <summary>
        /// 右边框鼠标释放
        /// </summary>
        private void Panel6_Right_MouseUp(object sender, MouseEventArgs e)
        {
            bolRight = false;
        }
        /// <summary>
        /// 下边框鼠标按下
        /// </summary>
        private void Panel4_Bottom_MouseDown(object sender, MouseEventArgs e)
        {
            y = e.Y;
            bolBottom = true;
        }
        /// <summary>
        /// 下边框鼠标移动
        /// </summary>
        private void Panel4_Bottom_MouseMove(object sender, MouseEventArgs e)
        {
            if (bolBottom)
            {
                Height += e.Y - y;
            }
        }
        /// <summary>
        /// 下边框鼠标释放
        /// </summary>
        private void Panel4_Bottom_MouseUp(object sender, MouseEventArgs e)
        {
            bolBottom = false;
        }
        /// <summary>
        /// 左下角鼠标按下
        /// </summary>
        private void Panel2_LCorner_MouseDown(object sender, MouseEventArgs e)
        {
            x = e.X;
            y = e.Y;
            bolLeftCornu = true;
        }
        /// <summary>
        /// 左下角鼠标移动
        /// </summary>
        private void Panel2_LCorner_MouseMove(object sender, MouseEventArgs e)
        {
            if (bolLeftCornu)
            {
                Width += x - e.X;
                Left += e.X - x;
                Height += e.Y - y;
            }
        }
        /// <summary>
        /// 左下角鼠标释放
        /// </summary>
        private void Panel2_LCorner_MouseUp(object sender, MouseEventArgs e)
        {
            x = e.X;
            y = e.Y;
            bolLeftCornu = false;
        }
        /// <summary>
        /// 右下角鼠标按下
        /// </summary>
        private void Panel3_RCorner_MouseDown(object sender, MouseEventArgs e)
        {
            bolRightCornu = true;
        }
        /// <summary>
        /// 右下角鼠标移动
        /// </summary>
        private void Panel3_RCorner_MouseMove(object sender, MouseEventArgs e)
        {
            if (bolRightCornu)
            {
                Width += e.X - x;
                Height += e.Y - y;
            }
        }
        /// <summary>
        /// 右下角鼠标释放
        /// </summary>
        private void Panel3_RCorner_MouseUp(object sender, MouseEventArgs e)
        {
            bolRightCornu = false;
        }
        /// <summary>
        /// ContextMenuSrip控件选择退出
        /// </summary>
        private void ToolStripMenuItem8_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        /// <summary>
        /// ContextMenuSrip控件选择肤色:蓝色经典
        /// </summary>
        private void ToolStripMenuItem9_Click(object sender, EventArgs e)
        {
            panel1_Title.BackgroundImage = Properties.Resources.top; 
            panel5_Left.BackgroundImage = Properties.Resources.left; 
            panel6_Right.BackgroundImage = Properties.Resources.right; 
            panel4_Bottom.BackgroundImage = Properties.Resources.bottom; 
            pictureBox3_Min.Image = Properties.Resources.min; 
            if (bol == true)
            {
                pictureBox2_Max.Image = Properties.Resources.max;
            }
            else
            {
                pictureBox2_Max.Image = Properties.Resources.max_Normal;
            }
            pictureBox1_Close.Image = Properties.Resources.close;
            toolStripMenuItem9.Checked = true;
            toolStripMenuItem10.Checked = false;
            toolStripMenuItem11.Checked = false;
            menuStrip1.BackgroundImage = Properties.Resources.menu;
            BackgroundImage = Properties.Resources.background;
        }
        /// <summary>
        /// ContextMenuSrip控件选择肤色:绿色家园
        /// </summary>
        private void ToolStripMenuItem10_Click(object sender, EventArgs e)
        {
            panel1_Title.BackgroundImage = Properties.Resources.top1;
            panel5_Left.BackgroundImage = Properties.Resources.left1;
            panel6_Right.BackgroundImage = Properties.Resources.right1;
            panel4_Bottom.BackgroundImage = Properties.Resources.bottom1;
            pictureBox3_Min.Image = Properties.Resources.min1;
            if (bol == true)
            {
                pictureBox2_Max.Image = Properties.Resources.max1;
            }
            else
            {
                pictureBox2_Max.Image = Properties.Resources.max_normal1;
            }
            pictureBox1_Close.Image = Properties.Resources.close1;
            toolStripMenuItem9.Checked = false;
            toolStripMenuItem10.Checked = true;
            toolStripMenuItem11.Checked = false;
            menuStrip1.BackgroundImage = Properties.Resources.menu1;
            BackgroundImage = Properties.Resources.background1;
        }
        /// <summary>
        /// ContextMenuSrip控件选择肤色:紫色小花
        /// </summary>
        private void ToolStripMenuItem11_Click(object sender, EventArgs e)
        {
            panel1_Title.BackgroundImage = Properties.Resources.top2;
            panel5_Left.BackgroundImage = Properties.Resources.left2;
            panel6_Right.BackgroundImage = Properties.Resources.right2;
            panel4_Bottom.BackgroundImage = Properties.Resources.bottom2;
            pictureBox3_Min.Image = Properties.Resources.min2;
            if (bol == true)
            {
                pictureBox2_Max.Image = Properties.Resources.max2;
            }
            else
            {
                pictureBox2_Max.Image = Properties.Resources.max_normal2;
            }
            pictureBox1_Close.Image = Properties.Resources.close2;
            toolStripMenuItem9.Checked = false;
            toolStripMenuItem10.Checked = false;
            toolStripMenuItem11.Checked = true;
            menuStrip1.BackgroundImage = Properties.Resources.menu2;
            BackgroundImage = Properties.Resources.background2;
        }
        /// <summary>
        /// 在最大化窗体和正常窗体之间切换
        /// </summary>
        private void Panel1_Title_DoubleClick(object sender, EventArgs e)
        {
            PictureBox2_Max_Click(sender, e);
        }
    }
}

(4) 生成效果

 

 

 

(5)一个遗憾

         本实例有一个缺点,就是窗体左上角显示的切换后的皮肤名称(toolStripMenuItem9、10、11)会随着窗体最大化而被拉伸,字体扭曲了,很难看。我没有解决掉也没时间去解决。有兴趣的网友一旦解决了这个缺陷,请贴出来你用好点子。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/558153.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Git常见命令行操作和IDEA图形化界面操作

设置Git用户名和标签 在安装完Git以后需要设置用户和签名&#xff0c;至于为什么要设置用户签名可以看一下这篇文章【学了就忘】Git基础 — 11.配置Git用户签名说明 - 简书 (jianshu.com) 基本语法&#xff1a; git config --global user.name 用户名 git config --global u…

SpringBoot项目创建及简单使用

目录 一.SpringBoot项目 1.1SpringBoot的介绍 1.2SpringBoot优点 二.SpringBoot项目的创建 三.注意点 一.SpringBoot项目 1.1SpringBoot的介绍 Spring是为了简化Java程序而开发的&#xff0c;那么SpringBoot则是为了简化Spring程序的。 Spring 框架&#xff1a; Spring…

ARM之栈与方法

ARM之栈与方法 计算机中的栈是一种线性表&#xff0c;它被限定只能在一端进行插入和删除操作&#xff08;先进后出&#xff09;。通常将可以插入和删除操作的一端称为栈顶&#xff0c;相对的一端为栈底。 通常栈有递增堆栈&#xff08;向高地址方向生长&#xff09;、递减堆栈…

鸿蒙OpenHarmony【搭建Ubuntu环境】

搭建Ubuntu环境 在嵌入式开发中&#xff0c;很多开发者习惯于使用Windows进行代码的编辑&#xff0c;比如使用Windows的Visual Studio Code进行OpenHarmony代码的开发。但当前阶段&#xff0c;大部分的开发板源码还不支持在Windows环境下进行编译&#xff0c;如Hi3861、Hi3516…

Day37 IO流的操作

Day37 IO流的操作 文章目录 Day37 IO流的操作Java的文件拷贝利用 文件字节输出流 向文件写入数据利用 文件字节输入流 读取文件里的数据利用 带缓冲区的字节输出流 向文件写入数据利用 带有缓冲区的字节输入流 读取文件里的数据利用 字符输出转换流 向文件写入数据利用 字符输入…

Java全套智慧校园系统源码springboot+elmentui +Quartz可视化校园管理平台系统源码 建设智慧校园的5大关键技术

Java全套智慧校园系统源码springbootelmentui Quartz可视化校园管理平台系统源码 建设智慧校园的5大关键技术 智慧校园指的是以物联网为基础的智慧化的校园工作、学习和生活一体化环境&#xff0c;这个一体化环境以各种应用服务系统为载体&#xff0c;将教学、科研、管理和校园…

豆瓣影评信息爬取 (爬虫)

代码块&#xff1a; from lxml import etree import requestsheaders{User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0 }url_list[] for i in range(0,5):i*20urlsf"https:…

day02-新增员工

day01 新增员工业务逻辑整理 EmployeeController.java PostMappingApiOperation("新增员工")public Result save(RequestBody EmployeeDTO employeeDTO){System.out.println("当前线程的ID:" Thread.currentThread().getId());log.info("新增员工&a…

[leetcode] 56. 合并区间

文章目录 题目描述解题方法排序java代码复杂度分析 题目描述 以数组 intervals 表示若干个区间的集合&#xff0c;其中单个区间为 intervals[i] [starti, endi] 。请你合并所有重叠的区间&#xff0c;并返回 一个不重叠的区间数组&#xff0c;该数组需恰好覆盖输入中的所有区…

UWB人员定位系统适用的场景有哪些?​​​​​​​10厘米工业级实时轨迹高精度定位

UWB人员定位系统适用的场景有哪些&#xff1f;10厘米工业级实时轨迹高精度定位 一、应用场景 1、商场与零售领域&#xff1a;商场可以使用UWB人员定位系统来跟踪顾客的行踪&#xff0c;以收集顾客行为数据&#xff0c;为营销策略提供有力支持。帮助商场优化商品布局和陈列&…

在龙梦迷你电脑福珑2.0上使用Fedora 28 龙梦版

在龙梦迷你电脑福珑2.0上使用Fedora 28 龙梦版。这个版本的操作系统ISO文件是&#xff1a;Fedora28_for_loongson_MATE_Live_7.2.iso 。它在功能方面不错。能放音乐&#xff0c;能看cctv直播&#xff0c;有声音&#xff0c;能录屏&#xff0c;能在局域网里用PuTTY的ssh方式连接…

【Java EE】依赖注入DI详解

文章目录 &#x1f334;什么是依赖注入&#x1f340;依赖注入的三种方法&#x1f338;属性注入(Field Injection)&#x1f338;构造方法注入&#x1f338;Setter注入&#x1f338;三种注入优缺点分析 &#x1f333;Autowired存在的问题&#x1f332;解决Autowired对应多个对象问…

dp思维 枚举

题目链接 #include<bits/stdc.h> using namespace std; #define i64 long long const i64 mod 1e9 7; int main() {int n;cin >> n;vector<char>s(n 1);for (int i 1; i < n; i) {cin >> s[i];}//用ans记录所有满足条件的答案数量&#xff0c;c…

SQL增加主键约束的条件

结论 常见认为设为主键的条件为&#xff1a; 值唯一不含空值 具体实施中会出现各种问题 添加主键约束的条件细则&#xff1a; 值唯一数据中不含空值在定义时需要not null约束&#xff08;使用check约束不行&#xff09; 验证实验 接下来我做了关于这个细则的验证实验&am…

万物皆可计算|下一个风口:近内存计算-2

虽然PIM可以有缓解内存墙的问题&#xff0c;但是PIM设计面临着一系列技术和工程上的挑战&#xff0c;这些挑战直接影响着PIM技术的实用化和广泛应用&#xff1a; 地址翻译与操作映射&#xff1a; 在传统计算机体系结构中&#xff0c;地址空间由操作系统管理和调度&#xff0c;通…

万物皆可计算|下一个风口:近内存计算-1

传统的冯诺依曼架构虽然广泛应用于各类计算系统&#xff0c;但其分离的数据存储与处理单元导致了数据传输瓶颈&#xff0c;特别是在处理内存密集型任务时&#xff0c;CPU或GPU需要频繁地从内存中读取数据进行运算&#xff0c;然后再将结果写回内存&#xff0c;这一过程涉及大量…

Vue3:响应式数据的基本使用(ref、reactive)

一、前言 在Vue3中&#xff0c;如果数据不是响应式数据&#xff0c;当数据的值发生改变时&#xff0c;页面上的数据是不会发生改变的。因此本文主要介绍Vue3中响应式数据的使用&#xff0c;包括ref和reactive的基本使用。 二、ref 1、ref —— 创建基本类型的响应式数据 re…

电大搜题微信公众号:重庆开放大学学子的学习利器

在当今信息化时代&#xff0c;学习已经成为每个人不可或缺的一部分。然而&#xff0c;对于重庆开放大学的学子们来说&#xff0c;由于远程教育的特殊性&#xff0c;他们面临着更大的学习挑战。幸运的是&#xff0c;他们现在可以依靠一款强大的学习利器——电大搜题微信公众号&a…

软考中级网络工程师-2024上岸宝典

1.软考是什么 简单说就是计算机技术 相关的国家级证书考试&#xff0c;想听专业点给大家截一张官网的图&#xff0c;不想听废话直接往下。 同为国家级证书的&#xff1a;注册会计师、法律职业资格证、一级建筑师&#xff0c;证书的价值是比较高的。 很多人都是在求职前或者大…

【面试经典 150 | 二叉树层序遍历】二叉树的右视图

文章目录 写在前面Tag题目来源解题思路方法一&#xff1a;层序遍历方法二&#xff1a;深度优先搜索 写在最后 写在前面 本专栏专注于分析与讲解【面试经典150】算法&#xff0c;两到三天更新一篇文章&#xff0c;欢迎催更…… 专栏内容以分析题目为主&#xff0c;并附带一些对于…