Переход фокуса
От: urban1981  
Дата: 30.11.10 11:45
Оценка:
Здравствуйте.У меня приложение для Win CE 6 0. Есть свой Control
ImageButton
. Подскажите пожалуйста как сделать чтобы фокус переходил по нажатию клавиш "влево" "вправо". Заранее благодарю.


using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace ONK
{    

    public class ImageButton : Control
    {

        private Image image;
        private Bitmap m_bmpOffscreen;
        private bool toBlink = false;
        private bool bFocus;

        public Image Image
        {
            get
            {
                return image;
            }
            set
            {
                image = value;
                this.Invalidate(); 
            }
        }

        public bool ToBlink
        {
            get
            {
                return toBlink;
            }
            set
            {
                toBlink = value;
                this.Invalidate(); 
            }
        }

        public Color BlinkColor
        {
            get { return offColor; }
            set
            {
                offColor = value;
                Invalidate();
            }
        }

        public Color FocusPen
        {
            get { return focusPen; }
            set
            {
                focusPen = value;
                Invalidate();
            }
        }
        private Color focusPen = Color.Pink;

        private Color offColor = Color.LightGray;



        public ImageButton()
        {
            ToBlink = false;
            this.Size = new Size(21, 21);
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            Graphics gxOff;       //Offscreen graphics
            Rectangle imgRect; //image rectangle
            Brush backBrush;   //brush for filling a backcolor
            if (m_bmpOffscreen == null) //Bitmap for doublebuffering
            {
                m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height);
            }
            gxOff = Graphics.FromImage(m_bmpOffscreen);
            gxOff.Clear(this.BackColor);
            if (!ToBlink) backBrush = new SolidBrush(Parent.BackColor);
            else backBrush = new SolidBrush(BlinkColor);//change the background when it's pressed
            gxOff.FillRectangle(backBrush, this.ClientRectangle);
            if (image != null)
            {
                int imageLeft = (this.Width - image.Width) / 2;
                int imageTop = (this.Height - image.Height) / 2;
                if (!ToBlink)
                {
                    imgRect = new Rectangle(imageLeft, imageTop, image.Width, image.Height);
                }
                else //The button was pressed
                {
                    //Shift the image by one pixel
                    imgRect = new Rectangle(imageLeft + 1, imageTop + 1, image.Width, image.Height);
                }
                //Set transparent key
                ImageAttributes imageAttr = new ImageAttributes();
                imageAttr.SetColorKey(BackgroundImageColor(image), BackgroundImageColor(image));
                //Draw image
                gxOff.DrawImage(image, imgRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr);
            }

            if (ToBlink) //The button was pressed
            {
                //Prepare rectangle
                Rectangle rc = this.ClientRectangle;
                rc.Width--;
                rc.Height--;
                //Draw rectangle
                gxOff.DrawRectangle(new Pen(Color.Black), rc);
            }

            //Draw from the memory bitmap
            e.Graphics.DrawImage(m_bmpOffscreen, 0, 0);

            // Create a new pen.
            Pen focusPen = new Pen(FocusPen);

            // Set the pen's width.
            focusPen.Width = 2.0F;

            // Set the LineJoin property.
            focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;

            if (base.Focused)
                e.Graphics.DrawRectangle(focusPen, new Rectangle(Width * 10 / 200, Height * 10 / 200, Width * 90 / 100, Height * 90 / 100));
            focusPen.Dispose();
            base.OnPaint(e);
        }

        protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
        {
            //Do nothing
        }

        //private bool przPress = true;

        protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            ToBlink = true;
            base.OnMouseDown(e);
            this.Invalidate();
        }

        protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
        {
            ToBlink = false;
            base.OnMouseUp(e);
            this.Invalidate();
        }

        private Color BackgroundImageColor(Image image)
        {
            Bitmap bmp = new Bitmap(image);
            return bmp.GetPixel(0, 0);
        }

        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            this.Invalidate();
        }

        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
            this.Invalidate();
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);
            if (Parent.Equals(null))
                return;

            switch (e.KeyData)
            {
                case Keys.Right:
                    base.SelectNextControl(this, true, true, true, true);
                    break;
                case Keys.Left:
                    base.SelectNextControl(this, false, true, true, true);
                    break;                    
            }
            this.Invalidate();
        }

    }
}
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.