12.复选框UICheckBox类

再次声明:以下所有控件的图片素材来自于NeoForce Controls(http://www.tomshane.cz/neoforce/default.aspx),好像它没有公开源代码,所以代码参考自xWinForms 3.0(http://sourceforge.net/projects/xwinforms/)。

CheckBox和Button的区别在于:

控件使用的图像如下:

复选框使用的图像

以下是复选框的代码:

namespace StunEngine.SceneNodes.UI
{
    /// <summary>
    /// 复选框控件
    /// </summary>
    public class UICheckBox:UIButtonBase
    {
        成员变量和构造函数

        属性

        事件

        /// <summary>
        /// 绘制复选框
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Draw(GameTime gameTime,bool useReflection)
        {
            if (isVisible == true)
            {               
                //如果文字不为null,则绘制文字
                if (text != null)
                {
                    Color textColor = IsMouseInside || (uiManager.ActiveControl == this) ? textColorHighlight : TextColor;
                    
                    // 设置文本的淡入淡出的透明度
                    Color alphaTextColor = new Color(textColor.R, textColor.G, textColor.B, scene.TransitionAlpha);
                    //绘制文本
                    StunXnaGE.SpriteBatch.DrawString(font, text, new Vector2(position.X+imageSize .X+textOffsetX , position.Y + size.Y / 2 - font.MeasureString(text).Y / 2), alphaTextColor, rotation, origin, 1.0f, spriteEffect, layerDepth);
                }                
                
                //获取图像淡入淡出的透明颜色
                alphaTextureColor = new Color(color, scene.TransitionAlpha);
                
                if (!_checked)
                {
                    //根据鼠标移入还是按下设置图像源矩阵
                    Rectangle rect = IsMouseInside || (uiManager.ActiveControl == this) ? rectSourceHighlight: rectSourceOrigin;
                    //绘制按钮背景图像
                    StunXnaGE.SpriteBatch.Draw(material.Textures[0], new Rectangle((int)position.X, (int)(position.Y + size.Y / 2-imageSize.Y  /2), (int)imageSize.X, (int)imageSize.Y), rect, alphaTextureColor, rotation, origin, spriteEffect, layerDepth);
                }
                else
                {
                    //根据鼠标移入还是按下设置图像源矩阵
                    Rectangle rect = IsMouseInside || (uiManager.ActiveControl == this) ? rectSourceHighlightChecked : rectSourceChecked;
                    //绘制按钮背景图像
                    StunXnaGE.SpriteBatch.Draw(material.Textures[0], new Rectangle((int)position.X, (int)(position.Y + size.Y / 2 - imageSize.Y / 2), (int)imageSize.X, (int)imageSize.Y), rect, alphaTextureColor, rotation, origin, spriteEffect, layerDepth);
                }
                
                //鼠标点击切换选中状态
                if (IsMouseInside && Input.MouseLeftButtonJustPressed)
                {
                    this.OnCheckedChanged(EventArgs .Empty );
                }
            }
        }

        单元测试
        

    }
}

单元测试截图如下:

单元测试截图


发布时间:2010/1/18 11:38:57  阅读次数:7129

2006 - 2024,推荐分辨率1024*768以上,推荐浏览器Chrome、Edge等现代浏览器,截止2021年12月5日的访问次数:1872万9823 站长邮箱

沪ICP备18037240号-1

沪公网安备 31011002002865号