鼠标悬停在表单上吗?

发布于 2024-10-07 20:28:44 字数 1110 浏览 1 评论 0原文

此代码不适用于 Delphi XE:

http://delphi.about。 com/cs/adptips2000/a/bltip0800_5.htm

procedure TForm1.FormDeactivate(Sender: TObject) ;
begin
   ReleaseCapture;
end;

procedure TForm1.FormMouseMove
(Sender: TObject; Shift: TShiftState; X,Y: Integer) ;
begin
  If GetCapture = 0 then
    SetCapture(Form1.Handle) ;
    if PtInRect(Rect(Form1.Left,
                    Form1.Top,
                    Form1.Left + Form1.Width,
                    Form1.Top + Form1.Height),
                    ClientToScreen(Point(x, y))) then
     Form1.Caption := 'Mouse is over form' else
     Form1.Caption := 'Mouse is outside of form';
end;

没有错误 - 它只是没有效果。

请帮忙。

编辑1

事实证明问题不在于代码,甚至鼠标 FormMouseEnter 和 FormMouseLeave 也不起作用,因为我将表单传递给我使用如下函数创建的单元:

procedure Slide(Form :TForm;显示:布尔值);

我在此过程中调用 Show 方法。我怎样才能克服这个问题?

谢谢。

编辑2

我不想使用我现在发布的功能。我想使用下面人们建议的内容(FormMouseEnter 和 FormMouseLeave),但它在我的情况下也不起作用。

This code is not working for me on Delphi XE:

http://delphi.about.com/cs/adptips2000/a/bltip0800_5.htm

procedure TForm1.FormDeactivate(Sender: TObject) ;
begin
   ReleaseCapture;
end;

procedure TForm1.FormMouseMove
(Sender: TObject; Shift: TShiftState; X,Y: Integer) ;
begin
  If GetCapture = 0 then
    SetCapture(Form1.Handle) ;
    if PtInRect(Rect(Form1.Left,
                    Form1.Top,
                    Form1.Left + Form1.Width,
                    Form1.Top + Form1.Height),
                    ClientToScreen(Point(x, y))) then
     Form1.Caption := 'Mouse is over form' else
     Form1.Caption := 'Mouse is outside of form';
end;

No errors - it just has no effect.

Please help.

EDIT 1

It turned out the problem is not with the code, even mouse FormMouseEnter and FormMouseLeave are not working because I'm passing the form to a Unit I created with a function like this:

procedure Slide(Form: TForm; Show: Boolean);

I'm calling the Show method from inside this procedure. How can I overcome this problem?

Thanks.

EDIT 2

I don't want to use the function I posted now. I want to use what the people suggested below (FormMouseEnter and FormMouseLeave) but it's not working in my case as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

失退 2024-10-14 20:28:44

您可以使用 OnMouseEnterOnMouseLeave 事件来跟踪鼠标是否位于窗体上,而无需捕获鼠标光标。

You could use OnMouseEnter and OnMouseLeave events to keep track of whether the mouse is over the form or not without capturing the mouse cursor.

英雄似剑 2024-10-14 20:28:44

只需在相应表单的 OnMouseEnter 和 OnMouseLeave 事件中输入必要的代码即可。就我而言,我所做的就是:

在 Delphi 中创建一个新项目
选择您正在使用的表单
转到对象检查器
如有必要,切换到对象检查器中的“事件”选项卡
向下滚动到 OnMouseEnter 事件,双击旁边的空白区域,这将生成一个 EventHandler。确保事件处理程序中包含以下代码:

procedure TForm1.FormMouseEnter(Sender: TObject);
begin
  Self.Caption := 'Mouse in form';
end;

再次转到对象检查器
找到 OnMouseLeave 事件,双击其右侧的白色区域以生成新的事件处理程序,并向其中添加以下代码

procedure TForm1.FormMouseLeave(Sender: TObject);
begin
  Self.Caption := 'Mouse outside form';
end;

运行应用程序...将鼠标移到表单上,标题将更改为“Mouse”在窗体内”,将其移到窗体外,标题将显示“鼠标在窗体外”

作为魅力(在 Delphi 2010 中测试)

This is just a matter of entering the necessary code in the OnMouseEnter and OnMouseLeave events of the corresponding form. In my case, all I did was :

Create a new project in Delphi
Select the form you are working with
Go to the Object Inspector
Switch to the Events tab in the object inspector if necessary
Scroll down to the OnMouseEnter event, double click in the white space next to it which will generate an EventHandler. Make sure you end up with the following code in the event handler :

procedure TForm1.FormMouseEnter(Sender: TObject);
begin
  Self.Caption := 'Mouse in form';
end;

