ButtonRenderer クラス
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)


ButtonRenderer クラスには、ボタン コントロールを描画するために使用できる一連の static メソッドが用意されています。コントロールの描画とは、コントロールのユーザー インターフェイスを描画することです。ボタンを描画するには、DrawButton メソッドのうちいずれかを使用します。これらのメソッドには、ボタン上にテキストやイメージを描画するものなど、さまざまなオプションが用意されています。
オペレーティング システムで visual スタイルが有効にされており、現在のアプリケーションに visual スタイルが適用される場合、DrawButton は現在の visual スタイルでボタンを描画します。それ以外の場合、DrawButton はクラシック Windows スタイルでボタンを描画します。この機能は、描画するカスタム コントロールをオペレーティング システムの現在の visual スタイル設定に自動的に一致させる必要がある場合に便利です。
このクラスは、System.Windows.Forms.VisualStyles.VisualStyleElement.Button.PushButton クラスが公開する要素のいずれかに設定された System.Windows.Forms.VisualStyles.VisualStyleRenderer の機能をラップします。詳細については、「visual スタイルが使用されているコントロールのレンダリング」を参照してください。
Windows XP Home Edition, Windows XP Professional x64 Edition, Windows Server 2003 プラットフォームメモ : visual スタイルは、これらのプラットフォームでのみサポートされます。

