Thursday, October 23, 2008

Hide ListView border in .NET Compact Framework

To disable (or hide) the border of a ListView control in .NET Compact Framework,

1. Declare the following PInvoke methods...

#region PInvoke Declarations

const int GWL_STYLE = -16;

const int WS_BORDER = 0x00800000;

const int SWP_NOSIZE = 0x1;

const int SWP_NOMOVE = 0x2;

const int SWP_FRAMECHANGED = 0x20;

 

[DllImport("coredll.dll")]

private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

 

[DllImport("coredll.dll")]

private extern static void SetWindowLong(IntPtr hwnd, int nIndex, int dwNewLong);

 

[DllImport("coredll.dll")]

private static extern bool SetWindowPos(IntPtr hwnd, IntPtr hWndInsertAfter,

   int x, int y, int cx, int cy, int uflags);

#endregion



2. Write a ShowListViewBorder method...

/// <summary>

/// Shows the list view border.

/// </summary>

/// <param name="handle">The handle.</param>

/// <param name="showBorder">if set to <c>true</c> [show border].</param>

private void ShowListViewBorder(IntPtr handle, bool showBorder)

{

   int style = GetWindowLong(handle, GWL_STYLE);

   if (showBorder)

   {

        style |= WS_BORDER;

   }

   else

   {

        style &= ~WS_BORDER;

   }

   SetWindowLong(handle, GWL_STYLE, style);

   SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);

}



3. Call the ShowListViewBorder...

ShowListViewBorder(window.lvwDetails.Handle, false);

1 comment:

Anonymous said...

Ashvin, this is the simplest most elegant example that I've found in 2 days of searching. Thank you.

Bo Gusman