這是前面一篇博文:更換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控制項(附源碼)也可為大家提供一個思路。