DrawButton メソッドを使用してボタンを描画するカスタム コントロールを作成する方法を、次のコード例に示します。ボタンがクリックされると、コントロールは、元のボタンの境界内に小さいボタンを描画し、DrawParentBackground メソッドを使用して、元のボタンの残りの部分を塗りつぶします。
Imports System Imports System.Drawing Imports System.Windows.Forms Imports System.Windows.Forms.VisualStyles Namespace ButtonRendererSample Class Form1 Inherits Form Public Sub New() Dim Button1 As New CustomButton() Controls.Add(Button1) If Application.RenderWithVisualStyles Then Me.Text = "Visual Styles Enabled" Else Me.Text = "Visual Styles Disabled" End If End Sub <STAThread()> _ Shared Sub Main() ' If you do not call EnableVisualStyles below, then ' ButtonRenderer automatically detects this and draws ' the button without visual styles. Application.EnableVisualStyles() Application.Run(New Form1()) End Sub End Class Public Class CustomButton Inherits Control Private clickRectangleValue As New Rectangle() Private state As PushButtonState = PushButtonState.Normal Public Sub New() With Me Size = New Size(100, 40) Location = New Point(50, 50) Font = SystemFonts.IconTitleFont Text = "Click here" End With End Sub ' Define the bounds of the smaller pressed button. Public ReadOnly Property ClickRectangle() As Rectangle Get With clickRectangleValue .X = Me.ClientRectangle.X + CInt(0.2 * _ Me.ClientRectangle.Width) .Y = Me.ClientRectangle.Y + CInt(0.2 * _ Me.ClientRectangle.Height) .Width = Me.ClientRectangle.Width - _ CInt(0.4 * Me.ClientRectangle.Width) .Height = Me.ClientRectangle.Height - _ CInt(0.4 * Me.ClientRectangle.Height) End With Return clickRectangleValue End Get End Property ' Draw the large or small button, depending on the current state. Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) MyBase.OnPaint(e) ' Draw the smaller pressed button image. If state = PushButtonState.Pressed Then ' Set the background color to the parent if visual styles ' are disabled, because DrawParentBackground will only paint ' over the control background if visual styles are enabled. If Application.RenderWithVisualStyles Then Me.BackColor = Color.Azure Else Me.BackColor = Me.Parent.BackColor End If ' If you comment out the call to DrawParentBackground, ' the background of the control will still be visible ' outside the pressed button, if visual styles are enabled. ButtonRenderer.DrawParentBackground(e.Graphics, _ Me.ClientRectangle, Me) ButtonRenderer.DrawButton(e.Graphics, ClickRectangle, _ Me.Text, Me.Font, True, state) ' Draw the bigger unpressed button image. Else ButtonRenderer.DrawButton(e.Graphics, Me.ClientRectangle, _ Me.Text, Me.Font, False, state) End If End Sub ' Draw the smaller pressed button image. Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs) MyBase.OnMouseDown(e) With Me .Text = "Clicked!" .state = PushButtonState.Pressed End With Invalidate() End Sub ' Draw the button in the hot state. Protected Overrides Sub OnMouseEnter(ByVal e As EventArgs) MyBase.OnMouseEnter(e) With Me .Text = "Click here" .state = PushButtonState.Hot End With Invalidate() End Sub ' Draw the button in the unpressed state. Protected Overrides Sub OnMouseLeave(ByVal e As EventArgs) MyBase.OnMouseLeave(e) With Me .Text = "Click here" .state = PushButtonState.Normal End With Invalidate() End Sub ' Draw the button hot if the mouse is released on the button. Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs) MyBase.OnMouseUp(e) OnMouseEnter(e) End Sub ' Detect when the cursor leaves the button area while it ' is pressed. Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs) MyBase.OnMouseMove(e) ' Detect when the left mouse button is down and ' the cursor has left the pressed button area. If (e.Button And MouseButtons.Left) = MouseButtons.Left And Not _ Me.ClientRectangle.Contains(e.Location) And _ state = PushButtonState.Pressed Then OnMouseLeave(e) End If End Sub End Class End Namespace
using System; using System.Drawing; using System.Windows.Forms; using System.Windows.Forms.VisualStyles; namespace ButtonRendererSample { class Form1 : Form { public Form1() : base() { CustomButton Button1 = new CustomButton(); Controls.Add(Button1); if (Application.RenderWithVisualStyles) this.Text = "Visual Styles Enabled"; else this.Text = "Visual Styles Disabled"; } [STAThread] static void Main() { // If you do not call EnableVisualStyles below, then // ButtonRenderer automatically detects this and draws // the button without visual styles. Application.EnableVisualStyles(); Application.Run(new Form1()); } } public class CustomButton : Control { private Rectangle clickRectangleValue = new Rectangle(); private PushButtonState state = PushButtonState.Normal; public CustomButton() : base() { this.Size = new Size(100, 40); this.Location = new Point(50, 50); this.Font = SystemFonts.IconTitleFont; this.Text = "Click here"; } // Define the bounds of the smaller pressed button. public Rectangle ClickRectangle { get { clickRectangleValue.X = ClientRectangle.X + (int)(.2 * ClientRectangle.Width); clickRectangleValue.Y = ClientRectangle.Y + (int)(.2 * ClientRectangle.Height); clickRectangleValue.Width = ClientRectangle.Width - (int)(.4 * ClientRectangle.Width); clickRectangleValue.Height = ClientRectangle.Height - (int)(.4 * ClientRectangle.Height); return clickRectangleValue; } } // Draw the large or small button, depending on the current state. protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); // Draw the smaller pressed button image if (state == PushButtonState.Pressed) { // Set the background color to the parent if visual styles // are disabled, because DrawParentBackground will only paint // over the control background if visual styles are enabled. this.BackColor = Application.RenderWithVisualStyles ? Color.Azure : this.Parent.BackColor; // If you comment out the call to DrawParentBackground, // the background of the control will still be visible // outside the pressed button, if visual styles are enabled. ButtonRenderer.DrawParentBackground(e.Graphics, ClientRectangle, this); ButtonRenderer.DrawButton(e.Graphics, ClickRectangle, this.Text, this.Font, true, state); } // Draw the bigger unpressed button image. else { ButtonRenderer.DrawButton(e.Graphics, ClientRectangle, this.Text, this.Font, false, state); } } // Draw the smaller pressed button image. protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); this.Text = "Clicked!"; state = PushButtonState.Pressed; Invalidate(); } // Draw the button in the hot state. protected override void OnMouseEnter(EventArgs e) { base.OnMouseEnter(e); this.Text = "Click here"; state = PushButtonState.Hot; Invalidate(); } // Draw the button in the unpressed state. protected override void OnMouseLeave(EventArgs e) { base.OnMouseLeave(e); this.Text = "Click here"; state = PushButtonState.Normal; Invalidate(); } // Draw the button hot if the mouse is released on the button. protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); OnMouseEnter(e); } // Detect when the cursor leaves the button area while // it is pressed. protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); // Detect when the left mouse button is down and // the cursor has left the pressed button area. if ((e.Button & MouseButtons.Left) == MouseButtons.Left && !ClientRectangle.Contains(e.Location) && state == PushButtonState.Pressed) { OnMouseLeave(e); } } } }
#using <System.dll> #using <System.Drawing.dll> #using <System.Windows.Forms.dll> using namespace System; using namespace System::Drawing; using namespace System::Windows::Forms; using namespace System::Windows::Forms::VisualStyles; namespace ButtonRendererSample { public ref class CustomButton : public Control { private: Rectangle clickRectangleValue; private: PushButtonState state; public: CustomButton() { __super::Control(); this->Size = System::Drawing::Size(100, 40); this->Location = Point(50, 50); this->Font = SystemFonts::IconTitleFont; this->Text = "Click here"; clickRectangleValue = Rectangle(); state = PushButtonState::Normal; } // Define the bounds of the smaller pressed button. public: property Rectangle ClickRectangle { Rectangle get() { clickRectangleValue.X = ClientRectangle.X + (int)(.2 * ClientRectangle.Width); clickRectangleValue.Y = ClientRectangle.Y + (int)(.2 * ClientRectangle.Height); clickRectangleValue.Width = ClientRectangle.Width - (int)(.4 * ClientRectangle.Width); clickRectangleValue.Height = ClientRectangle.Height - (int)(.4 * ClientRectangle.Height); return clickRectangleValue; } } // Draw the large or small button, depending on the current state. protected: virtual void OnPaint(PaintEventArgs^ e) override { __super::OnPaint(e); // Draw the smaller pressed button image if (state == PushButtonState::Pressed) { // Set the background color to the parent if visual styles // are disabled, because DrawParentBackground will only paint // over the control background if visual styles are enabled. if (Application::RenderWithVisualStyles) { this->BackColor = Color::Azure; } else { this->BackColor = this->Parent->BackColor; } // If you comment out the call to DrawParentBackground , // the background of the control will still be visible // outside the pressed button, if visual styles are enabled. ButtonRenderer::DrawParentBackground(e->Graphics, ClientRectangle, this); ButtonRenderer::DrawButton(e->Graphics, ClickRectangle, this->Text, this->Font, true, state); } // Draw the bigger unpressed button image. else { ButtonRenderer::DrawButton(e->Graphics, ClientRectangle, this->Text, this->Font, false, state); } } // Draw the smaller pressed button image. protected: virtual void OnMouseDown(MouseEventArgs^ e) override { __super::OnMouseDown(e); this->Text = "Clicked!"; state = PushButtonState::Pressed; Invalidate(); } // Draw the button in the hot state. protected: virtual void OnMouseEnter(EventArgs^ e) override { __super::OnMouseEnter(e); this->Text = "Click here"; state = PushButtonState::Hot; Invalidate(); } // Draw the button in the unpressed state. protected: virtual void OnMouseLeave(EventArgs^ e) override { __super::OnMouseLeave(e); this->Text = "Click here"; state = PushButtonState::Normal; Invalidate(); } // Draw the button hot if the mouse is released on the button. protected: virtual void OnMouseUp(MouseEventArgs^ e) override { __super::OnMouseUp(e); OnMouseEnter(e); } // Detect when the cursor leaves the button area while // it is pressed. protected: virtual void OnMouseMove(MouseEventArgs^ e) override { __super::OnMouseMove(e); // Detect when the left mouse button is down and // the cursor has left the pressed button area. if ((e->Button & ::MouseButtons::Left) == ::MouseButtons::Left && !ClientRectangle.Contains(e->Location) && state == PushButtonState::Pressed) { OnMouseLeave(e); } } }; ref class Form1 : public Form { public: Form1() { __super::Form(); CustomButton^ Button1 = gcnew CustomButton(); Controls->Add(Button1); if (Application::RenderWithVisualStyles) { this->Text = "Visual Styles Enabled"; } else { this->Text = "Visual Styles Disabled"; } } }; } using namespace ButtonRendererSample; [STAThread] int main() { // If you do not call EnableVisualStyles below, then // ButtonRenderer automatically detects this and draws // the button without visual styles. Application::EnableVisualStyles(); Application::Run(gcnew Form1()); }

System.Windows.Forms.ButtonRenderer


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ButtonRenderer メンバ
System.Windows.Forms 名前空間
Application.EnableVisualStyles
Application.RenderWithVisualStyles プロパティ
System.Windows.Forms.VisualStyles.VisualStyleRenderer
System.Windows.Forms.VisualStyles.VisualStyleElement
ButtonRenderer プロパティ


関連項目
ButtonRenderer クラスSystem.Windows.Forms 名前空間
Application.EnableVisualStyles
Application.RenderWithVisualStyles プロパティ
System.Windows.Forms.VisualStyles.VisualStyleRenderer
System.Windows.Forms.VisualStyles.VisualStyleElement
ButtonRenderer メソッド

名前 | 説明 | |
---|---|---|
![]() | DrawButton | オーバーロードされます。 ボタン コントロールを描画します。 |
![]() | DrawParentBackground | 指定した領域にコントロールの親の背景を描画します。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | IsBackgroundPartiallyTransparent | ボタンの背景の一部が半透明、またはアルファ ブレンドされているかどうかを示します。 |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 ( Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |

関連項目
ButtonRenderer クラスSystem.Windows.Forms 名前空間
Application.EnableVisualStyles
Application.RenderWithVisualStyles プロパティ
System.Windows.Forms.VisualStyles.VisualStyleRenderer
System.Windows.Forms.VisualStyles.VisualStyleElement
ButtonRenderer メンバ
visual スタイルを使用して、または使用せずに、ボタン コントロールを描画するために使用されるメソッドを提供します。このクラスは継承できません。
ButtonRenderer データ型で公開されるメンバを以下の表に示します。


名前 | 説明 | |
---|---|---|
![]() | DrawButton | オーバーロードされます。 ボタン コントロールを描画します。 |
![]() | DrawParentBackground | 指定した領域にコントロールの親の背景を描画します。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。) |
![]() | GetType | 現在のインスタンスの Type を取得します。 (Object から継承されます。) |
![]() | IsBackgroundPartiallyTransparent | ボタンの背景の一部が半透明、またはアルファ ブレンドされているかどうかを示します。 |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。) |
![]() | ToString | 現在の Object を表す String を返します。 (Object から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |

関連項目
ButtonRenderer クラスSystem.Windows.Forms 名前空間
Application.EnableVisualStyles
Application.RenderWithVisualStyles プロパティ
System.Windows.Forms.VisualStyles.VisualStyleRenderer
System.Windows.Forms.VisualStyles.VisualStyleElement
Weblioに収録されているすべての辞書からButtonRendererを検索する場合は、下記のリンクをクリックしてください。

- ButtonRendererのページへのリンク