Esempio n. 1
0
        private void OnDrawTextAndImage(Graphics g)
        {
            SolidBrush brushText;

            if (Enabled)
            {
                brushText = new SolidBrush(ForeColor);
            }
            else
            {
                brushText = new SolidBrush(ControlPaint.DisabledForeColor);
            }

            StringFormat sf = ControlPaint.GetStringFormat(this.TextAlign);

            sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show;

            if (this.Image != null)
            {
                Rectangle rc         = new Rectangle();
                Point     ImagePoint = new Point(6, 4);
                switch (this.ImageAlign)
                {
                case ContentAlignment.MiddleRight:
                {
                    rc.Width     = this.ClientRectangle.Width - this.Image.Width - 8;
                    rc.Height    = this.ClientRectangle.Height;
                    rc.X         = 0;
                    rc.Y         = 0;
                    ImagePoint.X = rc.Width;
                    ImagePoint.Y = this.ClientRectangle.Height / 2 - Image.Height / 2;
                    break;
                }

                case ContentAlignment.TopCenter:
                {
                    ImagePoint.Y = 2;
                    ImagePoint.X = (this.ClientRectangle.Width - this.Image.Width) / 2;
                    rc.Width     = this.ClientRectangle.Width;
                    rc.Height    = this.ClientRectangle.Height - this.Image.Height - 4;
                    rc.X         = this.ClientRectangle.X;
                    rc.Y         = this.Image.Height;
                    break;
                }

                case ContentAlignment.MiddleCenter:
                {
                    ImagePoint.X = (this.ClientRectangle.Width - this.Image.Width) / 2;
                    ImagePoint.Y = (this.ClientRectangle.Height - this.Image.Height) / 2;
                    rc.Width     = 0;
                    rc.Height    = 0;
                    rc.X         = this.ClientRectangle.Width;
                    rc.Y         = this.ClientRectangle.Height;
                    break;
                }

                default:
                {
                    ImagePoint.X = 6;
                    ImagePoint.Y = this.ClientRectangle.Height / 2 - Image.Height / 2;
                    rc.Width     = this.ClientRectangle.Width - this.Image.Width;
                    rc.Height    = this.ClientRectangle.Height;
                    rc.X         = this.Image.Width;
                    rc.Y         = 0;
                    break;
                }
                }
                ImagePoint.X += locPoint.X;
                ImagePoint.Y += locPoint.Y;

                if (this.Enabled)
                {
                    g.DrawImage(this.Image, ImagePoint);
                }
                else
                {
                    System.Windows.Forms.ControlPaint.DrawImageDisabled(g, this.Image, ImagePoint.X, ImagePoint.Y, this.BackColor);
                }

                if (ContentAlignment.MiddleCenter != this.ImageAlign)
                {
                    g.DrawString(this.Text, this.Font, brushText, rc, sf);
                }
            }
            else
            {
                g.DrawString(this.Text, this.Font, brushText, this.ClientRectangle, sf);
            }

            brushText.Dispose();
            sf.Dispose();
        }