C#创建不规则美化窗体

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 notify
{
    public partial class FormMain : Form
    {
        PictureBox LeftTitleBmp = new PictureBox();
        PictureBox MidTitleBmp = new PictureBox();
        PictureBox RightTitleBmp = new PictureBox();
        PictureBox LogoBmp = new PictureBox();
        PictureBox CloseBmp = new PictureBox();
        PictureBox MaxBmp = new PictureBox();
        PictureBox MinBmp = new PictureBox();
        PictureBox TopMostBmp = new PictureBox();
        PictureBox BackGroundBmp = new PictureBox();
       
        ToolTip MyToolTip = new ToolTip();

        // 定义用来移动不规则窗体的相关参数
        // 记录鼠标指针的坐标
        private Point mouseOffset;
        // 记录鼠标按键是否按下 
        private bool isMouseDown = false;

        // 窗体初始大小
        int WinWidth = Convert.ToInt32(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width * 0.625);
        int WinHeight = Convert.ToInt32(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height * 0.625);
        // 是否最大化
        private bool isMax = false;

        public FormMain()
        {
            InitializeComponent();
        }

        protected override void  OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            Text = "Microsoft Visual C#";
           
            BackColor = SystemColors.Desktop;
            TransparencyKey = SystemColors.Desktop;
            FormBorderStyle = FormBorderStyle.None;
            ShowInTaskbar = false;
            Width = WinWidth;
            Height = WinHeight;

            Controls.Add(LeftTitleBmp);
            Controls.Add(MidTitleBmp);
            Controls.Add(RightTitleBmp);
            Controls.Add(LogoBmp);
            Controls.Add(CloseBmp);
            Controls.Add(MaxBmp);
            Controls.Add(MinBmp);
            Controls.Add(TopMostBmp);
            Controls.Add(BackGroundBmp);

            MidTitleBmp.MouseDoubleClick += new MouseEventHandler(MidTitleBmp_MouseDoubleClick);
            MidTitleBmp.MouseDown += new MouseEventHandler(MidTitleBmp_MouseDown);
            MidTitleBmp.MouseMove += new MouseEventHandler(MidTitleBmp_MouseMove);
            MidTitleBmp.MouseUp += new MouseEventHandler(MidTitleBmp_MouseUp);
            MidTitleBmp.Paint += new PaintEventHandler(MidTitleBmp_Paint);


            CloseBmp.MouseHover += new EventHandler(CloseBmp_MouseHover);
            CloseBmp.MouseLeave += new EventHandler(CloseBmp_MouseLeave);
            CloseBmp.MouseDown += new MouseEventHandler(CloseBmp_MouseDown);

            MaxBmp.MouseHover += new EventHandler(MaxBmp_MouseHover);
            MaxBmp.MouseLeave += new EventHandler(MaxBmp_MouseLeave);
            MaxBmp.MouseDown += new MouseEventHandler(MaxBmp_MouseDown);

            MinBmp.MouseHover += new EventHandler(MinBmp_MouseHover);
            MinBmp.MouseLeave += new EventHandler(MinBmp_MouseLeave);
            MinBmp.MouseDown += new MouseEventHandler(MinBmp_MouseDown);

            TopMostBmp.MouseHover += new EventHandler(TopMostBmp_MouseHover);
            TopMostBmp.MouseLeave += new EventHandler(TopMostBmp_MouseLeave);
            TopMostBmp.MouseDown += new MouseEventHandler(TopMostBmp_MouseDown);


            WinInitSkin();

            //WinNotifyIcon();

            ShowToolTip();
        }

        public void ShowToolTip()
        {
            MyToolTip.AutoPopDelay = 5000;
            MyToolTip.InitialDelay = 1000;
            MyToolTip.ReshowDelay = 500;
            MyToolTip.ShowAlways = true;

            //MyToolTip.SetToolTip(CloseBmp,"Close");
            //MyToolTip.SetToolTip(MaxBmp, "Max");
            //MyToolTip.SetToolTip(MinBmp, "Min");
            //MyToolTip.SetToolTip(TopMostBmp, "UnTopMost");
        }

        public void WinInitSkin()
        {
            LeftTitleBmp.Image = Image.FromFile(@"skin/public_board_title_left.bmp");
            LeftTitleBmp.SizeMode = PictureBoxSizeMode.AutoSize;
            LeftTitleBmp.Location = new Point(0, 0);
                       
            MidTitleBmp.Image = Image.FromFile(@"skin/public_board_title_mid.bmp");
            MidTitleBmp.SizeMode = PictureBoxSizeMode.AutoSize;
            MidTitleBmp.Location = new Point(0, 0);
            MidTitleBmp.Width = Screen.PrimaryScreen.Bounds.Width * 2;
            MidTitleBmp.SizeMode = PictureBoxSizeMode.StretchImage;
            MidTitleBmp.SendToBack();
                      
            RightTitleBmp.Image = Image.FromFile(@"skin/public_board_title_right.bmp");
            RightTitleBmp.SizeMode = PictureBoxSizeMode.AutoSize;
            RightTitleBmp.Location = new Point(ClientSize.Width - RightTitleBmp.Width, 0);
            RightTitleBmp.BringToFront();

            LogoBmp.Image = Bitmap.FromFile(@"skin/logo.ico");
            LogoBmp.SizeMode = PictureBoxSizeMode.AutoSize;
            LogoBmp.Location = new Point(LeftTitleBmp.Width, (LeftTitleBmp.Height - LogoBmp.Height) / 2);
            LogoBmp.BringToFront();

            CloseBmp.Image = Image.FromFile(@"skin/public_close_normal.bmp");
            CloseBmp.SizeMode = PictureBoxSizeMode.AutoSize;
            CloseBmp.Location = new Point((ClientSize.Width - CloseBmp.Width), (RightTitleBmp.Height - CloseBmp.Height) / 2);
            CloseBmp.BringToFront();

            MaxBmp.Image = Image.FromFile(@"skin/public_mainicon_max_normal.bmp");
            MaxBmp.SizeMode = PictureBoxSizeMode.AutoSize;
            MaxBmp.Location = new Point((CloseBmp.Location.X - MaxBmp.Width), CloseBmp.Location.Y);
            MaxBmp.BringToFront();
          
            MinBmp.Image = Image.FromFile(@"skin/public_mainicon_min_normal.bmp");
            MinBmp.SizeMode = PictureBoxSizeMode.AutoSize;
            MinBmp.Location = new Point((MaxBmp.Location.X - MinBmp.Width), MaxBmp.Location.Y);
            MinBmp.BringToFront();

            if (TopMost)
            {
                TopMostBmp.Image = Image.FromFile(@"skin/tosimplebutton_normal.bmp");
            }
            else
            {
                TopMostBmp.Image = Image.FromFile(@"skin/un_topmost_normal.bmp");
            }
            TopMostBmp.SizeMode = PictureBoxSizeMode.AutoSize;
            TopMostBmp.Location = new Point((MinBmp.Location.X - TopMostBmp.Width), MinBmp.Location.Y);
            TopMostBmp.BringToFront();

            BackGroundBmp.Image = Image.FromFile(@"skin/bg02.bmp");
            BackGroundBmp.SizeMode = PictureBoxSizeMode.AutoSize;
            BackGroundBmp.Location = new Point(0, LeftTitleBmp.Height);
            BackGroundBmp.SizeMode = PictureBoxSizeMode.Normal;
            BackGroundBmp.Width = ClientSize.Width;
            BackGroundBmp.Height = ClientSize.Height - LeftTitleBmp.Height;
            BackGroundBmp.SizeMode = PictureBoxSizeMode.StretchImage;
            BackGroundBmp.BringToFront();


        }

        void MidTitleBmp_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            MaxBmp_MouseDown(sender,e);
        }

        void MidTitleBmp_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawString(Text, new Font("Verdana", 10), Brushes.White, new PointF(50.0F, (MidTitleBmp.Height - Font.Height)/2.0F));
            e.Graphics.Flush();
            e.Dispose();
        }

        void TopMostBmp_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (TopMost)
                {
                    TopMostBmp.Image = Image.FromFile(@"skin/tosimplebutton_down.bmp");
                    //TopMostBmp.Image = Image.FromFile(@"skin/un_topmost_down.bmp");
                    TopMost = false;
                }
                else
                {
                    TopMostBmp.Image = Image.FromFile(@"skin/un_topmost_down.bmp");
                    //TopMostBmp.Image = Image.FromFile(@"skin/tosimplebutton_down.bmp");
                    TopMost = true;
                }
            }
        }

        void TopMostBmp_MouseLeave(object sender, EventArgs e)
        {
            if (TopMost)
            {
                TopMostBmp.Image = Image.FromFile(@"skin/tosimplebutton_normal.bmp");
            }
            else
            {
                TopMostBmp.Image = Image.FromFile(@"skin/un_topmost_normal.bmp");
            }
        }

        void TopMostBmp_MouseHover(object sender, EventArgs e)
        {
            if (TopMost)
            {
                TopMostBmp.Image = Image.FromFile(@"skin/tosimplebutton_hover.bmp");
                MyToolTip.SetToolTip(TopMostBmp, "UnTopMost");
            }
            else
            {
                TopMostBmp.Image = Image.FromFile(@"skin/un_topmost_hover.bmp");
                MyToolTip.SetToolTip(TopMostBmp, "TopMost");
            }
        }

        void MinBmp_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                MinBmp.Image = Image.FromFile(@"skin/public_mainicon_min_down.bmp");
                WindowState = FormWindowState.Minimized;
            }
        }

        void MinBmp_MouseLeave(object sender, EventArgs e)
        {
            MinBmp.Image = Image.FromFile(@"skin/public_mainicon_min_normal.bmp");
        }

        void MinBmp_MouseHover(object sender, EventArgs e)
        {
            MinBmp.Image = Image.FromFile(@"skin/public_mainicon_min_hover.bmp");
            MyToolTip.SetToolTip(MinBmp, "Min");
        }

        void MaxBmp_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (isMax)
                {
                    MaxBmp.Image = Image.FromFile(@"skin/public_mainicon_origin_down.bmp");
                    Width = WinWidth;
                    Height = WinHeight;
                    Location = new Point(0, 0);
                    WinInitSkin();
                    isMax = false;
                }
                else
                {
                    MaxBmp.Image = Image.FromFile(@"skin/public_mainicon_max_down.bmp");
                    Width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
                    Height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
                    Location = new Point(0, 0);
                    WinInitSkin();
                    isMax = true;
                }
            }
        }

        void MaxBmp_MouseLeave(object sender, EventArgs e)
        {
            if (isMax)
            {
                MaxBmp.Image = Image.FromFile(@"skin/public_mainicon_origin_normal.bmp");
            }
            else
            {
                MaxBmp.Image = Image.FromFile(@"skin/public_mainicon_max_normal.bmp");
            }
        }

        void MaxBmp_MouseHover(object sender, EventArgs e)
        {
            if (isMax)
            {
                MaxBmp.Image = Image.FromFile(@"skin/public_mainicon_origin_hover.bmp");
                MyToolTip.SetToolTip(MaxBmp, "Normal");
            }
            else
            {
                MaxBmp.Image = Image.FromFile(@"skin/public_mainicon_max_hover.bmp");
                MyToolTip.SetToolTip(MaxBmp, "Max");
            }
        }

        void CloseBmp_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                CloseBmp.Image = Image.FromFile(@"skin/public_close_down.bmp");
                this.Close();
            }
        }

        void CloseBmp_MouseLeave(object sender, EventArgs e)
        {
            CloseBmp.Image = Image.FromFile(@"skin/public_close_normal.bmp");
        }

        void CloseBmp_MouseHover(object sender, EventArgs e)
        {
            CloseBmp.Image = Image.FromFile(@"skin/public_close_hover.bmp");
            MyToolTip.SetToolTip(CloseBmp, "Close");
        }

        // Mid_Title_Bmp MouseUp / MouseMove / MouseDown 事件用来处理窗体移动
        void MidTitleBmp_MouseUp(object sender, MouseEventArgs e)
        {
            // 修改鼠标状态isMouseDown的值 // 确保只有鼠标左键按下并移动时,才移动窗体
            if (e.Button == MouseButtons.Left)
            {
                isMouseDown = false;
            }
        }

        void MidTitleBmp_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown)
            {
                Point mousePos = Control.MousePosition;
                mousePos.Offset(mouseOffset.X, mouseOffset.Y);
                Location = mousePos;
            }
        }

        void MidTitleBmp_MouseDown(object sender, MouseEventArgs e)
        {
            int xOffset;
            int yOffset;
            if (e.Button == MouseButtons.Left)
            {
                xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
                yOffset = -e.Y - SystemInformation.CaptionHeight -
                SystemInformation.FrameBorderSize.Height;
                mouseOffset = new Point(xOffset, yOffset);
                isMouseDown = true;
            }
        }

        // NotifyIcon
        private void WinNotifyIcon()
        {
            NotifyIcon MyNotifyIcon = new NotifyIcon();
            MyNotifyIcon.Icon = new Icon(@"skin/logo.ico");
            MyNotifyIcon.Visible = true;

            ContextMenuStrip MyNotifyContextMenuStrip = new ContextMenuStrip();
            for (int i = 0; i < 5; i++)
            {
                MyNotifyContextMenuStrip.Items.AddRange(new ToolStripItem[] { new ToolStripMenuItem(i.ToString()) });
                MyNotifyContextMenuStrip.Items[i].Image = Image.FromFile(@"skin/logo.ico");
            }
            MyNotifyIcon.ContextMenuStrip = MyNotifyContextMenuStrip;

            MyNotifyContextMenuStrip.ItemClicked += new ToolStripItemClickedEventHandler(MyNotifyContextMenuStrip_ItemClicked);
        }

        void MyNotifyContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            FormMain fm = new FormMain();
            fm.Show();
        }
    }
}
 
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值