实现控件的移动,改变大小(delphi实现)
主要使用perform方法
function perform(msg: cardinal; wparam, lparam: longint): longint;
只要能够使用类似于win32api的函数sendmessage(),本方法同样可在其他环境里应用。
用delphi实现
首先,建立一个应用程序,在一个窗体里加入一个panel1,保存为main.pas;
然后,分别在panel1的鼠标移动、鼠标按下事件里添加代码;
鼠标移动:控制光标的形状
procedure tform1.panel1mousemove(sender: tobject; shift: tshiftstate; x,
y: integer);
begin
if (x>=0)and(x<=3) then
begin
if (y>=0)and(y<=3) then panel1.cursor:=crsizenwse;
if (y>3)and(y<panel1.height-3) then panel1.cursor:=crsizewe;
if (y>=panel1.height-3)and(y<=panel1.height) then panel1.cursor:=crsizenesw;
end
else if (x>3)and(x<panel1.width-3) then
begin
if (y>=0)and(y<=3) then panel1.cursor:=crsizens;
if (y>3)and(y<panel1.height-3) then panel1.cursor:=crarrow;
if (y>=panel1.height-3)and(y<=panel1.width) then panel1.cursor:=crsizens;
end
else if (x>=panel1.width-3)and(x<=panel1.width) then
begin
if (y>=0)and(y<=3) then panel1.cursor:=crsizenesw;
if (y>3)and(y<panel1.height-3) then panel1.cursor:=crsizewe;
if (y>=panel1.height-3)and(y<=panel1.width) then panel1.cursor:=crsizenwse;
end;
end;
鼠标按下:控制panel的大小或位置
procedure tform1.panel1mousedown(sender: tobject; button: tmousebutton;
shift: tshiftstate; x, y: integer);
begin
releasecapture;
if (x>=0)and(x<=3) then
begin
file://左上角方向改变大小
if (y>=0)and(y<=3) then panel1.perform(wm_syscommand,$f004,0);
file://左侧
if (y>3)and(y<panel1.height-3) then panel1.perform(wm_syscommand,$f001,0);
file://左下角
if (y>=panel1.height-3)and(y<=panel1.height) then panel1.perform(wm_syscommand,$f007,0);
end
else if (x>3)and(x<panel1.width-3) then
begin
file://上侧
if (y>=0)and(y<=3) then panel1.perform(wm_syscommand,$f003,0);
file://移动控件
if (y>3)and(y<panel1.height-3) then panel1.perform(wm_syscommand,$f012,0);
file://下侧
if (y>=panel1.height-3)and(y<=panel1.width) then panel1.perform(wm_syscommand,$f006,0);
end
else if (x>=panel1.width-3)and(x<=panel1.width) then
begin
file://右上角
if (y>=0)and(y<=3) then panel1.perform(wm_syscommand,$f005,0);
file://右侧
if (y>3)and(y<panel1.height-3) then panel1.perform(wm_syscommand,$f002,0);
file://右下角
if (y>=panel1.height-3)and(y<=panel1.width) then panel1.perform(wm_syscommand,$f008,0);
end;
end;
主要使用 perform 方法为
附录1.全部代码如下:
unit main;
interface
uses
windows, messages, sysutils, classes, graphics, controls, forms, dialogs,
stdctrls, extctrls;
type
tform1 = class(tform)
panel1: tpanel;
procedure panel1mousedown(sender: tobject; button: tmousebutton;
shift: tshiftstate; x, y: integer);
procedure panel1mousemove(sender: tobject; shift: tshiftstate; x,
y: integer);
private
{ private declarations }
public
{ public declarations }
end;
var
form1: tform1;
implementation
{$r *.dfm}
procedure tform1.panel1mousedown(sender: tobject; button: tmousebutton;
shift: tshiftstate; x, y: integer);
begin
releasecapture;
if (x>=0)and(x<=3) then
begin
if (y>=0)and(y<=3) then panel1.perform(wm_syscommand,$f004,0);
if (y>3)and(y<panel1.height-3) then panel1.perform(wm_syscommand,$f001,0);
if (y>=panel1.height-3)and(y<=panel1.height) then panel1.perform(wm_syscommand,$f007,0);
end
else if (x>3)and(x<panel1.width-3) then