DrawListViewSubItemEventArgs 클래스

정의

DrawSubItem 이벤트에 대한 데이터를 제공합니다.

public ref class DrawListViewSubItemEventArgs : EventArgs
public class DrawListViewSubItemEventArgs : EventArgs
type DrawListViewSubItemEventArgs = class
    inherit EventArgs
Public Class DrawListViewSubItemEventArgs
Inherits EventArgs
상속
DrawListViewSubItemEventArgs

예제

다음 코드 예제에 대 한 사용자 지정 그리기를 제공 하는 방법에 설명 된 ListView 제어 합니다. ListView 배경이 그라데이션 예제에서 컨트롤입니다. 음수 값을 사용 하 여 하위 항목에 빨간 전경 및 검은색 배경 있습니다.

에 대 한 처리기를 ListView.DrawItem 이벤트 전체 항목의 배경을 그립니다. 에 대 한 처리기를 ListView.DrawSubItem 이벤트 텍스트 값 및 텍스트와 음수 값이 있는 하위 항목의 배경을 그립니다. 에 대 한 처리기를 DrawColumnHeader 이벤트 각 열 머리글을 그립니다.

ContextMenu 구성 요소는 세부 정보 보기 및 목록 보기 간에 전환 하는 방법을 제공 합니다. 목록 뷰에서 ListView.DrawItem 이벤트가 발생 합니다. 이 경우 텍스트와 배경 둘 다에서 그려지는 ListView.DrawItem 이벤트 처리기입니다.

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Globalization;
using System.Windows.Forms;

public class ListViewOwnerDraw : Form
{
    private ListView listView1 = new ListView();
    private ContextMenu contextMenu1 = new ContextMenu();

    public ListViewOwnerDraw()
    {
        // Initialize the ListView control.
        listView1.BackColor = Color.Black;
        listView1.ForeColor = Color.White;
        listView1.Dock = DockStyle.Fill;
        listView1.View = View.Details;
        listView1.FullRowSelect = true;

        // Add columns to the ListView control.
        listView1.Columns.Add("Name", 100, HorizontalAlignment.Center);
        listView1.Columns.Add("First", 100, HorizontalAlignment.Center);
        listView1.Columns.Add("Second", 100, HorizontalAlignment.Center);
        listView1.Columns.Add("Third", 100, HorizontalAlignment.Center);

        // Create items and add them to the ListView control.
        ListViewItem listViewItem1 = new ListViewItem(new string[] { "One", "20", "30", "-40" }, -1);
        ListViewItem listViewItem2 = new ListViewItem(new string[] { "Two", "-250", "145", "37" }, -1);
        ListViewItem listViewItem3 = new ListViewItem(new string[] { "Three", "200", "800", "-1,001" }, -1);
        ListViewItem listViewItem4 = new ListViewItem(new string[] { "Four", "not available", "-2", "100" }, -1);
        listView1.Items.AddRange(new ListViewItem[] { listViewItem1, listViewItem2, listViewItem3, listViewItem4 });

        // Initialize the shortcut menu and 
        // assign it to the ListView control.
        contextMenu1.MenuItems.Add("List",
            new EventHandler(menuItemList_Click));
        contextMenu1.MenuItems.Add("Details",
            new EventHandler(menuItemDetails_Click));
        listView1.ContextMenu = contextMenu1;

        // Configure the ListView control for owner-draw and add 
        // handlers for the owner-draw events.
        listView1.OwnerDraw = true;
        listView1.DrawItem += new
            DrawListViewItemEventHandler(listView1_DrawItem);
        listView1.DrawSubItem += new
            DrawListViewSubItemEventHandler(listView1_DrawSubItem);
        listView1.DrawColumnHeader += new
            DrawListViewColumnHeaderEventHandler(listView1_DrawColumnHeader);

        // Add a handler for the MouseUp event so an item can be 
        // selected by clicking anywhere along its width.
        listView1.MouseUp += new MouseEventHandler(listView1_MouseUp);

        // Add handlers for various events to compensate for an 
        // extra DrawItem event that occurs the first time the mouse 
        // moves over each row. 
        listView1.MouseMove += new MouseEventHandler(listView1_MouseMove);
        listView1.ColumnWidthChanged += new ColumnWidthChangedEventHandler(listView1_ColumnWidthChanged);
        listView1.Invalidated += new InvalidateEventHandler(listView1_Invalidated);

        // Initialize the form and add the ListView control to it.
        this.ClientSize = new Size(450, 150);
        this.FormBorderStyle = FormBorderStyle.FixedSingle;
        this.MaximizeBox = false;
        this.Text = "ListView OwnerDraw Example";
        this.Controls.Add(listView1);
    }

