panel重写碰到的问题

来源:互联网 发布:常州中医院预约软件 编辑:程序博客网 时间:2024/05/09 17:20

在写酒店房态的时候,碰到这样的一个问题,请大家帮忙解决,我在加载不同的房态图的时候,我要显不同的房态,

 

房间号的背景色,用不同的颜色表示,可是我不同在外面赋不同的值,也能得到不同的值,可是每次还是加载的初始

 

 

的颜色值

 

代码如下:

 

public partial class RoomPanel : UserControl
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(RoomPanel));

        BufferedGraphicsContext currentContext = new BufferedGraphicsContext();
        /// <summary>
        /// 图形缓冲区
        /// </summary>
        private BufferedGraphics myBuffer;
        private SmallImage[] smallimageList = new SmallImage[2];
        private int index;
        private IContainer components;
        /// <summary>
        /// 索引
        /// </summary>
        public int Index
        {
            get { return index; }
            set { index = value; }
        }
        private string roomID;
        /// <summary>
        /// 房间
        /// </summary>
        public string RoomID
        {
            get { return roomID; }
            set { roomID = value; }
        }

 

        public RoomPanel()
        {
            // InitializeComponent();
            solidbrush = new SolidBrush(this.GetCurrentColor());

            //////设置图片显示信息
            //smallimageList[0] = new SmallImage();
            //smallimageList[1] = new SmallImage();
            //smallimageList[0].ImageName = "0";
            //smallimageList[0].Visible = true;
            //smallimageList[0].Rectanlge = new Rectangle(0, this.Height / 2 - 8, 16, 16);
            //smallimageList[1].ImageName = "1";
            //smallimageList[1].Visible = true;
            //smallimageList[1].Rectanlge = new Rectangle(this.Width - 16, this.Height / 2 - 8, 16, 16);
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            myBuffer = currentContext.Allocate(this.CreateGraphics(), this.ClientRectangle);
            DrawPanel(myBuffer.Graphics);
        }
        TextFormatFlags flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter;
        // Image image;
        private SolidBrush solidbrush;
        /// <summary>
        /// 保存显示的内容
        /// </summary>
        private string[] content;

        /// <summary>
        /// 设置显示的内容
        /// </summary>
        public void SetText(string[] text)
        {
            content = text;
            this.Refresh();
        }

        private int colorstate;
        public int ColorState
        {
            get { return colorstate; }
            set { colorstate = value; }

        }


        public virtual Color GetCurrentColor()
        {
            int state = this.ColorState;
            Color color1 = DefaultBackColor;
            switch (state)
            {
                case 0:
                    color1 = Color.Red;
                    break;
                case 2:
                    color1 = Color.Yellow;
                    break;
                case 3:
                    color1 = Color.Blue;
                    break;
            }

            return color1;
        }

        public override Color BackColor
        {
            get
            {
                return this.GetCurrentColor();
            }

        }


        protected override void OnPaintBackground(PaintEventArgs e)
        {

        }
        protected override void OnPaint(PaintEventArgs e)
        {
            myBuffer.Render();
            if (!selected)
            {
                if (this.Focused)
                {
                    DrawFocusStyle(e.Graphics);
                }
            }
        }
        /// <summary>
        /// 绘制控件到缓冲区
        /// </summary>
        /// <param name="g"></param>
        private void DrawPanel(Graphics g)
        {
            //填冲背景色
           
            g.FillRectangle(solidbrush, this.ClientRectangle);

            ////画一个边框
            g.DrawRectangle(Pens.BurlyWood, 0, 0, this.Width - 1, this.Height - 1);

            //画文字
            DrawText(g);

            //画选中时的边框
            if (selected)
            {
                g.DrawRectangle(Pens.Blue, 0, 0, this.Width - 1, this.Height - 1);
                g.DrawRectangle(Pens.Blue, 1, 1, this.Width - 3, this.Height - 3);
                g.DrawRectangle(Pens.Blue, 2, 2, this.Width - 5, this.Height - 5);
                //g.DrawRectangle(Pens.BlueViolet, 2, 2, this.Width - 4, this.Height - 4);
            }

            //画小图标
            DrawSmallIco(g);

        }

        /// <summary>
        /// 绘制获得焦点的样式
        /// </summary>
        /// <param name="g"></param>
        private void DrawFocusStyle(Graphics g)
        {
            SolidBrush br = new SolidBrush(Color.FromArgb(100, Color.CadetBlue));
            g.FillRectangle(br, 0, 0, this.Width - 1, this.Height - 1);
            g.DrawRectangle(Pens.Blue, 0, 0, this.Width - 1, this.Height - 1);
            br.Dispose();
        }

        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            //
            Graphics g = this.CreateGraphics();
            DrawFocusStyle(g);
        }

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

        /// <summary>
        /// 画文字
        /// </summary>
        /// <param name="g"></param>
        private void DrawText(Graphics g)
        {
            if (content != null && content.Length > 0)
            {

                int height = this.Height / content.Length;
                Rectangle rectangle = new Rectangle();
                rectangle.X = 0;
                rectangle.Y = 0;
                rectangle.Width = this.Width;
                rectangle.Height = height;
                foreach (string temp in content)
                {
                    TextRenderer.DrawText(g, temp, this.Font, rectangle, this.ForeColor, flags);
                    rectangle.Y += height;
                }
            }
        }
        private void SynvRoomPanel_BackColorChanged(object sender, EventArgs e)
        {
            solidbrush.Color = GetCurrentColor(); //BackColor;
            this.Refresh();
        }
        /// <summary>
        /// 设置图标是否可见
        /// </summary>
        public void IcoVisible(int imageIndex, bool visible)
        {
            if (imageIndex > -1 && imageIndex < smallimageList.Length)
            {
                if (visible != smallimageList[imageIndex].Visible)
                {
                    smallimageList[imageIndex].Visible = visible;
                    this.Refresh();

                }
            }
        }
        /// <summary>
        /// 强制更新
        /// </summary>
        public override void Refresh()
        {
            if (myBuffer != null)
            {
                DrawPanel(myBuffer.Graphics);
                myBuffer.Render();
            }
        }

        /// <summary>
        /// 重新设置为初始状态
        /// </summary>
        public void Reset()
        {
            this.content = null;
            this.selected = false;
            this.roomID = string.Empty;
            this.BackColor = Color.White;

        }
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            this.Focus();
        }
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            this.Cursor = Cursors.Default;
            this.Refresh();
        }
        private bool selected = false;
        /// <summary>
        /// 设置或获得选中状态
        /// </summary>
        public bool Selected
        {
            get { return selected; }
            set
            {
                if (selected != value)
                {
                    selected = value;
                    this.Refresh();
                }


            }
        }
        /// <summary>
        /// 画小图标
        /// </summary>
        /// <param name="g"></param>
        private void DrawSmallIco(Graphics g)
        {
            //if (smallimageList != null && smallimageList.Length > 0)
            //{
            //    foreach (SmallImage smallimage in smallimageList)
            //    {
            //        if (smallimage.Visible)
            //        {
            //            image = (Image)resources.GetObject(smallimage.ImageName);
            //            g.DrawImage(image, smallimage.Rectanlge);
            //        }
            //    }
            //}

        }
        /// <summary>
        /// 小图片信息
        /// </summary>
        private class SmallImage
        {
            bool visible = false;
            /// <summary>
            /// 是否显示
            /// </summary>
            public bool Visible
            {
                get { return visible; }
                set { visible = value; }
            }

            private string imageName;
            /// <summary>
            /// 显示的图片名称
            /// </summary>
            public string ImageName
            {
                get { return imageName; }
                set { imageName = value; }
            }

            private Rectangle rectanlge;
            /// <summary>
            /// 显示的大小
            /// </summary>
            public Rectangle Rectanlge
            {
                get { return rectanlge; }
                set { rectanlge = value; }
            }
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);
            //按回车时触发
            if (e.KeyChar == 13)
            {
                this.OnClick(EventArgs.Empty);
            }
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            //
            // RoomPanel
            //
            this.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.Name = "RoomPanel";
            this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.RoomPanel_MouseClick);
            this.ResumeLayout(false);

        }

        private void RoomPanel_MouseClick(object sender, MouseEventArgs e)
        {
            MessageBox.Show("panel");
        }
    }

原创粉丝点击