Recent

Author Topic: Canvas.DrawFocusRect  (Read 537 times)

backprop

  • Jr. Member
  • **
  • Posts: 90
Canvas.DrawFocusRect
« on: August 08, 2024, 06:24:46 pm »
I think I used Canvas.DrawFocusRect on Paintbox in Delphi to select part of image inside MouseMoveEvent, but on linux that do not draw anything.

For instance, plain PaintBox1.Canvas.DrawFocusRect (rect(0,0,100,100)) draw nothing, while Canvas.Rectangle draws rectangle...

Can anyone provide working example?


« Last Edit: August 08, 2024, 07:57:17 pm by backprop »

zeljko

  • Hero Member
  • *****
  • Posts: 1639
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: Canvas.DrawFocusRect
« Reply #1 on: August 08, 2024, 09:28:40 pm »
You should do all paintings in PaintBox.OnPaint event, otherwise nothing will be painted with cocoa or qt widgetsets. Even on windows or using gtk2 it is wise to use paint events to draw stuff.

wp

  • Hero Member
  • *****
  • Posts: 12299
Re: Canvas.DrawFocusRect
« Reply #2 on: August 08, 2024, 09:52:14 pm »
Try this (just a form with a paintbox and events attached):

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, ExtCtrls,Grids,SysUtils, Forms, Controls, Graphics, Dialogs;
  9.  
  10. type
  11.   TForm1 = class(TForm)
  12.     Paintbox1: TPaintbox;
  13.     procedure FormCreate(Sender:TObject);
  14.     procedure PaintBox1MouseDown(Sender:TObject;Button:TMouseButton;Shift:
  15.       TShiftState;X,Y:Integer);
  16.     procedure PaintBox1MouseMove(Sender:TObject;Shift:TShiftState;X,Y:Integer);
  17.     procedure PaintBox1Paint(Sender:TObject);
  18.   private
  19.     FStartPt, FMousePt: TPoint;
  20.  
  21.   public
  22.  
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. procedure TForm1.PaintBox1MouseDown(Sender:TObject;Button:TMouseButton;Shift:
  33.   TShiftState;X,Y:Integer);
  34. begin
  35.   FStartPt := Point(X, Y);
  36.   FMousePt := FStartPt;
  37. end;
  38.  
  39. procedure TForm1.FormCreate(Sender:TObject);
  40. begin
  41.   FMousePt := Point(-9999, -9999);
  42. end;
  43.  
  44. procedure TForm1.PaintBox1MouseMove(Sender:TObject;Shift:TShiftState;
  45.   X,Y:Integer);
  46. begin
  47.   if (ssLeft in Shift) then
  48.   begin
  49.     FMousePt := Point(X, Y);
  50.     Paintbox1.Invalidate;
  51.   end;
  52. end;
  53.  
  54. procedure TForm1.PaintBox1Paint(Sender:TObject);
  55. var
  56.   R: TRect;
  57. begin
  58.   Paintbox1.Canvas.Brush.Color := clWhite;
  59.   Paintbox1.Canvas.FillRect(0, 0, Paintbox1.Width, Paintbox1.Height);
  60.   if (FMousePt.X > -9999) and (FMousePt.Y > -9999) then
  61.   begin
  62.     R := Rect(FStartPt.X, FStartPt.Y, FMousePt.X, FMousePt.Y);
  63.     R.NormalizeRect;
  64.     Paintbox1.Canvas.DrawFocusRect(R);
  65.   end;
  66. end;
  67.  
  68. end.

A simple Paintbox.Canvas.DrawRect in the Paintbox.OnPaint did not show the focus rect, too. I don't know whether this is specific to my Linux system (Manjaro XFCE in dark mode), but the FocusRect appeared when I filled the background with white color.

backprop

  • Jr. Member
  • **
  • Posts: 90
Re: Canvas.DrawFocusRect
« Reply #3 on: August 09, 2024, 07:52:30 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.PaintBox1Paint(Sender: TObject);
  2. begin
  3.   PaintBox1.Canvas.Draw(0, 0, bmp);
  4.  
  5.   PaintBox1.Canvas.Pen.Width := 1;
  6.   PaintBox1.Canvas.Brush.Style := bsClear;
  7.   PaintBox1.Canvas.Pen.Mode := pmNot;
  8.   PaintBox1.Canvas.Pen.Style := psDot;
  9.   PaintBox1.Canvas.Pen.Color := clWhite;
  10.  
  11.   //Paintbox1.Canvas.DrawFocusRect(Selection);
  12.   PaintBox1.Canvas.Rectangle(Selection);
  13. end;

This works as intended, but DrawFocusRect do nothing.

Linux Neon, X11, defailt theme.
« Last Edit: August 09, 2024, 08:00:47 pm by backprop »

 

TinyPortal © 2005-2018