Link to home
Start Free TrialLog in
Avatar of retnuh
retnuh

asked on

Transparent rectangle component

I'm trying to make a resizable and moveable component at runtime, well I got that to work from someones example. But I can't get the darn thing to draw transparent. Basically I need a framed rectangle. here's the code I'm working from.
also when I switch what its inherited from to TGraphicControl transparency works, but I loose the ability to move and resize the component.

unit analysisControl;

interface

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

type
  TAnalysisControl = class(TCustomControl)
  private
    FCanvas: TCanvas;
  protected
    procedure Paint; override;
    property Canvas: TCanvas read FCanvas;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure MouseDown(Button : TMouseButton; Shift : TShiftState; X,Y:integer); override;
    procedure MouseMove(Shift : TShiftState; X,Y:integer); override;
  end;

procedure Register;

implementation

uses WinProcs;
const
  sizeBorder = 2;
  sc_SizeLeft =       $F001;
  sc_SizeRight =      $F002;
  sc_SizeTop =        $F003;
  sc_SizeTopLeft =    $F004;
  sc_SizeTopRight =   $F005;
  sc_SizeBottom =     $F006;
  sc_SizeBottomRight = $F008;
  sc_SizeBottomLeft = $F007;
  sc_DragMove =       $F012;

procedure TAnalysisControl.MouseDown(Button : TMouseButton; Shift : TShiftState; X,Y:integer);
begin
 if Button = mbLeft then
 begin
  WinProcs.ReleaseCapture;
  if(X >= Width - sizeBorder) and
     NOT((Y<=sizeborder) or (Y >= Height - sizeborder)) then
     Self.Perform(WM_SysCommand, sc_SizeRight,0)
  else
  If NOT((X<= sizeBorder) or (X >= Width - sizeBorder)) and
     (Y <= sizeBorder) then
     self.Perform(WM_SysCommand,sc_SizeTop,0)
  else
  if(X<= sizeborder) and (y <= sizeborder) then
    self.Perform(WM_SysCommand,sc_SizeTopLeft,0)
  else
  if(X >= width - sizeborder) and (y<= sizeborder) then
    self.Perform(WM_SysCommand, sc_SizeTopRight,0)
  else
  if NOT((X<= sizeborder) or (X>= width - sizeborder)) and
     (y>= height - sizeborder) then
     self.Perform(WM_SysCommand,sc_SizeBottom,0)
  else
  if(Y>= height - sizeborder) and (x<=sizeborder) then
    self.Perform(WM_SysCommand,sc_sizebottomleft,0)
  else
  if(Y >= height - SizeBorder) and (X>=width-sizeborder) then
    self.Perform(WM_SysCommand,sc_SizeBottomRight,0)
  else
  if NOT((Y <= sizeBorder) or (Y>= height - sizeborder)) and
     (X<= sizeborder) then
     self.Perform(WM_SysCommand,sc_SizeLeft,0)
  else
  begin
   self.Perform(WM_SysCommand,sc_DragMove,0);
  end;
 end;
end;

procedure TAnalysisControl.Paint;
begin
  canvas.pen.color := clMaroon;
  canvas.brush.Color := clWhite;
  canvas.brush.style := bsClear;
  canvas.Rectangle(GetClientRect.Left,GetClientRect.Top,GetClientRect.Right,GetClientRect.Bottom);
end;

procedure Tanalysiscontrol.MouseMove(Shift:TShiftState; x,y:integer);
begin
  if(X <= sizeborder) or (X >= width - sizeborder) then
    begin
      if(Y>= height-sizeborder) then
        begin
          if(X>= width - sizeborder) then
            Cursor := crSizeNWSE
          else
            cursor := crSizeNESW;
        end
      else
        if(Y <= sizeborder) then
          begin
            if(x >= width - sizeborder) then
              cursor := crSizeNESW
            else
              cursor := crSizeNWSE;
          end
        else
          cursor := crSizeWE;
     end
  else
    if(Y <= sizeBorder) or (Y >= height - sizeborder) then
      begin
        cursor := crSizeNS;
      end
    else
      Cursor := crDefault;
end;

procedure Register;
begin
  RegisterComponents('Samples', [TanalysisControl]);
end;

constructor TAnalysisControl.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FCanvas := TControlCanvas.Create;
  TControlCanvas(FCanvas).Control := Self;
  width := 96;
  height := 16;
end;

destructor TAnalysisControl.Destroy;
begin
  FCanvas.Free;
  inherited Destroy;
end;

end.



I know its a bit long to post, but its driving me nuts, and I'm sure its probably something pretty easy, I've tried several different methods, from several diferent tips and tricks sites, but its just my luck none of them worked for me.
Avatar of retnuh
retnuh

ASKER

Edited text of question
Hi tenuh,

try this out, it draws an animated rectangle around an image :

Is this the thing you need?

  procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;Shift : TShiftState; X,Y:integer);
  begin
    ImageMouse := True;
    ImageRect.Left := X;
    ImageRect.Top  := Y;
    ImageRect.Right := X;
    ImageRect.Bottom  := Y;
    Image1.Canvas.DrawFocusRect(ImageRect);
  end;

  procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,  Y: Integer);
  Var
    NewRect : tRect;
  begin
    if ImageMouse then
      If (X > ImageRect.Left) AND (Y > ImageRect.Top) then  begin
        {Restore the background}
        Image1.Canvas.DrawFocusRect(ImageRect);
        {Change Rectangle}
        ImageRect.Right := X;
        ImageRect.Bottom := Y;
        {Draw Focus Rectangle}
        Image1.Canvas.DrawFocusRect(ImageRect);
      end;
  end;

  procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X,  Y: Integer);

  begin
    {Restore the background}
    if ImageMouse then begin
      ImageRect.Right := X;
      ImageRect.Bottom := Y;
      Image1.Canvas.DrawFocusRect(ImageRect);
      ImageMouse := False;
      Image1.Canvas.CopyRect(Image1.Canvas.ClipRect,
                                               Image1.Canvas,ImageRect)
    end;
  end;

Zif.
Avatar of retnuh

ASKER

With the code below when the component is inherited from TCustomControl it still fills the rectangle even though the brush style is set to bsClear, when the component is inherited from TGraphicControl the rectangle is transparent, but I loose the ability to move and resize it. If I could base it off of TGraphicControl and still move and resize it that would work great. Also if I could figure out what it is in TWinControl that stops the bsClear style from working correctly that would work.

  canvas.pen.color := clMaroon;
  canvas.brush.Color := clWhite;
  canvas.brush.style := bsClear;
  canvas.Rectangle(GetClientRect.Left,GetClientRect.Top,GetClientRect.Right,GetClientRect.Bottom);

ASKER CERTIFIED SOLUTION
Avatar of Jacco
Jacco
Flag of Netherlands image

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
Yo,
bsClear as a brush style only affects drawing operations on the canvas. To get your component transparent, you have to stick to TGraphicControl. All descendants of TWinControl get a WM_ERASEBACKGROUND message that you will have to work around then. Difficult, because you have to notify the underlying controls to repaint themselves. Flicker on the screen and nasty programming is the result of this approach.
TControl introduces protected MouseMove/MouseUp/MouseDown handlers that you can overwrite to implement your move/resize behaviour at runtime.
If this is what you need and if I should give you an example, please re-open the question.

Slash/d003303
Avatar of retnuh

ASKER

The CreateParams works, now if only I could get it to refresh on mouseup, I can deal with it not being transparent while dragging. I have notice that it refreshes when you resize it, I'm playing around with that now. Any more info would be great.
try to override the MouseUp handler, call inherited and invalidate.

Slash/d003303
Avatar of retnuh

ASKER

I tried overriding the MouseUp, but its not working, I've also tried messing with WMERASEBACKGROUND but the redrawing is so often that the flicking is terrible.
Avatar of retnuh

ASKER

Well I figured it out, after
self.Perform(WM_SysCommand,sc_DragMove,0);
I called:
parent.repaint;
paint;
then I found that I had to call every paint method for this type of component other wise all the other ones disappear.