    // Clean up any resources being used.        
    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            contextMenu1.Dispose();
        }
        base.Dispose(disposing);
    }

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

    // Sets the ListView control to the List view.
    private void menuItemList_Click(object sender, EventArgs e)
    {
        listView1.View = View.List;
        listView1.Invalidate();
    }

    // Sets the ListView control to the Details view.
    private void menuItemDetails_Click(object sender, EventArgs e)
    {
        listView1.View = View.Details;

        // Reset the tag on each item to re-enable the workaround in
        // the MouseMove event handler.
        foreach (ListViewItem item in listView1.Items)
        {
            item.Tag = null;
        }
    }

    // Selects and focuses an item when it is clicked anywhere along 
    // its width. The click must normally be on the parent item text.
    private void listView1_MouseUp(object sender, MouseEventArgs e)
    {
        ListViewItem clickedItem = listView1.GetItemAt(5, e.Y);
        if (clickedItem != null)
        {
            clickedItem.Selected = true;
            clickedItem.Focused = true;
        }
    }

    // Draws the backgrounds for entire ListView items.
    private void listView1_DrawItem(object sender,
        DrawListViewItemEventArgs e)
    {
        if ((e.State & ListViewItemStates.Selected) != 0)
        {
            // Draw the background and focus rectangle for a selected item.
            e.Graphics.FillRectangle(Brushes.Maroon, e.Bounds);
            e.DrawFocusRectangle();
        }
        else
        {
            // Draw the background for an unselected item.
            using (LinearGradientBrush brush =
                new LinearGradientBrush(e.Bounds, Color.Orange,
                Color.Maroon, LinearGradientMode.Horizontal))
            {
                e.Graphics.FillRectangle(brush, e.Bounds);
            }
        }

        // Draw the item text for views other than the Details view.
        if (listView1.View != View.Details)
        {
            e.DrawText();
        }
    }

    // Draws subitem text and applies content-based formatting.
    private void listView1_DrawSubItem(object sender,
        DrawListViewSubItemEventArgs e)
    {
        TextFormatFlags flags = TextFormatFlags.Left;

        using (StringFormat sf = new StringFormat())
        {
            // Store the column text alignment, letting it default
            // to Left if it has not been set to Center or Right.
            switch (e.Header.TextAlign)
            {
                case HorizontalAlignment.Center:
                    sf.Alignment = StringAlignment.Center;
                    flags = TextFormatFlags.HorizontalCenter;
                    break;
                case HorizontalAlignment.Right:
                    sf.Alignment = StringAlignment.Far;
                    flags = TextFormatFlags.Right;
                    break;
            }

            // Draw the text and background for a subitem with a 
            // negative value. 
            double subItemValue;
            if (e.ColumnIndex > 0 && Double.TryParse(
                e.SubItem.Text, NumberStyles.Currency,
                NumberFormatInfo.CurrentInfo, out subItemValue) &&
                subItemValue < 0)
            {
                // Unless the item is selected, draw the standard 
                // background to make it stand out from the gradient.
                if ((e.ItemState & ListViewItemStates.Selected) == 0)
                {
                    e.DrawBackground();
                }

                // Draw the subitem text in red to highlight it. 
                e.Graphics.DrawString(e.SubItem.Text,
                    listView1.Font, Brushes.Red, e.Bounds, sf);

                return;
            }

            // Draw normal text for a subitem with a nonnegative 
            // or nonnumerical value.
            e.DrawText(flags);
        }
    }

    // Draws column headers.
    private void listView1_DrawColumnHeader(object sender,
        DrawListViewColumnHeaderEventArgs e)
    {
        using (StringFormat sf = new StringFormat())
        {
            // Store the column text alignment, letting it default
            // to Left if it has not been set to Center or Right.
            switch (e.Header.TextAlign)
            {
                case HorizontalAlignment.Center:
                    sf.Alignment = StringAlignment.Center;
                    break;
                case HorizontalAlignment.Right:
                    sf.Alignment = StringAlignment.Far;
                    break;
            }

            // Draw the standard header background.
            e.DrawBackground();

            // Draw the header text.
            using (Font headerFont =
                        new Font("Helvetica", 10, FontStyle.Bold))
            {
                e.Graphics.DrawString(e.Header.Text, headerFont,
                    Brushes.Black, e.Bounds, sf);
            }
        }
        return;
    }

    // Forces each row to repaint itself the first time the mouse moves over 
    // it, compensating for an extra DrawItem event sent by the wrapped 
    // Win32 control. This issue occurs each time the ListView is invalidated.
    private void listView1_MouseMove(object sender, MouseEventArgs e)
    {
        ListViewItem item = listView1.GetItemAt(e.X, e.Y);
        if (item != null && item.Tag == null)
        {
            listView1.Invalidate(item.Bounds);
            item.Tag = "tagged";
        }
    }

    // Resets the item tags. 
    void listView1_Invalidated(object sender, InvalidateEventArgs e)
    {
        foreach (ListViewItem item in listView1.Items)
        {
            if (item == null) return;
            item.Tag = null;
        }
    }

    // Forces the entire control to repaint if a column width is changed.
    void listView1_ColumnWidthChanged(object sender, 
        ColumnWidthChangedEventArgs e)
    {
        listView1.Invalidate();
    }
}
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Globalization
Imports System.Windows.Forms

