ListView style tricks

Attempting to make a ListView control in C# resemble anything close to the ones showcased in Microsoft's own software - particularly in vista, unearths a slew of quirks and issues. In my endeavour to mimic the latest MS Live apps I spent hours sifting through Google, here’s what I’ve learnt.

Enabling Vista selection glyph’s (as opposed to the solid blue bar)

At first I tried setting up a VisualStyleRenderer however, even using the numerical piece and state numbers from tmschema.h fails, which is a pity as it would offer more control than the solution presented. A simple interop call to SetWindowTheme does the trick.


[DllImport("uxtheme", CharSet = CharSet.Unicode)]
public extern static Int32 SetWindowTheme(IntPtr hWnd, String textSubAppName, String textSubIdList);        

/// <summary> 
/// Window procedure override. 
/// </summary> 
protected override void WndProc(ref Message m) 

    switch (m.Msg) 
    { 
        case 0x1: // WM_CREATE. 
           { 
                SetWindowTheme(this.Handle, "Explorer", null); 
                base.WndProc(ref m); 
                break; 
           } 
     } 
}

Killing the focus rectangle (Deprecated)

[EDIT] Revised method ListView style tricks - Revenge of the focus rectangle. 

Now that we have themed selection, the focus rectangle ( black-white alternating rubber band ) sticks out like Jessica Simpson at a Mensa meeting. To rectify this simply override WM_SETFOCUS in the WndProc signalling that we’ve handled the notification.

case 0x07: // WM_SETFOCUS. 
   { 
          m.Result = new IntPtr(1); 
          base.WndProc(ref m); 
          break; 
   }

I’ve not tested the implications of this thoroughly but it seems to work. Purists may claim that, as this is a per-user system preference it shouldn’t be disabled but if MS does it I don’t see the harm – personally I think it’s unsightly, a throwback from from an earlier era.

Hiding vertical grid lines in details mode

Unfortunately with vista theming turned on, the ListView in details mode comes with vertical gridlines whether you want them or not. Luckily setting a background image hides them ala Windows Media Player 11 – of course you likely incur the overhead associated with such a hack but it’s minimal and the only method I could find.

/// <summary>
/// Hack to disable gridlines using a dummy image as background.
/// </summary>
private void NoGridBgImgHack()
{
    this.BackgroundImage = new Bitmap(16, 16, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
    this.BackgroundImageTiled = true;

    using (Graphics Gfx = Graphics.FromImage(this.BackgroundImage))
    {
        Gfx.Clear(this.BackColor);
    }
}

Adjusting list Item’s height

The ListView quickly becomes very cramped esp. with groups enabled. In order to increase the height of a list item whilst maintaining our original font size alter the ListView’s associated ImageList dimensions.

0 Comments  |  Share