Recent

Author Topic: ListBox / focusrect  (Read 7949 times)

adc

  • New Member
  • *
  • Posts: 12
ListBox / focusrect
« on: October 30, 2015, 09:56:43 am »
Hi folks,

I can't find out how to remove focusrect on selected item from ListBox, can anyone land an hand?

Cheers,

M.

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: ListBox / focusrect
« Reply #1 on: October 30, 2015, 11:46:15 am »
Without the FocusRect, the Listbox would be pretty much useless: The ListBox is a control to select items from a list, and the FocusRect shows the user which item is selected.

If you use a Listbox with a different intention then you can owner-draw the items, I think you have full control over painting attributes there. Or you could use a read-only Memo instead of a ListBox.

bytebites

  • Hero Member
  • *****
  • Posts: 642
Re: ListBox / focusrect
« Reply #2 on: October 30, 2015, 12:20:54 pm »
ListBox1.ItemIndex:=-1;

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: ListBox / focusrect
« Reply #3 on: October 30, 2015, 01:02:37 pm »
But when the user clicks into the list the FocusRect will be back...

adc

  • New Member
  • *
  • Posts: 12
Re: ListBox / focusrect
« Reply #4 on: October 30, 2015, 02:46:27 pm »
Indeed, ListBox would be useless without a focus, what i ment was, and perhaps i didn't explained it well enough is the lined rect, you know
when you use Custom  drawItem and lbOwnderDraw method.

I have a listbox with multicolor (background) for items. That is fine and working. Also when itemIndex is changed i draw a rect (FillRect) and selected item receives a focus, that is fine also. What isn't is that there is also a gray hollow rectangle on selected item and that is the one
i would like to remove or paint in Fillrect color if possible so it isn't visible. 

look at attached picture, that gray rectangle is what i would like to remove somehow.

... i looked at some Delphi questions for the same thing and it mentioned that the value for that particular focus rect is XORed  on original
Fillrect color, if i understood articles correctly. But i can't figure out how to turn it off in Lazarus.

 Any ideas?
« Last Edit: October 30, 2015, 02:54:35 pm by adc »

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: ListBox / focusrect
« Reply #5 on: October 30, 2015, 03:13:15 pm »
XOR means that the first paint inverts color, the second paint inverts the inverted colors, i.e. restores the original colors. Therefore, you just have to call DrawFocusrect (a less-known method of TCanvas) a second time to remove the focus rect. This works in Windows where the focus rect is xor-painted (https://stackoverflow.com/questions/28649219/how-to-remove-the-dotted-focus-rectangle-from-tlistbox) - no idea about Linux etc, though:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  2.   ARect: TRect; State: TOwnerDrawState);
  3. begin
  4.   with ListBox1.Canvas do begin
  5.     if odFocused in State then
  6.       Brush.Color := clHighlight else
  7.       Brush.Color := clWindow;
  8.     FillRect(ARect);
  9.     TextOut(ARect.left, ARect.Top, Listbox1.Items[Index]);
  10.     if odFocused in State then
  11.       DrawFocusRect(ARect);
  12.   end
  13. end;  
  14.  

adc

  • New Member
  • *
  • Posts: 12
Re: ListBox / focusrect
« Reply #6 on: October 30, 2015, 03:21:06 pm »
Thanx for replay,

Problem is, for "odFocused: i get an error: Identifier not found "odFocused"

I am on Mac, perhaps that is a problem?  I am using latest builds of FPC & Lazarus on MacOS Yosemite 10.10.

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: ListBox / focusrect
« Reply #7 on: October 30, 2015, 03:42:07 pm »
Sorry, I forgot:
Code: Pascal  [Select][+][-]
  1. uses
  2.   LCLType;

adc

  • New Member
  • *
  • Posts: 12
Re: ListBox / focusrect
« Reply #8 on: October 30, 2015, 03:53:01 pm »
Thanx,

 now it did pass & compile but no joy, look


 

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: ListBox / focusrect
« Reply #9 on: October 30, 2015, 04:19:09 pm »
Ok, I don't have MacOS, so just guessing. Maybe, in the OnDrawItem event handler, set the Listbox1.Canvas.PenStyle to psClear.

adc

  • New Member
  • *
  • Posts: 12
Re: ListBox / focusrect
« Reply #10 on: October 30, 2015, 04:39:26 pm »
Tried that and with brush bsClear also, same results.

Interesting thing though, when ListBox style is lbStandard that rect isn't visible (no custom colors then though). Would it help to search trough unit sources of ListBox and search for lbStandard method to inspect how is it done, or am i missing something else?

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: ListBox / focusrect
« Reply #11 on: October 30, 2015, 04:48:09 pm »
Certainly, you can try, but I am not very optimistic. Maybe you should post your question also in the Lazarus mailing list because many developers do not read the forum.

adc

  • New Member
  • *
  • Posts: 12
Re: ListBox / focusrect
« Reply #12 on: October 30, 2015, 04:58:26 pm »
OK, i'll try asking in Lazarus mailing list. If you by any chance stumble into solution & it's not too much of a hassle, please
drop me a line.

Thank you  :)

adc

  • New Member
  • *
  • Posts: 12
Re: ListBox / focusrect
« Reply #13 on: November 01, 2015, 06:04:49 pm »
... in the meantime i did some more chacking on wretched focus rect on mac and came to a half-solution. bytebites gave me an idea and it works. If one uses onSelectionChange event to repaint listbox using OnDrawItem method with own color background as marker for current selected item (meaning, repaint your item & background at index as you want it) and then after that sets index to -1 you effectively hide focused rect, leaving the selected item to look the way one wanted. Only thing to keep in mind is you have to track your last selected item index before you set it to -1, otherwise it would be lost in case you need to do something to that specific cell that you just painted. .. hm, hope i explained it well. Method suggested by wp work on Windows, but not on Mac, which obviously has to do something with how mac system handles system color themes. And i would say it would be nice if developers could be so kind to resolve the issue. And for the time being there you have it (;

M.


Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: ListBox / focusrect
« Reply #14 on: September 12, 2020, 11:10:09 pm »
I know this is an old post, but I foud it when searching for the same problem.
For anyone else googling for it: use lbOwnerDrawFixed and unset lboDrawFocusRect from the listbox options.

Bart

 

TinyPortal © 2005-2018