ListView NM_CUSTOMDRAW event not working when ListViewItem BackColor property

J

jrhoads23

Hello,
I am writing a custom list view and started off by reading the great
MSDN article "Customizing a Control's Appearance Using Custom Draw" at
http://msdn.microsoft.com/library/d...ellcc/platform/commctls/custdraw/custdraw.asp.

In my list view I am catching the NM_CUSTOMDRAW event in the OCM_NOTIFY
message. Below in my code snippit you will notice that I am handling
all the appropriate paint stages. Everything seems to work fine until
my ListViewItems I add to my listview has the BackColor property set.
I am setting the BackColor of the ListViewItem to Blue and then
returning CDRF_DODEFAULT from the CDDS_SUBITEM case. This SHOULD tell
the system to handle the painting for each sub item, and thus, paint
each one with a blue background. But it does not do this, it paints a
white background for each item. It is like it is ignoring the BackColor
property of the ListViewItem. One thing I notice is that if I break
from the CCDS_ITEMPREPAINT case (instead of return), it DOES paint the
listview correctly with all the Blue background, but the CCDS_SUBITEM |
CCDS_ITEMPREPAINT case never gets called.
Another question, should I be calling return (do not process
base.WndProc) or break (will process base.WndProc) from the
CDDS_PREPAINT, CCDS_ITEMPREPAINT, ... cases?

See below with my comments...

What am I doing wrong???????


In my app's form load....
private void Form1_Load(object sender, System.EventArgs e)
{
int i;
for (i = 0; i < 5, i++)
{
System.Windows.Forms.ListViewItem lvi = new
System.Windows.Forms.ListViewItem("First Column");
lvi.SubItems.Add("Column 2");
lvi.BackColor = System.Drawing.Color.Blue;
listView1.Items.Add(lvi);
}
}

In my custom list view class...
protected override void WndProc(ref System.Windows.Forms.Message m)
{
if (m.Msg == (int)Win32Constants.OCM_NOTIFY)
{
Win32Structures.NMITEMACTIVATE pNMIA =
(Win32Structures.NMITEMACTIVATE)System.Runtime.InteropServices.Marshal.PtrToStructure(m.LParam,
typeof(Win32Structures.NMITEMACTIVATE));
switch (pNMIA.hdr.code)
{
case (int)Win32Constants.NM_CUSTOMDRAW:
Win32Structures.NMLVCUSTOMDRAW pNMCD =
(Win32Structures.NMLVCUSTOMDRAW)System.Runtime.InteropServices.Marshal.PtrToStructure(m.LParam,
typeof(Win32Structures.NMLVCUSTOMDRAW));
switch (pNMCD.nmcd.dwDrawStage)
{
case (int)Win32Constants.CCDS_PREPAINT:
// tell the system to notify us for each list view item
m.Result = (IntPtr)Win32Constants.CDRF_NOTIFYITEMDRAW;
// do I call return here or break?
return;

case (int)Win32Constants.CCDS_ITEMPREPAINT:
// tell the system to notify us for each list view sub item
m.Result = (IntPtr)Win32Constants.CDRF_NOTIFYSUBITEMDRAW;
// do I call return here or break?
// if I call break here, all listview items have the
correct background color, but the case below never gets called
return;

case ((int)Win32Constants.CCDS_SUBITEM |
(int)Win32Constants.CCDS_ITEMPREPAINT):
// tell the system to just handle the paint themselves
m.Result = (IntPtr)Win32Constants.CDRF_DODEFAULT;
// do I call return here or break?
return;

default:
m.Result = (IntPtr)Win32Constants.CDRF_DODEFAULT;
// do I call return here or break?
return;
}
break;
}
}

base.WndProc(ref m);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top