共用方式為


ListView.DrawColumnHeader 事件

定義

當已繪製 ListView 的詳細資料檢視而且 OwnerDraw 屬性設定為 true 時發生。

public:
 event System::Windows::Forms::DrawListViewColumnHeaderEventHandler ^ DrawColumnHeader;
public event System.Windows.Forms.DrawListViewColumnHeaderEventHandler DrawColumnHeader;
public event System.Windows.Forms.DrawListViewColumnHeaderEventHandler? DrawColumnHeader;
member this.DrawColumnHeader : System.Windows.Forms.DrawListViewColumnHeaderEventHandler 
Public Custom Event DrawColumnHeader As DrawListViewColumnHeaderEventHandler 

事件類型

範例

下列程式碼範例提供事件處理常式的實作 DrawColumnHeader 。 如需完整的範例,包括事件處理常式的實作 DrawSubItem ,請參閱 OwnerDraw 參考主題。

// 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;
}
' 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

備註

此事件可讓您使用擁有者繪圖自訂控制項的外觀 ListView 。 只有當 屬性設定為 trueView 屬性設定為 View.DetailsOwnerDraw ,才會引發這個屬性。 這個事件可能會針對 控制項中的每個資料行標頭髮生。 如需擁有者繪圖的詳細資訊,請參閱 OwnerDraw 屬性參考主題。

如需處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