Results 1 to 17 of 17

Thread: Column header click event in flat listview

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2016
    Posts
    230

    Column header click event in flat listview

    Hello

    For aesthetic reasons, the listviews in my program are made to look flat, and I had to use custom API calls to do that, because the "flat appearance" property in VB6 does not work.

    Code:
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nindex As Long) As Long
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nindex As Long, ByVal dwNewLong As Long) As Long
    
    Public Sub flattenLvHeader(ByRef lv As Object)
       ' hSysHeader = FindWindowEx(lv.hWnd, 0, "SysHeader32", vbNullString) ' For VB5 listviews
       hSysHeader = FindWindowEx(lv.hWnd, 0, "msvb_lib_header", vbNullString) ' For VB6 listviews
       SetWindowLong hSysHeader, GWL_STYLE, GetWindowLong(hSysHeader, GWL_STYLE) And Not HDS_BUTTONS
    End Sub
    
    (...)
    
    Call flattenLvHeader(ListView1)
    However, that prevents the event from firing when the user clicks on a listview header, and I need that event to let the user sort the listview by clicking a column header.

    Does anyone know how to make a listview look flat so that clicking on the column header still fires an event?

  2. #2
    gibra
    Guest

    Re: Column header click event in flat listview

    What version of listview you use?

    - Microsoft Windows Common Controls 5.0 (SP2)
    - Microsoft Windows Common Controls 6.0 (SP6)

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2016
    Posts
    230

    Re: Column header click event in flat listview

    Microsoft Windows Common Controls 6.0 (SP6)

  4. #4
    gibra
    Guest

    Re: Column header click event in flat listview

    See the HDS_FLAT flag

    Header Control Styles (Windows)
    https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2016
    Posts
    230

    Re: Column header click event in flat listview

    Can't say that I know what to do with that. Replacing HDS_BUTTONS with HDS_FLAT (which I found from one source only that it should be &H200, is that even correct?) makes the listview headers no longer flat.

  6. #6
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: Column header click event in flat listview

    Ah you need to understand how to set/unset Bits and Masking.

    I suspect you just want to Or HDS_FLAT, but definitely try and figure out how to set bits, using VB.

    Code:
    SetWindowLong hSysHeader, GWL_STYLE, GetWindowLong(hSysHeader, GWL_STYLE) Or HDS_FLAT

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2016
    Posts
    230

    Re: Column header click event in flat listview

    Thank you, unfortunately that does not make the listview header flat as "And Not HDS_BUTTONS" does.

    Code:
    Private Const HDS_FLAT As Long = &H200

  8. #8
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: Column header click event in flat listview

    Your project must not be manifested for CC 6.0 ?
    honestly I can't even get your FindWindow call to work.
    Last edited by DEXWERX; Sep 7th, 2016 at 10:12 AM.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jul 2016
    Posts
    230

    Re: Column header click event in flat listview

    I don't know how a project is manifested for CC 6.0, but "Project > Components > Microsoft Windows Common Controls 6.0 (SP6)" is ticked, and if I try to untick it I get "Can't remove control or reference; in use", and I'm running it in Windows XP using "Windows Classic style", and the VBP file has:
    Code:
    Object={831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0; mscomctl.ocx
    What else should I check?

  10. #10
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: Column header click event in flat listview

    the code you posted doesn't work either. *shrugs*

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jul 2016
    Posts
    230

    Re: Column header click event in flat listview

    The code I posted works just fine here. It also works in the compiled program when run on Windows XP, 7 and 10.

    Uses these constants:
    Code:
    Private Const HDS_BUTTONS As Long = &H2
    Private Const GWL_STYLE = -16
    Private hSysHeader As Long

  12. #12
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: Column header click event in flat listview

    Hi OldClock,

    Just to start, I always use the VB6 SP6 ListView as well (Microsoft Windows Common Controls 6.0 (SP6), mscomctl.ocx).

    It sounds like you're determined to get it working with the ListView headers. But I'll share the way I've long done it. I assume you're using the ListView in "Report" mode.

    I also often use the "Flat" mode for its appearance. I'm unclear why you say that doesn't work. It works fine for me. I suppose it does still leave the black single-pixel border around the control, and also the headers still have a 3D appearance, but the list itself is flat.

    Here's a picture of how it looks for me:

    Name:  lv.gif
Views: 2000
Size:  8.9 KB

    Now, regarding clicking the headers, the ColumnClick event also works just fine. However, I'm not entirely sure why but long ago, I abandoned using the column headers. I always just turn them off. In their place, I use labels correctly placed above the columns. And sometimes, these labels are clickable, allowing me to do certain things to the listview (such as sort it, etc). I might even occasionally popup a context menu when the label is clicked.

    Using labels does have a couple of downsides though. For one, you can't resize your column widths, or rearrange them. If you need these features, you'll need to stick with the listview column headers.

    I just thought I'd provide information about my experiences with listview.

    Regards,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jul 2016
    Posts
    230

    Re: Column header click event in flat listview

    Setting appearance to flat does indeed change something, but the effect is incomplete. One must manually change the column headers to look consistent.

    Example working project:
    https://filebin.net/o48wzeqbmx6oi84s


    Screenshot:
    https://filebin.net/o48wzeqbmx6oi84s...907_190407.png
    Name:  screenshot_20160907_190407.jpg
Views: 1842
Size:  11.8 KB
    Listview appearance from the left: 3D, flat, 3D and API-flattened, flat and API-flattened.

    Unfortunately, the column header click event does not fire for the really flat listviews - 3 and 4.
    Last edited by OldClock; Sep 7th, 2016 at 12:10 PM.

  14. #14
    PowerPoster
    Join Date
    Jun 2015
    Posts
    2,224

    Re: Column header click event in flat listview

    It may be possible with subclassing, but I can't figure out what messages and notifications are involved.
    Last edited by DEXWERX; Sep 7th, 2016 at 01:29 PM.

  15. #15
    gibra
    Guest

    Re: Column header click event in flat listview

    Quote Originally Posted by OldClock View Post
    Unfortunately, the column header click event does not fire for the really flat listviews - 3 and 4.
    As explained in the Microsoft article Header Control Styles (post #4) about HDS_BUTTONS flag:

    Each item in the control looks and behaves like a push button.

    Of course, if you remove the HDS_BUTTONS, the listview will lost this feature, of course ColumnClick event doesn't fire anymore.

  16. #16
    New Member
    Join Date
    Nov 2020
    Posts
    7

    Re: Column header click event in flat listview

    Has anyone found a solution for flat and clickable header?

  17. #17
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Column header click event in flat listview

    You would be better off starting a new thread on the subject. There's a fair chance that this one won't get all that many views, and it never got to a solution anyways. You'll get more eyes on the question with a new thread.
    My usual boring signature: Nothing

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width