Groups | Blog | Home
all groups > dotnet general > may 2006 >

dotnet general : Listview and subclassing


Steamboat
5/27/2006 4:28:02 PM
I have read two excellent articles on how to subclass .NET controls so that
you can hook into the control’s WndProc, which is very helpful since you
cannot access the WndProc directly for .NET Compact Framework controls. The
links are:

http://blog.opennetcf.org/ayakhnin/CategoryView.aspx?category=ListView

http://blogs.msdn.com/netcfteam/archive/2005/07/24/442616.aspx

Sub-classing allows you to trap windows messages and provide handlers for
them.

I have run into a difficulty though when trying to request owner drawn
messages. I have a listview control that I have sub-classed. The reason that
I did this is so I could increase the height of the listview rows. I figured
that I could set up the control to be owner drawn and trap the WM_DRAWITEM
and WM_MEASUREITEM messages. The way that I have done this is shown below.
It is the result of putting several things that I have read together:

/// <summary>
/// Replaces the windows procedure for <see>control</see> with the
/// one specified in the constructor.
/// </summary>
public void SetHook()
{
IntPtr hwnd = control.Handle;
if (hwnd == IntPtr.Zero)
throw new InvalidOperationException(
"Handle for control has not been created");

// ADDED to make control OWNER DRAWN
IntPtr pStyle = Win32.GetWindowLong(hwnd, Win32.GWL_STYLE);
Int32 style = pStyle.ToInt32();

style &= ~LVS_ICON;
style &= ~LVS_SMALLICON;
style &= ~LVS_LIST;
style |= (LVS_OWNERDRAWFIXED | LVS_REPORT);
Win32.SetWindowLong(hwnd, Win32.GWL_STYLE, style);
// END ADDED to make control OWNER DRAWN

oldWndProc = Win32.SetWindowLong(hwnd, Win32.GWL_WNDPROC,
Marshal.GetFunctionPointerForDelegate(newWndProc));

}

The problem is that I never see any WM_DRAWITEM or WM_MEASUREITEM messages.
I do see a WM_STYLECHANGED message but that is the only additional message.
Moreover, the items are never drawn when I modify the style.

I have been working on this problem for over a week and if anyone sees
anything that I might be missing, I would appreciate their help.

Thanks for any help.

Stuart Nathan
5/28/2006 5:33:57 PM
What I know is that the only way I could get the listview to do what I
wanted was to write my own. I wanted to have a transparent listview.

AddThis Social Bookmark Button