방법: 비주얼 스타일 요소 렌더링

System.Windows.Forms.VisualStyles 네임스페이스는 시각적 스타일에 의해 지원되는 Windows UI(사용자 인터페이스) 요소를 나타내는 VisualStyleElement 개체를 노출합니다. 이 토픽에서는 VisualStyleRenderer 클래스를 사용하여 시작 메뉴의 로그오프종료 단추를 나타내는 VisualStyleElement를 렌더링하는 방법을 보여 줍니다.

시각적 스타일 요소를 렌더링하려면

  1. VisualStyleRenderer를 만들고 그리려는 요소로 설정합니다. Application.RenderWithVisualStyles 속성 및 VisualStyleRenderer.IsElementDefined 메서드의 사용에서 시각적 스타일을 사용하지 않거나 요소가 정의되지 않은 경우 VisualStyleRenderer 생성자가 예외를 throw합니다.

    private:
        VisualStyleRenderer^ renderer;
        VisualStyleElement^ element;
    
    public:
        CustomControl()
        {
            this->Location = Point(50, 50);
            this->Size = System::Drawing::Size(200, 200);
            this->BackColor = SystemColors::ActiveBorder;
            this->element = 
                VisualStyleElement::StartPanel::LogOffButtons::Normal;
            if (Application::RenderWithVisualStyles &&
                VisualStyleRenderer::IsElementDefined(element))
            {
                renderer = gcnew VisualStyleRenderer(element);
            }
        }
    
    private VisualStyleRenderer renderer = null;
    private readonly VisualStyleElement element =
        VisualStyleElement.StartPanel.LogOffButtons.Normal;
    
    public CustomControl()
    {
        this.Location = new Point(50, 50);
        this.Size = new Size(200, 200);
        this.BackColor = SystemColors.ActiveBorder;
    
        if (Application.RenderWithVisualStyles &&
            VisualStyleRenderer.IsElementDefined(element))
        {
            renderer = new VisualStyleRenderer(element);
        }
    }
    
    Private renderer As VisualStyleRenderer = Nothing
    Private element As VisualStyleElement = _
        VisualStyleElement.StartPanel.LogOffButtons.Normal
    
    Public Sub New()
        Me.Location = New Point(50, 50)
        Me.Size = New Size(200, 200)
        Me.BackColor = SystemColors.ActiveBorder
    
        If Application.RenderWithVisualStyles And _
            VisualStyleRenderer.IsElementDefined(element) Then
                renderer = New VisualStyleRenderer(element)
        End If
    End Sub
    
  2. DrawBackground 메서드를 호출하여 현재 VisualStyleRenderer가 나타내는 요소를 렌더링합니다.

    protected:
        virtual void OnPaint(PaintEventArgs^ e) override
        {
            // Draw the element if the renderer has been set.
            if (renderer != nullptr)
            {
                renderer->DrawBackground(e->Graphics, this->ClientRectangle);
            }
    
            // Visual styles are disabled or the element is undefined,
            // so just draw a message.
            else
            {
                this->Text = "Visual styles are disabled.";
                TextRenderer::DrawText(e->Graphics, this->Text, this->Font,
                    Point(0, 0), this->ForeColor);
            }
        }
    
    protected override void OnPaint(PaintEventArgs e)
    {
        // Draw the element if the renderer has been set.
        if (renderer != null)
        {
            renderer.DrawBackground(e.Graphics, this.ClientRectangle);
        }
    
        // Visual styles are disabled or the element is undefined,
        // so just draw a message.
        else
        {
            this.Text = "Visual styles are disabled.";
            TextRenderer.DrawText(e.Graphics, this.Text, this.Font,
                new Point(0, 0), this.ForeColor);
        }
    }
    
    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        ' Draw the element if the renderer has been set.
        If (renderer IsNot Nothing) Then
            renderer.DrawBackground(e.Graphics, Me.ClientRectangle)
    
        ' Visual styles are disabled or the element is undefined, 
        ' so just draw a message.
        Else
            Me.Text = "Visual styles are disabled."
            TextRenderer.DrawText(e.Graphics, Me.Text, Me.Font, _
                New Point(0, 0), Me.ForeColor)
        End If
    End Sub
    

코드 컴파일

이 예제에는 다음 사항이 필요합니다.

참고 항목