• 更换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控件(附源码)也可为大家提供一个思路。

  • 相关阅读:
    jmeter的基本使用过程
    selenide UI自动化进阶二 pageObject实现页面管理
    Page Object 设计模式介绍
    自动化测试元素查找利器firepath介绍
    selenide 自动化UI测试中Configuration全局配置项目
    selenide 自动化测试进阶一: 查找元素和相关操作
    Selenide 简单实现自动化测试
    python操作MySQL数据库
    一次验证手机号固话号 正则表达式
    算法入门刷题笔记 算法竞赛入门经典++第六章例题 6-6--6-9,6-12,6-14,6-15 树与二叉树
  • 原文地址:https://www.cnblogs.com/DebugLZQ/p/3021659.html
Copyright © 2020-2023  润新知