Solved

Transparent rectangle component

Posted on 1998-07-06
9
1,545 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
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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now