Link to home
Start Free TrialLog in
Avatar of saravananvg
saravananvgFlag for India

asked on

Color the selected region on a TPanel

Hello,

   I would like to divide the TPanel into say 24x18 blocks and once the user starts selecting these blocks I would like to fill them with the selected color, similar to masking. I am infact showing a video and would like to mask with the selected color on the video when the user selects the rectangular region.

with regards,
padmaja.
Avatar of Lukasz Zielinski
Lukasz Zielinski
Flag of Poland image

write Your own component based on TCustomPanel, where You can move Canvas property to public section, then use Canvas' to draw anything You want.

ziolko.
Avatar of saravananvg

ASKER

Hello Sir,

  I did find a solution for my question. I shall post it soon.

with regards,
padmaja.
Hello Sir,

  Here is the solution.


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
    Capturing : bool;
    Captured : bool;
    StartPlace : TPoint;
    EndPlace : TPoint;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
implementation

{$R *.DFM}

function MakeRect(Pt1 : TPoint;
                  Pt2 : TPoint) : TRect;
begin
  if pt1.x < pt2.x then begin
    Result.Left := pt1.x;
    Result.Right := pt2.x;
  end else begin
    Result.Left := pt2.x;
    Result.Right := pt1.x;
  end;
  if pt1.y < pt2.y then begin
    Result.Top := pt1.y;
    Result.Bottom := pt2.y;
  end else begin
    Result.Top := pt2.y;
    Result.Bottom := pt1.y;
  end;
end;
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Captured then
  StartPlace.x := X;
  StartPlace.y := Y;
  EndPlace.x := X;
  EndPlace.y := Y;
  Capturing := true;
  Captured := true;
end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  S : TRect;
  tmpi,tmpj,tmpk1,tmpk2 : Integer;
begin
  if Capturing then
  begin
    EndPlace.x := X;
    EndPlace.y := Y;
    tmpk1:=4;
    tmpk2:=24;
    tmpi:=0;
    repeat
    tmpj:=0;
    repeat
      if PtInRect(rect(tmpj*Form1.Width div tmpk1,tmpi*Form1.Height div tmpk2,(tmpj+1)*Form1.Width div tmpk1,(tmpi+1)*Form1.Height div tmpk2),Point(EndPlace.X,EndPlace.Y)) then
      begin
        S.Left := tmpj*Form1.Width div tmpk1;
        S.Top := tmpi*Form1.Height div tmpk2;
        S.Right := (tmpj+1)*Form1.Width div tmpk1;
        S.Bottom := (tmpi+1)*Form1.Height div tmpk2;
        Form1.Canvas.Brush.Color := CLyELLOW;
        Form1.Canvas.FillRect(S);
      end;
    tmpj := tmpj+1;
    until tmpj>=tmpk1;
    tmpi := tmpi+1;
    until tmpi>=tmpk2;
  end;
end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Capturing := false;
end;
end.

with regards,
padmaja.
ASKER CERTIFIED SOLUTION
Avatar of GranMod
GranMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial