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
    }
}
相关推荐
无小道9 分钟前
Qt——常用控件
开发语言·qt
大空大地202612 分钟前
流程控制语句--switch多分支语句使用、while循环语句的使用、do...while语句、for循环
c#
aini_lovee26 分钟前
MATLAB基于小波技术的图像融合实现
开发语言·人工智能·matlab
R1nG86339 分钟前
多线程安全设计 CANN Runtime关键数据结构的锁优化
开发语言·cann
初次见面我叫泰隆40 分钟前
Qt——5、Qt系统相关
开发语言·qt·客户端开发
亓才孓1 小时前
[Class的应用]获取类的信息
java·开发语言
开开心心就好1 小时前
AI人声伴奏分离工具,离线提取伴奏K歌用
java·linux·开发语言·网络·人工智能·电脑·blender
Never_Satisfied1 小时前
在JavaScript / HTML中,关于querySelectorAll方法
开发语言·javascript·html
3GPP仿真实验室1 小时前
【Matlab源码】6G候选波形:OFDM-IM 增强仿真平台 DM、CI
开发语言·matlab·ci/cd
devmoon1 小时前
在 Polkadot 上部署独立区块链Paseo 测试网实战部署指南
开发语言·安全·区块链·polkadot·erc-20·测试网·独立链