1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
| unit Resizer;
interface
uses
SysUtils, Windows, Messages, Classes, Graphics, Controls,
Forms, Dialogs;
type
TResizer = class (TCustomControl)
private
FOldRgn:HRGN;
FHandledControl: TControl;
FBorderColor: TColor;
FResizePointColor: TColor;
FBorderWidth: Integer;
FBitmap:TBitmap;
procedure SetHandledControl(const Value: TControl);
procedure SetBorderColor(const Value: TColor);
procedure SetResizePointColor(const Value: TColor);
procedure SetBorderWidth(const Value: Integer);
procedure WMNCHitTest(var Message: TWMNCHitTest); message WM_NCHITTEST;
procedure WMMove(var Message: TWMMove); message WM_MOVE;
procedure DefineBitmap(Bitmap:TBitMap);
protected
function InnerRect:TRect;
procedure Resize; override;
procedure AjustResizer;
procedure Paint; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
X: Integer; Y: Integer); override;
public
property BorderWidth:Integer read FBorderWidth write SetBorderWidth default 6;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property HandledControl: TControl read FHandledControl write SetHandledControl;
property BorderColor:TColor read FBorderColor write SetBorderColor;
property ResizePointColor:TColor read FResizePointColor write SetResizePointColor;
end;
const
crDeplacer=1;
implementation
{$R *.res}
{
*********************************** TResizer ***********************************
}
procedure TResizer.AjustResizer;
var
R:TRect;
begin
Visible:=False;
if Assigned(HandledControl) then
begin
Parent:=HandledControl.Parent;
R:=HandledControl.BoundsRect;
Dec(R.Left,BorderWidth);
Dec(R.Top,BorderWidth);
Inc(R.Right,BorderWidth);
Inc(R.Bottom,BorderWidth);
BoundsRect:=R;
Visible:=True;
end;
end;
constructor TResizer.Create(AOwner: TComponent);
begin
inherited;
FBorderWidth:=6;
FOldRgn:=Integer(nil);
Cursor:=crDeplacer;
FResizePointColor:=clNavy;
FBorderColor:=clWhite;
FBitmap:=TBitmap.Create;
DefineBitmap(FBitmap);
end;
procedure TResizer.DefineBitmap(Bitmap:TBitMap);
var
I,J:Integer;
begin
with Bitmap do
begin
Width:=8;
Height:=8;
PixelFormat:=pf24bit;
if BorderColor<0 then
Bitmap.Canvas.Brush.Color:=ColorToRGB(BorderColor)
else
Canvas.Brush.Color:=BorderColor;
Canvas.FillRect(Rect(0,0,8,8));
I:=1;
while I<8 do
begin
J:=0;
while J<7 do
begin
if (((I div 2)+ (J div 2)) mod 2)<>0 then
Canvas.Pixels[J,I]:=ResizePointColor;
Inc(J,2);
end;
Inc(I,2);
end;
end;
end;
destructor TResizer.Destroy;
begin
HandledControl:=nil;
FBitmap.Destroy;
DeleteObject(FOldRgn);
inherited;
end;
procedure TResizer.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
Y: Integer);
begin
inherited;
if ssLeft in Shift then
begin
ReleaseCapture;
Perform(WM_SYSCOMMAND,$f012,0);
end;
end;
procedure TResizer.Paint;
var
R:TRect;
begin
Canvas.Brush.Color:=BorderColor;
Canvas.Brush.Bitmap:=FBitmap;
Canvas.FillRect(ClientRect);
Canvas.Brush.Bitmap:=nil;
Canvas.Brush.Color:=ResizePointColor;
Canvas.FillRect(Rect(0,0,BorderWidth,BorderWidth));
Canvas.FillRect(Rect((Width-BorderWidth) div 2,0,(Width+BorderWidth)div 2,BorderWidth) );
Canvas.FillRect(Rect(Width-BorderWidth-1,0,Width,BorderWidth) );
Canvas.FillRect(Rect(Width-BorderWidth-1,(Height-BorderWidth)div 2,Width,(Height+BorderWidth)div 2) );
Canvas.FillRect(Rect(Width-BorderWidth-1, Height-BorderWidth-1,Width,Height) );
Canvas.FillRect(Rect((Width-BorderWidth)div 2, Height-BorderWidth-1,(Width+BorderWidth)div 2, Height) );
Canvas.FillRect(Rect(0, Height-BorderWidth-1, BorderWidth, Height) );
Canvas.FillRect(Rect(0,(Height-BorderWidth)div 2, BorderWidth, (Height+BorderWidth)div 2));
end;
procedure TResizer.SetBorderWidth(const Value: Integer);
begin
FBorderWidth := Value;
AjustResizer;
end;
procedure TResizer.SetHandledControl(const Value: TControl);
begin
if FHandledControl <> Value then
begin
FHandledControl := Value;
if Assigned(FHandledControl) then
AjustResizer
else
begin
visible:=false;
Parent:=nil;
end;
end;
end;
procedure TResizer.SetBorderColor(const Value: TColor);
begin
if FBorderColor<>Value then
begin
FBorderColor := Value;
DefineBitmap(FBitmap);
Invalidate;
end;
end;
procedure TResizer.SetResizePointColor(const Value: TColor);
begin
if FResizePointColor<>Value then
begin
FResizePointColor := Value;
DefineBitmap(FBitmap);
Invalidate;
end;
end;
procedure TResizer.WMNCHitTest(var Message: TWMNCHitTest);
var
P:TPoint;
begin
// Tester si l'evenement de la souris est definit pour nous
Message.Result:=HTTRANSPARENT;
P:=ScreenToClient(Point(Message.XPos,Message.YPos));
if P.X<10 then
begin
// On est dans la partie gauche du control
if P.Y<10 then
Message.Result:=HTTOPLEFT // Le coin superieur gauche
else
begin
if Height-P.Y<10 then
Message.Result:=HTBOTTOMLEFT // Le coin inferieur gauche
else
begin
if Abs(P.Y - (Height div 2))<5 then
Message.Result:=HTLEFT
else
Message.Result:=HTCLIENT;
end;
end;
end
else
if (Width-P.X)<10 then
begin
// On est dans la partie droite du controle
if P.Y<10 then
Message.Result:=HTTOPRIGHT // Le coin superieur droit
else
begin
if Height-P.Y<10 then
Message.Result:=HTBOTTOMRIGHT // Le coin inferieur droit
else
begin
if Abs(P.Y - (Height div 2))<5 then
Message.Result:=HTRIGHT
else
Message.Result:=HTCLIENT;
end;
end;
end
else
begin
if P.Y<10 then
begin
// On est dans le haut du controle
if Abs(P.X-(Width div 2))<5 then
Message.Result:=HTTOP
else
Message.Result:=HTCLIENT;
end
else
if (Height-P.Y<10) then
begin
// On est dans le bat du controle
if Abs(P.X-(Width div 2))<5 then
Message.Result:=HTBOTTOM
else
Message.Result:=HTCLIENT;
end
end;
end;
function TResizer.InnerRect: TRect;
begin
Result:=BoundsRect;
Inc(Result.Left,BorderWidth);
Inc(Result.Top,BorderWidth);
Dec(Result.Right,BorderWidth);
Dec(Result.Bottom,BorderWidth);
end;
procedure TResizer.WMMove(var Message: TWMMove);
begin
inherited;
if Assigned(HandledControl) then
begin
HandledControl.BoundsRect:=InnerRect;
end;
end;
procedure TResizer.Resize;
var
rgn, rgn2:HRGN;
begin
// creer une region de clipping
rgn :=CreateRectRgn(0,0,Width-1,Height-1);
rgn2:=CreateRectRgn(BorderWidth,BorderWidth,Width-(BorderWidth+1),Height-(BorderWidth+1));
CombineRgn(rgn,rgn,rgn2,RGN_DIFF);
SetWindowRgn(Handle,rgn,Visible);
DeleteObject(rgn2);
DeleteObject(FOldRgn);
FOldRgn:=rgn;
if Assigned(HandledControl) then
begin
HandledControl.BoundsRect:=InnerRect;
end;
inherited;
end;
initialization
Screen.Cursors[crDeplacer]:=LoadCursor(HInstance,IDC_HAND); {'CR_DEPLACER'}
end. |