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_SizeTo p,0)
else
if(X<= sizeborder) and (y <= sizeborder) then
self.Perform(WM_SysCommand ,sc_SizeTo pLeft,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_SizeBo ttom,0)
else
if(Y>= height - sizeborder) and (x<=sizeborder) then
self.Perform(WM_SysCommand ,sc_sizebo ttomleft,0 )
else
if(Y >= height - SizeBorder) and (X>=width-sizeborder) then
self.Perform(WM_SysCommand ,sc_SizeBo ttomRight, 0)
else
if NOT((Y <= sizeBorder) or (Y>= height - sizeborder)) and
(X<= sizeborder) then
self.Perform(WM_SysCommand ,sc_SizeLe ft,0)
else
begin
self.Perform(WM_SysCommand ,sc_DragMo ve,0);
end;
end;
end;
procedure TAnalysisControl.Paint;
begin
canvas.pen.color := clMaroon;
canvas.brush.Color := clWhite;
canvas.brush.style := bsClear;
canvas.Rectangle(GetClient Rect.Left, GetClientR ect.Top,Ge tClientRec t.Right,Ge tClientRec t.Bottom);
end;
procedure Tanalysiscontrol.MouseMove (Shift:TSh iftState; 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('Sample s', [TanalysisControl]);
end;
constructor TAnalysisControl.Create(AO wner: TComponent);
begin
inherited Create(AOwner);
FCanvas := TControlCanvas.Create;
TControlCanvas(FCanvas).Co ntrol := 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.
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
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
else
If NOT((X<= sizeBorder) or (X >= Width - sizeBorder)) and
(Y <= sizeBorder) then
self.Perform(WM_SysCommand
else
if(X<= sizeborder) and (y <= sizeborder) then
self.Perform(WM_SysCommand
else
if(X >= width - sizeborder) and (y<= sizeborder) then
self.Perform(WM_SysCommand
else
if NOT((X<= sizeborder) or (X>= width - sizeborder)) and
(y>= height - sizeborder) then
self.Perform(WM_SysCommand
else
if(Y>= height - sizeborder) and (x<=sizeborder) then
self.Perform(WM_SysCommand
else
if(Y >= height - SizeBorder) and (X>=width-sizeborder) then
self.Perform(WM_SysCommand
else
if NOT((Y <= sizeBorder) or (Y>= height - sizeborder)) and
(X<= sizeborder) then
self.Perform(WM_SysCommand
else
begin
self.Perform(WM_SysCommand
end;
end;
end;
procedure TAnalysisControl.Paint;
begin
canvas.pen.color := clMaroon;
canvas.brush.Color := clWhite;
canvas.brush.style := bsClear;
canvas.Rectangle(GetClient
end;
procedure Tanalysiscontrol.MouseMove
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('Sample
end;
constructor TAnalysisControl.Create(AO
begin
inherited Create(AOwner);
FCanvas := TControlCanvas.Create;
TControlCanvas(FCanvas).Co
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.
Hi tenuh,
try this out, it draws an animated rectangle around an image :
Is this the thing you need?
procedure TForm1.Image1MouseDown(Sen der: 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.DrawFocusRec t(ImageRec t);
end;
procedure TForm1.Image1MouseMove(Sen der: 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.DrawFocusRec t(ImageRec t);
{Change Rectangle}
ImageRect.Right := X;
ImageRect.Bottom := Y;
{Draw Focus Rectangle}
Image1.Canvas.DrawFocusRec t(ImageRec t);
end;
end;
procedure TForm1.Image1MouseUp(Sende r: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer);
begin
{Restore the background}
if ImageMouse then begin
ImageRect.Right := X;
ImageRect.Bottom := Y;
Image1.Canvas.DrawFocusRec t(ImageRec t);
ImageMouse := False;
Image1.Canvas.CopyRect(Ima ge1.Canvas .ClipRect,
Image1.Canvas,ImageRect)
end;
end;
Zif.
try this out, it draws an animated rectangle around an image :
Is this the thing you need?
procedure TForm1.Image1MouseDown(Sen
begin
ImageMouse := True;
ImageRect.Left := X;
ImageRect.Top := Y;
ImageRect.Right := X;
ImageRect.Bottom := Y;
Image1.Canvas.DrawFocusRec
end;
procedure TForm1.Image1MouseMove(Sen
Var
NewRect : tRect;
begin
if ImageMouse then
If (X > ImageRect.Left) AND (Y > ImageRect.Top) then begin
{Restore the background}
Image1.Canvas.DrawFocusRec
{Change Rectangle}
ImageRect.Right := X;
ImageRect.Bottom := Y;
{Draw Focus Rectangle}
Image1.Canvas.DrawFocusRec
end;
end;
procedure TForm1.Image1MouseUp(Sende
begin
{Restore the background}
if ImageMouse then begin
ImageRect.Right := X;
ImageRect.Bottom := Y;
Image1.Canvas.DrawFocusRec
ImageMouse := False;
Image1.Canvas.CopyRect(Ima
Image1.Canvas,ImageRect)
end;
end;
Zif.
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(GetClient Rect.Left, GetClientR ect.Top,Ge tClientRec t.Right,Ge tClientRec t.Bottom);
canvas.pen.color := clMaroon;
canvas.brush.Color := clWhite;
canvas.brush.style := bsClear;
canvas.Rectangle(GetClient
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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/MouseDow n 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
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/MouseDow
If this is what you need and if I should give you an example, please re-open the question.
Slash/d003303
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
Slash/d003303
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.
ASKER
Well I figured it out, after
self.Perform(WM_SysCommand ,sc_DragMo ve,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.
self.Perform(WM_SysCommand
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.
ASKER