DELPHI label标签怎么在FORM里用鼠标拖着满街跑?就象桌面图标一样
2个回答
展开全部
楼主真是执着啊,开了好几个贴。这个问题一点都不难,不过要注意一点,如果每次拖动都要改变Label位置的话,Label的OnMouseMove事件会不断被触发,Label 就会像猴子一样在屏幕上跳来跳去。最好像Delphi一样,拖动的时候画个拖曳框,最后停止拖曳的时候再把Label的位置改变,这样效率较高。
代码如下:
type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
procedure Label1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Label1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure Label1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
FDrag: Boolean;
FDragDC: HDC;
FOrgPos: TPoint;
FPos: TPoint;
procedure DrawDragRect;
public
{ Public declarations }
end;
...
procedure TForm1.DrawDragRect;
var
dx, dy, x1, y1, x2, y2: Integer;
brOld: HBRUSH;
begin
dx := FPos.X - FOrgPos.X;
dy := FPos.Y - FOrgPos.Y;
x1 := Label1.Left + dx;
y1 := Label1.Top + dy;
x2 := x1 + Label1.Width;
y2 := y1 + Label1.Height;
brOld := SelectObject(FDragDC, GetStockObject(NULL_BRUSH));
Windows.Rectangle(FDragDC, x1, y1, x2, y2);
SelectObject(FDragDC, brOld);
end;
procedure TForm1.Label1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
GetCursorPos(FOrgPos);
Windows.ScreenToClient(Label1.Parent.Handle, FOrgPos);
FPos := FOrgPos;
FDragDC := GetDC(Label1.Parent.Handle);
SetROP2(FDragDC, R2_NOTXORPEN);
DrawDragRect;
FDrag := True;
end;
procedure TForm1.Label1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if FDrag then
begin
DrawDragRect;
GetCursorPos(FPos);
Windows.ScreenToClient(Label1.Parent.Handle, FPos);
DrawDragRect;
end;
end;
procedure TForm1.Label1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
FDrag := False;
DrawDragRect;
ReleaseDC(Label1.Parent.Handle, FDragDC);
Label1.Left := Label1.Left + FPos.X - FOrgPos.X;
Label1.Top := Label1.Top + FPos.Y - FOrgPos.Y;
end;
代码如下:
type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
procedure Label1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Label1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure Label1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
FDrag: Boolean;
FDragDC: HDC;
FOrgPos: TPoint;
FPos: TPoint;
procedure DrawDragRect;
public
{ Public declarations }
end;
...
procedure TForm1.DrawDragRect;
var
dx, dy, x1, y1, x2, y2: Integer;
brOld: HBRUSH;
begin
dx := FPos.X - FOrgPos.X;
dy := FPos.Y - FOrgPos.Y;
x1 := Label1.Left + dx;
y1 := Label1.Top + dy;
x2 := x1 + Label1.Width;
y2 := y1 + Label1.Height;
brOld := SelectObject(FDragDC, GetStockObject(NULL_BRUSH));
Windows.Rectangle(FDragDC, x1, y1, x2, y2);
SelectObject(FDragDC, brOld);
end;
procedure TForm1.Label1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
GetCursorPos(FOrgPos);
Windows.ScreenToClient(Label1.Parent.Handle, FOrgPos);
FPos := FOrgPos;
FDragDC := GetDC(Label1.Parent.Handle);
SetROP2(FDragDC, R2_NOTXORPEN);
DrawDragRect;
FDrag := True;
end;
procedure TForm1.Label1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if FDrag then
begin
DrawDragRect;
GetCursorPos(FPos);
Windows.ScreenToClient(Label1.Parent.Handle, FPos);
DrawDragRect;
end;
end;
procedure TForm1.Label1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
FDrag := False;
DrawDragRect;
ReleaseDC(Label1.Parent.Handle, FDragDC);
Label1.Left := Label1.Left + FPos.X - FOrgPos.X;
Label1.Top := Label1.Top + FPos.Y - FOrgPos.Y;
end;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询