 |
|
 |
I like your clean code.
btw, I am going to use your code (modified version) into a commercial project, Should I need a license?
|
|
|
|
 |
|
 |
This was almost exactly as I was looking for but I wanted to handle the drawing from the control's owner class. Adding this piece of code to EventListView class will allow the owner of the EventListView to receive an event on each call to ICustomDraw methods.
Add this to the EventListView class.
public delegate void ListViewCustomDrawEventHandler(int idCtrl, ref Win32.NMCUSTOMDRAW nmcd, ref int cdrf);
public event ListViewCustomDrawEventHandler OnPrePaintEvent;
public event ListViewCustomDrawEventHandler OnPostPaintEvent;
public event ListViewCustomDrawEventHandler OnPreEraseEvent;
public event ListViewCustomDrawEventHandler OnPostEraseEvent;
public event ListViewCustomDrawEventHandler OnItemPrePaintEvent;
public event ListViewCustomDrawEventHandler OnItemPostPaintEvent;
public event ListViewCustomDrawEventHandler OnItemPreEraseEvent;
public event ListViewCustomDrawEventHandler OnItemPostEraseEvent;
Then add this peice of code to each of the ICustomDraw members of EventListView
public int OnPrePaint(int idCtrl, ref Win32.NMCUSTOMDRAW nmcd)
{
int cdrf = (int)Win32.CDRF.CDRF_DODEFAULT;
OnPrePaintEvent(idCtrl, ref nmcd, ref cdrf);
return cdrf;
}
You can then implement the event handler on the owner control and you return one of CDRF flags. If you don't implement the event, the default draw is performed
|
|
|
|
 |
|
 |
It looks like the PREPAINT event handles both the item background and item draw events. Is there a way to check if the current PREPAINT pass is for the item background or for the actual item draw?
|
|
|
|
 |
|
 |
Your code was of great help to me. I wanted to show where a dragged item would end up and needed some custom drawing. It was a snap to integrate your code.
Keep up the good work
|
|
|
|
 |
|
 |
Brian,
I've been looking everywhere for how to do an OwnerDrawn ListView in Compact Framework. It appears that this is perhaps the best approach I've seen. What I want to do is simply adjust the height of the listview items regardless of the font size. (It appears that the default behavior in CF 2.0 is to make the listview height dependent upon the font size.) I'm going to approach the problem by following your model. Hopefully, it will work!
John F.
|
|
|
|
 |
|
 |
Windows automatically use the imagelist height or the font height. Basically, in the constructor you will store the original imagelist or font in a variable then create a dummy imagelist or font with the desired height then assign it to the listview. This will re-evaluate the height of the items. In the CustomDraw method use the original imagelist or font.
|
|
|
|
 |
|
 |
Mahika,
Thanks for your response. I will try this.
-John
John F.
|
|
|
|
 |
|
 |
Hi,
I'm having trouble changing the size of an item, (height) to allow for more than one line of text and other things such as images
|
|
|
|
 |
|
 |
What happens when you have more than one column of data? I havent yet figured out how to get the subitems to paint using this project.
|
|
|
|
 |
|
 |
Set UseItemStyleForSubItems property to false to change look of subitems.
YourListViewItem.UseItemStyleForSubItems = false;
|
|
|
|
 |
|
 |
Nice article, helping me along my way nicely.
One issue though that I havent been able to resolve... the items are no longer highlighted by the system. Any way to forgo the custom drawing for selected items?
|
|
|
|
 |
|
 |
Had a brain fart yesterday. Checking for the Item.Selected property works fine.
|
|
|
|
 |
|
|
 |
|
 |
Hi!
Fine article! I have got a problem with scrollbar: I need to handle a scroll event without WndProc. So I set the scrollable prop to false and placed there a custom ScrollBar. That works fine with events, but if I scrolled down I got no access to the invisible items. If I set the scrollable prop to true my custom SB is placed beneath the ListViews origin. If I draw my custom SB at the place where the ListViews origin appears, the origin is at the top and I cannot access my custom SB anymore. Is there a way to hide the origin SB through WIN32 Api?
Please help.
Sebastian.
|
|
|
|
 |
|
 |
Thanks for the article. I don't seem to get notifications for Subitems. Any suggestions?
|
|
|
|
 |
|
 |
I've found the best thing to do is just manually iterate through your subitems in the CDDS_ITEMPREPAINT handler. Here is some of my code (tried to remove the unnecessary bits):
ListViewItem item = Items[ nmcd.dwItemSpec ];
for( int subItemIndex = 0; subItemIndex < item.SubItems.Count; subItemIndex++ )
{
RECT subItemRect = new RECT();
subItemRect.top = subItemIndex;
subItemRect.left = 0;
SendMessage( Handle, LVM_GETSUBITEMRECT, item.Index, ref subItemRect );
subItemRect.right = subItemRect.left + SendMessage( Handle, LVM_GETCOLUMNWIDTH, subItemIndex, 0 );
if( subItemIndex == 0 )
{
Rectangle rectIcon = item.GetBounds( ItemBoundsPortion.Icon );
item.ImageList.Draw( g, rectIcon.Left, rectIcon.Top, rectIcon.Width, rectIcon.Height, item.ImageIndex );
subItemRect.left += rectIcon.Width + 3;
}
RectangleF rectItem = RectangleF.FromLTRB( subItemRect.left, subItemRect.top, subItemRect.right, subItemRect.bottom );
g.FillRectangle( new SolidBrush( bgColor ), rectItem );
g.DrawString( item.SubItems[ subItemIndex ].Text, item.Font, new SolidBrush( textColor ), rectItem, stringFormat );
}
|
|
|
|
 |
 | Why ?  |  | phlaphi | 23:00 2 Jan '05 |
|
 |
Hi, I'm wondering why you've decided to not take owner draw approach ? Is this caused by a performance concern - does owner drawing is slower than that you've presented in your article ?
Rgs.
.
phlaphi
|
|
|
|
 |
|
 |
Hi there phlaphi,
There are really 3 ways of drawing a listview ctrl.
1) Do everything yourself
2) Owner draw (where you get supplied with the information and you do the drawing
3) Custom draw were you can choose to do certain sections of the drawing and let the rest be done for you.
prob not the best explanation i know but if you have a read on MSDN you'll find a proper explanation.
regards
Brian Keating
All the best
Brian.
|
|
|
|
 |
|
 |
I wonder, Would this example work on Compact Framework?
|
|
|
|
 |
|
 |
Hi dandolo,
To tell the truth I'm not sure, I don't program for the compact framework. However if the same notification messages get sent (and I would guess they are sent) then I can't see there being a problem.
Regards
Brian Keating
www.briankeating.net
|
|
|
|
 |
|
 |
The problem is that Compact Framework (CF) does not support overriding WndProc. I have already come accross such remarks in the web and checked this trying to compile it under CF. The compiler did not recognized Windows.Forms.Message class, and said that there is no such member as Wndproc. There should be some other way to catch win messages in CF.
|
|
|
|
 |