Public Class ListViewOwnerDraw
    Inherits Form
    Private WithEvents listView1 As New ListView()
    Private WithEvents contextMenu1 As New ContextMenu()
    Private WithEvents listMenuItem As New MenuItem("List")
    Private WithEvents detailsMenuItem As New MenuItem("Details")

    Public Sub New()

        ' Initialize the shortcut menu. 
        contextMenu1.MenuItems.AddRange(New MenuItem() _
            {Me.listMenuItem, Me.detailsMenuItem})

        ' Initialize the ListView control.
        With Me.listView1
            .BackColor = Color.Black
            .ForeColor = Color.White
            .Dock = DockStyle.Fill
            .View = View.Details
            .FullRowSelect = True
            .OwnerDraw = True
            .ContextMenu = Me.contextMenu1
        End With

        ' Add columns to the ListView control.
        With Me.listView1.Columns
            .Add("Name", 100, HorizontalAlignment.Center)
            .Add("First", 100, HorizontalAlignment.Center)
            .Add("Second", 100, HorizontalAlignment.Center)
            .Add("Third", 100, HorizontalAlignment.Center)
        End With

        ' Create items and add them to the ListView control.
        Dim listViewItem1 As New ListViewItem(New String() _
            {"One", "20", "30", "-40"}, -1)
        Dim listViewItem2 As New ListViewItem(New String() _
            {"Two", "-250", "145", "37"}, -1)
        Dim listViewItem3 As New ListViewItem(New String() _
            {"Three", "200", "800", "-1,001"}, -1)
        Dim listViewItem4 As New ListViewItem(New String() _
            {"Four", "not available", "-2", "100"}, -1)
        Me.listView1.Items.AddRange(New ListViewItem() _
            {listViewItem1, listViewItem2, listViewItem3, listViewItem4})

        ' Initialize the form and add the ListView control to it.
        With Me
            .ClientSize = New Size(450, 150)
            .FormBorderStyle = FormBorderStyle.FixedSingle
            .MaximizeBox = False
            .Text = "ListView OwnerDraw Example"
            .Controls.Add(Me.listView1)
        End With

    End Sub

    ' Clean up any resources being used.        
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            contextMenu1.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

    <STAThread()> _
    Shared Sub Main()
        Application.Run(New ListViewOwnerDraw())
    End Sub

    ' Sets the ListView control to the List view.
    Private Sub menuItemList_Click(ByVal sender As Object, _
        ByVal e As EventArgs) _
        Handles listMenuItem.Click

        Me.listView1.View = View.List

    End Sub

    ' Sets the ListView control to the Details view.
    Private Sub menuItemDetails_Click(ByVal sender As Object, _
        ByVal e As EventArgs) _
        Handles detailsMenuItem.Click

        Me.listView1.View = View.Details

        ' Reset the tag on each item to re-enable the workaround 
        ' in the MouseMove event handler.
        For Each item As ListViewItem In listView1.Items
            item.Tag = Nothing
        Next

    End Sub

    ' Selects and focuses an item when it is clicked anywhere along 
    ' its width. The click must normally be on the parent item text.
    Private Sub listView1_MouseUp(ByVal sender As Object, _
        ByVal e As MouseEventArgs) _
        Handles listView1.MouseUp

        Dim clickedItem As ListViewItem = Me.listView1.GetItemAt(5, e.Y)
        If (clickedItem IsNot Nothing) Then
            clickedItem.Selected = True
            clickedItem.Focused = True
        End If

    End Sub

    ' Draws the backgrounds for entire ListView items.
    Private Sub listView1_DrawItem(ByVal sender As Object, _
        ByVal e As DrawListViewItemEventArgs) _
        Handles listView1.DrawItem

        If Not (e.State And ListViewItemStates.Selected) = 0 Then

            ' Draw the background for a selected item.
            e.Graphics.FillRectangle(Brushes.Maroon, e.Bounds)
            e.DrawFocusRectangle()

        Else

            ' Draw the background for an unselected item.
            Dim brush As New LinearGradientBrush(e.Bounds, Color.Orange, _
                Color.Maroon, LinearGradientMode.Horizontal)
            Try
                e.Graphics.FillRectangle(brush, e.Bounds)
            Finally
                brush.Dispose()
            End Try

        End If

        ' Draw the item text for views other than the Details view.
        If Not Me.listView1.View = View.Details Then
            e.DrawText()
        End If

    End Sub

    ' Draws subitem text and applies content-based formatting.
    Private Sub listView1_DrawSubItem(ByVal sender As Object, _
        ByVal e As DrawListViewSubItemEventArgs) _
        Handles listView1.DrawSubItem

        Dim flags As TextFormatFlags = TextFormatFlags.Left

        Dim sf As New StringFormat()
        Try

            ' Store the column text alignment, letting it default
            ' to Left if it has not been set to Center or Right.
            Select Case e.Header.TextAlign
                Case HorizontalAlignment.Center
                    sf.Alignment = StringAlignment.Center
                    flags = TextFormatFlags.HorizontalCenter
                Case HorizontalAlignment.Right
                    sf.Alignment = StringAlignment.Far
                    flags = TextFormatFlags.Right
            End Select

            ' Draw the text and background for a subitem with a 
            ' negative value. 
            Dim subItemValue As Double
            If e.ColumnIndex > 0 AndAlso _
                Double.TryParse(e.SubItem.Text, NumberStyles.Currency, _
                NumberFormatInfo.CurrentInfo, subItemValue) AndAlso _
                subItemValue < 0 Then

                ' Unless the item is selected, draw the standard 
                ' background to make it stand out from the gradient.
                If (e.ItemState And ListViewItemStates.Selected) = 0 Then
                    e.DrawBackground()
                End If

                ' Draw the subitem text in red to highlight it. 
                e.Graphics.DrawString(e.SubItem.Text, _
                    Me.listView1.Font, Brushes.Red, e.Bounds, sf)

                Return

            End If

            ' Draw normal text for a subitem with a nonnegative 
            ' or nonnumerical value.
            e.DrawText(flags)

        Finally
            sf.Dispose()
        End Try

    End Sub

    ' Draws column headers.
    Private Sub listView1_DrawColumnHeader(ByVal sender As Object, _
        ByVal e As DrawListViewColumnHeaderEventArgs) _
        Handles listView1.DrawColumnHeader

        Dim sf As New StringFormat()
        Try

            ' Store the column text alignment, letting it default
            ' to Left if it has not been set to Center or Right.
            Select Case e.Header.TextAlign
                Case HorizontalAlignment.Center
                    sf.Alignment = StringAlignment.Center
                Case HorizontalAlignment.Right
                    sf.Alignment = StringAlignment.Far
            End Select

            ' Draw the standard header background.
            e.DrawBackground()

            ' Draw the header text.
            Dim headerFont As New Font("Helvetica", 10, FontStyle.Bold)
            Try
                e.Graphics.DrawString(e.Header.Text, headerFont, _
                    Brushes.Black, e.Bounds, sf)
            Finally
                headerFont.Dispose()
            End Try

        Finally
            sf.Dispose()
        End Try

    End Sub

    ' Forces each row to repaint itself the first time the mouse moves over 
    ' it, compensating for an extra DrawItem event sent by the wrapped 
    ' Win32 control.
    Private Sub listView1_MouseMove(ByVal sender As Object, _
        ByVal e As MouseEventArgs) _
        Handles listView1.MouseMove

        Dim item As ListViewItem = listView1.GetItemAt(e.X, e.Y)
        If item IsNot Nothing AndAlso item.Tag Is Nothing Then
            listView1.Invalidate(item.Bounds)
            item.Tag = "tagged"
        End If

    End Sub

    ' Resets the item tags. 
    Private Sub listView1_Invalidated(ByVal sender As Object, _
        ByVal e As InvalidateEventArgs) Handles listView1.Invalidated

        For Each item As ListViewItem In listView1.Items
            If item Is Nothing Then Return
            item.Tag = Nothing
        Next

    End Sub

    ' Forces the entire control to repaint if a column width is changed.
    Private Sub listView1_ColumnWidthChanged(ByVal sender As Object, _
        ByVal e As ColumnWidthChangedEventArgs) Handles listView1.ColumnWidthChanged

        listView1.Invalidate()

    End Sub

