예제 #1
0
        protected override void OnPaint(PaintEventArgs e)
        {
            if (Image == null)
            {
                base.OnPaint(e);
            }
            else
            {
                InvokePaintBackground(this, e);
                PaintBackground(e);

                Size         textSize = Size.Empty;
                Image        image    = (IsMouseHover && HoverImage != null) ? HoverImage : Image;
                StringFormat sf       = new StringFormat();
                sf.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Hide;
                if (!string.IsNullOrEmpty(Text))
                {
                    textSize = Size.Ceiling(e.Graphics.MeasureString(Text, Font, Width - image.Width, sf));
                }

                Size clientSize = ClientSize;

                // Draw Image
                int totalWidth = image.Width + textSize.Width;
                //int totalHeight = Math.Max(Image.Height, textSize.Height);

                var rectImage = new Rectangle((clientSize.Width - totalWidth) / 2, (clientSize.Height - image.Height) / 2, image.Width, image.Height);

                if (!Enabled)
                {
                    PaintHelper.DrawImageDisabled(e.Graphics,
                                                  image,
                                                  rectImage,
                                                  new Rectangle(0, 0, image.Width, image.Height),
                                                  BackColor);
                }
                else
                {
                    e.Graphics.DrawImage(image
                                         , rectImage
                                         , 0, 0, image.Width, image.Height
                                         , GraphicsUnit.Pixel);
                }

                // Draw Text
                if (!string.IsNullOrEmpty(Text))
                {
                    e.Graphics.DrawString(Text
                                          , Font
                                          , new SolidBrush(ForeColor)
                                          , (clientSize.Width - totalWidth) / 2 + image.Width
                                          , (clientSize.Height - textSize.Height) / 2 + 2
                                          , sf);
                }
            }
        }