Smartphon2003中没有RadioButton,但是RadioButton又是我们界面布局中常用的控件。虽然如今Smartphone2003已经很少了,但是总还有吧。我们不就碰到这样一个项目。

这里采用比较简单的方法,利用切换两张图片实现RadioButton选择与未选择时的效果,但是特别注意的是:Smartphone只有键盘没有触摸屏,因此控件之间的切换是通过导航键(上下左右)切换,因此一定要处理好这点。代码中注释很详细,参考如下:

public class RadioButton : Control
    {
        #region Fields
        private string _groupKey = string.Empty;
        private bool _checked = false;

        private const int CHeight = 24; //控件的高度
        private const int CTextOffset = 2;  //文字与RaidoButton之间的偏移距离;

        private Image _checkedImage = null;
        private Image _unCheckedImage = null;

        //private ImageAttributes _imgAttr = null;

        #endregion

        public RadioButton()
        {
            /// <summary>
            /// Windows.Forms 类撰写设计器支持所必需的
            /// </summary>
            InitializeComponent();
            //
            // TODO: 在 InitializeComponent 调用之后添加任何构造函数代码
            //

            //_imgAttr = new ImageAttributes();
            //_imgAttr.SetColorKey(Color.White, Color.White);

            _checkedImage = RS.GetCheckedRadioButton();
            _unCheckedImage = RS.GetDefaultRadioButton();
        }

        #region Properties

        /// <summary>
        /// 分组
        /// </summary>
        public string GroupKey
        {
            get { return _groupKey; }
            set { _groupKey = value; }
        }

        /// <summary>
        /// 是否选择;
        /// </summary>
        public bool Checked
        {
            get { return _checked; }
            set
            {
                if (value != _checked)
                {
                    _checked = value;
                    this.Invalidate();
                }
            }
        }

        #endregion

        #region Override

        protected override void OnGotFocus(EventArgs e)
        {
            foreach (Control ctl in this.Parent.Controls)
            {
                if (ctl is RadioButton)
                {
                    RadioButton btn = (RadioButton)ctl;
                    if (btn.GroupKey.Equals(this.GroupKey))
                    {
                        btn.Checked = false;
                    }
                }
            }
            this.Checked = true;
            base.OnGotFocus(e);

        }

        /// <summary>
        /// 限制控件的高度;
        /// </summary>
        /// <param name="e"></param>
        protected override void OnResize(EventArgs e)
        {
            this.Height = CHeight;
        }

        /// <summary>
        /// 处理上下控件的焦点切换
        /// </summary>
        /// <param name="e"></param>
        protected override void OnKeyDown(KeyEventArgs e)
        {
            int index = -1;
            switch (e.KeyCode)
            {
                case Keys.Down:
                    index = this.Parent.Controls.GetChildIndex(this);
                    do
                    {
                        if ((index + 1) == this.Parent.Controls.Count)
                            index = -1; //roll over
                        index++;
                    } while (!IsFocusable(this.Parent.Controls[index]));
                    break;

                case Keys.Up:
                    index = this.Parent.Controls.GetChildIndex(this);
                    do
                    {
                        if (index == 0)
                            index = this.Parent.Controls.Count; //roll over
                        index--;
                    } while (!IsFocusable(this.Parent.Controls[index]));
                    break;
            }
            if (index > -1 && index < this.Parent.Controls.Count)
                this.Parent.Controls[index].Focus();
            base.OnKeyDown(e);
        }

        protected override void Dispose(bool disposing)
        {
            if (_checkedImage != null)
            {
                _checkedImage.Dispose();
                _checkedImage = null;
            }
            if (_unCheckedImage != null)
            {
                _unCheckedImage.Dispose();
                _unCheckedImage = null;
            }
            base.Dispose(disposing);
        }
        #endregion

        #region 绘制RadioButton

        /// <summary>
        /// 绘制RadioButton
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {          
            //e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(Color.Black), 1f, 1f);
            DrawButton(e.Graphics);
        }

        private void DrawButton(Graphics g)
        {
            if (!Visible)
            {
                return;
            }
            //绘制Image && Text
            int imgWidth = 0;
            int imgHeight = 0;
            float txtHeight = 0;
            float txtWidth = 0;

            Image img;
            if (Checked)
            {
                img = _checkedImage;
            }
            else
            {
                img = _unCheckedImage;
            }

            imgWidth = img.Width;
            imgHeight = img.Height;

            if (Text.Length > 0)
            {
                SizeF txtSizeF = g.MeasureString(Text, Font);
                txtHeight = txtSizeF.Height;
                txtWidth = txtSizeF.Width;
            }

            int imgX = 2;
            int imgY = (Size.Height - imgHeight) / 2;

            //Rectangle rect = new Rectangle();
            //rect.X = imgX;
            //rect.Y = imgY;
            //rect.Width = imgWidth;
            //rect.Height = imgHeight;

            //g.DrawImage(img, rect, 0, 0, imgWidth, imgHeight, GraphicsUnit.Pixel, _imgAttr);
            g.DrawImage(img, imgX, imgY);

            float txtX = imgX + imgWidth + CTextOffset;
            float txtY = (Size.Height - txtHeight) / 2F;
            Color color = ForeColor;
            if (!Enabled)
            {
                color = SystemColors.GrayText;
            }
            Brush cl = new SolidBrush(color);
            try
            {
                g.DrawString(Text, Font, cl, txtX, txtY);
            }
            finally
            {
                cl.Dispose();
            }
        }

        private void DrawButton()
        {
            using (Graphics g = this.CreateGraphics())
            {
                DrawButton(g);
            }
        }
        #endregion

        #region Methods

        private bool IsFocusable(Control control)
        {
            if (control is Label)
                return false;
            else
                return true;
        }

        #endregion

        #region 组件设计器生成的代码
        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
        }
        #endregion
    }

相关文章: