C# 自定义窗体(一)

自定义窗体

设置WinForm窗体属性

C# 自定义窗体(一)_第1张图片
1、因为不考虑dpi、和字体得到缩放所以将窗体的缩放模式和窗体边框都设置为None
2、定义窗体标题栏高度
3、定义窗体标题栏边框绘制颜色
4、重写窗体的OnPaint方法:

protected override void OnPaint(PaintEventArgs e) {
        base.OnPaint(e);

        e.Graphics.DrawLine(titlePen,new Point(0,titleHeight),new Point(Width,titleHeight));
    }

可以在属性里面找到OnPaint的方法双击Vs生成相应的代码块;

自定义窗体需要按钮(最大、最小、还原、关闭)

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace form.Controls {
    [DefaultEvent("Clicked")]
    public partial class ImageButton : UserControl {
        private Size imageSize = new Size(24, 24);
        public Size ImageSize {
            get {return imageSize; }
            set {
                if (imageSize != value) {
                    imageSize = value;
                }
            }
        }

        private bool mouseIsDown;
        protected bool MouseIsDown {
            get { return mouseIsDown; }
            set {
                mouseIsDown = value;
                Invalidate();
            }
        }

        private bool mouseIsHover;
        protected bool MouseIsHover {
            get { return mouseIsHover; }
            set {
                mouseIsHover = value;
                Invalidate();
            }
        }

        private Image ImageHover { get; set; }

        private Image ImageNormal { get; set; }

        private Image ImageDown { get; set; }

        public ImageButton() {
            InitializeComponent();
        }

        protected override void OnMouseHover(EventArgs e) {
            base.OnMouseHover(e);
            MouseIsDown = false;
            MouseIsHover = true;
        }

        protected override void OnMouseDown(MouseEventArgs e) {
            base.OnMouseDown(e);
            MouseIsHover = false;
            MouseIsDown = true;
        }

        protected override void OnMouseLeave(EventArgs e) {
            base.OnMouseLeave(e);
            MouseIsHover = MouseIsDown = false;
        }

        protected override void OnPaint(PaintEventArgs e) {
            base.OnPaint(e);
            Image img = MouseIsHover ? ImageHover : (MouseIsDown ? ImageDown : ImageNormal);
            if (img!=null) {
                Rectangle imgRect = GetImageRect();
                e.Graphics.DrawImage(img, imgRect);
            }
     
        }

        protected virtual Rectangle GetImageRect() {
            int width = this.Size.Width;
            int heigh = this.Size.Height;

            return new Rectangle((ClientRectangle.Width - ImageSize.Width) / 2,
                (ClientRectangle.Height - ImageSize.Height) / 2, ImageSize.Width, ImageSize.Height);
        }

        public void SetImages(Image imageNormal, Image imageHover, Image imageDown) {
            ImageNormal = imageNormal;
            ImageHover = imageHover;
            ImageDown = imageDown;
            Invalidate();
        }

    }
}

窗体中使用自定义控件

1、将需要切图设置到自定义图片控件中
2、代码中设置控件位置
3、实现图片控件的Click事件,在Click中改变窗体状态

using System;
using System.Drawing;
using System.Windows.Forms;

namespace form {
    public partial class Form1 : Form {
        private const int titleHeight = 30;
        private static Pen titlePen = new Pen(Color.Black);

        private bool windowMax = false;
        public bool WindowMax {
            get {return windowMax;}
            set {
                if (windowMax != value) {
                    windowMax = value;
                    if (windowMax) {
                        imageMax.SetImages(Properties.Resources.imageWindow_24px, Properties.Resources.imageWindowHover_24px, 
                                           Properties.Resources.imageWindowDown_24px);
                    }
                    else {
                        imageMax.SetImages(Properties.Resources.imageMax_24px, Properties.Resources.imageMaxHover_24px, 
                                           Properties.Resources.imageMaxDown_24px);
                    }
                }
            }
        }

        public Form1() {
            InitializeComponent();
        }

        protected override void OnShown(EventArgs e) {
            base.OnShown(e);
            IniLocation();

            imageMin.SetImages(Properties.Resources.imageMin_24px, Properties.Resources.imageMinHover_24px, Properties.Resources.imageMinDown_24px);
            imageMax.SetImages(Properties.Resources.imageMax_24px, Properties.Resources.imageMaxHover_24px, Properties.Resources.imageMaxDown_24px);
            imageClose.SetImages(Properties.Resources.imageClose_24px, Properties.Resources.imageCloseHover_24px, Properties.Resources.imageCloseDown_24px);
        }

        protected override void OnPaint(PaintEventArgs e) {
            base.OnPaint(e);
            e.Graphics.DrawLine(titlePen,new Point(0,titleHeight),new Point(Width,titleHeight));
        }

        protected override void OnSizeChanged(EventArgs e) {
            base.OnSizeChanged(e);
            IniLocation();
        }

        private void imageMin_Click(object sender, EventArgs e) {
            WindowState = FormWindowState.Minimized;
        }

        private void imageMax_Click(object sender, EventArgs e) {
            if (WindowState != FormWindowState.Maximized) {
                WindowState = FormWindowState.Maximized;
                WindowMax = true;
            }
            else {
                WindowState = FormWindowState.Normal;
                WindowMax = false;
            }
        }

        private void imageClose_Click(object sender, EventArgs e) {
            Close();
        }

        private void IniLocation() {
            int centerHeigh = (titleHeight - imageClose.Height) / 2;

            imageClose.Location = new Point(Width-10-imageClose.Width, centerHeigh);
            imageMax.Location = new Point(imageClose.Left-10-imageMax.Width, centerHeigh);
            imageMin.Location = new Point(imageMax.Left - 10-imageMin.Width, centerHeigh);
        }
    }
}

C# 自定义窗体(一)_第2张图片

你可能感兴趣的:(C#学习)