你是完全不会还是遇到什么问题了?
一般的做法是用SetCapture获得鼠标焦点,然后记录鼠标的位置,在OnMouseMove事件里计算鼠标的位移,然后在移动控件。我猜测你的问题可能是Label没有Handle,无法传递参数给SetCapture;建议你改用TStaticText,功能和Label一样,但是一个独立的窗体控件。如果非要用Label,你应该在Label的OnMouseDown事件里设置一个标志,在它的MouseMove或者父窗体的MouseMove里改变位置。
一般的做法是用SetCapture获得鼠标焦点,然后记录鼠标的位置,在OnMouseMove事件里计算鼠标的位移,然后在移动控件。我猜测你的问题可能是Label没有Handle,无法传递参数给SetCapture;建议你改用TStaticText,功能和Label一样,但是一个独立的窗体控件。如果非要用Label,你应该在Label的OnMouseDown事件里设置一个标志,在它的MouseMove或者父窗体的MouseMove里改变位置。
加一个全局变量POINT OldPoint;void __fastcall TForm1::Label1MouseMove(TObject *Sender, TShiftState Shift,
int X, int Y)
{
POINT pt;
GetCursorPos(&pt); if(Shift.Contains(ssLeft))
{
Label1->Left += pt.x - OldPoint.x;
Label1->Top += pt.y - OldPoint.y; OldPoint = pt;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Label1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
GetCursorPos(&OldPoint);
}
//---------------------------------------------------------------------------
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
Label1: TLabel;
procedure Label1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Label1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
private
xx: TPoint;
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Label1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
GetCursorPos(xx);
end;procedure TForm1.Label1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
yy: TPoint;
begin
GetCursorPos(yy); if ssLeft in Shift then
begin
Label1.Left := Label1.Left + yy.x - xx.x;
Label1.Top := Label1.Top + yy.y - xx.y;
xx := yy;
end;
end;end.
所以,先给大伙2分吧,期望有好的回答。
Shift: TShiftState; X, Y: Integer);
begin
if Button=mbleft then
begin
ReleaseCapture;
Label.perfrom(WM_SYSCOMMAND,$F012,0);
END;
end;
Label1.perform(WM_SysCommand, $F012, 0);