C# WinForm开发系列之自定义无边框窗体(最大化,最小化,关闭,拉伸和移动的相关知识)

本片博客主要讲的事最近总结的一些关于窗体的自定义:
1.首先创建一个BaseForm窗体,通过设置属性FormBorderStyle=None为无边框窗体,代码如下:
 public partial class BaseForm : Form
 {
        // 图片名称
        public const String IMG_MIN = "btn_min";
        public const String IMG_MAX = "btn_max";
        public const String IMG_RESTORE = "btn_restore";
        public const String IMG_CLOSE = "btn_close";
        public const String IMG_BG = "img_bg";
        // 图片缓存
        private Bitmap closeBmp = null;
        private Bitmap minBmp = null;
        private Bitmap maxBmp = null;
        private Bitmap restoreBmp = null;
        public BaseForm()
        {
            InitializeComponent();
            initForm();
        }
        // 初始化界面的方法
        private void initForm()
        {
            // 获取最大化、最小化、关闭的背景图片
            this.minBmp = ResUtils.GetResAsImage(IMG_MIN);
            this.maxBmp = ResUtils.GetResAsImage(IMG_MAX);
            this.closeBmp = ResUtils.GetResAsImage(IMG_CLOSE);
            this.restoreBmp = ResUtils.GetResAsImage(IMG_RESTORE);
            // 设置Tip提示信息
            this.TipMain.SetToolTip(this.BtnClose, "关闭");
            this.TipMain.SetToolTip(this.BtnMin, "最小化");
            this.TipMain.SetToolTip(this.BtnMax, "最大化");
            if (this.WindowState == FormWindowState.Maximized)
            {
                this.TipMain.SetToolTip(this.BtnMax, "还原");
                this.BtnMax.BackgroundImage = this.restoreBmp;
                this.BtnMax.Invalidate();
            }
        }
        // 重写窗体消息轮询方法(不熟悉的可以搜索下win32api)
        //protected void WndProc2(ref Message m){//TODO下篇}
        // 鼠标单击(这里为节省代码篇幅将最小化、最大化、关闭的事件统一处理,也可以分单处理)
        private void BtnWnd_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (sender == this.BtnClose)
                {
                    this.Close();
                }
                else if (sender == this.BtnMax)
                {
                    if (this.WindowState == FormWindowState.Normal)
                    {
                        this.WindowState = FormWindowState.Maximized;
                        this.BtnMax.BackgroundImage = this.restoreBmp;
     
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值