VisualStyleElementとは? わかりやすく解説

VisualStyleElement クラス

メモ : このクラスは、.NET Framework version 2.0 で新しく追加されたものです。

visual スタイルで描画するコントロール要素またはユーザー インターフェイス (UI) 要素を示します。

名前空間: System.Windows.Forms.VisualStyles
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

Public Class VisualStyleElement
Dim instance As VisualStyleElement
public class VisualStyleElement
public ref class VisualStyleElement
public class VisualStyleElement
public class VisualStyleElement
解説解説

VisualStyleElement クラスは、オペレーティング システムの現在の visual スタイルで定義された Windows コントロール要素またはユーザー インターフェイス (UI) 要素を示します。visual スタイル要素は、クラス、部品、および状態によって定義されます。

System.Windows.Forms.VisualStyles 名前空間は、visual スタイルで定義できるコントロールとユーザー インターフェイス要素を表す数百個の VisualStyleElement オブジェクトを公開します。これらのオブジェクトは、VisualStyleElement の入れ子になったクラスの static プロパティとして公開されます。それぞれのプロパティについて、保有するクラスの修飾名が描画する要素のクラスと部品を表し、プロパティ名が要素の状態を表します。たとえば、VisualStyleElement.Button.PushButton.Pressed プロパティは、押された状態のボタンを visual スタイルで描画するのに使用できる VisualStyleElement を返します。Pressed プロパティ名は状態に対応し、PushButton は部品に対応し、Button は visual スタイル要素のクラスを示します。

visual スタイル要素を描画するには、VisualStyleRenderer を作成し、描画する VisualStyleElement にそれを設定して、DrawBackground メソッドを呼び出します。現在の visual スタイルで特定の要素の定義が提供されているかどうかを判断するには、その要素を引数として使用して IsElementDefined メソッドを呼び出します。

