Valhalla Legends Archive

Programming => General Programming => Topic started by: Maddox on August 05, 2003, 12:16 AM

Title: Drawing a focus rect for an owner-drawn listbox
Post by: Maddox on August 05, 2003, 12:16 AM
I'm trying to draw a focus rect when a user clicks an item in my owner-drawn listbox. So far I've only been able to draw one when a user mouses over an item. I tried processing the ODA_FOCUS and ODA_SELECT messages to no avail. I also tried using DefWindowProc() following the advice of DM, but this did not work either. Any help would be appreciated.
Title: Re:Drawing a focus rect for an owner-drawn listbox
Post by: Skywing on August 05, 2003, 01:38 AM
Quote from: Maddox on August 05, 2003, 12:16 AM
I'm trying to draw a focus rect when a user clicks an item in my owner-drawn listbox. So far I've only been able to draw one when a user mouses over an item. I tried processing the ODA_FOCUS and ODA_SELECT messages to no avail. I also tried using DefWindowProc() following the advice of DM, but this did not work either. Any help would be appreciated.
The best way to handle this is to simply check the item state for the item being currently drawn in WM_DRAWITEM and draw a focus rect there if necessary.

Edit: Use WM_DRAWITEM and not WM_PAINT.
Title: Re:Drawing a focus rect for an owner-drawn listbox
Post by: DarkMinion on August 05, 2003, 01:06 PM
Sky, I'm curious as to how that works, because in MSDN documentation, it says that both wParam and lParam are unused in WM_PAINT.

I guess the documentation is wrong?
Title: Re:Drawing a focus rect for an owner-drawn listbox
Post by: Skywing on August 05, 2003, 01:17 PM
Quote from: DarkMinion on August 05, 2003, 01:06 PM
Sky, I'm curious as to how that works, because in MSDN documentation, it says that both wParam and lParam are unused in WM_PAINT.

I guess the documentation is wrong?
No, that should have been WM_DRAWITEM and not WM_PAINT.  Sorry (although you could still use the item-from-point stuff with WM_PAINT).
Title: Re:Drawing a focus rect for an owner-drawn listbox
Post by: DarkMinion on August 05, 2003, 01:59 PM
Ah, I knew I wasn't crazy!
Title: Re:Drawing a focus rect for an owner-drawn listbox
Post by: Maddox on August 05, 2003, 04:51 PM
Quote from: Skywing on August 05, 2003, 01:38 AM
Quote from: Maddox on August 05, 2003, 12:16 AM
I'm trying to draw a focus rect when a user clicks an item in my owner-drawn listbox. So far I've only been able to draw one when a user mouses over an item. I tried processing the ODA_FOCUS and ODA_SELECT messages to no avail. I also tried using DefWindowProc() following the advice of DM, but this did not work either. Any help would be appreciated.
The best way to handle this is to simply check the item state for the item being currently drawn in WM_DRAWITEM and draw a focus rect there if necessary.

Edit: Use WM_DRAWITEM and not WM_PAINT.

I've already tried this and it draws the rect only when the cursor is over an item.
Title: Re:Drawing a focus rect for an owner-drawn listbox
Post by: Adron on August 06, 2003, 07:21 AM
Maybe you have some kind of modern listbox? Normally focus will stay on an item you've clicked
Title: Re:Drawing a focus rect for an owner-drawn listbox
Post by: Skywing on August 06, 2003, 01:12 PM
Quote from: Adron on August 06, 2003, 07:21 AM
Maybe you have some kind of modern listbox? Normally focus will stay on an item you've clicked
To my knowledge there's no built in way to do that with class "LISTBOX".

Here's what I use:

   if(DrawStruct.itemState & ODS_SELECTED) {
      FillRect(DrawStruct.hDC, &DrawStruct.rcItem, GetSysColorBrush(COLOR_HIGHLIGHT));
      TextColor = RGB(0xff, 0xff, 0xff);
   }

   if(DrawStruct.itemState & ODS_FOCUS && GetFocus() == DrawStruct.hwndItem)
      DrawFocusRect(DrawStruct.hDC, &DrawStruct.rcItem);

Note that this only draws the focus rect when your program has focus.  If you activate a window not in the tree which the ListBox is a member of, the focus rect will disappear.