• 基于.Net Core实现自定义皮肤WidForm窗口


    前言

    今天一起来实现基于.Net Core、Windows Form实现自定义窗口皮肤,并实现窗口移动功能。

    素材

    准备素材:边框、标题栏、关闭按钮图标。

    在这里插入图片描述

    窗体设计

    1、创建Window窗体项目

    在这里插入图片描述

    在这里插入图片描述

    2、窗体设计

    拖拉4个Panel控件,分别用于:标题栏、关闭按钮、窗体、底部边框。
    在这里插入图片描述

    3、添加图片到资源

    右键项目=》属性=》资源=》创建资源
    在这里插入图片描述

    点击添加资源=》添加现有文件。
    在这里插入图片描述

    4、设置控件背景图片

    选择对应的Panel控件,分别设置标题栏、窗体、底部、关闭按钮。

    在这里插入图片描述
    在这里插入图片描述

    5、效果

    此时运行项目,窗体效果如下:

    在这里插入图片描述

    窗体事件

    窗口的皮肤已经自定义完毕,下面我们为窗口添加事件:关闭和移动。

    1、关闭窗口

    为关闭按钮,添加关闭事件,按钮如下。

    private void panel1_Click(object sender, EventArgs e)
    {
        this.Close();
    }
    
    • 1
    • 2
    • 3
    • 4

    2、窗口移动代码

    public void FrmMove(Form Frm, MouseEventArgs e)  //Form或MouseEventArgs添加命名空间using System.Windows.Forms;
            {
                if (e.Button == MouseButtons.Left)
                {
                    Point myPosittion = Control.MousePosition;//获取当前鼠标的屏幕坐标
                    myPosittion.Offset(CPoint.X, CPoint.Y);//重载当前鼠标的位置
                    Frm.DesktopLocation = myPosittion;//设置当前窗体在屏幕上的位置
                    Tem_place = 0;
                    this.Height = FrmHeight;
                }
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    private void timer1_Tick(object sender, EventArgs e)
            {
                if (this.Top < 3 && Tem_place==0)//如果窗体被移到屏幕的顶部
                    {
                        if (this.Handle == FormNameAt(Cursor.Position.X, Cursor.Position.Y))//当鼠标移致到该窗体上
                        {
                            panel_Title.Tag = 1;//设置标识,用于判断窗体在屏幕顶部
                            timer2.Enabled = false;//不对窗体进行拉伸操作
                            this.Top = 0;//使窗体致顶
                        }
                        else
                        {
                            panel_Title.Tag = 1;//设置标识,用于判断窗体在屏幕顶部
                            timer2.Enabled = true;//将窗体在顶部进行隐藏
                        }
                    }
                    else
                    {
                        if (this.Left < 3 || this.Right > GetSystemMetrics(0) - 3)//如果窗体被移到屏幕的左端或右端
                        {
                            if (this.Left < 3)//如果窗体被移到屏幕的左端
                            {
                                if (this.Handle == FormNameAt(Cursor.Position.X, Cursor.Position.Y))//当鼠标移致到该窗体上
                                {
                                    panel_Title.Tag = 2;//设置标识,用于判断窗体在屏幕左端
                                    timer2.Enabled = false;
                                    Frm_Height = this.Height;
                                    this.Left = 0;//使窗体致左
                                    this.Top = 0;
                                    this.Height = Screen.AllScreens[0].Bounds.Height;
                                    Tem_place = 1;
                                }
                                else
                                {
                                    panel_Title.Tag = 2;
                                    timer2.Enabled = true;//将窗体在左端进行隐藏
                                }
                            }
                            if (this.Right > GetSystemMetrics(0) - 3)//如果窗体被移到屏幕的右端
                            {
                                if (this.Handle == FormNameAt(Cursor.Position.X, Cursor.Position.Y))//当鼠标移致到该窗体上
                                {
                                    panel_Title.Tag = 3;//设置标识,用于判断窗体在屏幕右端
                                    timer2.Enabled = false;
                                    Frm_Height = this.Height;
                                    this.Left = GetSystemMetrics(0) - this.Width;//使窗体致右
                                    this.Top = 0;
                                    this.Height = Screen.AllScreens[0].Bounds.Height;
                                    Tem_place = 1;
                                }
                                else
                                {
                                    panel_Title.Tag = 3;
                                    timer2.Enabled = true;//将窗体在右端进行隐藏
                                }
                            }
    
                        }
                    }
    
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    好了,今天就分享到这边,需要示例代码的获取👇🏻。

    欢迎点赞、收藏、关注、评论啦 、查看👇🏻获取联系方式👇🏻

  • 相关阅读:
    使用Pytorch手写ViT — VisionTransformer
    常用Doc命令
    亚马逊秋季促销指南——如何更好的利用促销?
    Mac笔记本聚焦SpotLight占用内存太高的 解法
    Python基础复习【第二弹】【黑马】
    Dijkstra算法
    桥接模式(结构型)
    2023.11.13 hive数据仓库之分区表与分桶表操作,与复杂类型的运用
    Git如何上传代码至GitHub
    力扣记录:剑指offer(4)——JZ33-42
  • 原文地址:https://blog.csdn.net/daremeself/article/details/133304790