更換Winform 皮膚(下)—-完全GDI+繪製更換Winform 皮膚(上)—-使用現有皮膚

來源:互聯網
上載者:User

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.