zoukankan      html  css  js  c++  java
  • 软件易容术换肤

                                          软件易容术-----换肤
                                                          电子科技大学03级02班 周银辉

    1, 看看成果:

    换肤前
    pastime_img2.PNG

    换肤后
    pastime_img.PNG

    (说明: 这里仅仅借用的是"千千静听"中皮肤包中的图片, 本示例程序中的皮肤包文件格式以及换肤方案均为作者原创)

    2  如何实现:

    2.1 皮肤配置文件:
    在研究如何实现换肤之前,应该仔细看看以下XML文件,它是皮肤配置文件,由它来组织皮肤包中的图片并告诉软件如何来绘制皮肤:

    皮肤配置文件(点击加号展开)


    很明显地,该配置文件中规定了皮肤中的各个元素的位置大小等相关信息. 比如:

       <Play>
          
    <Position>
            
    <Left>164</Left>
            
    <Top>39</Top>
            
    <Right>183</Right>
            
    <Bottom>58</Bottom>
          
    </Position>
          
    <Iamge>play.bmp</Iamge>
        
    </Play>

    规定的"播放"按钮在主窗口中的位置以及其对应的图片.

    很简单地,我们只要解析该XML文件并将其对应的图片绘制在指定的位置就可以了(当然此时还只是视觉上的,因为按钮还有事件等,稍后讨论)

    2.2 如何组织窗口上的元素

    "窗口上的元素"指的是窗口上的"按钮","滚动条"等. 注意:它们不是"Button" "ScrollBar"等控件,它们只是在指定位置上的图片,并响应相应事件)
    为更好地组织各个元素,我们设计了一个"皮肤元素接口",让所有元素都实现该接口:

    public interface ISkinElement
        
    {
            
    string Name
            
    {
                
    get;
            }


            Position Position
            
    {
                
    get;
                
    set;
            }


            ElementStatus Status
            
    {
                
    get;
                
    set;
            }


            
    void Paint(Graphics g);

            
    event SkinElementStatusChangedHandler SkinElementStatusChanged;
        }

    其中
    Name表示该元素的名称.

    Position规定了该元素的大小和位置,它是这样一个结构:

        /// <summary>
        
    /// 位置信息,由左上角X、Y坐标和右下角X、Y坐标组成
        
    /// </summary>

        public struct Position
        
    {
            
    public int Left;
            
    public int Top;
            
    public int Right;
            
    public int Bottom;

            
    public Position(int left, int top, int right, int bottom)
            
    {
                
    this.Left = left;
                
    this.Top = top;
                
    this.Right = right;
                
    this.Bottom = bottom;
            }

        }

    Status表示该元素的当前状态,它是这样一个枚举:

        /// <summary>
        
    /// 元素状态
        
    /// </summary>

        public enum ElementStatus
        
    {
            Normal,
            MouseHover,
            MouseDown,
            Disabled
        }

    void Paint(Graphics g)是该元素的绘制函数
    event SkinElementStatusChangedHandler SkinElementStatusChanged;是该元素状态发生改变时所引发的事件.

    然后,比如"播放"按钮,就可以是如下的一个类:

    播放按捏(点击加号展开)

    其它元素同理.


    2.3 如何绘制元素

    以按钮为例:
    一张按钮图片由四部分组成 play.bmp, 它分别代表按钮的不同状态(Normal,MouseEnter,MouseDown, Disable), 我们只需要根据按钮的当前状态切取图片的不同部分并绘制在指定位置上便可.比如:

            /// <summary>
            
    /// 绘制普通的类似于按钮的皮肤元素。
             
    /// 进度条、音量控制等不应该采用此函数
             
    /// </summary>
            
    /// <param name="g">用其进行绘制</param>
            
    /// <param name="pos">绘制的位置</param>
            
    /// <param name="imgPath">绘制的图片的路径(相对路径,在此之前应该确定SkinRootDir属性已经被设置)</param>
            
    /// <param name="status">皮肤元素的当前状态</param>

            public static void PaintSkinElement(Graphics g, Position pos, string imgPath, ElementStatus status)
            
    {
                ImageAttributes imgAttributes 
    = new ImageAttributes();
                imgAttributes.SetColorKey(transparentKey, transparentKey);

                
    try
                
    {
                    Image img 
    = Image.FromFile(SkinRootDir + System.IO.Path.DirectorySeparatorChar + imgPath);
                    Rectangle destRect 
    = new Rectangle(pos.Left, pos.Top, pos.Right - pos.Left, pos.Bottom - pos.Top);

                    
    int x = 0;
                    
    int y = 0;
                    
    int width = img.Width / 4;
                    
    int height = img.Height;

                    
    switch (status)
                    
    {
                        
    case ElementStatus.Normal:
                            x 
    = 0;
                            
    break;
                        
    case ElementStatus.MouseHover:
                            x 
    = width;
                            
    break;
                        
    case ElementStatus.MouseDown:
                            x 
    = 2 * width;
                            
    break;
                        
    case ElementStatus.Disabled:
                            x 
    = 3 * width;
                            
    break;
                        
    default:
                            
    break;
                    }


                    g.DrawImage(img, destRect, x, y, width, height, GraphicsUnit.Pixel, imgAttributes);
                }

                
    catch
                
    {
                }

            }


    2.4 如何响应键盘鼠标等事件

    以鼠标事件为例:
    其实皮肤上的元素并没有这些事件,我们只是当用户点击主窗口时,根据鼠标点击的位置来确定点击在了哪个元素指上,并引发该元素所对应的事件.
    查找鼠标点击的元素:

         /// <summary>
            
    /// 确定在指定的皮肤元素集合中,指定的点包含在哪个元素中.
            
    /// 如果同时包含在多个元素中,则以最内层的那个为准.
            
    /// <para>注意:由于元素之间没有Z轴层次关系,所以不应该让两个元素处在相交却不包含的关系中</para>
            
    /// </summary>
            
    /// <param name="elementList">元素集合,在它们中间查找</param>
            
    /// <param name="loc">要进行判断的点</param>
            
    /// <returns>如果不包含在指定的任一元素中,则返回null,否则返回包含该点的元素</returns>

            public static ISkinElement FindSkinElementFormPosition(List<ISkinElement> elementList, Point loc)
            
    {
                Rectangle rect 
    = new Rectangle();
                Rectangle lastRect 
    = Rectangle.Empty;
                ISkinElement res 
    = null;
                
    foreach (ISkinElement element in elementList)
                
    {
                    rect.X 
    = element.Position.Left;
                    rect.Y 
    = element.Position.Top;
                    rect.Width 
    = element.Position.Right - element.Position.Left;
                    rect.Height 
    = element.Position.Bottom - element.Position.Top;
                    
    if (rect.Contains(loc))
                    
    {
                        
    if (lastRect == Rectangle.Empty || lastRect.Contains(rect))
                        
    {
                            lastRect 
    = rect;
                            res 
    = element;
                        }

                    }

                }


                
    return res;
            }

    比如,我们鼠标点击落在"关闭"元素内时,将关闭窗口:

     private void FormMain_MouseClick(object sender, MouseEventArgs e)
            
    {
                ISkinElement element 
    =
                  Helper.FindSkinElementFormPosition(
    this.skinMainFormElementList, e.Location);

                
    if (element != null)
                
    {
                    
    switch (element.Name)
                    
    {
                        
    case "ExitElement":
                            
    this.Close();
                            
    break;
                        
    default:
                            
    break;
                    }

                }

            }


    2.5 局部区域更新

    比如鼠标移动到"播放"按钮上时,其状态将转换为"MouseEnter",将重新绘制该按钮以响应鼠标.此时只需要更新该按钮所对应的区域便可:

     private void FormMain_MouseMove(object sender, MouseEventArgs e)
     
    {
         ISkinElement element 
    =
                 Helper.FindSkinElementFormPosition(
    this.skinMainFormElementList, e.Location);
         
    if (element != null &&
                    element.Status 
    != ElementStatus.MouseHover &&
                    element.Status 
    != ElementStatus.Disabled)
            
    {
                element.Status 
    = ElementStatus.MouseHover;
                
    this.UpdateSkinElement(element);
            }

    }

    其中UpdateSkinElement(ISkinElement element):
            /// <summary>
            
    /// 更新指定皮肤元素(重新绘制皮肤的指定区域)
            
    /// </summary>
            
    /// <param name="element">要被重绘的元素</param>

            private void UpdateSkinElement(ISkinElement element)
            
    {
                Rectangle updateRect 
    = new Rectangle(
                        element.Position.Left,
                        element.Position.Top,
                        element.Position.Right 
    - element.Position.Left,
                        element.Position.Bottom 
    - element.Position.Top);

                
    this.Invalidate(updateRect);
                
    this.Update();
            }


    3 DEMO下载:
    https://files.cnblogs.com/zhouyinhui/Pastime.rar
  • 相关阅读:
    SpringBoot学习笔记(14)----应用监控-HTTP方式
    SpringBoot学习笔记(13)----使用Spring Session+redis实现一个简单的集群
    SpringBoot学习笔记(12)----SpringBoot实现多个 账号轮询发送邮件
    SpringBoot学习笔记(11)-----SpringBoot中使用rabbitmq,activemq消息队列和rest服务的调用
    SpringBoot学习笔记(8)-----SpringBoot文件上传
    SpringBoot学习笔记(7)-----CORS支持解决跨域问题
    设计模式:迭代器模式(Iterator)
    设计模式:适配器模式(Adapter)
    设计模式:状态模式(State)
    设计模式:抽象工厂模式(Abstract Factory)
  • 原文地址:https://www.cnblogs.com/zhouyinhui/p/544132.html
Copyright © 2011-2022 走看看