记录一下,同时本着互联网的分享精神分享出来,以便后来者引用。先放效果图:
这里用到几个东西,gif好看的背景图好难找,基本都是需要钱下载,我这里放两个给需要用到的小伙伴,告诉一下大家:这些好看的动图我都是去那些需要钱下载的网站,然后点击进去后用gif录制工具给录制的,推荐这个工具给大家:GifCam,直接搜索去官网下载这个软件就好。
我这个登陆窗体用到两个功能点:1、是用到自定义圆角窗体的代码段,首先先把属性:backgroundimagelayout=Stretch;接着fromborderstyle=none;2、运用GDI+来加载动态图片GIF的到背景图,直接在属性里设置不行,这里有一个注意的地方就是如果你运行起来发现背景图有闪烁,要去设置窗体属性DoubleBuffered为true。
这里贴一下源码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using Oracle.ManagedDataAccess.Client;
using System.Configuration;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using SpeechLib;
using System.Drawing.Imaging;
namespace web_quece
{
public partial class login_queue : Form
{
DB_Contr bc = new DB_Contr();
DataView dv = new DataView();
SetWD swd = new SetWD();
public login_queue()
{
InitializeComponent();
}
//窗体移动的代码引用
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int IParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0xF010;
public const int HTCAPTION = 0x0002;
private void Main_MouseDown(object sender, MouseEventArgs e)
{
//拖动窗体
this.Cursor = System.Windows.Forms.Cursors.Hand;//改变鼠标样式
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
this.Cursor = System.Windows.Forms.Cursors.Default;
}
//窗体resize事件窗体调用窗体圆角设置函数
private void Form1_Resize(object sender, EventArgs e)
{
SetWindowRegion();
}
public void SetWindowRegion()
{//设置窗体圆角函数,同时条用类setWD的函数。
System.Drawing.Drawing2D.GraphicsPath FormPath;
FormPath = new System.Drawing.Drawing2D.GraphicsPath();
Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
FormPath = swd.GetRoundedRectPath(rect, 10);
this.Region = new Region(FormPath);
}
private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
{
int diameter = radius;
Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
GraphicsPath path = new GraphicsPath();
// 左上角
path.AddArc(arcRect, 180, 90);
// 右上角
arcRect.X = rect.Right - diameter;
path.AddArc(arcRect, 270, 90);
// 右下角
arcRect.Y = rect.Bottom - diameter;
path.AddArc(arcRect, 0, 90);
// 左下角
arcRect.X = rect.Left;
path.AddArc(arcRect, 90, 90);
path.CloseFigure();//闭合曲线
return path;
}
//给窗体加载gif背景图
Bitmap bitmap = new Bitmap(Application.StartupPath + "\\GIF_01.gif"); //实例化一个GDI 绘图图面对象
bool current = false; //初始化一个bool型的变量current
public void PlayImage()
{
if (!current) //当该值为true时
{
ImageAnimator.Animate(bitmap, new EventHandler(this.OnFrameChanged)); //将多帧图片显示为动画图像
current = true; //设定current的值为true
}
}
private void OnFrameChanged(object o, EventArgs e)
{
this.Invalidate(); //使控件的整个图片无效并导致重绘事件
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawImage(this.bitmap, new Point(1, 1)); //从指定的位置绘制图片
ImageAnimator.UpdateFrames(); //使该帧在当前正被图画处理的所有图像中前移
}
private void login_queue_Load(object sender, EventArgs e)
{
PlayImage(); //播放
ImageAnimator.Animate(bitmap, new EventHandler(this.OnFrameChanged)); //将多帧图像显示为动画
}
}
}
//此处为引用的定义窗体类,之前忘记贴出来了,2021年8月23日更新
class Set_WD
{
//定义圆角窗体画笔类
public GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
{
int diameter = radius;
Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
GraphicsPath path = new GraphicsPath();
// 左上角
path.AddArc(arcRect, 180, 90);
// 右上角
arcRect.X = rect.Right - diameter;
path.AddArc(arcRect, 270, 90);
// 右下角
arcRect.Y = rect.Bottom - diameter;
path.AddArc(arcRect, 0, 90);
// 左下角
arcRect.X = rect.Left;
path.AddArc(arcRect, 90, 90);
path.CloseFigure();//闭合曲线
return path;
}
}