Commentor Blog

When Quality Matters

Commentor A/S

When Quality Matters

Contact usSend mail

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

ListView Extended Styles in .NETCF

In this article I would like to demonstrate how to extend the ListViewcontrol in the .NET Compact Framework. We will focus on enabling someof the ListView Extended Styles. If we take a look at the WindowsMobile 5.0 Pocket PC SDK we will see that there are certain features ofListView that aren't provided by the .NET Compact Framework.

Anexample of the ListView extended styles is displaying gridlines arounditems and subitems, double buffering, and drawing a gradientbackground. These extended styles can be enabled in native code byusing the ListView_SetExtendedListViewStyle macro or by sendingLVM_SETEXTENDEDLISTVIEWSTYLE messages to the ListView.

Send Message

Wewill be using a lot of P/Invoking so let's start with creating aninternal static class called NativeMethods. We need a P/Invokedeclaration for SendMessage(HWND, UINT, UINT, UINT).

internal static class NativeMethods
{
  [DllImport("coredll.dll")]
  public static extern uint SendMessage(IntPtr hwnd, uint msg, uint wparam, uint lparam);
}

Enabling and Disabling Extended Styles

Nowthat we have our SendMessage P/Invoke declaration in place, we canbegin extending the ListView control. Let's start off with creating aclass called ListViewEx that inherits from ListView. We need to lookinto the native header files of the Pocket PC SDK to get the ListViewMessages. For now we will only need LVM_[GET/SET]EXTENDEDLISTVIEWSTYLEmessage which will be the main focus of all the examples. I willdeclare my class as a partial class and create all the pieces one byone for each example. Let's create a private method called SetStyle(),this method will enable/disable extended styles for the ListView

public partial class ListViewEx : ListView
{
  private const uint LVM_FIRST = 0x1000;
  private const uint LVM_SETEXTENDEDLISTVIEWSTYLE = LVM_FIRST + 54;
  private const uint LVM_GETEXTENDEDLISTVIEWSTYLE = LVM_FIRST + 55;

  private void SetStyle(uint style, bool enable)
  {
    uint currentStyle = NativeMethods.SendMessage(
      Handle,
      LVM_GETEXTENDEDLISTVIEWSTYLE,
      0,
      0);

    if (enable)
      NativeMethods.SendMessage(
        Handle,
        LVM_SETEXTENDEDLISTVIEWSTYLE,
        0,
        currentStyle | style);
    else
      NativeMethods.SendMessage(
        Handle,
        LVM_SETEXTENDEDLISTVIEWSTYLE,
        0,
        currentStyle & ~style);
  }
}

Grid Lines

Formy first example, let's enable GridLines in the ListView control. Wecan do this by using LVS_EX_GRIDLINES. This displays gridlines arounditems and sub-items and is available only in conjunction with theDetails mode.

public partial class ListViewEx : ListView
{
  private const uint LVS_EX_GRIDLINES = 0x00000001;

  private bool gridLines = false;
  public bool GridLines
  {
    get { return gridLines; }
    set
    {
      gridLines = value;
      SetStyle(LVS_EX_GRIDLINES, gridLines);
    }
  }
}

Whatthe code above did was add the LVS_EX_GRIDLINES style to the existingextended styles by using the SetStyle() helper method we first created.

Aninteresting discovery to this is that the Design Time attributes of theCompact Framework ListView control includes the GridLines property. Nowthat we created the property in the code, when we open the VisualStudio Properties Window for our ListViewEx we will notice thatGridLines property we created falls immediately under the "Appearance"category and even includes a description :)

Double Buffering

Doyou notice that when you populate a ListView control with a lot ofitems, the drawing flickers a lot when you scroll up and down the list?Although it is not in the Pocket PC documentation for Windows Mobile5.0, the ListView actually has an extended style calledLVS_EX_DOUBLEBUFFER. Enabling the LVS_EX_DOUBLEBUFFER solves theflickering issue and gives the user a more smooth scrolling experience.

public partial class ListViewEx : ListView
{
  private const uint LVS_EX_DOUBLEBUFFER = 0x00010000;

  private bool doubleBuffering = false;
  public bool DoubleBuffering
  {
    get { return doubleBuffering; }
    set
    {
      doubleBuffering = value;
      SetStyle(LVS_EX_DOUBLEBUFFER, doubleBuffering);
    }
  }
}

Gradient Background

Anothercool extended style is the LVS_EX_GRADIENT. This extended style draws agradient background similar to the one found in Pocket Outlook. It usesthe system colors and fades from right to left. But what is really coolabout this is that this is done by the OS. All we had to do was enablethe style.

public partial class ListViewEx : ListView
{
  private const uint LVS_EX_GRADIENT = 0x20000000;

  private bool gradient = false;
  public bool Gradient
  {
    get { return doubleBuffering; }
    set
    {
      gradient = value;
      SetStyle(LVS_EX_GRADIENT, gradient);
    }
  }
}

Ifyou want to look more into extended styles then I suggest you check outthe Pocket PC Platform SDK documentation. There a few other extendedstyles that I did not discuss that might be useful for you. You can getthe definitions in a file called commctrl.h in your Windows Mobile SDK"INCLUDE" directory.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by christian.resma.helle on Thursday, October 30, 2008 7:25 AM
Permalink | Comments (2) | Post RSSRSS comment feed

Related posts

Comments

tnomeralc web design toys us

Saturday, April 18, 2009 1:25 PM

tnomeralc web design toys

Very nice tips.. Thank you for sharing..

tukang nggame

Sunday, May 03, 2009 4:05 AM

tukang nggame

Very nice post

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

Friday, July 30, 2010 8:37 AM