ToolStripRenderer 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
ToolStrip 개체에 대한 그리기 기능을 처리합니다.
public ref class ToolStripRenderer abstract
public abstract class ToolStripRenderer
type ToolStripRenderer = class
Public MustInherit Class ToolStripRenderer
- 상속
-
ToolStripRenderer
- 파생
예제
다음 코드 예제에서는 사용자 지정 ToolStripRenderer 클래스를 구현하는 방법을 보여 줍니다. 클래스는 GridStripRenderer
컨트롤 모양 GridStrip
에서 GridStrip
테두리, ToolStripButton 테두리 및 ToolStripButton 이미지의 세 가지 측면을 사용자 지정합니다. 전체 코드 목록은 참조 하세요 방법: 사용자 지정 ToolStripRenderer 구현합니다.
// This class implements a custom ToolStripRenderer for the
// GridStrip control. It customizes three aspects of the
// GridStrip control's appearance: GridStrip border,
// ToolStripButton border, and ToolStripButton image.
internal class GridStripRenderer : ToolStripRenderer
{
// The style of the empty cell's text.
private static StringFormat style = new StringFormat();
// The thickness (width or height) of a
// ToolStripButton control's border.
static int borderThickness = 2;
// The main bitmap that is the source for the
// subimagesthat are assigned to individual
// ToolStripButton controls.
private Bitmap bmp = null;
// The brush that paints the background of
// the GridStrip control.
private Brush backgroundBrush = null;
// This is the static constructor. It initializes the
// StringFormat for drawing the text in the empty cell.
static GridStripRenderer()
{
style.Alignment = StringAlignment.Center;
style.LineAlignment = StringAlignment.Center;
}
// This method initializes the GridStripRenderer by
// creating the image that is used as the source for
// the individual button images.
protected override void Initialize(ToolStrip ts)
{
base.Initialize(ts);
this.InitializeBitmap(ts);
}
// This method initializes an individual ToolStripButton
// control. It copies a subimage from the GridStripRenderer's
// main image, according to the position and size of
// the ToolStripButton.
protected override void InitializeItem(ToolStripItem item)
{
base.InitializeItem(item);
GridStrip gs = item.Owner as GridStrip;
// The empty cell does not receive a subimage.
if ((item is ToolStripButton) &&
(item != gs.EmptyCell))
{
// Copy the subimage from the appropriate
// part of the main image.
Bitmap subImage = bmp.Clone(
item.Bounds,
PixelFormat.Undefined);
// Assign the subimage to the ToolStripButton
// control's Image property.
item.Image = subImage;
}
}
// This utility method creates the main image that
// is the source for the subimages of the individual
// ToolStripButton controls.
private void InitializeBitmap(ToolStrip toolStrip)
{
// Create the main bitmap, into which the image is drawn.
this.bmp = new Bitmap(
toolStrip.Size.Width,
toolStrip.Size.Height);
// Draw a fancy pattern. This could be any image or drawing.
using (Graphics g = Graphics.FromImage(bmp))
{
// Draw smoothed lines.
g.SmoothingMode = SmoothingMode.AntiAlias;
// Draw the image. In this case, it is
// a number of concentric ellipses.
for (int i = 0; i < toolStrip.Size.Width; i += 8)
{
g.DrawEllipse(Pens.Blue, 0, 0, i, i);
}
}
}
// This method draws a border around the GridStrip control.
protected override void OnRenderToolStripBorder(
ToolStripRenderEventArgs e)
{
base.OnRenderToolStripBorder(e);
ControlPaint.DrawFocusRectangle(
e.Graphics,
e.AffectedBounds,
SystemColors.ControlDarkDark,
SystemColors.ControlDarkDark);
}
// This method renders the GridStrip control's background.
protected override void OnRenderToolStripBackground(
ToolStripRenderEventArgs e)
{
base.OnRenderToolStripBackground(e);
// This late initialization is a workaround. The gradient
// depends on the bounds of the GridStrip control. The bounds
// are dependent on the layout engine, which hasn't fully
// performed layout by the time the Initialize method runs.
if (this.backgroundBrush == null)
{
this.backgroundBrush = new LinearGradientBrush(
e.ToolStrip.ClientRectangle,
SystemColors.ControlLightLight,
SystemColors.ControlDark,
90,
true);
}
// Paint the GridStrip control's background.
e.Graphics.FillRectangle(
this.backgroundBrush,
e.AffectedBounds);
}
// This method draws a border around the button's image. If the background
// to be rendered belongs to the empty cell, a string is drawn. Otherwise,
// a border is drawn at the edges of the button.
protected override void OnRenderButtonBackground(
ToolStripItemRenderEventArgs e)
{
base.OnRenderButtonBackground(e);
// Define some local variables for convenience.
Graphics g = e.Graphics;
GridStrip gs = e.ToolStrip as GridStrip;
ToolStripButton gsb = e.Item as ToolStripButton;
// Calculate the rectangle around which the border is painted.
Rectangle imageRectangle = new Rectangle(
borderThickness,
borderThickness,
e.Item.Width - 2 * borderThickness,
e.Item.Height - 2 * borderThickness);
// If rendering the empty cell background, draw an
// explanatory string, centered in the ToolStripButton.
if (gsb == gs.EmptyCell)
{
e.Graphics.DrawString(
"Drag to here",
gsb.Font,
SystemBrushes.ControlDarkDark,
imageRectangle, style);
}
else
{
// If the button can be a drag source, paint its border red.
// otherwise, paint its border a dark color.
Brush b = gs.IsValidDragSource(gsb) ? b =
Brushes.Red : SystemBrushes.ControlDarkDark;
// Draw the top segment of the border.
Rectangle borderSegment = new Rectangle(
0,
0,
e.Item.Width,
imageRectangle.Top);
g.FillRectangle(b, borderSegment);
// Draw the right segment.
borderSegment = new Rectangle(
imageRectangle.Right,
0,
e.Item.Bounds.Right - imageRectangle.Right,
imageRectangle.Bottom);
g.FillRectangle(b, borderSegment);
// Draw the left segment.
borderSegment = new Rectangle(
0,
0,
imageRectangle.Left,
e.Item.Height);
g.FillRectangle(b, borderSegment);
// Draw the bottom segment.
borderSegment = new Rectangle(
0,
imageRectangle.Bottom,
e.Item.Width,
e.Item.Bounds.Bottom - imageRectangle.Bottom);
g.FillRectangle(b, borderSegment);
}
}
}
' This class implements a custom ToolStripRenderer for the
' GridStrip control. It customizes three aspects of the
' GridStrip control's appearance: GridStrip border,
' ToolStripButton border, and ToolStripButton image.
Friend Class GridStripRenderer
Inherits ToolStripRenderer
' The style of the empty cell's text.
Private Shared style As New StringFormat()
' The thickness (width or height) of a
' ToolStripButton control's border.
Private Shared borderThickness As Integer = 2
' The main bitmap that is the source for the
' subimagesthat are assigned to individual
' ToolStripButton controls.
Private bmp As Bitmap = Nothing
' The brush that paints the background of
' the GridStrip control.
Private backgroundBrush As Brush = Nothing
' This is the static constructor. It initializes the
' StringFormat for drawing the text in the empty cell.
Shared Sub New()
style.Alignment = StringAlignment.Center
style.LineAlignment = StringAlignment.Center
End Sub
' This method initializes the GridStripRenderer by
' creating the image that is used as the source for
' the individual button images.
Protected Overrides Sub Initialize(ts As ToolStrip)
MyBase.Initialize(ts)
Me.InitializeBitmap(ts)
End Sub
' This method initializes an individual ToolStripButton
' control. It copies a subimage from the GridStripRenderer's
' main image, according to the position and size of
' the ToolStripButton.
Protected Overrides Sub InitializeItem(item As ToolStripItem)
MyBase.InitializeItem(item)
Dim gs As GridStrip = item.Owner
' The empty cell does not receive a subimage.
If ((TypeOf (item) Is ToolStripButton) And _
(item IsNot gs.EmptyCell)) Then
' Copy the subimage from the appropriate
' part of the main image.
Dim subImage As Bitmap = bmp.Clone(item.Bounds, PixelFormat.Undefined)
' Assign the subimage to the ToolStripButton
' control's Image property.
item.Image = subImage
End If
End Sub
' This utility method creates the main image that
' is the source for the subimages of the individual
' ToolStripButton controls.
Private Sub InitializeBitmap(toolStrip As ToolStrip)
' Create the main bitmap, into which the image is drawn.
Me.bmp = New Bitmap(toolStrip.Size.Width, toolStrip.Size.Height)
' Draw a fancy pattern. This could be any image or drawing.
Dim g As Graphics = Graphics.FromImage(bmp)
Try
' Draw smoothed lines.
g.SmoothingMode = SmoothingMode.AntiAlias
' Draw the image. In this case, it is
' a number of concentric ellipses.
Dim i As Integer
For i = 0 To toolStrip.Size.Width - 8 Step 8
g.DrawEllipse(Pens.Blue, 0, 0, i, i)
Next i
Finally
g.Dispose()
End Try
End Sub
' This method draws a border around the GridStrip control.
Protected Overrides Sub OnRenderToolStripBorder(e As ToolStripRenderEventArgs)
MyBase.OnRenderToolStripBorder(e)
ControlPaint.DrawFocusRectangle(e.Graphics, e.AffectedBounds, SystemColors.ControlDarkDark, SystemColors.ControlDarkDark)
End Sub
' This method renders the GridStrip control's background.
Protected Overrides Sub OnRenderToolStripBackground(e As ToolStripRenderEventArgs)
MyBase.OnRenderToolStripBackground(e)
' This late initialization is a workaround. The gradient
' depends on the bounds of the GridStrip control. The bounds
' are dependent on the layout engine, which hasn't fully
' performed layout by the time the Initialize method runs.
If Me.backgroundBrush Is Nothing Then
Me.backgroundBrush = New LinearGradientBrush(e.ToolStrip.ClientRectangle, SystemColors.ControlLightLight, SystemColors.ControlDark, 90, True)
End If
' Paint the GridStrip control's background.
e.Graphics.FillRectangle(Me.backgroundBrush, e.AffectedBounds)
End Sub
' This method draws a border around the button's image. If the background
' to be rendered belongs to the empty cell, a string is drawn. Otherwise,
' a border is drawn at the edges of the button.
Protected Overrides Sub OnRenderButtonBackground(e As ToolStripItemRenderEventArgs)
MyBase.OnRenderButtonBackground(e)
' Define some local variables for convenience.
Dim g As Graphics = e.Graphics
Dim gs As GridStrip = e.ToolStrip
Dim gsb As ToolStripButton = e.Item
' Calculate the rectangle around which the border is painted.
Dim imageRectangle As New Rectangle(borderThickness, borderThickness, e.Item.Width - 2 * borderThickness, e.Item.Height - 2 * borderThickness)
' If rendering the empty cell background, draw an
' explanatory string, centered in the ToolStripButton.
If gsb Is gs.EmptyCell Then
e.Graphics.DrawString("Drag to here", gsb.Font, SystemBrushes.ControlDarkDark, imageRectangle, style)
Else
' If the button can be a drag source, paint its border red.
' otherwise, paint its border a dark color.
Dim b As Brush = IIf(gs.IsValidDragSource(gsb), Brushes.Red, SystemBrushes.ControlDarkDark)
' Draw the top segment of the border.
Dim borderSegment As New Rectangle(0, 0, e.Item.Width, imageRectangle.Top)
g.FillRectangle(b, borderSegment)
' Draw the right segment.
borderSegment = New Rectangle(imageRectangle.Right, 0, e.Item.Bounds.Right - imageRectangle.Right, imageRectangle.Bottom)
g.FillRectangle(b, borderSegment)
' Draw the left segment.
borderSegment = New Rectangle(0, 0, imageRectangle.Left, e.Item.Height)
g.FillRectangle(b, borderSegment)
' Draw the bottom segment.
borderSegment = New Rectangle(0, imageRectangle.Bottom, e.Item.Width, e.Item.Bounds.Bottom - imageRectangle.Bottom)
g.FillRectangle(b, borderSegment)
End If
End Sub
End Class
설명
클래스를 ToolStripRenderer 사용하여 특정 스타일 또는 테마를 에 적용합니다 ToolStrip. 사용자 지정 그리기 대신는 및 ToolStripItemToolStrip 포함 된 개체를 속성에서 ToolStripRenderer상속 하는 개체로 설정 ToolStrip.Renderer 합니다. 에 의해 ToolStripRenderer 지정된 그리기는 포함 된 항목 뿐만 아니라 에 적용 ToolStrip됩니다.
여러 가지 방법으로 ToolStrip 컨트롤에서 사용자 지정 그리기 작업을 수행할 수 있습니다. 다른 Windows Forms 컨트롤과 마찬가지로 ToolStrip 및 ToolStripItem은 둘 다 재정의 가능한 OnPaint
메서드와 Paint
이벤트가 있습니다. 일반 그리기와 마찬가지로 좌표계는 컨트롤의 클라이언트 영역을 기준으로 합니다. 즉, 컨트롤의 왼쪽 위 모서리는 0, 0입니다.
ToolStripItem에 대한 Paint
이벤트 및 OnPaint
메서드는 다른 컨트롤 페인트 이벤트처럼 동작합니다.
클래스에는 ToolStripRenderer 의 배경, 항목 배경, 항목 이미지, 항목 화살표, 항목 텍스트 및 테두리를 그리기 위한 재정의 가능한 메서드가 있습니다 ToolStrip. 이러한 메서드에 대한 이벤트 인수는 원하는 대로 조정할 수 있는 사각형, 색 및 텍스트 형식과 같은 여러 속성을 노출합니다.
항목을 그리는 방법의 몇 가지 측면만 조정하려면 일반적으로 ToolStripRenderer를 재정의합니다.
새 항목을 작성하고 그림의 모든 측면을 제어하려면 OnPaint
메서드를 재정의합니다.
OnPaint
내에서 ToolStripRenderer의 메서드를 사용할 수 있습니다.
기본적으로 ToolStrip은 이중 버퍼링되어 OptimizedDoubleBuffer 설정을 활용합니다.
생성자
ToolStripRenderer() |
ToolStripRenderer 클래스의 새 인스턴스를 초기화합니다. |
필드
Offset2X |
x축의 오프셋을 두 배로 늘리기 위한 오프셋 승수를 가져오거나 설정합니다. |
Offset2Y |
y축의 오프셋을 두 배로 늘리기 위한 오프셋 승수를 가져오거나 설정합니다. |
메서드
이벤트
적용 대상
추가 정보
.NET