简体   繁体   中英

VB.Net/C# - Listview Gridlines

How can I show only vertical Gridlines in my Listview, without horizontal gridlines? Like for example in taskmanager? Is there a way to do this with GDI? I am not sure how I can realize that, every helpful comment is appreciated :)

When you and I create controls we usually overload the onPaint method and do all our drawing there. Most of the default controls that you find in the toolbox however, redraws certain parts at certain messages, which makes it hard to draw over them as you never know when the control will redraw. It is possible to do so however, and the only way to figure out when to draw is by trail and error.

If you're lucky it's enough to redraw at a couple of events, but, for the most part overloading the WndProc method is the way to go.

I'm currently on a windows 8 computer, where the grid lines aren't drawn at all, so it's kinda hard to write a good example of how to remove the horizontal ones, but I'd just paint over them with the background color and then draw the vertical ones afterwards. The code below draws horizontal grid lines:

Public Class MyListView
Inherits ListView

Private Sub DrawLines() Handles Me.MouseUp
    Dim G = CreateGraphics()
    Dim x As Integer

    For i = 0 To Columns.Count - 1
        x += Columns(i).Width
        G.DrawLine(New Pen(Color.FromArgb(230, 230, 230)), x, 0, x, Height)
    Next
End Sub

Protected Overrides Sub WndProc(ByRef m As Message)
    MyBase.WndProc(m) ' To prevent something from drawing you can simply not do this call at a specific message
End Sub

End Class

A list of windows messages

Spy++ can also be helpful to find the right messages

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM