W
web_lance
Unregistered / Unconfirmed
GUEST, unregistred user!
主要的功能是要实现运行时对控件的Sizing和dragging。
调用方法:在OnMouseDown中加入代码ControlSizer.Attach(TControl(Sender));
将下面源码中的注释符去掉,编译出错,其中一条错误提示是
Cannot focus a disabled or invisible window.
另外,有些地方应该还需要改进一下,麻烦各位指点一二。
unit ControlSizer;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, Graphics;
type
TControlSizer = class(TCustomControl)
private
{ Private declarations }
FPosList: array [1..8] of Integer;
FRectList: array [1..8] of TRect;
FControl: TControl;
FPatternColor: TColor;
procedure SetPatternColor(const Value: TColor);
procedure WMNCHitTest(var Msg: TWMNCHitTest); message WM_NCHITTEST;
procedure WMSize(var Msg:TWMSize); message WM_SIZE;
procedure WMLButtonDown(var Msg: TWMLButtonDown); message WM_LBUTTONDOWN;
procedure WMMove(var Msg: TWMMove); message WM_MOVE;
protected
{ Protected declarations }
procedure CreateParams(var Params: TCreateParams); override;
procedure CreateHandle; override;
procedure Paint; override;
procedure SizerControlExit(Sender: TObject);
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Attach(AControl: TControl);
published
{ Published declarations }
property Control: TControl read FControl;
property PatternColor: TColor read FPatternColor write SetPatternColor default clBlack;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TControlSizer]);
end;
{ TControlSizer }
procedure TControlSizer.Attach(AControl: TControl);
var
R: TRect;
begin
if AControl <> nil then
begin
FControl := AControl;
R := FControl.BoundsRect;
InflateRect(R, 2, 2);
BoundsRect := R;
Parent := FControl.Parent;
end;
end;
constructor TControlSizer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Height := 28;
Width := 28;
FPatternColor := clBlack;
FPosList[1] := htTopLeft;
FPosList[2] := htTop;
FPosList[3] := htTopRight;
FPosList[4] := htRight;
FPosList[5] := htBottomRight;
FPosList[6] := htBottom;
FPosList[7] := htBottomLeft;
FPosList[8] := htLeft;
OnExit := SizerControlExit;
end;
procedure TControlSizer.CreateHandle;
begin
inherited Createhandle;
end;
procedure TControlSizer.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.ExStyle := Params.ExStyle or WS_Ex_Transparent;
end;
destructor TControlSizer.Destroy;
begin
inherited Destroy;
end;
procedure TControlSizer.Paint;
var
I: Integer;
begin
Canvas.Brush.Color := FPatternColor;
for I := 1 to 8 do
begin
with FRectList do
begin
Canvas.Rectangle(Left, Top, Right, Bottom);
end;
end;
end;
procedure TControlSizer.SetPatternColor(const Value: TColor);
begin
if FPatternColor <> Value then
begin
FPatternColor := Value;
Repaint;
end;
end;
procedure TControlSizer.SizerControlExit(Sender: TObject);
begin
Free;
end;
procedure TControlSizer.WMLButtonDown(var Msg: TWMLButtonDown);
const
SC_DragMove: Longint = $F012;
begin
Perform(WM_SYSCOMMAND, SC_DragMove, 0);
end;
procedure TControlSizer.WMMove(var Msg: TWMMove);
var
R: TRect;
begin
R := BoundsRect;
InflateRect(R, -2, -2);
end;
procedure TControlSizer.WMNCHitTest(var Msg: TWMNCHitTest);
var
Pt: TPoint;
I: Integer;
begin
Pt := Point(Msg.XPos, Msg.YPos);
Pt := ScreenToClient(Pt);
Msg.Result := 0;
for I := 1 to 8 do
begin
if PtInRect(FRectList, Pt) then Msg.Result := FPosList;
end;
if Msg.Result = 0 then inherited;
end;
procedure TControlSizer.WMSize(var Msg: TWMSize);
var
R: TRect;
X, Y: Integer;
begin
R := BoundsRect;
InflateRect( R, -2, -2);
if Odd(Width) then X := 2 else X := 3;
if Odd(Height) then Y := 2 else Y := 3;
FRectList[1] := Rect(0, 0, 5, 5);
FRectList[2] := Rect(Width div 2 - X, 0, Width div 2 + 5 - X, 5);
FRectList[3] := Rect(Width - 5, 0, Width, 5);
FRectList[4] := Rect(Width - 5, Height div 2 - Y, Width, Height div 2 + 5 - Y);
FRectList[5] := Rect(Width - 5, Height - 5, Width, Height);
FRectList[6] := Rect(Width div 2 - X, Height - 5, Width div 2 + 5 - X, Height);
FRectList[7] := Rect(0, Height - 5, 5, Height);
FRectList[8] := Rect(0, Height div 2 - Y, 5, Height div 2 + 5 - Y);
end;
end.
调用方法:在OnMouseDown中加入代码ControlSizer.Attach(TControl(Sender));
将下面源码中的注释符去掉,编译出错,其中一条错误提示是
Cannot focus a disabled or invisible window.
另外,有些地方应该还需要改进一下,麻烦各位指点一二。
unit ControlSizer;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, Graphics;
type
TControlSizer = class(TCustomControl)
private
{ Private declarations }
FPosList: array [1..8] of Integer;
FRectList: array [1..8] of TRect;
FControl: TControl;
FPatternColor: TColor;
procedure SetPatternColor(const Value: TColor);
procedure WMNCHitTest(var Msg: TWMNCHitTest); message WM_NCHITTEST;
procedure WMSize(var Msg:TWMSize); message WM_SIZE;
procedure WMLButtonDown(var Msg: TWMLButtonDown); message WM_LBUTTONDOWN;
procedure WMMove(var Msg: TWMMove); message WM_MOVE;
protected
{ Protected declarations }
procedure CreateParams(var Params: TCreateParams); override;
procedure CreateHandle; override;
procedure Paint; override;
procedure SizerControlExit(Sender: TObject);
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Attach(AControl: TControl);
published
{ Published declarations }
property Control: TControl read FControl;
property PatternColor: TColor read FPatternColor write SetPatternColor default clBlack;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TControlSizer]);
end;
{ TControlSizer }
procedure TControlSizer.Attach(AControl: TControl);
var
R: TRect;
begin
if AControl <> nil then
begin
FControl := AControl;
R := FControl.BoundsRect;
InflateRect(R, 2, 2);
BoundsRect := R;
Parent := FControl.Parent;
end;
end;
constructor TControlSizer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Height := 28;
Width := 28;
FPatternColor := clBlack;
FPosList[1] := htTopLeft;
FPosList[2] := htTop;
FPosList[3] := htTopRight;
FPosList[4] := htRight;
FPosList[5] := htBottomRight;
FPosList[6] := htBottom;
FPosList[7] := htBottomLeft;
FPosList[8] := htLeft;
OnExit := SizerControlExit;
end;
procedure TControlSizer.CreateHandle;
begin
inherited Createhandle;
end;
procedure TControlSizer.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.ExStyle := Params.ExStyle or WS_Ex_Transparent;
end;
destructor TControlSizer.Destroy;
begin
inherited Destroy;
end;
procedure TControlSizer.Paint;
var
I: Integer;
begin
Canvas.Brush.Color := FPatternColor;
for I := 1 to 8 do
begin
with FRectList do
begin
Canvas.Rectangle(Left, Top, Right, Bottom);
end;
end;
end;
procedure TControlSizer.SetPatternColor(const Value: TColor);
begin
if FPatternColor <> Value then
begin
FPatternColor := Value;
Repaint;
end;
end;
procedure TControlSizer.SizerControlExit(Sender: TObject);
begin
Free;
end;
procedure TControlSizer.WMLButtonDown(var Msg: TWMLButtonDown);
const
SC_DragMove: Longint = $F012;
begin
Perform(WM_SYSCOMMAND, SC_DragMove, 0);
end;
procedure TControlSizer.WMMove(var Msg: TWMMove);
var
R: TRect;
begin
R := BoundsRect;
InflateRect(R, -2, -2);
end;
procedure TControlSizer.WMNCHitTest(var Msg: TWMNCHitTest);
var
Pt: TPoint;
I: Integer;
begin
Pt := Point(Msg.XPos, Msg.YPos);
Pt := ScreenToClient(Pt);
Msg.Result := 0;
for I := 1 to 8 do
begin
if PtInRect(FRectList, Pt) then Msg.Result := FPosList;
end;
if Msg.Result = 0 then inherited;
end;
procedure TControlSizer.WMSize(var Msg: TWMSize);
var
R: TRect;
X, Y: Integer;
begin
R := BoundsRect;
InflateRect( R, -2, -2);
if Odd(Width) then X := 2 else X := 3;
if Odd(Height) then Y := 2 else Y := 3;
FRectList[1] := Rect(0, 0, 5, 5);
FRectList[2] := Rect(Width div 2 - X, 0, Width div 2 + 5 - X, 5);
FRectList[3] := Rect(Width - 5, 0, Width, 5);
FRectList[4] := Rect(Width - 5, Height div 2 - Y, Width, Height div 2 + 5 - Y);
FRectList[5] := Rect(Width - 5, Height - 5, Width, Height);
FRectList[6] := Rect(Width div 2 - X, Height - 5, Width div 2 + 5 - X, Height);
FRectList[7] := Rect(0, Height - 5, 5, Height);
FRectList[8] := Rect(0, Height div 2 - Y, 5, Height div 2 + 5 - Y);
end;
end.