Change source code - moving and resizing.
Posted on 2001-07-06
Change source code - moving and resizing.
This is a source code (component) for resizing and moving specific control.
I need to do changes on it. That it has ability to enable and disable moving and resizing.
Something like that :
SizePan1.enablemoving := True; or SizePan1.enablemoving := False;
SizePan1.enableresizing := True; or SizePan1.enableresizing := False;
And one more thing. In this code is not working OnClick event and maybe others too.
I need that OnClick and other events are enabled. That they are working.
If it is posible to do something like that :
SizePan1.enableevents := True; or SizePan1.enableevents := False;
uses Windows, Messages, Classes, Controls, ExtCtrls, Forms, AdvGrid;
// declare the record types to translate mouse messages...
type TCMMouseEnter = record
Msg : Cardinal;
Unused : Integer;
Sender : TControl;
Result : LongInt;
type TCMMouseLeave = TCMMouseEnter;
type TSizePan = class(TPanel)
CONSTRUCTOR Create(AOwner:TComponent); override;
// variable to hold the value passed to WM_SYSCOMMAND...
// variable to hold the "default" cursor, just in case it isn't "crDefault"
// -> Bug fix (explained below)
procedure ClientMouseMove(Sender:TObject; Shift: TShiftState; X, Y: Integer);
// Custom events to trap the MouseEnter and mouse leave messages...
// note the CM_ prefix - these are custom Windows messages
// similar to standard WM_ messages, but used internally by Delphi applications...
procedure CMMouseEnter(var Msg : TCMMouseEnter); message CM_MouseEnter;
procedure CMMouseLeave(var Msg : TCMMouseLeave); message CM_MouseLeave;
// override these default mouse events
procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
property OnClientMouseMove:TMouseMoveEvent read fClientMouseMove write fClientMouseMove;
// Added this procedure to install the component on the pallette...
// constants passed to WM_SYSCOMMAND
// For some reason, these are not listed in the help file,
// I had to play around a bit to find them...
SizeE = $F002; // east
SizeW = $F001; // west
SizeN = $F003; // north
SizeS = $F006; // south
SizeNW = $F004; // northwest
SizeNE = $F005; // northeast
SizeSW = $F007; // southwest
SizeSE = $F008; // southeast
// Drags the entire panel ...
MoveIt = $F012;
begin bug fix -
For some reason, the CMouseLeave event isn't always called.
Windows seems to drop the ball if the mouse leaves the control too quickly.
So we need to jump through some hoops here to be sure the cursor gets restored
// Declare a "dummy" class
// this "trick" just exposes the protected methods of TWinControl...
// the constructor is only needed here for the bug fix,
// but you might need it later if you decide to enhance the control...
// I assume that the Parent control will be the same as AOwner...
// Save the defined Client MouseMove event (if there is one)...
// and assign the new one...
// save the default screen cursor...
procedure TSizePan.ClientMouseMove(Sender:TObject; Shift: TShiftState; X, Y: Integer);
// restore the cursor...
// call any other code that is attached to this event...
if assigned(fClientMouseMove) then fClientMouseMove(Sender,Shift,X,Y);
(* end bug fix *)
// save the value of the screen cursor when we enter the panel...
procedure TSizePan.CMMouseEnter(var Msg : TCMMouseEnter);
// and restore it when we leave...
procedure TSizePan.CMMouseLeave(var Msg : TCMMouseLeave);
// Note that nothing is really "happening" here as far as sizing the panel-
// we are just setting things up in case MouseDown gets called...
procedure TSizePan.MouseMove(Shift: TShiftState; X, Y: Integer);
Edge=10; // how close to the edge do we get before saying we're "on" it? (in pixels)
Corner=20; // the corners are a bit harder to detect, so we add some tolerance
// Create a TPoint from from the mouse coordinates passed to the function...
// All this if-then-else stuff decides which constant will be passed to WM_SYSCOMMAND
// Here we just use some math to create a small square at each corner,
// then check to see if the mouse is within any them...
if ptInRect(Rect(0,0,Corner,Corner),P) then fSysCmd:=SizeNW
else if ptInRect(Rect(0,Height-Corner,Corner,Height),P) then fSysCmd:=SizeSW
else if ptInRect(Rect(Width-Corner,Height-Corner,Width,Height),P) then fSysCmd:=SizeSE
else if ptInRect(Rect(Width-Corner,0,Width,Corner),P) then fSysCmd:=SizeNE
// if it's not in a corner, then is it near an edge?
else if (X < Edge) then fSysCmd:=SizeW
else if ( X > (Width-Edge) ) then fSysCmd:=SizeE
else if (Y < Edge) then fSysCmd:=SizeN
else if ( Y > (Height-3) ) then fSysCmd:=SizeS
// if it's none of the above, then MouseDown should drag the whole panel...
// Now that we have the correct system command, we can use its
// value to determine which screen cursor to display...
with Screen do case fSysCmd of
procedure TSizePan.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
// Give the new cursor a unique integer constant...
// I declared a variable to hold the value of the "Align" property
// this is more-or-less "reserved for future enhancements" ...
// the four-sided arrow (move) cursor is not available in Delphi 2,
// so I borrow this one from Windows...
// make sure the default MouseMove stuff gets executed...
// We don't drag with the right button, so bail out if it's down...
if Button=mbRight then EXIT;
// if the Left button is down, and we're not on an edge, then we must be moving...
if (ssLeft in Shift) and (Screen.Cursor=crDefault) then Screen.Cursor:=crMove;
// save the setting of the Align property...
// this really isn't needed here, but I had hoped to adapt this code
// to create something like a "dockable" panel...
// Let go of the mouse capture...
// not sure about this, but I read somewhere that it's a good idea...
// "Perform" tells the component to execute an API call on itself...
Perform(WM_SysCommand,fSysCmd,0); //<- And here's the heart of the whole component!
// Restore the settings, and we're done!