// Draw the button in its current state on a Graphics surface. internal virtual void Draw(Graphics graphics) { ButtonState state = CalculateState(); Size clientSize = ClientSize; int x = 0; int y = 0; int width = clientSize.Width; int height = clientSize.Height; // Draw the border and background. ThemeManager.MainPainter.DrawButton (graphics, x, y, width, height, state, ForeColor, BackColor, isDefault); // Draw the focus rectangle. if (hasFocus) { ControlPaint.DrawFocusRectangle (graphics, new Rectangle(x + 4, y + 4, width - 8, height - 8), ForeColor, BackColor); } // Draw the button image. Image image = this.image; if (image == null && imageList != null && imageIndex != -1) { image = imageList.Images[imageIndex]; } if (image != null) { int imageX = x; int imageY = y; int imageWidth = image.Width; int imageHeight = image.Height; switch (imageAlign) { case ContentAlignment.TopLeft: break; case ContentAlignment.TopCenter: { imageX += (width - imageWidth) / 2; } break; case ContentAlignment.TopRight: { imageX += width - imageWidth; } break; case ContentAlignment.MiddleLeft: { imageY += (height - imageHeight) / 2; } break; case ContentAlignment.MiddleCenter: { imageX += (width - imageWidth) / 2; imageY += (height - imageHeight) / 2; } break; case ContentAlignment.MiddleRight: { imageX += width - imageWidth; imageY += (height - imageHeight) / 2; } break; case ContentAlignment.BottomLeft: { imageY += height - imageHeight; } break; case ContentAlignment.BottomCenter: { imageX += (width - imageWidth) / 2; imageY += height - imageHeight; } break; case ContentAlignment.BottomRight: { imageX += width - imageWidth; imageY += height - imageHeight; } break; } if (pressed) { ++imageX; ++imageY; } if (Enabled) { graphics.DrawImage(image, imageX, imageY); } else { ControlPaint.DrawImageDisabled (graphics, image, imageX, imageY, BackColor); } } // Get the text layout rectangle. RectangleF layout = new RectangleF (x + 2, y + 2, width - 4, height - 4); if (entered && pressed) { layout.Offset(1.0f, 1.0f); } // If we are using "middle" alignment, then shift the // text down to account for the descent. This makes // the button "look better". Font font = Font; if ((TextAlign & (ContentAlignment.MiddleLeft | ContentAlignment.MiddleCenter | ContentAlignment.MiddleRight)) != 0) { layout.Offset(0.0f, GetDescent(graphics, font) / 2.0f); } // Draw the text on the button. String text = Text; if (text != null && text != String.Empty) { if (Enabled) { Brush brush = new SolidBrush(ForeColor); graphics.DrawString(text, font, brush, layout, format); brush.Dispose(); } else { ControlPaint.DrawStringDisabled (graphics, text, font, BackColor, layout, format); } } }
private void DrawImage(Graphics g) { if (this.ImageList == null || this.ImageIndex == -1) { return; } if (this.ImageIndex < 0 || this.ImageIndex >= this.ImageList.Images.Count) { return; } Image _Image = this.ImageList.Images[this.ImageIndex]; Point pt = Point.Empty; switch (this.ImageAlign) { case ContentAlignment.TopLeft: pt.X = contentRect.Left; pt.Y = contentRect.Top; break; case ContentAlignment.TopCenter: pt.X = (Width - _Image.Width) / 2; pt.Y = contentRect.Top; break; case ContentAlignment.TopRight: pt.X = contentRect.Right - _Image.Width; pt.Y = contentRect.Top; break; case ContentAlignment.MiddleLeft: pt.X = contentRect.Left; pt.Y = (Height - _Image.Height) / 2; break; case ContentAlignment.MiddleCenter: pt.X = (Width - _Image.Width) / 2; pt.Y = (Height - _Image.Height) / 2; break; case ContentAlignment.MiddleRight: pt.X = contentRect.Right - _Image.Width; pt.Y = (Height - _Image.Height) / 2; break; case ContentAlignment.BottomLeft: pt.X = contentRect.Left; pt.Y = contentRect.Bottom - _Image.Height; break; case ContentAlignment.BottomCenter: pt.X = (Width - _Image.Width) / 2; pt.Y = contentRect.Bottom - _Image.Height; break; case ContentAlignment.BottomRight: pt.X = contentRect.Right - _Image.Width; pt.Y = contentRect.Bottom - _Image.Height; break; } if (this.ButtonState == CustomButtonState.Pressed) { pt.Offset(1, 1); } if (this.Enabled) { this.ImageList.Draw(g, pt, this.ImageIndex); } else { ControlPaint.DrawImageDisabled(g, _Image, pt.X, pt.Y, this.BackColor); } }
private void DrawImage(Graphics g) { var image = Enabled ? ImageEnabled : (ImageDisabled ?? ImageEnabled); ImageAttributes imageAttr = null; if (null == image) { return; } if (m_monochrom) { imageAttr = new ImageAttributes(); // transform the monochrom image // white -> BackColor // black -> ForeColor var colorMap = new ColorMap[2]; colorMap[0] = new ColorMap { OldColor = Color.White, NewColor = BackColor }; colorMap[1] = new ColorMap { OldColor = Color.Black, NewColor = ForeColor }; imageAttr.SetRemapTable(colorMap); } Rectangle rect = new Rectangle(0, 0, image.Width, image.Height); if (!Enabled && (null == ImageDisabled)) { using (Bitmap bitmapMono = new Bitmap(image, ClientRectangle.Size)) { if (imageAttr != null) { using (Graphics gMono = Graphics.FromImage(bitmapMono)) { gMono.DrawImage(image, new[] { new Point(0, 0), new Point(image.Width - 1, 0), new Point(0, image.Height - 1) }, rect, GraphicsUnit.Pixel, imageAttr); } } ControlPaint.DrawImageDisabled(g, bitmapMono, 0, 0, BackColor); } } else { // Three points provided are upper-left, upper-right and // lower-left of the destination parallelogram. Point[] pts = new Point[3]; pts[0].X = Enabled && m_mouseOver && m_mouseCapture ? 1 : 0; pts[0].Y = Enabled && m_mouseOver && m_mouseCapture ? 1 : 0; pts[1].X = pts[0].X + ClientRectangle.Width; pts[1].Y = pts[0].Y; pts[2].X = pts[0].X; pts[2].Y = pts[1].Y + ClientRectangle.Height; if (imageAttr == null) { g.DrawImage(image, pts, rect, GraphicsUnit.Pixel); } else { g.DrawImage(image, pts, rect, GraphicsUnit.Pixel, imageAttr); } } }