C# 使用GDI+设计登录窗体(窗体渐变和关闭淡出)

C# 使用GDI+实现窗体渐变和关闭淡出效果

1、窗体渐变色

GDI+ :(Graphics Device Interface)

csharp 复制代码
#region 窗体渐变效果  重写OnPaint
protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    //获取画布
    Graphics graphics = e.Graphics;
    //获取矩形(目标窗体)
    Rectangle  rec = new Rectangle(0,0,this.Width,this.Height);
    //画刷 :指定渐变色和渐变方向
    LinearGradientBrush linearGradientBrush = new LinearGradientBrush(rec, Color.FromArgb(255, 101, 127), Color.FromArgb(93, 127, 124), LinearGradientMode.BackwardDiagonal);
    graphics.FillRectangle(linearGradientBrush, rec);
}
#endregion

1.重写(override)

重写(override):子类对父类中方法进行按照自身需要进行对方法体进行重写,重写方法与父类方法在访问权限、返回值、方法名以及参数不变。

2.重载(overload)

重载(overload):在一个类中对同一方法名按照不同的需求编写出多个方法,方法名相同,但是参数的类型或者个数不能相同,返回值类型不能作为重载的标志。

3.覆写(new)

覆写(overwrite):用 new 实现。在子类中用 new 关键字修饰定义的与父类中同名的方法,也称为覆盖,覆盖不会改变父类方法的功能。

2、无边框拖动(点标题框)

csharp 复制代码
 #region 无边框拖动
 private Point mPoint;
 private void labTitle_MouseDown(object sender, MouseEventArgs e)
 {
     //获取焦点
     mPoint = e.Location;
 }

 private void labTitle_MouseMove(object sender, MouseEventArgs e)
 {
     //左键才可拖动
     if (e.Button == MouseButtons.Left)
     {
         this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
     }
 }
 #endregion

3、关闭图标

图标获取(阿里巴巴矢量图):https://www.iconfont.cn/

csharp 复制代码
using Timer = System.Windows.Forms.Timer;
public LoginForm()
{
    InitializeComponent();

    //初始化定时器,绑定触发函数
    mCloseTimer.Interval = 10;
    mCloseTimer.Tick += mCloseTimer_Tick;
}
#region 淡出效果
private Timer mCloseTimer = new Timer();
private void mCloseTimer_Tick(object? sender, EventArgs e)
{
    if (this.Opacity >= 0.025)
    {
        this.Opacity -= 0.025;
    }
    else
    {
        this.mCloseTimer.Enabled = false;
        this.Close();
    }
}
/// <summary>
/// 关闭淡出效果
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void picClose_Click(object sender, EventArgs e)
{
    //开启定时器
    this.mCloseTimer.Enabled = true;
}
#endregion

4、完整代码

csharp 复制代码
using System.Drawing.Drawing2D;
using Timer = System.Windows.Forms.Timer;

namespace LoginForm
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();

            //初始化定时器,绑定触发函数
            mCloseTimer.Interval = 10;
            mCloseTimer.Tick += mCloseTimer_Tick;
        }

        

        #region 窗体渐变效果
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            //获取画布
            Graphics graphics = e.Graphics;
            //获取矩形(目标窗体)
            Rectangle rec = new Rectangle(0, 0, this.Width, this.Height);
            //画刷 :指定渐变色和渐变方向
            LinearGradientBrush linearGradientBrush = new LinearGradientBrush(rec, Color.FromArgb(255, 101, 127), Color.FromArgb(93, 127, 124), LinearGradientMode.BackwardDiagonal);
            graphics.FillRectangle(linearGradientBrush, rec);
        }
        #endregion

        #region 无边框拖动
        private Point mPoint;
        private void labTitle_MouseDown(object sender, MouseEventArgs e)
        {
            //获取焦点
            mPoint = e.Location;
        }

        private void labTitle_MouseMove(object sender, MouseEventArgs e)
        {
            //左键才可拖动
            if (e.Button == MouseButtons.Left)
            {
                //移动距离
                this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
            }
        }
        #endregion

        #region 淡出效果
        private Timer mCloseTimer = new Timer();
        private void mCloseTimer_Tick(object? sender, EventArgs e)
        {
            if (this.Opacity >= 0.025)
            {
                this.Opacity -= 0.025;
            }
            else
            {
                this.mCloseTimer.Enabled = false;
                this.Close();
            }
        }
        /// <summary>
        /// 关闭淡出效果
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void picClose_Click(object sender, EventArgs e)
        {
            //开启定时器
            this.mCloseTimer.Enabled = true;
        }
        #endregion
    }
}
相关推荐
Ray Liang10 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
Scout-leaf3 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
用户298698530144 天前
程序员效率工具:Spire.Doc如何助你一键搞定Word表格排版
后端·c#·.net
mudtools5 天前
搭建一套.net下能落地的飞书考勤系统
后端·c#·.net
玩泥巴的5 天前
搭建一套.net下能落地的飞书考勤系统
c#·.net·二次开发·飞书
唐宋元明清21885 天前
.NET 本地Db数据库-技术方案选型
windows·c#
郑州光合科技余经理5 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
lindexi5 天前
dotnet DirectX 通过可等待交换链降低输入渲染延迟
c#·directx·d2d·direct2d·vortice
feifeigo1235 天前
matlab画图工具
开发语言·matlab
dustcell.5 天前
haproxy七层代理
java·开发语言·前端