Solved

Transparent rectangle component

Posted on 1998-07-06
9
1,563 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:retnuh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 

Author Comment

by:retnuh
ID: 1357192
Edited text of question
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1357193
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.
0
 

Author Comment

by:retnuh
ID: 1357194
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);

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Accepted Solution

by:
Jacco earned 100 total points
ID: 1357195
Your component doesn't need a Canvas (a CustomContol already has one). So delete your property an private variable van the creation/destruction of it.

override the following:

procedure TAnalysisControl.CreateParams(var Params : TCreateParams);
begin
  inherited CreateParams(Params);
  Params.ExStyle:=Params.ExStyle or WS_EX_TRANSPARENT;
end;

Only while dragging it is not showing the proper background...

But this should get you going.
0
 
LVL 4

Expert Comment

by:d003303
ID: 1357196
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
0
 

Author Comment

by:retnuh
ID: 1357197
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.
0
 
LVL 4

Expert Comment

by:d003303
ID: 1357198
try to override the MouseUp handler, call inherited and invalidate.

Slash/d003303
0
 

Author Comment

by:retnuh
ID: 1357199
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.
0
 

Author Comment

by:retnuh
ID: 1357200
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.

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question