0

I know that there are available Properties for ListView to change the common styles but I just want to test this. That is for example the View = View.LargeIcon applies the style LVS_ICON = 0 to the ListView or GridLines = true applies the style LVS_EX_GRIDLINES = 1 to the ListView. I would like to test with CreateParams. I think using GetWindowLong and SetWindowLong Win32 functions would be OK, but for convenience, as far as I know, CreateParams can change the style of a control. But this time with a ListView, I can't make it work, it has no effect at all, I wonder if ListView is a special case?. Here is my code:

public class CustomListView : ListView {        
    protected override CreateParams CreateParams {
      get {
       CreateParams cp = base.CreateParams;
       cp.Style |= 3; //Apply LVS_LIST (View as List)
       return cp;
      }
    }
}

Only LVS_EX_GRIDLINES = 1 makes some effect but the effect is not Grid lines are drawn on the ListView but the Border becomes thicker and looks like 3D-border. That's strange, most of other applies have not effect at all.

Could you explain it or at least give me some example which works? Again please don't give me any solution or code which uses GetWindowLong and SetWindowLong, just use CreateParams.

Thanks!

King King
  • 61,710
  • 16
  • 105
  • 130
  • `LVS_EX_GRIDLINES` is an *extended* list view style, and is not one of the normal Window styles that can be accessed with `GetWindowLong`. Instead you set these styles after the control has been created using `LVM_SETEXTENDEDLISTVIEWSTYLE` and retrieve them with `LVM_GETEXTENDEDLISTVIEWSTYLE`. – Jonathan Potter Jun 13 '13 at 01:43

1 Answers1

0

If it helps to explain how this works, this is handled internally by the ListView.UpdateExtendedStyles function, which gets called when one of the properties relating to an extended style is set.

Quoting from the relevant section on MSDN

msdn

Reflector disassembles the function as follows

protected void UpdateExtendedStyles()
{
    if (base.IsHandleCreated)
    {
        int lparam = 0;
        int wparam = 0x10cfd;
        switch (this.activation)
        {
            case ItemActivation.OneClick:
                lparam |= 0x40;
                break;

            case ItemActivation.TwoClick:
                lparam |= 0x80;
                break;
        }
        if (this.AllowColumnReorder)
        {
            lparam |= 0x10;
        }
        if (this.CheckBoxes)
        {
            lparam |= 4;
        }
        if (this.DoubleBuffered)
        {
            lparam |= 0x10000;
        }
        if (this.FullRowSelect)
        {
            lparam |= 0x20;
        }
        if (this.GridLines)
        {
            lparam |= 1;
        }
        if (this.HoverSelection)
        {
            lparam |= 8;
        }
        if (this.HotTracking)
        {
            lparam |= 0x800;
        }
        if (this.ShowItemToolTips)
        {
            lparam |= 0x400;
        }
        base.SendMessage(0x1036, wparam, lparam);
        base.Invalidate();
    }
}

EDIT

The reason you can't use CreateParams is because it's not relevant for a ListView see MSDN here - excerpt copied below

msdn

Roger Rowland
  • 25,885
  • 11
  • 72
  • 113
  • Using `SendMessage` works OK, I know that. But I can see it's obviously that `CreateParams` doesn't work. `CreateParams` also has its `ExStyle`. I've tried with `GetWindongLong` and `SetWindowLong` with both `Style` (GWL_STYLE) and `Extended style` (GWL_EXSTYLE) but they don't work. It looks like that only `SendMessage` works. – King King Jun 13 '13 at 08:49
  • @KingKing that's correct about `CreateParams` - see edit above. – Roger Rowland Jun 13 '13 at 09:04
  • 1
    Thanks, I didn't notice that info although I visited that documentation page (I just looked in the example section). – King King Jun 13 '13 at 09:06
  • I did have a chance to view the `ListView.cs` (not the whole source code but much enough) and I can see they use `CreateParams` there but with the help of `GetWindowLong` and it's not all the code changing the styles for `ListView` because they also use `SendMessage` and I can see there are only some styles involved in the `CreateParams`. – King King Jun 13 '13 at 10:32