更换Winform 皮肤(下)完全GDI+绘制

这是前面一篇博文:更换Winform 皮肤(上)----使用现有皮肤的后篇。

主要是自己绘制Winform界面,搜索了网上的相关资源。实现了一个登陆页面。效果如下:

下面来,看看我是如何实现的。

首先,在Winform工程Demo中添加一些素材文件,并将其添加到资源里面,DebugLZQ用的是VS2012,直接拖过去就好。

2.设置该窗体的FormBorderStyle为None。

3.在更改窗体的后台cs代码如下:

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 SkinMySelf.Properties;

///自定义窗体皮肤Demo
///by DebugLZQ
///http://www.cnblogs.com/DebugLZQ
///
namespace SkinMySelf
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //this.SetStyle(ControlStyles.ResizeRedraw, true);                    
        }

        Image imgTop = (Image)Resources.top;//窗体顶部图片
        Image imgBottom = (Image)Resources.bottom;//窗体底部图片

        //绘制窗体
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            //this.BackColor = Color.Violet;
            this.BackColor = Color.FromArgb(255,60,60);
            //绘制皮肤
            Graphics g = e.Graphics;
            //绘制窗体顶部左上角图片
            g.DrawImage(imgTop, 0, 0, imgTop.Width, imgTop.Height);
            //绘制窗体底部左下角图片
            g.DrawImage(imgBottom, 0, e.ClipRectangle.Height - imgBottom.Height, imgBottom.Width, imgBottom.Height);
        }     

        //让窗体可拖动
        private Point mouseOffset; //记录鼠标指针的坐标
        private bool isMouseDown = false;//记录鼠标按键是否按下

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseOffset = new Point(-e.X, -e.Y);
                isMouseDown = true;
            }
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMouseDown = false;
            }
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown)
            {
                Point mousePos = Control.MousePosition;
                mousePos.Offset(mouseOffset.X, mouseOffset.Y);
                Location = mousePos;
            }
        }


    }
}

自己定义Winform皮肤的原理就会这样,各位可以自由发挥了!
自定义完成后,可以和普通的窗体一样,进行控件的拖入编辑,不影响正常使用。

程序运行效果,如下: 

-----

是不是很简单?之前DebugLZQ有一篇博文:在Winform窗体中使用WPF控件(附源码)也可为大家提供一个思路。

原文地址:https://www.cnblogs.com/DebugLZQ/p/3021659.html