如何通过按住窗口内的控件移动窗口位置(100分)

  • 主题发起人 主题发起人 ForDelphi
  • 开始时间 开始时间
F

ForDelphi

Unregistered / Unconfirmed
GUEST, unregistred user!
要求按住Label控件并拖动实现与按住窗口顶部一样的移动功能。
 
下面代码可以

procedure TMainForm.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Button<>mbRight then
begin
ReleaseCapture;
MainForm.Perform(WM_SysCommand,$f017,0);
end;
end;
 
方法一:
procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
releaseCapture;
Form1.Perform(WM_SysCommand,$F017,0);
end;

procedure TForm1.Button1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
releaseCapture;
Form1.Perform(WM_SysCommand,$F017,0);
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
releaseCapture;
Form1.Perform(WM_SysCommand,$F017,0);
end;

方法二:
Private
procedure WMNCHitTest(Var Msg:TWMNCHitTest);Message WM_NCHITTEST;


Procedure TForm1.WMNCHitTest(Var Msg:TWMNCHitTest);
Begin
inherited;
if Msg.Result=htClient then
Msg.Result:=htCaption;
End;

方法三:
procedure tForm1.formmousedown(sender: tobject; button: tmousebutton;
shift: tshiftstate; x, y: integer);
const
sc_dragmove = $f012;
begin
releasecapture;
twincontrol(application.mainform).perform(wm_syscommand,sc_dragmove, 0);
end;
 
在对Label控件操作时,发送窗口移动消息
 
只用指定Label的OnMouseDown事件中写上面的代码即可。
 
public
{ Public declarations }
procedure WMNCHitTest(var Message: TWMNCHitTest); message WM_NCHITTEST;
end;

var
MainForm: TMainForm;

implementation

{$R *.DFM}

procedure TForm1.WMNCHitTest(var Message: TWMNCHitTest);
begin
inherited;
Message.Result := HTCAPTION;
end;
 
多人接受答案了。
 
后退
顶部