VisualStyleElement と VisualStyleRenderer は、Windows プラットフォーム SDK の Windows シェルから visual スタイル API の機能をラップします。visual スタイルの詳細については、MSDN ライブラリ (http://msdn.microsoft.com/library/) のプラットフォーム SDK ドキュメントの「Using Windows XP Visual Styles」を参照してください。

Windows XP Home Edition, Windows XP Professional x64 Edition, Windows Server 2003 プラットフォームメモ : visual スタイルは、これらのプラットフォームでのみサポートされます。

使用例使用例

System.Windows.Forms.VisualStyles 名前空間で公開されるすべての VisualStyleElement オブジェクトを参照および描画するための UI を提供する、完全なアプリケーションのコード例を次に示します。要素を表示するには、ウィンドウの左側にある ListView コントロールを使用して、描画する要素の状態を表すノードに移動し、そのノードをクリックします。このサンプルでは、現在の visual スタイルで要素が定義されている場合にのみ、選択した要素が描画されます。

Imports System
Imports System.Text
Imports System.Drawing
Imports System.Collections.Generic
Imports System.Reflection
Imports System.Windows.Forms
Imports System.Windows.Forms.VisualStyles

Namespace VisualStyleElementViewer

    Class Form1
        Inherits Form

        Public Sub New()
            Dim ElementViewer1 As New
 ElementViewer()
            With Me
                .Controls.Add(ElementViewer1)
                .Text = ElementViewer1.Text
                .Size = New Size(700, 550)
            End With
        End Sub

        <STAThread()> Shared Sub Main()
            Application.EnableVisualStyles()
            Application.Run(New Form1())
        End Sub
    End Class

    Public Class ElementViewer
        Inherits UserControl

        Private element As VisualStyleElement
        Private renderer As VisualStyleRenderer
        Private elementDictionary As New
 Dictionary(Of String, _
            VisualStyleElement)
        Private descriptionRect As Rectangle
        Private displayRect As Rectangle
        Private displayRectFull As Rectangle
        Private currentTrueSize As New
 Size()
        Private elementDescription As New
 StringBuilder()
        Private label1 As New
 Label()
        Private WithEvents treeView1 As
 New TreeView()
        Private WithEvents domainUpDown1 As
 New DomainUpDown()
        Private drawElement As Boolean
 = False

        Public Sub New()
            With Me
                .Location = New Point(10, 10)
                .Size = New Size(650, 500)
                .Text = "VisualStyleElement Viewer"
                .Font = SystemFonts.IconTitleFont
                .BackColor = Color.White
                .BorderStyle = BorderStyle.Fixed3D
                .AutoSize = True
            End With
        End Sub 'New

        Private Sub ElementViewer_Load(ByVal
 sender As Object, _
            ByVal e As EventArgs) Handles
 Me.Load

            ' Make sure the visual styles are enabled before 
            ' going any further.
            If Not Application.RenderWithVisualStyles
 Then
                Return
            End If

            With label1
                .Location = New Point(320, 10)
                .Size = New Size(300, 60)
                .Text = "Expand the element class nodes in the
 " + _
                    "tree view to access visual style elements.
 " + _
                    "Click an element name to draw the element
 " + _
                    "below. To change the size of a resizable
 " + _
                    "element, use the spin control."
            End With

            With domainUpDown1
                .Location = New Point(320, 80)
                .Size = New Size(70, 30)
                .ReadOnly = True
                .Items.Add(elementSizes.Large)
                .Items.Add(elementSizes.Medium)
                .Items.Add(elementSizes.TrueSize)
                .SelectedIndex = 2
                .DownButton()
            End With

            descriptionRect = New Rectangle(320, 120, 250, 50)
            displayRect = New Rectangle(320, 160, 0, 0)
            displayRectFull = New Rectangle(320, 160, 300, 200)

            ' Initialize the element and renderer to known good values.
            element = VisualStyleElement.Button.PushButton.Normal
            renderer = New VisualStyleRenderer(element)

            SetupElementCollection()
            SetupTreeView()

            Me.Controls.AddRange(New Control()
 {treeView1, _
                domainUpDown1, label1})
        End Sub

        ' Use reflection to build a Dictionary of all 
        ' VisualStyleElement objects exposed in the 
        ' System.Windows.Forms.VisualStyles namespace.
        Private Sub SetupElementCollection()
            Dim elementName As New
 StringBuilder()
            Dim currentElement As VisualStyleElement
            Dim tempObject As Object
            Dim plusSignIndex As Integer
 = 0

            ' Get array of first-level nested types within 
            ' VisualStyleElement; these are the element classes.
            Dim elementClasses As Type() =
 _
                GetType(VisualStyleElement).GetNestedTypes()

            Dim elementClass As Type
            For Each elementClass In
 elementClasses

                ' Get an array of second-level nested types within
                ' VisualStyleElement; these are the element parts.
                Dim elementParts As Type()
 = _
                    elementClass.GetNestedTypes()

                ' Get the index of the first '+' character in 
                ' the full element class name.
                plusSignIndex = elementClass.FullName.IndexOf("+")

                Dim elementPart As Type
                For Each elementPart In
 elementParts

                    ' Get an array of Shared property details 
                    ' for  the current type. Each of these types have
 
                    ' properties that return VisualStyleElement objects.
                    Dim elementProperties As
 PropertyInfo() = _
                        elementPart.GetProperties( _
                        (BindingFlags.Static Or BindingFlags.Public))

                    ' For each property, insert the unique full element
   
                    ' name and the element into the collection.
                    Dim elementProperty As
 PropertyInfo
                    For Each elementProperty
 In elementProperties

                        ' Get the element.
                        tempObject = elementProperty.GetValue( _
                            Nothing, BindingFlags.Static, Nothing,
 _
                            Nothing, Nothing)
                        currentElement = CType(tempObject, _
                            VisualStyleElement)

                        ' Append the full element name.
                        elementName.Append(elementClass.FullName, _
                            plusSignIndex + 1, _
                            elementClass.FullName.Length - _
                            plusSignIndex - 1)
                        elementName.Append(("." +
 _
                            elementPart.Name.ToString() + _
                            "." + elementProperty.Name))

                        ' Add the element and element name to 
                        ' the Dictionary.
                        elementDictionary.Add(elementName.ToString(), _
                            currentElement)

                        ' Clear the element name for the next iteration.
                        elementName.Remove(0, elementName.Length)
                    Next elementProperty
                Next elementPart
            Next elementClass
        End Sub

        ' Initialize the tree view with the element names.
        Private Sub SetupTreeView()

            With treeView1
                .Location = New Point(10, 10)
                .Size = New Size(300, 450)
                .BorderStyle = BorderStyle.FixedSingle
                .BackColor = Color.WhiteSmoke
                .SelectedNode = Nothing
                .BeginUpdate()
            End With

            ' An index into the top-level tree nodes.
            Dim nodeIndex As Integer
 = 0

            ' An index into the first '.' character in an element name.
            Dim firstDotIndex As Integer
 = 0

            ' Initialize the element class name to compare 
            ' with the class name of the first element 
            ' in the Dictionary, and set this name to the first 
            ' top-level node.
            Dim compareClassName As New
 StringBuilder("Button")
            treeView1.Nodes.Add( _
                New TreeNode(compareClassName.ToString()))

            ' The current element class name.
            Dim currentClassName As New
 StringBuilder()

            ' The text for each second-level node.
            Dim nodeText As New
 StringBuilder()

            Dim entry As KeyValuePair(Of
 String, VisualStyleElement)
            For Each entry In
 elementDictionary

                ' Isolate the class name of the current element.
                firstDotIndex = entry.Key.IndexOf(".")
                currentClassName.Append(entry.Key, 0, firstDotIndex)

                ' Determine whether we need to increment to the next
 
                ' element class.
                If currentClassName.ToString() <> _
                    compareClassName.ToString() Then

                    ' Increment the index to the next top-level node
 
                    ' in the tree view.
                    nodeIndex += 1

                    ' Update the class name to compare with.
                    compareClassName.Remove(0, compareClassName.Length)
                    compareClassName.Append(entry.Key)
                    compareClassName.Remove(firstDotIndex, _
                        compareClassName.Length - firstDotIndex)

                    ' Add a new top-level node to the tree view.
                    Dim node As New
 TreeNode(compareClassName.ToString())
                    treeView1.Nodes.Add(node)
                End If

                ' Get the text for the new second-level node.
                nodeText.Append(entry.Key, firstDotIndex + 1, _
                    entry.Key.Length - firstDotIndex - 1)

                ' Create and insert the new second-level node.
                Dim newNode As New
 TreeNode(nodeText.ToString())
                newNode.Name = entry.Key
                treeView1.Nodes(nodeIndex).Nodes.Add(newNode)

                currentClassName.Remove(0, currentClassName.Length)
                nodeText.Remove(0, nodeText.Length)
            Next entry

            treeView1.EndUpdate()
        End Sub

        Protected Overrides Sub
 OnPaint(ByVal e As PaintEventArgs)
            MyBase.OnPaint(e)

            ' Do nothing further if visual styles are disabled.
            If Not Application.RenderWithVisualStyles
 Then
                Me.Text = "Visual styles are
 disabled."
                TextRenderer.DrawText(e.Graphics, Me.Text, Me.Font,
 _
                    Me.Location, Me.ForeColor)
                Return
            End If

            ' Draw the element description.
            TextRenderer.DrawText(e.Graphics, _
                elementDescription.ToString(), Me.Font, _
                descriptionRect, Me.ForeColor, _
                TextFormatFlags.WordBreak)

            ' Draw the element, if an element is selected.
            If drawElement Then
                renderer.DrawBackground(e.Graphics, Me.displayRect)
            End If
        End Sub

        ' Set the element to draw.
        Private Sub treeView1_AfterSelect(ByVal
 sender As Object, _
            ByVal e As TreeViewEventArgs) Handles
 treeView1.AfterSelect

            ' Clear the element description.
            elementDescription.Remove(0, elementDescription.Length)

            ' If the user clicked a first-level node, disable drawing.
            If e.Node.Nodes.Count > 0 Then
                drawElement = False
                elementDescription.Append("No element is selected")
                domainUpDown1.Enabled = False

            ' The user clicked an element node.
            Else
                ' Add the element name to the description.
                elementDescription.Append(e.Node.Text)

                ' Get the element that corresponds to the selected 
 
                ' node's name.
                Dim key As String
 = e.Node.Name
                element = elementDictionary(key)

                ' Disable resizing if the element is not defined.
                If Not VisualStyleRenderer.IsElementDefined(element)
 Then
                    drawElement = False
                    elementDescription.Append(" is not defined.")
                    domainUpDown1.Enabled = False
                Else
                    ' Set the element to the renderer.
                    drawElement = True
                    renderer.SetParameters(element)
                    elementDescription.Append(" is defined.")

                    ' Get the system-defined size of the element.
                    Dim g As Graphics = Me.CreateGraphics()
                    currentTrueSize = renderer.GetPartSize(g, _
                        ThemeSizeType.True)
                    g.Dispose()
                    displayRect.Size = currentTrueSize

                    domainUpDown1.Enabled = True
                    domainUpDown1.SelectedIndex = 2
                End If
            End If
            Invalidate()
        End Sub

        ' Resize the element display area.
        Private Sub domainUpDown1_SelectedItemChanged(ByVal
 sender As Object, _
            ByVal e As EventArgs) _
            Handles domainUpDown1.SelectedItemChanged

            Select Case CInt(domainUpDown1.SelectedItem)
                Case CInt(elementSizes.TrueSize)
                    displayRect.Size = currentTrueSize
                Case CInt(elementSizes.Medium)
                    displayRect.Size = _
                        New Size(displayRectFull.Width / 2, _
                        displayRectFull.Height / 2)
                Case CInt(elementSizes.Large)
                    displayRect.Size = displayRectFull.Size
            End Select

            Invalidate()
        End Sub

        ' These values represent the options in the UpDown control.
        Private Enum elementSizes
            TrueSize
            Medium
            Large
        End Enum

    End Class
End Namespace
using System;
using System.Text;
using System.Drawing;
using System.Collections.Generic;
using System.Reflection;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;

namespace VisualStyleElementViewer
{
    class Form1 : Form
    {
        public Form1()
        {
            ElementViewer ElementViewer1 = new ElementViewer();
            this.Controls.Add(ElementViewer1);
            this.Text = ElementViewer1.Text;
            this.Size = new Size(700, 550);
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }
    }

    public class ElementViewer : UserControl
    {
        private VisualStyleElement element;
        private VisualStyleRenderer renderer;
        private Dictionary<string, VisualStyleElement>
 elementDictionary =
            new Dictionary<string, VisualStyleElement>();

        private Rectangle descriptionRect;
        private Rectangle displayRect;
        private Rectangle displayRectFull;
        private Size currentTrueSize = new
 Size();
        private StringBuilder elementDescription = new
 StringBuilder();
        private Label label1 = new Label();
        private TreeView treeView1 = new TreeView();
        private DomainUpDown domainUpDown1 = new
 DomainUpDown();
        private bool drawElement = false;

        public ElementViewer()
            : base()
        {
            this.Location = new Point(10, 10);
            this.Size = new Size(650, 500);
            this.Text = "VisualStyleElement Viewer";
            this.Font = SystemFonts.IconTitleFont;
            this.BackColor = Color.White;
            this.BorderStyle = BorderStyle.Fixed3D;
            this.AutoSize = true;
            this.Load += new EventHandler(ElementViewer_Load);
        }

        void ElementViewer_Load(object sender, EventArgs e)
        {
            // Make sure the visual styles are enabled before 
            // going any further.
            if (!Application.RenderWithVisualStyles)
            {
                return;
            }

            label1.Location = new Point(320, 10);
            label1.Size = new Size(300, 60);
            label1.Text = "Expand the element class nodes
 " +
                "in the tree view to access visual style
 elements. " +
                "Click an element name to draw the element below. To "
 +
                "change the size of a resizable element, use the " +
                "spin control.";

            domainUpDown1.Location = new Point(320, 80);
            domainUpDown1.Size = new Size(70, 30);
            domainUpDown1.ReadOnly = true;
            domainUpDown1.Items.Add(elementSizes.Large);
            domainUpDown1.Items.Add(elementSizes.Medium);
            domainUpDown1.Items.Add(elementSizes.TrueSize);
            domainUpDown1.SelectedIndex = 2;
            domainUpDown1.SelectedItemChanged +=
                new EventHandler(domainUpDown1_SelectedItemChanged);
            domainUpDown1.DownButton();

            descriptionRect = new Rectangle(320, 120, 250, 50);
            displayRect = new Rectangle(320, 160, 0, 0);
            displayRectFull = new Rectangle(320, 160, 300, 200);

            // Initialize the element and renderer to known good values.
            element = VisualStyleElement.Button.PushButton.Normal;
            renderer = new VisualStyleRenderer(element);

            SetupElementCollection();
            SetupTreeView();

            this.Controls.AddRange(new Control[]
 { treeView1, 
                domainUpDown1, label1 });
        }

        // Use reflection to build a Dictionary of all 
        // VisualStyleElement objects exposed in the 
        // System.Windows.Forms.VisualStyles namespace.
        private void SetupElementCollection()
        {
            StringBuilder elementName = new StringBuilder();
            VisualStyleElement currentElement;
            int plusSignIndex = 0;

            // Get array of first-level nested types within 
            // VisualStyleElement; these are the element classes.
            Type[] elementClasses =
                typeof(VisualStyleElement).GetNestedTypes();

            foreach (Type elementClass in elementClasses)
            {
                // Get an array of second-level nested types within
                // VisualStyleElement; these are the element parts.
                Type[] elementParts = elementClass.GetNestedTypes();

                // Get the index of the first '+' character in 
                // the full element class name.
                plusSignIndex = elementClass.FullName.IndexOf('+');

                foreach (Type elementPart in
 elementParts)
                {
                    // Get an array of static property details 
                    // for  the current type. Each of these types have
 
                    // properties that return VisualStyleElement objects.
                    PropertyInfo[] elementProperties =
                        elementPart.GetProperties(BindingFlags.Static |
                        BindingFlags.Public);

                    // For each property, insert the unique full element
   
                    // name and the element into the collection.
                    foreach (PropertyInfo elementProperty in
                        elementProperties)
                    {
                        // Get the element.
                        currentElement =
                            (VisualStyleElement)elementProperty.
                            GetValue(null, BindingFlags.Static,
 null,
                            null, null);

                        // Append the full element name.
                        elementName.Append(elementClass.FullName,
                            plusSignIndex + 1,
                            elementClass.FullName.Length -
                            plusSignIndex - 1);
                        elementName.Append("." +
                            elementPart.Name.ToString() + "." +
                            elementProperty.Name);

                        // Add the element and element name to 
                        // the Dictionary.
                        elementDictionary.Add(elementName.ToString(),
                            currentElement);

                        // Clear the element name for the 
                        // next iteration.
                        elementName.Remove(0, elementName.Length);
                    }
                }
            }
        }

        // Initialize the tree view with the element names.
        private void SetupTreeView()
        {
            treeView1.Location = new Point(10, 10);
            treeView1.Size = new Size(300, 450);
            treeView1.BorderStyle = BorderStyle.FixedSingle;
            treeView1.BackColor = Color.WhiteSmoke;
            treeView1.SelectedNode = null;
            treeView1.AfterSelect +=
                new TreeViewEventHandler(treeView1_AfterSelect);

            treeView1.BeginUpdate();

            // An index into the top-level tree nodes.
            int nodeIndex = 0;

            // An index into the first '.' character in an element name.
            int firstDotIndex = 0;

            // Initialize the element class name to compare 
            // with the class name of the first element  
            // in the Dictionary, and set this name to the first 
            // top-level node.
            StringBuilder compareClassName =
                new StringBuilder("Button");
            treeView1.Nodes.Add(
                new TreeNode(compareClassName.ToString()));

            // The current element class name.
            StringBuilder currentClassName = new StringBuilder();

            // The text for each second-level node.
            StringBuilder nodeText = new StringBuilder();

            foreach (KeyValuePair<string,
 VisualStyleElement> entry
                in elementDictionary)
            {
                // Isolate the class name of the current element.
                firstDotIndex = entry.Key.IndexOf('.');
                currentClassName.Append(entry.Key, 0, firstDotIndex);

                // Determine whether we need to increment to the next
 
                // element class.
                if (currentClassName.ToString() !=
                    compareClassName.ToString())
                {
                    // Increment the index to the next top-level node
 
                    // in the tree view.
                    nodeIndex++;

                    // Get the new class name to compare with.
                    compareClassName.Remove(0, compareClassName.Length);
                    compareClassName.Append(entry.Key);
                    compareClassName.Remove(firstDotIndex,
                        compareClassName.Length - firstDotIndex);

                    // Add a new top-level node to the tree view.
                    treeView1.Nodes.Add(
                        new TreeNode(compareClassName.ToString()));
                }

                // Get the text for the new second-level node.
                nodeText.Append(entry.Key, firstDotIndex + 1,
                    entry.Key.Length - firstDotIndex - 1);

                // Create and insert the new second-level node.
                TreeNode newNode = new TreeNode(nodeText.ToString());
                newNode.Name = entry.Key;
                treeView1.Nodes[nodeIndex].Nodes.Add(newNode);

                currentClassName.Remove(0, currentClassName.Length);
                nodeText.Remove(0, nodeText.Length);
            }

            treeView1.EndUpdate();
        }

        protected override void OnPaint(PaintEventArgs
 e)
        {
            base.OnPaint(e);

            // Do nothing further if visual styles are disabled.
            if (!Application.RenderWithVisualStyles)
            {
                this.Text = "Visual styles are disabled.";
                TextRenderer.DrawText(e.Graphics, this.Text, this.Font
,
                    this.Location, this.ForeColor);
                return;
            }

            // Draw the element description.
            TextRenderer.DrawText(e.Graphics, elementDescription.ToString(),
                this.Font, descriptionRect, this.ForeColor
,
                TextFormatFlags.WordBreak);

            // Draw the element, if an element is selected.
            if (drawElement)
            {
                renderer.DrawBackground(e.Graphics, this.displayRect);
            }
        }

        // Set the element to draw.
        void treeView1_AfterSelect(object sender, TreeViewEventArgs
 e)
        {
            // Clear the element description.
            elementDescription.Remove(0, elementDescription.Length);

            // If the user clicked a first-level node, disable drawing.
            if (e.Node.Nodes.Count > 0)
            {
                drawElement = false;
                elementDescription.Append("No element is selected");
                domainUpDown1.Enabled = false;
            }

            // The user clicked an element node.
            else
            {
                // Add the element name to the description.
                elementDescription.Append(e.Node.Text);

                // Get the element that corresponds to the selected
  
                // node's name.
                String key = e.Node.Name;
                element = elementDictionary[key];

                // Disable resizing if the element is not defined.
                if (!VisualStyleRenderer.IsElementDefined(element))
                {
                    drawElement = false;
                    elementDescription.Append(" is not defined.");
                    domainUpDown1.Enabled = false;
                }
                else
                {
                    // Set the element to the renderer.
                    drawElement = true;
                    renderer.SetParameters(element);
                    elementDescription.Append(" is defined.");

                    // Get the system-defined size of the element.
                    Graphics g = this.CreateGraphics();
                    currentTrueSize = renderer.GetPartSize(g,
                        ThemeSizeType.True);
                    g.Dispose();
                    displayRect.Size = currentTrueSize;

                    domainUpDown1.Enabled = true;
                    domainUpDown1.SelectedIndex = 2;
                }
            }

            Invalidate();
        }

        // Resize the element display area.
        void domainUpDown1_SelectedItemChanged(object sender,
            EventArgs e)
        {
            switch ((int)domainUpDown1.SelectedItem)
            {
                case (int)elementSizes.TrueSize:
                    displayRect.Size = currentTrueSize;
                    break;
                case (int)elementSizes.Medium:
                    displayRect.Size =
                        new Size(displayRectFull.Width / 2,
                        displayRectFull.Height / 2);
                    break;
                case (int)elementSizes.Large:
                    displayRect.Size = displayRectFull.Size;
                    break;
            }

            Invalidate();
        }

        // These values represent the options in the UpDown control.
        private enum elementSizes
        {
            TrueSize,
            Medium,
            Large
        };
    }
}
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
#using <System.dll>

using namespace System;
using namespace System::Text;
using namespace System::Drawing;
using namespace System::Collections::Generic;
using namespace System::Reflection;
using namespace System::Windows::Forms;
using namespace System::Windows::Forms::VisualStyles;

namespace VisualStyleElementViewer
{
    public ref class ElementViewer : public
 UserControl
    {
    private:
        VisualStyleElement^ element;
        VisualStyleRenderer^ renderer;
        Dictionary<String^, VisualStyleElement^>^ elementDictionary;
        Rectangle descriptionRect;
        Rectangle displayRect;
        Rectangle displayRectFull;
        System::Drawing::Size currentTrueSize;
        StringBuilder^ elementDescription;
        Label^ infoLabel;
        TreeView^ treeView;
        DomainUpDown^ domainUpDown;
        bool drawElement;

    public:
        ElementViewer():UserControl()
        {
            elementDictionary = 
                gcnew Dictionary<String^, VisualStyleElement^>();
            currentTrueSize = System::Drawing::Size();
            elementDescription = gcnew StringBuilder();
            infoLabel = gcnew Label();
            treeView = gcnew TreeView();
            domainUpDown = gcnew DomainUpDown();

            this->Location = Point(10, 10);
            this->Size = System::Drawing::Size(650, 500);
            this->Text = "VisualStyleElement Viewer";
            this->Font = SystemFonts::IconTitleFont;
            this->BackColor = Color::White;
            this->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
            this->AutoSize = true;
            this->Load += gcnew EventHandler(this,
 
                &ElementViewer::ElementViewer_Load);
        }

    public:
        void ElementViewer_Load(Object^ sender, EventArgs^ e)
        {
            // Make sure the visual styles are enabled before
            // going any further.
            if (!Application::RenderWithVisualStyles)
            {
                return;
            }

            infoLabel->Location = Point(320, 10);
            infoLabel->Size = System::Drawing::Size(300, 60);
            infoLabel->Text = "Expand the element class
 nodes " +
                "in the tree view to access visual style
 elements. " +
                "Click an element name to draw the element below. To "
 +
                "change the size of a resizable element, use the " +
                "spin control.";

            domainUpDown->Location = Point(320, 80);
            domainUpDown->Size = System::Drawing::Size(70, 30);
            domainUpDown->ReadOnly = true;
            domainUpDown->Items->Add(elementSizes::Large);
            domainUpDown->Items->Add(elementSizes::Medium);
            domainUpDown->Items->Add(elementSizes::TrueSize);
            domainUpDown->SelectedIndex = 2;
            domainUpDown->SelectedItemChanged +=
                gcnew EventHandler(this, 
                    &ElementViewer::DomainUpDown_SelectedItemChanged);
            domainUpDown->DownButton();

            descriptionRect = Rectangle(320, 120, 250, 50);
            displayRect = Rectangle(320, 160, 0, 0);
            displayRectFull = Rectangle(320, 160, 300, 200);

            // Initialize the element and renderer to known good values.
            element = VisualStyleElement::Button::PushButton::Normal;
            renderer = gcnew VisualStyleRenderer(element);

            SetupElementCollection();
            SetupTreeView();

            this->Controls->AddRange(gcnew array<Control^>{treeView
,
                domainUpDown, infoLabel });
        }

        // Use reflection to build a Dictionary of all
        // VisualStyleElement objects exposed in the
        // System.Windows.Forms.VisualStyles namespace.
    private:
        void SetupElementCollection()
        {
            StringBuilder^ elementName = gcnew StringBuilder();
            VisualStyleElement^ currentElement;
            int plusSignIndex = 0;

            // Get array of first-level nested types within
            // VisualStyleElement; these are the element classes.
            array<Type^>^ elementClasses =
                VisualStyleElement::typeid->GetNestedTypes();

            for each (Type^ elementClass in
 elementClasses)
            {
                // Get an array of second-level nested types within
                // VisualStyleElement; these are the element parts.
                array<Type^>^ elementParts = elementClass->GetNestedTypes();

                // Get the index of the first '+' character in
                // the full element class name.
                plusSignIndex = elementClass->FullName->IndexOf('+');

                for each (Type^ elementPart in
 elementParts)
                {
                    // Get an array of static property details
                    // for  the current type. Each of these types have
                    // properties that return VisualStyleElement objects.
                    array<PropertyInfo^>^ elementProperties =
                        elementPart->GetProperties(BindingFlags::Static |
                        BindingFlags::Public);

                    // For each property, insert the unique full element
                    // name and the element into the collection.
                    for each(PropertyInfo^ elementProperty in
                        elementProperties)
                    {
                        // Get the element.
                        currentElement =
                            (VisualStyleElement^)elementProperty->
                            GetValue(nullptr, BindingFlags::Static, nullptr,
                            nullptr, nullptr);

                        // Append the full element name.
                        elementName->Append(elementClass->FullName,
                            plusSignIndex + 1,
                            elementClass->FullName->Length -
                            plusSignIndex - 1);
                        elementName->Append("." +
                            elementPart->Name + "." +
                            elementProperty->Name);

                        // Add the element and element name to
                        // the Dictionary.
                        elementDictionary->Add(elementName->ToString(),
                            currentElement);

                        // Clear the element name for the
                        // next iteration.
                        elementName->Remove(0, elementName->Length);
                    }
                }
            }
        }

        // Initialize the tree view with the element names.
    private:
        void SetupTreeView()
        {
            treeView->Location = Point(10, 10);
            treeView->Size = System::Drawing::Size(300, 450);
            //    treeView->BorderStyle = BorderStyle.FixedSingle;
            treeView->BackColor = Color::WhiteSmoke;
            treeView->SelectedNode = nullptr;
            treeView->AfterSelect +=
                gcnew TreeViewEventHandler(this, 
                &ElementViewer::TreeView_AfterSelect);

            treeView->BeginUpdate();

            // An index into the top-level tree nodes.
            int nodeIndex = 0;

            // An index into the first '.' character in an element name.
            int firstDotIndex = 0;

            // Initialize the element class name to compare
            // with the class name of the first element
            // in the Dictionary, and set this name to the first
            // top-level node.
            StringBuilder^ compareClassName =
                gcnew StringBuilder("Button");
            treeView->Nodes->Add(
                gcnew TreeNode(compareClassName->ToString()));

            // The current element class name.
            StringBuilder^ currentClassName = gcnew StringBuilder();

            // The text for each second-level node.
            StringBuilder^ nodeText = gcnew StringBuilder();

            for each(KeyValuePair<String^, VisualStyleElement^>^
 entry 
                in elementDictionary)
            {
                // Isolate the class name of the current element.
                firstDotIndex = entry->Key->IndexOf('.');
                currentClassName->Append(entry->Key, 0, firstDotIndex);

                // Determine whether we need to increment to the next
                // element class.
                if (currentClassName->ToString() !=
                    compareClassName->ToString())
                {
                    // Increment the index to the next top-level node
                    // in the tree view.
                    nodeIndex++;

                    // Get the new class name to compare with.
                    compareClassName->Remove(0, compareClassName->Length);
                    compareClassName->Append(entry->Key);
                    compareClassName->Remove(firstDotIndex,
                        compareClassName->Length - firstDotIndex);

                    // Add a new top-level node to the tree view.
                    treeView->Nodes->Add(
                        gcnew TreeNode(compareClassName->ToString()));
                }

                // Get the text for the new second-level node.
                nodeText->Append(entry->Key, firstDotIndex + 1,
                    entry->Key->Length - firstDotIndex - 1);

                // Create and insert the new second-level node.
                TreeNode^ newNode = gcnew TreeNode(nodeText->ToString());
                newNode->Name = entry->Key;
                treeView->Nodes[nodeIndex]->Nodes->Add(newNode);

                currentClassName->Remove(0, currentClassName->Length);
                nodeText->Remove(0, nodeText->Length);
            }
            treeView->EndUpdate();
        }

    protected:
        virtual void OnPaint(PaintEventArgs^ e) override
        {
            __super::OnPaint(e);

            // Do nothing further if visual styles are disabled.
            if (!Application::RenderWithVisualStyles)
            {
                this->Text = "Visual styles are disabled.";
                TextRenderer::DrawText(e->Graphics, this->Text,
 this->Font,
                    this->Location, this->ForeColor);
                return;
            }

            // Draw the element description.
            TextRenderer::DrawText(e->Graphics, elementDescription->ToString()
,
                this->Font, descriptionRect, this->ForeColor
,
                TextFormatFlags::WordBreak);

            // Draw the element, if an element is selected.
            if (drawElement)
            {
                renderer->DrawBackground(e->Graphics, this->displayRect);
            }
        }

        // Set the element to draw.
    public:
        void TreeView_AfterSelect(Object^ sender, TreeViewEventArgs^
 e)
        {
            // Clear the element description.
            elementDescription->Remove(0, elementDescription->Length);

            // If the user clicked a first-level node, disable drawing.
            if (e->Node->Nodes->Count > 0)
            {
                drawElement = false;
                elementDescription->Append("No element is selected");
                domainUpDown->Enabled = false;
            }

            // The user clicked an element node.
            else
            {
                // Add the element name to the description.
                elementDescription->Append(e->Node->Text);

                // Get the element that corresponds to the selected
                // node's name.
                String^ key = e->Node->Name;
                element = elementDictionary[key];

                // Disable resizing if the element is not defined.
                if (!VisualStyleRenderer::IsElementDefined(element))
                {
                    drawElement = false;
                    elementDescription->Append(" is not defined.");
                    domainUpDown->Enabled = false;
                }
                else
                {
                    // Set the element to the renderer.
                    drawElement = true;
                    renderer->SetParameters(element);
                    elementDescription->Append(" is defined.");

                    // Get the system-defined size of the element.
                    Graphics^ g = this->CreateGraphics();
                    currentTrueSize = renderer->GetPartSize(g,
                        ThemeSizeType::True);
                    delete g;
                    displayRect.Size = currentTrueSize;

                    domainUpDown->Enabled = true;
                    domainUpDown->SelectedIndex = 2;
                }
            }

            Invalidate();
        }

        // Resize the element display area.
    public:
        void DomainUpDown_SelectedItemChanged(Object^ sender,
            EventArgs^ e)
        {
            switch ((int)domainUpDown->SelectedItem)
            {
            case this->elementSizes::TrueSize:
                displayRect.Size = currentTrueSize;
                break;
            case this->elementSizes::Medium:
                displayRect.Size = 
                    System::Drawing::Size(displayRectFull.Width / 2, 
                        displayRectFull.Height / 2);
                break;
            case this->elementSizes::Large:
                displayRect.Size = displayRectFull.Size;
                break;
            }

            Invalidate();
        }

        // These values represent the options in the UpDown control.
    private:
        enum class elementSizes
        {
            TrueSize,
            Medium,
            Large
        };
    };
    public ref class ElementViewerForm : public
 Form
    {
    public:
        ElementViewerForm()
        {
            ElementViewer^ elementViewer = gcnew ElementViewer();
            this->Controls->Add(elementViewer);
            this->Text = elementViewer->Text;
            this->Size = System::Drawing::Size(700, 550);
        }
    };
}

using namespace VisualStyleElementViewer;

[STAThread]
int main()
{
    Application::EnableVisualStyles();
    Application::Run(gcnew ElementViewerForm());
}

継承階層継承階層
System.Object
  System.Windows.Forms.VisualStyles.VisualStyleElement
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
VisualStyleElement メンバ
System.Windows.Forms.VisualStyles 名前空間
VisualStyleRenderer
VisualStyleInformation

VisualStyleElement プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ ClassName この VisualStyleElement が表す visual スタイル要素のクラス名を取得します。
パブリック プロパティ Part この VisualStyleElement が表す visual スタイル要素の部品を示す値を取得します。
パブリック プロパティ State この VisualStyleElement が表す visual スタイル要素の状態を示す値を取得します。
参照参照

関連項目

VisualStyleElement クラス
System.Windows.Forms.VisualStyles 名前空間
VisualStyleRenderer
VisualStyleInformation

VisualStyleElement メソッド


パブリック メソッドパブリック メソッド

プロテクト メソッドプロテクト メソッド
参照参照

関連項目

VisualStyleElement クラス
System.Windows.Forms.VisualStyles 名前空間
VisualStyleRenderer
VisualStyleInformation

VisualStyleElement メンバ

visual スタイルで描画するコントロール要素またはユーザー インターフェイス (UI) 要素を示します。

VisualStyleElement データ型で公開されるメンバを以下の表に示します。


パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ ClassName この VisualStyleElement が表す visual スタイル要素のクラス名を取得します。
パブリック プロパティ Part この VisualStyleElement が表す visual スタイル要素の部品を示す値を取得します。
パブリック プロパティ State この VisualStyleElement が表す visual スタイル要素の状態を示す値を取得します。
パブリック メソッドパブリック メソッド
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

VisualStyleElement クラス
System.Windows.Forms.VisualStyles 名前空間
VisualStyleRenderer
VisualStyleInformation


このページでは「.NET Framework クラス ライブラリ リファレンス」からVisualStyleElementを検索した結果を表示しています。
Weblioに収録されているすべての辞書からVisualStyleElementを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からVisualStyleElement を検索

英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「VisualStyleElement」の関連用語

VisualStyleElementのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



VisualStyleElementのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS