Recent

Author Topic: This should be straightforward: how do I change the color before DrawFocusRect?  (Read 703 times)

EganSolo

  • Full Member
  • ***
  • Posts: 215
I'm a newbie when it comes to graphics in Lazarus. I'm much more of a back-end type of coder.

I've been toying with the canvas_test example project (accessible from Tools -> Example Projects and in the unit RectangleTest, around line 69, you'll find:
Code: Pascal  [Select][+][-]
  1.     // Different rectangles
  2.     MyBitmap.Canvas.Brush.Color := clRed;
  3.     MyBitmap.Canvas.Pen.Color := clBlack;
  4.     MyBitmap.Canvas.DrawFocusRect(Bounds(25, 75, 50, 50));
  5.  

I wondered how to change the color of the rectangle produced by DrawFocusedRect. Changing the color of the pen and/or the brush has no effect. Am I missing something, or is DrawFocusRect purpose is to draw a black rectangle?

By the way, I tried to approximate that effect using the following modified code from line 72 in the same unit
Code: Pascal  [Select][+][-]
  1.       //I've added the following two lines
  2.       MyBitmap.Canvas.Pen.Color = clBlue;
  3.       MyBitmap.Canvas.Pen.Style  = psDot;
  4.       MyBitmap.Canvas.Rectangle(Bounds(325, 75, 50, 50));
  5.  

That bit of code works but the 'dot' style is coarser than the one produced by DrawFocusRect. Is there a way to approximate that style using Draw?

Thanks!

440bx

  • Hero Member
  • *****
  • Posts: 2362
I wondered how to change the color of the rectangle produced by DrawFocusedRect. Changing the color of the pen and/or the brush has no effect. Am I missing something, or is DrawFocusRect purpose is to draw a black rectangle?
I'm going to provide you with an answer that is Windows specific but, I suspect it likely applies to other O/Ss FPC/Lazarus supports (due to the LCL).  In Windows, the focus rect is drawn using an XOR operation (see MSDN), because of that the rectangle color is determined by the background color.

By the way, I tried to approximate that effect <snip>

That bit of code works but the 'dot' style is coarser than the one produced by DrawFocusRect. Is there a way to approximate that style using Draw?
Again, under Windows, the reason that the dot style is coarser is because of the possible pen styles in GDI which are quite limited. The rectangle can be made "finer" using GDI+. (I don't have an example of how it's done in Pascal but, I believe I've seen some examples in C on the net.)

HTH.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

EganSolo

  • Full Member
  • ***
  • Posts: 215
HTH: Thanks so much for your clear and concise answer! It's answers like these that make this forum so great!.
Much appreciated.
Egan

440bx

  • Hero Member
  • *****
  • Posts: 2362
HTH: Thanks so much for your clear and concise answer! It's answers like these that make this forum so great!.
Much appreciated.
Egan
You're welcome.  My pleasure.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

circular

  • Hero Member
  • *****
  • Posts: 3709
    • Personal webpage
Indeed psDot is in fact small dashes of 2 pixels. You can achieve the 1 pixel by :
- setting pen mode to pmXor
- setting pen color to white or any color you like
- drawing horizontal line with it
- drawing the same horizontal line but shifted one pixel to the right (it should be as well one pixel shorter)

Same logic for vertical lines

In the end don’t forget to set back the pen mode to normal (pmCopy I think)

Regards
Conscience is the debugger of the mind

wp

  • Hero Member
  • *****
  • Posts: 8358
Another way to draw a 1-pixel dotted line is to turn off the pen's Cosmetic property:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.PaintBox1Paint(Sender: TObject);
  2. var
  3.   wasCosmetic: Boolean;
  4. begin
  5.   wasCosmetic := Paintbox1.Canvas.Pen.Cosmetic;
  6.   Paintbox1.Canvas.Pen.Cosmetic := false;
  7.   Paintbox1.Canvas.Pen.Style := psDot;
  8.   Paintbox1.Canvas.Pen.Color := clRed;   // use any color
  9.   Paintbox1.Canvas.Rectangle(0, 0, Paintbox1.Width, Paintbox1.Height);
  10.   Paintbox1.Canvas.Pen.Cosmetic := wasCosmetic;
  11. end;
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

circular

  • Hero Member
  • *****
  • Posts: 3709
    • Personal webpage
 :D simpler indeed

Conscience is the debugger of the mind

 

TinyPortal © 2005-2018