End Class

설명

합니다 ListView.DrawSubItem 이벤트를 사용 하면 사용자 지정 (또는 소유자 그리기) 모양의 ListView 세부 정보 뷰에서 컨트롤입니다.

ListView.DrawSubItem 이벤트를 발생을 ListView 제어 해당 ListView.OwnerDraw 속성이로 설정 되어 true 및 해당 View 속성 Details. DrawListViewSubItemEventArgs 전달 된 이벤트 처리기에 대 한 정보를 포함 합니다 ListViewItem.ListViewSubItem 그릴 하위 항목을 그릴 수 있도록 하는 메서드도 제공 합니다.

사용 된 ItemState 또는 Item 그릴 하위 항목의 부모 항목에 대 한 정보를 검색 하는 속성입니다. 검색할 합니다 ListViewItem.ListViewSubItem 사용 하 여 자체는 SubItem 속성입니다. 사용 하 여는 Header 검색할 속성의 ColumnHeader 하위 항목 표시 되는 열의 헤더를 나타내는입니다.

사용 합니다 Graphics 속성에서 지정한 영역 내에서 실제 그리기를 수행 하는 Bounds 속성입니다. 표준을 그릴 ListView 사용자 지정 필요 하지 않은 요소를 사용 합니다 DrawBackgroundDrawText, 및 DrawFocusRectangle 메서드.

