We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Color the selected region on a TPanel

saravananvg
saravananvg asked
on
Medium Priority
212 Views
Last Modified: 2010-04-05
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.
Comment
Watch Question

Lukasz Zielinskisoftware developer
CERTIFIED EXPERT
Top Expert 2008

Commented:
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.

Author

Commented:
Hello Sir,

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

with regards,
padmaja.

Author

Commented:
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.
Commented:
Closed, 200 points refunded.
GranMod
The Experts Exchange
Community Support Moderator of all Ages

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.