[C#][Winfrom]自定义窗体主题

首先在窗体类里面声明两个变量,来监视鼠标的动作

//鼠标按下标识
 bool mouseDown = false;
Point mouseOffset;

在Load事件里加载所有的图片

if (File.Exists(GlobalInfo.AppPath + "\\Picture\\login.png"))
    this.BackgroundImage = new Bitmap(GlobalInfo.AppPath + "\\Picture\\login.png");
 
if (File.Exists(GlobalInfo.AppPath + "\\Picture\\button.png"))
{
    this.simpleButton1.ImageLocation = ImageLocation.MiddleCenter;
    this.simpleButton1.Image = new Bitmap(GlobalInfo.AppPath + "\\Picture\\button.png");
 
    this.simpleButton2.ImageLocation = ImageLocation.MiddleCenter;
    this.simpleButton2.Image = new Bitmap(GlobalInfo.AppPath + \\Picture\\button.png);
}

绘制按钮的时候添加上文字

//绘制登录按钮
private void simpleButton1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawString("登录", new Font("新宋体", 12, FontStyle.Bold), Brushes.White, 30, 8);
}

窗体的鼠标事件

//鼠标按下
private void FrmLogin_MouseDown(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        mouseDown = true;
        mouseOffset = new Point(-e.X, -e.Y);
    }
}
//鼠标松开
private void FrmLogin_MouseUp(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        mouseDown = false;
    }
}
//鼠标按下移动
private void FrmLogin_MouseMove(object sender, MouseEventArgs e)
{
    if (mouseDown)
    {
        Point mousePos = Control.MousePosition;
        mousePos.Offset(mouseOffset.X, mouseOffset.Y);
        this.Location = mousePos;
    }
}
原文地址:https://www.cnblogs.com/Hsppl/p/2601017.html