运行时改变控件的大小(点击后立刻ReleaseCapture,然后计算位移,最后发消息改变位置)——最有趣的是TPanel其实也有窗口标题,因此可发HTCAPTION消息

时间:2022-03-02 22:02:12

//光标在控件不同位置时的样式

// 由于拐角这点手动精确实在困难 所以用范围 范围+3 这样很容易就找到这一点了
procedure CtrlMouseMove(Ctrl: TWinControl; Shift: TShiftState;X, Y: Integer);
begin
with Ctrl do
begin
if (X >= ) and (X <= ) then
begin
if (Y >= ) and (Y <= ) then Cursor := crSizeNWSE;
if (Y > ) and (Y < Height - ) then Cursor := cRsizewe;
if (Y >= Height - ) and (Y <= Height) then Cursor := crSizeNESW;
end
else if (X > ) and (X < Width - ) then
begin
if (Y >= ) and (Y <= ) then Cursor := crSizeNS;
if (Y > ) and (Y < Height - ) then Cursor := cRarrow;
if (Y >= Height - ) and (Y <= Width) then Cursor := crSizeNS;
end
else if (X >= Width - ) and (X <= Width) then
begin
if (Y >= ) and (Y <= ) then Cursor := crSizeNESW;
if (Y > ) and (Y < Height - ) then Cursor := cRsizewe;
if (Y >= Height - ) and (Y <= Width) then Cursor := crSizeNWSE;
end;
end;
end;
//改变控件的大小
procedure CtrlMouseDown(Ctrl: TWinControl; Button: TMouseButton;  Shift: TShiftState; X, Y: Integer);
var
WParam: Integer;
begin
with Ctrl do
begin
if (X >= ) and (X <= ) then
begin
if (Y >= ) and (Y <= ) then WParam:=$F004;
if (Y > ) and (Y < Height - ) then WParam:=$F001;
if (Y >= Height - ) and (Y <= Height) then WParam:=$F007;
end
else if (X > ) and (X < Width - ) then
begin
if (Y >= ) and (Y <= ) then WParam:=$F003;
if (Y > ) and (Y < Height - ) then WParam:=$F012;
if (Y >= Height - ) and (Y <= Width) then WParam:=$F006;
end
else if (X >= Width - ) and (X <= Width) then
begin
if (Y >= ) and (Y <= ) then WParam:=$F005;
if (Y > ) and (Y < Height - ) then WParam:=$F002;
if (Y >= Height - ) and (Y <= Width) then WParam:=$F008;
end;
ReleaseCapture; // 不断调用API,冒充鼠标释放
SendMessage(Handle,WM_SYSCOMMAND,WParam,); // 发消息改变ctrl的位置,受启发,也可改成SendMessage(Handle,WM_NCLBUTTONDOWN,HTCAPTION,0);
end;
end;

使用

procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
CtrlMouseDown(Panel1, Button, Shift, X, Y);
end; procedure TForm1.Panel1MouseMove(Sender: TObject; Shift: TShiftState;
X, Y: Integer);
begin
CtrlMouseMove(Panel1, Shift, X, Y);
end;

参考: http://www.cnblogs.com/xe2011/p/3426494.html

------------------------------------------------------------------------------------------

这些 WParam:=$F004; 是什么意思呢?MSDN说明:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms646360(v=vs.85).aspx

还是不懂.

另外,这个例子为啥右键不能拖动?不也是MouseDown吗?

------------------------------------------------------------------------------------------