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
    }
}
相关推荐
weixin_520649878 小时前
WinForm数据展示组件ListView
c#
九转成圣8 小时前
Java 性能优化实战:如何将海量扁平数据高效转化为类目字典树?
java·开发语言·json
SmartRadio8 小时前
ESP32-S3 双模式切换实现:兼顾手机_路由器连接与WiFi长距离通信
开发语言·网络·智能手机·esp32·长距离wifi
laowangpython9 小时前
Rust 入门:GitHub 热门内存安全编程语言
开发语言·其他·rust·github
我叫汪枫9 小时前
在后台管理系统中,如何递归和选择保留的思路来过滤菜单
开发语言·javascript·node.js·ecmascript
_.Switch9 小时前
东方财富股票数据JS逆向:secids字段和AES加密实战
开发语言·前端·javascript·网络·爬虫·python·ecmascript
软件技术NINI9 小时前
webkit简介及工作流程
开发语言·前端·javascript·udp·ecmascript·webkit·yarn
Brendan_0019 小时前
JavaScript的Stomp.over
开发语言·javascript·ecmascript
念2349 小时前
f5 shape分析
开发语言·javascript·ecmascript
苍穹之跃9 小时前
某量JS逆向
开发语言·javascript·ecmascript