更換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控件(附源碼)也可為大家提供一個思路。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM