zoukankan      html  css  js  c++  java
  • C#制作高仿360安全卫士窗体<一>

    C#制作高仿360安全卫士窗体<一>

    开始写这一系列博客之前先要向大家说声抱歉,放肆雷特建立很久却很少有更新。当然博客人气也不旺,大部分都是看的人多评论收藏的人少。一直想要改变这种状态,正好赶上了最近工作上做了一个高仿360安全卫士窗体。所以将开发历程分享出来,一来希望能和大家交流共同学习成长;二来希望自己的放肆雷特能有人光顾。

    这一系列的文章所用到的技术首先要感谢苏飞大哥,我也是在很早以前看了他的QQ皮肤制作文章之后得到的灵感和一些相关知识。在这里帮他的C#论坛打个广告,里面有不少比较有用的资料。苏飞大哥也是很用心的打理这个论坛,论坛地址http://www.sufeinet.com/

    说了这么多也该进入主题了,首先为了让大家有兴趣看下去而且认同我开发的这个皮肤,所以先传上本程序的界面截图(不喜勿喷)。同时本人也先声明,本人属屌丝一枚,想法和思路可能比较落后,如果你有比较不错的想法和思路可以在本论坛中寻找本人qq或评论本文。

    第一章:360安全卫士窗体素材提取

    360安全卫士的窗体使用的素材比之前QQ的素材更容易提取首先安装360安全卫士,如果怕自己电脑中毒的请使用虚拟机进行安装。在这里不提供下载链接。安装好之后打开安装目录,我这里以安装到C盘为准:
    C:Program Files (x86)360360safeConfig ewui hemesdefault
    打开后,将default_theme.ui文件拷贝出来。再将它的后缀改名为rar。之后的操作你懂的呀。。操作之后得到

    第二章:360安全卫士风格按钮开发
    得到以上素材之后开始正式的编码,第一个要开发的控件是Button控件,为什么呢?因为窗体上的关闭、最大化、最小化这些按钮都可以使用。系统里面用到的WinApi类我就不放上来了,网上一搜一大把,如果有需要的可以单独向我索取。写按钮前,先枚举该按钮的所有状态,我这里定义5个状态,应该比较全了:

    复制代码
     1 /// <summary>
     2 /// 枚举按钮状态
     3 /// </summary>
     4 public enum State
     5 {
     6     Normal = 1,//按钮默认时
     7     MouseOver = 2,//鼠标移上按钮时
     8     MouseDown = 3,//鼠标按下按钮时
     9     Disable = 4,//当不启用按钮时(也就是按钮属性Enabled==Ture时)
    10     Default = 5//控件得到Tab焦点时
    11 }
    复制代码

    重绘按钮以及重绘其它控件及窗体用到的方法:
    1、该方法是将素材切割后再进行绘制,这样在按钮大小发生改变时也不会对素材的大小有影响:

    复制代码
      1 /// <summary>
      2 /// 绘图对像
      3 /// </summary>
      4 /// <param name="g">绘图对像</param>
      5 /// <param name="img">图片</param>
      6 /// <param name="r">绘置的图片大小、坐标</param>
      7 /// <param name="lr">绘置的图片边界</param>
      8 /// <param name="index">当前状态</param>
      9 /// <param name="Totalindex">状态总数</param>
     10 public static void DrawRect(Graphics g, Bitmap img, Rectangle r, Rectangle lr, int index, int Totalindex)
     11 {
     12     if (img == null) return;
     13     Rectangle r1, r2;
     14     int x = (index - 1) * img.Width / Totalindex;
     15     int y = 0;
     16     int x1 = r.Left;
     17     int y1 = r.Top;
     18 
     19     if (r.Height > img.Height && r.Width <= img.Width / Totalindex)
     20     {
     21         r1 = new Rectangle(x, y, img.Width / Totalindex, lr.Top);
     22         r2 = new Rectangle(x1, y1, r.Width, lr.Top);
     23         g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
     24 
     25         r1 = new Rectangle(x, y + lr.Top, img.Width / Totalindex, img.Height - lr.Top - lr.Bottom);
     26         r2 = new Rectangle(x1, y1 + lr.Top, r.Width, r.Height - lr.Top - lr.Bottom);
     27         if ((lr.Top + lr.Bottom) == 0) r1.Height = r1.Height - 1;
     28         g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
     29 
     30         r1 = new Rectangle(x, y + img.Height - lr.Bottom, img.Width / Totalindex, lr.Bottom);
     31         r2 = new Rectangle(x1, y1 + r.Height - lr.Bottom, r.Width, lr.Bottom);
     32         g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
     33     }
     34     else
     35         if (r.Height <= img.Height && r.Width > img.Width / Totalindex)
     36         {
     37             r1 = new Rectangle(x, y, lr.Left, img.Height);
     38             r2 = new Rectangle(x1, y1, lr.Left, r.Height);
     39             g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
     40             r1 = new Rectangle(x + lr.Left, y, img.Width / Totalindex - lr.Left - lr.Right, img.Height);
     41             r2 = new Rectangle(x1 + lr.Left, y1, r.Width - lr.Left - lr.Right, r.Height);
     42             g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
     43             r1 = new Rectangle(x + img.Width / Totalindex - lr.Right, y, lr.Right, img.Height);
     44             r2 = new Rectangle(x1 + r.Width - lr.Right, y1, lr.Right, r.Height);
     45             g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
     46         }
     47         else
     48             if (r.Height <= img.Height && r.Width <= img.Width / Totalindex)             {                 r1 = new Rectangle((index - 1) * img.Width / Totalindex, 0, img.Width / Totalindex, img.Height);                 g.DrawImage(img, new Rectangle(x1, y1, r.Width, r.Height), r1, GraphicsUnit.Pixel);             }             else if (r.Height > img.Height && r.Width > img.Width / Totalindex)
     49             {
     50                 //top-left
     51                 r1 = new Rectangle(x, y, lr.Left, lr.Top);
     52                 r2 = new Rectangle(x1, y1, lr.Left, lr.Top);
     53                 g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
     54 
     55                 //top-bottom
     56                 r1 = new Rectangle(x, y + img.Height - lr.Bottom, lr.Left, lr.Bottom);
     57                 r2 = new Rectangle(x1, y1 + r.Height - lr.Bottom, lr.Left, lr.Bottom);
     58                 g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
     59 
     60                 //left
     61                 r1 = new Rectangle(x, y + lr.Top, lr.Left, img.Height - lr.Top - lr.Bottom);
     62                 r2 = new Rectangle(x1, y1 + lr.Top, lr.Left, r.Height - lr.Top - lr.Bottom);
     63                 g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
     64 
     65                 //top
     66                 r1 = new Rectangle(x + lr.Left, y,
     67                     img.Width / Totalindex - lr.Left - lr.Right, lr.Top);
     68                 r2 = new Rectangle(x1 + lr.Left, y1,
     69                     r.Width - lr.Left - lr.Right, lr.Top);
     70                 g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
     71 
     72                 //right-top
     73                 r1 = new Rectangle(x + img.Width / Totalindex - lr.Right, y, lr.Right, lr.Top);
     74                 r2 = new Rectangle(x1 + r.Width - lr.Right, y1, lr.Right, lr.Top);
     75                 g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
     76 
     77                 //Right
     78                 r1 = new Rectangle(x + img.Width / Totalindex - lr.Right, y + lr.Top,
     79                     lr.Right, img.Height - lr.Top - lr.Bottom);
     80                 r2 = new Rectangle(x1 + r.Width - lr.Right, y1 + lr.Top,
     81                     lr.Right, r.Height - lr.Top - lr.Bottom);
     82                 g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
     83 
     84                 //right-bottom
     85                 r1 = new Rectangle(x + img.Width / Totalindex - lr.Right, y + img.Height - lr.Bottom,
     86                     lr.Right, lr.Bottom);
     87                 r2 = new Rectangle(x1 + r.Width - lr.Right, y1 + r.Height - lr.Bottom,
     88                     lr.Right, lr.Bottom);
     89                 g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
     90 
     91                 //bottom
     92                 r1 = new Rectangle(x + lr.Left, y + img.Height - lr.Bottom,
     93                     img.Width / Totalindex - lr.Left - lr.Right, lr.Bottom);
     94                 r2 = new Rectangle(x1 + lr.Left, y1 + r.Height - lr.Bottom,
     95                     r.Width - lr.Left - lr.Right, lr.Bottom);
     96                 g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
     97 
     98                 //Center
     99                 r1 = new Rectangle(x + lr.Left, y + lr.Top,
    100                     img.Width / Totalindex - lr.Left - lr.Right, img.Height - lr.Top - lr.Bottom);
    101                 r2 = new Rectangle(x1 + lr.Left, y1 + lr.Top,
    102                     r.Width - lr.Left - lr.Right, r.Height - lr.Top - lr.Bottom);
    103                 g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
    104             }
    105 }
    106 
    107 /// <summary>
    108 /// 绘图对像
    109 /// </summary>
    110 /// <param name="g"> 绘图对像</param>
    111 /// <param name="obj">图片对像</param>
    112 /// <param name="r">绘置的图片大小、坐标</param>
    113 /// <param name="index">当前状态</param>
    114 /// <param name="Totalindex">状态总数</param>
    115 public static void DrawRect(Graphics g, Bitmap img, Rectangle r, int index, int Totalindex)
    116 {
    117     if (img == null) return;
    118     int width = img.Width / Totalindex;
    119     int height = img.Height;
    120     Rectangle r1, r2;
    121     int x = (index - 1) * width;
    122     int y = 0;
    123     r1 = new Rectangle(x, y, width, height);
    124     r2 = new Rectangle(r.Left, r.Top, r.Width, r.Height);
    125     g.DrawImage(img, r2, r1, GraphicsUnit.Pixel);
    126 }
    复制代码

    2、该方法是将编译好的图片读取出来

    复制代码
     1 /// <summary>
     2 /// 得到要绘置的图片对像
     3 /// </summary>
     4 /// <param name="str">图像在程序集中的地址</param>
     5 /// <returns></returns>
     6 public static Bitmap GetResBitmap(string str)
     7 {
     8     Stream sm;
     9     sm = FindStream(str);
    10     if (sm == null) return null;
    11     return new Bitmap(sm);
    12 }
    13 
    14 /// <summary>
    15 /// 得到图程序集中的图片对像
    16 /// </summary>
    17 /// <param name="str">图像在程序集中的地址</param>
    18 /// <returns></returns>
    19 private static Stream FindStream(string str)
    20 {
    21     Assembly assembly = Assembly.GetExecutingAssembly();
    22     string[] resNames = assembly.GetManifestResourceNames();
    23     foreach (string s in resNames)
    24     {
    25         if (s == str)
    26         {
    27             return assembly.GetManifestResourceStream(s);
    28         }
    29     }
    30     return null;
    31 }
    复制代码

    Ok,基础打好之后就可以开始正式的进行按钮绘制,代码比较长,里面包括定义变量,定义按钮属性,处理按钮构造函数。这里我分享关键部分重写OnPaint代码:

    复制代码
     1 /// <summary>
     2 /// 重绘控件
     3 /// </summary>
     4 /// <param name="e"></param>
     5 protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
     6 {
     7     if (BackImg == null)
     8     {
     9         base.OnPaint(e);
    10         return;
    11     }
    12 
    13     int i = (int)state;
    14     if (this.Focused && state != State.MouseDown && _IsTabFocus == true) i = 5;
    15     if (!this.Enabled) i = 4;
    16     Rectangle rc = this.ClientRectangle;
    17     Graphics g = e.Graphics;
    18 
    19     base.InvokePaintBackground(this, new PaintEventArgs(e.Graphics, base.ClientRectangle));
    20     try
    21     {
    22         if (BackImg != null)
    23         {
    24             if (_BacklightLTRB != Rectangle.Empty)
    25             {
    26 
    27                 ImageDrawRect.DrawRect(g, BackImg, rc, Rectangle.FromLTRB(_BacklightLTRB.X, _BacklightLTRB.Y, _BacklightLTRB.Width, _BacklightLTRB.Height), i, 5);
    28             }
    29             else
    30             {
    31                 ImageDrawRect.DrawRect(g, BackImg, rc, Rectangle.FromLTRB(10, 10, 10, 10), i, 5);
    32             }
    33 
    34         }
    35     }
    36     catch
    37     { }
    38 
    39     Image img = null;
    40     Size txts, imgs;
    41 
    42     txts = Size.Empty;
    43     imgs = Size.Empty;
    44 
    45     if (this.Image != null)
    46     {
    47         img = this.Image;
    48     }
    49     else if (this.ImageList != null && this.ImageIndex != -1)
    50     {
    51         img = this.ImageList.Images[this.ImageIndex];
    52     }
    53 
    54     if (img != null)
    55     {
    56         imgs.Width = img.Width;
    57         imgs.Height = img.Height;
    58     }
    59 
    60     StringFormat format1;
    61     using (format1 = new StringFormat())
    62     {
    63         format1.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show;
    64         SizeF ef1 = g.MeasureString(this.Text, this.Font, new SizeF((float)rc.Width, (float)rc.Height), format1);
    65         txts = Size.Ceiling(ef1);
    66     }
    67 
    68     rc.Inflate(-4, -4);
    69     if (imgs.Width * imgs.Height != 0)
    70     {
    71         Rectangle imgr = rc;
    72         imgr = ImageDrawRect.HAlignWithin(imgs, imgr, this.ImageAlign);
    73         imgr = ImageDrawRect.VAlignWithin(imgs, imgr, this.ImageAlign);
    74         if (!this.Enabled)
    75         {
    76             ControlPaint.DrawImageDisabled(g, img, imgr.Left, imgr.Top, this.BackColor);
    77         }
    78         else
    79         {
    80             g.DrawImage(img, imgr.Left, imgr.Top, img.Width, img.Height);
    81         }
    82     }
    83 
    84     Rectangle txtr = rc;
    85     txtr = ImageDrawRect.HAlignWithin(txts, txtr, this.TextAlign);
    86     txtr = ImageDrawRect.VAlignWithin(txts, txtr, this.TextAlign);
    87 
    88     format1 = new StringFormat();
    89     format1.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show;
    90 
    91     if (this.RightToLeft == RightToLeft.Yes)
    92     {
    93         format1.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
    94     }
    95     brush = new SolidBrush(this.ForeColor);
    96     g.DrawString(this.Text, this.Font, brush, (RectangleF)txtr, format1);
    97     brush.Dispose();
    98 
    99 }
    复制代码

    其中关键部分已经出来,按钮也基本绘制完成。下一篇我将分享重绘主窗体,其中过程也没有详细讲解,主要原因是文采不好。如果有不了解的可以留言给我,我会及时回复。该皮肤的源码由于项目上正在使用暂时不进行公布,如果本文的反响和关注度都不错,我再决定是否提供下载。最后谢谢大家的关注。请期待下一篇的重绘主窗体。


    本文来自 放肆雷特 | 胖子的技术博客

  • 相关阅读:
    expect脚本实例
    Linux dialog详解(图形化shell)
    makefile——小试牛刀
    gdb入门
    linux常见系统调用函数列表
    linux前后台任务的切换以及执行暂停
    centos 7.0 lnmp安装部署步骤
    环境列表
    setjmp与longjmp非局部跳转函数的使用
    malloc,calloc,alloca和free函数
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3172662.html
Copyright © 2011-2022 走看看