在白色全屏幕形式中,我将有来自同一来源的七个帧。我想用鼠标移动它们并在退出时保存该位置,以便稍后在同一位置加载。我可以移动一个面板,如果我按住鼠标并移动它,从delphi.about.com
中得到的代码中移动它但如果我点击该面板内的WinControl,当然我没有从面板中获取OnMouseDown。
如何移动面板(或框架)移动其中的任何控件而不对其所具有的每个组件进行编码?
答案 0 :(得分:2)
这是一个快速示例,通过叠加解释我所指的内容:
TTransparentPanel = class(TPanel)
protected
procedure WMEraseBkGnd(var msg: TWMEraseBkGnd); message WM_ERASEBKGND;
procedure CreateParams(var params: TCreateParams); override;
end;
procedure TTransparentPanel.CreateParams(var params: TCreateParams);
begin
inherited CreateParams(params);
params.ExStyle := params.ExStyle or WS_EX_TRANSPARENT;
end;
procedure TTransparentPanel.WMEraseBkGnd(var msg: TWMEraseBkGnd);
begin
SetBkMode (msg.DC, TRANSPARENT);
msg.result := 1;
end;
在表格中你有这样的东西:
procedure TFormTest.FormCreate(Sender: TObject);
begin
FTransparentPanel := TTransparentPanel.Create(Self);
FTransparentPanel.Parent := self;
FTransparentPanel.Align := alClient;
FTransparentPanel.Visible := True;
FTransparentPanel.OnMouseDown := FormMouseDown;
FTransparentPanel.OnMouseUp := FormMouseUp;
FTransparentPanel.OnMouseMove := FormMouseMove;
end;
procedure TFormTest.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift:
TShiftState; X, Y: Integer);
var
I: Integer;
begin
// Check if there is a control under X, Y
FMoveControl := nil;
for I := 0 to ControlCount - 1 do
begin
if Controls[I] <> FTransparentPanel then
begin
if (Controls[I].Left <= X) and (Controls[I].Top <= Y) and
((Controls[I].Left + Controls[I].Width) >= X) and ((Controls[I].Top + Controls[I].Height) >= Y) then
begin
FMoveControl := Controls[I];
break;
end;
end;
end;
if Assigned(FMoveControl) then
begin
FStartLeft := FMoveControl.Left;
FStartTop := FMoveControl.Top;
FStartX := X;
FStartY := Y;
end;
end;
procedure TFormTest.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y:
Integer);
begin
// Move it
if Assigned(FMoveControl) then
begin
FMoveControl.Left := FStartLeft + (X - FStartX);
FMoveControl.Top := FStartTop + (Y - FStartY);
end;
end;
procedure TFormTest.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift:
TShiftState; X, Y: Integer);
begin
FMoveControl := nil;
end;
当您移动控件时,它会闪烁一些,但应该让您了解如何继续操作。