Go to the Object Inspector again
Find the OnMouseLeave event and double click in the white area to the right of it to generate a new event handler, and add the following code to it

procedure TForm1.FormMouseLeave(Sender: TObject);
begin
  Self.Caption := 'Mouse outside form';
end;

Run the app ... move your mouse over the form and the caption will change to 'Mouse inside form', move it outside the form and the caption will say 'Mouse outside form'

Works as a charm (Tested in Delphi 2010)

倚栏听风 2024-10-14 20:28:44

据我了解,为此使用 SetCapture 是一个坏主意。如果它的功能如您所愿,那么您将窃取其他所有人的鼠标消息,只是因为您不知道更好的跟踪鼠标的方法。

但 MSDN 说(http://msdn.microsoft.com/en-us/library/ms646262(VS.85).aspx)即使使用 SetCapture,来自外部的鼠标消息也不会被重定向到您的窗口,除非按下鼠标按钮(可能是一种措施来阻止您想要实现的目标:无正当理由窃取鼠标)。

你从哪里调用 Show() 并不重要,所以你的问题不在于此。

As far as I understand, using SetCapture for this is a bad idea. If it functioned like you wish, you would be robbing everyone else of the mouse messages just because you don't know a better way to track mouse.

But MSDN says (http://msdn.microsoft.com/en-us/library/ms646262(VS.85).aspx) that even with SetCapture, mouse messages from outside would not be redirected to your window unless mouse button is down (probably a measure to prevent exactly what are you trying to achieve: stealing mouse without a valid reason).

It doesn't matter where are you calling Show() from, so your problem is not in that.

浅笑依然 2024-10-14 20:28:44

我需要一个带有经过大量修改的标题的表单(frmTasks)。所以我创建了一个带有隐藏标题的表单。我用 TImage (imgRedLogo) 模拟标题,在上面绘制我需要的东西。
此代码允许用户单击假标题(图像)并移动表单。无需捕获鼠标即可像魅力一样工作。它甚至可以使用鼠标右键(如果您想禁用此“功能”,则必须测试 imgRedLogoMouseDown 中的“Button”参数)。

完整工作代码:

VAR
  Dragged  : Boolean= FALSE;
  IsOverImg: Boolean= FALSE;  { True if mouse if over the image }
  OldPos   : TPoint;

procedure TfrmTasks.imgRedLogoMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
 Dragged:= True;
 GetCursorPos(OldPos);
end;

procedure TfrmTasks.imgRedLogoMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
VAR NewPos: TPoint;
begin
 if Dragged AND IsOverImg then
  begin
    GetCursorPos(NewPos);
    frmTasks.Left:= frmTasks.Left- OldPos.X + NewPos.X;
    frmTasks.Top := frmTasks.Top - OldPos.Y + NewPos.Y;
    OldPos:= NewPos;
  end;
end;

procedure TfrmTasks.imgRedLogoMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
 if Dragged
 then Dragged:= False;
end;

procedure TfrmTasks.imgRedLogoMouseEnter(Sender: TObject);
begin
 IsOverImg:= TRUE;
end;

procedure TfrmTasks.imgRedLogoMouseLeave(Sender: TObject);
begin
 IsOverImg:= FALSE;
end;

享受吧。

I needed a form (frmTasks) with a heavily modified Caption. So I created a form with a hidden Caption. I simulate the Caption with a TImage (imgRedLogo) on which I draw the stuff I need.
This code lets the user to click the fake caption (the image) and move the form around. Works like a charm WITHOUT capturing the mouse. It works even with the right mouse button (you have to test the 'Button' parameter in imgRedLogoMouseDown if you want to disable this 'feature').

FULL WORKING CODE:

VAR
  Dragged  : Boolean= FALSE;
  IsOverImg: Boolean= FALSE;  { True if mouse if over the image }
  OldPos   : TPoint;

procedure TfrmTasks.imgRedLogoMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
 Dragged:= True;
 GetCursorPos(OldPos);
end;

procedure TfrmTasks.imgRedLogoMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
VAR NewPos: TPoint;
begin
 if Dragged AND IsOverImg then
  begin
    GetCursorPos(NewPos);
    frmTasks.Left:= frmTasks.Left- OldPos.X + NewPos.X;
    frmTasks.Top := frmTasks.Top - OldPos.Y + NewPos.Y;
    OldPos:= NewPos;
  end;
end;

procedure TfrmTasks.imgRedLogoMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
 if Dragged
 then Dragged:= False;
end;

procedure TfrmTasks.imgRedLogoMouseEnter(Sender: TObject);
begin
 IsOverImg:= TRUE;
end;

procedure TfrmTasks.imgRedLogoMouseLeave(Sender: TObject);
begin
 IsOverImg:= FALSE;
end;

Enjoy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文