ListView Custom Draw

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello there,
Wonder can anyone point me in the correct direction?
I would like to change the standard list view control so that i can have
alternated rows in a different color, i.e. white grey white grey
And I'd also like to change the color or the first colmn.

What is the best approach for same?

thanks
Brian
 
Hi agian,
I see that this sample uses owner draw,
what i really would like is to use custom draw like i did in my native c++
application.
any ideas on how i can do this in .NET?

Thanks
 
Hi Brian,

The *approach* is the same as in C++ - you will just have more hassle with
C# due to all this P/Invoke stuff.
I did it myself so I can claim this is possible :-)

As far as I remember, the main idea is to handle certain ListView
notification messages (CDDS_PREPAINT and CDDS_POSTPAINT) to let the control
know you are going to do some ownerdraw. I also remember there were several
modes possible - full owner-draw, owner draw for a particular column and so
on. The most complex thing is obviously handling the notification messages
and juggling with all these API structures in C# - so you will need a solid
background in P/Invoke.

Your first step will be to override the WndProc method in a control
inherited from the ListView and to react on the NM_CUSTOMDRAW message:

protected override void WndProc(ref Message message)
{
base.WndProc(ref message);

switch (message.Msg)
{
case (int)Win32API.CommonControls.OCM_NOTIFY:
Win32API.CommonControls.NMHDR msgHeader =
(Win32API.CommonControls.NMHDR)message.GetLParam(typeof(Win32API.CommonControls.NMHDR));

switch (msgHeader.code)
{
case (int)Win32API.CommonControls.NM_CUSTOMDRAW:
// Do the custom drawing.
break;
default:
break;
}

break;

default:
break;
}
}

The Win32API.CommonControls namespace was my own one - you will have to
declare your own constants and structures.
 
Hi Dmitriy,

Excellent, I started doing this yesterday myself and am making progress,
many thanks for excellent reply, i'm now confident i'm on the right road.

thanks again
brian

Dmitriy Lapshin said:
Hi Brian,

The *approach* is the same as in C++ - you will just have more hassle with
C# due to all this P/Invoke stuff.
I did it myself so I can claim this is possible :-)

As far as I remember, the main idea is to handle certain ListView
notification messages (CDDS_PREPAINT and CDDS_POSTPAINT) to let the control
know you are going to do some ownerdraw. I also remember there were several
modes possible - full owner-draw, owner draw for a particular column and so
on. The most complex thing is obviously handling the notification messages
and juggling with all these API structures in C# - so you will need a solid
background in P/Invoke.

Your first step will be to override the WndProc method in a control
inherited from the ListView and to react on the NM_CUSTOMDRAW message:

protected override void WndProc(ref Message message)
{
base.WndProc(ref message);

switch (message.Msg)
{
case (int)Win32API.CommonControls.OCM_NOTIFY:
Win32API.CommonControls.NMHDR msgHeader =
(Win32API.CommonControls.NMHDR)message.GetLParam(typeof(Win32API.CommonControls.NMHDR));

switch (msgHeader.code)
{
case (int)Win32API.CommonControls.NM_CUSTOMDRAW:
// Do the custom drawing.
break;
default:
break;
}

break;

default:
break;
}
}

The Win32API.CommonControls namespace was my own one - you will have to
declare your own constants and structures.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

Brian Keating EI9FXB said:
Hi agian,
I see that this sample uses owner draw,
what i really would like is to use custom draw like i did in my native c++
application.
any ideas on how i can do this in .NET?

Thanks
 
Back
Top