我想在窗体上拖动button控件,但是又不能让它拖出边界,有没有这个边界的控制函数呢?
拖动的代码写好了
如:
procedure TForm1.Button1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button = mbLeft then
begin
screen.Cursor := crSizeAll;
PBoxMoving := True;
MouseOrg := Point(x,y);
end
else
PBoxMoving := False;
end;procedure TForm1.Button1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if PBoxMoving then
begin
PBoxMoving := False;
screen.Cursor := crDefault;
end;
end;procedure TForm1.Button1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if PBoxMoving then
begin
button1.Left := button1.Left + ( x - MouseOrg.X);
button1.Top := button1.Top + ( y- MouseOrg.Y);
end
end;
拖动的代码写好了
如:
procedure TForm1.Button1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button = mbLeft then
begin
screen.Cursor := crSizeAll;
PBoxMoving := True;
MouseOrg := Point(x,y);
end
else
PBoxMoving := False;
end;procedure TForm1.Button1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if PBoxMoving then
begin
PBoxMoving := False;
screen.Cursor := crDefault;
end;
end;procedure TForm1.Button1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if PBoxMoving then
begin
button1.Left := button1.Left + ( x - MouseOrg.X);
button1.Top := button1.Top + ( y- MouseOrg.Y);
end
end;
procedure TForm1.Button1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
vRect: TRect;
begin
if Button = mbLeft then
begin
screen.Cursor := crSizeAll;
PBoxMoving := True;
MouseOrg := Point(x,y);
vRect.Left := TControl(Sender).Parent.ClientOrigin.X + X;
vRect.Top := TControl(Sender).Parent.ClientOrigin.Y + Y;
vRect.Right := TControl(Sender).Parent.ClientOrigin.X +
TControl(Sender).Parent.ClientWidth - TControl(Sender).Width + X;
vRect.Bottom := TControl(Sender).Parent.ClientOrigin.Y +
TControl(Sender).Parent.ClientHeight - TControl(Sender).Height + Y;
ClipCursor(@vRect);
end
else
PBoxMoving := False;
end;procedure TForm1.Button1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if PBoxMoving then
begin
PBoxMoving := False;
screen.Cursor := crDefault;
ClipCursor(nil);
end;
end;
不过还想问你一个问题:
我在用width 的时候总是分不清楚
例如:
ClientWidth,和Width的区别是什么?
也就是说clientWidth 比width 小的那部分指的是什么,如果窗体
多了一个滚动条,那么clientwidth 包括滚动条吗?
ClipCursor函数限制的区域矩形的坐标是相对于屏幕还是窗体呢?
Shift: TShiftState; X, Y: Integer);
begin
if PBoxMoving then
begin
PBoxMoving := False;
screen.Cursor := crDefault;
ClipCursor(nil);
end;
end;
都是客户区的宽高,不把标题栏和边框计算在内~~
自己改成Width、Height看效果~~
----------------
少问多测~~