사용 된 DrawDefault 속성 하위 항목을 그릴 운영 체제를 하려는 경우. 이 특정 하위 항목만 사용자 지정 하려는 경우에 유용 합니다.

참고

소유자 그리기를 재정의 하는 경우 그래픽 깜박임 문제를 방지 하려면 합니다 ListView 제어 하 고 설정 합니다 DoubleBuffered 속성을 true. 이 기능은 Windows XP 및 Windows Server 2003 제품군 에서만 사용할 수 있는 애플리케이션 호출을 Application.EnableVisualStyles 메서드.

생성자

DrawListViewSubItemEventArgs(Graphics, Rectangle, ListViewItem, ListViewItem+ListViewSubItem, Int32, Int32, ColumnHeader, ListViewItemStates)

DrawListViewSubItemEventArgs 클래스의 새 인스턴스를 초기화합니다.

속성

Bounds

그릴 ListViewItem.ListViewSubItem의 크기와 위치를 가져옵니다.

ColumnIndex

ListView이 표시되는 ListViewItem.ListViewSubItem 열의 인덱스를 가져옵니다.

DrawDefault

소유자 그리기 대신 운영 체제에서 ListViewItem.ListViewSubItem을 그려야 하는지 여부를 나타내는 값을 가져오거나 설정합니다.

Graphics

Graphics을 그리는 데 사용되는 ListViewItem.ListViewSubItem를 가져옵니다.

Header

ListView이 표시되는 ListViewItem.ListViewSubItem 열의 머리글을 가져옵니다.

Item

그릴 ListViewItem의 부모 ListViewItem.ListViewSubItem을 가져옵니다.

ItemIndex

그릴 ListViewItem의 부모 ListViewItem.ListViewSubItem에 대한 인덱스를 가져옵니다.

ItemState

그릴 ListViewItem의 부모 ListViewItem.ListViewSubItem에 대한 현재 상태를 가져옵니다.

SubItem

그릴 ListViewItem.ListViewSubItem을 가져옵니다.

메서드

DrawBackground()

현재 배경색을 사용하여 ListViewItem.ListViewSubItem의 배경을 그립니다.

DrawFocusRectangle(Rectangle)

부모 ListViewItem.ListViewSubItem에 포커스가 있는 경우 ListViewItem에 대한 포커스 영역을 그립니다.

DrawText()

현재 전경색을 사용하여 ListViewItem.ListViewSubItem의 텍스트를 그립니다.

DrawText(TextFormatFlags)

현재 전경색을 사용하여 ListViewItem.ListViewSubItem의 텍스트를 그리고 지정된 TextFormatFlags 값을 사용하여 서식을 지정합니다.

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

적용 대상

추가 정보