Delphi中如何定位光标所在的具体控件?
- 内容介绍
- 文章标签
- 相关推荐
本文共计480个文字,预计阅读时间需要2分钟。
为了在控件上获取光标位置,您可以使用以下步骤:
1. 确定当前光标位置。
2.找到光标所在的控件。
3.使用 `Windows.SetFocus` 方法将焦点设置到该控件。
具体操作如下:
- 使用 `GetCursorPos` 函数获取当前光标位置。
- 使用 `WindowFromPoint` 函数找到光标所在的窗口。- 如果是您的应用程序中的控件,您可能需要递归检查或使用特定的方法来确定确切的控制。示例代码(假设使用的是C#和WinForms):
csharpusing System;using System.Drawing;using System.Runtime.InteropServices;using System.Windows.Forms;
public class CursorPositionExample{ [DllImport(user32.dll)] private static extern bool GetCursorPos(out Point lpPoint);
[DllImport(user32.dll)] private static extern IntPtr WindowFromPoint(Point lpPoint);
[DllImport(user32.dll)] private static extern IntPtr SetFocus(IntPtr hWnd);
public static void Main() { Point cursorPos; GetCursorPos(out cursorPos);
IntPtr controlHandle=WindowFromPoint(cursorPos); if (controlHandle !=IntPtr.Zero) { // 获取控件的具体信息,例如名称或类型 // ...
// 将焦点设置到该控件 SetFocus(controlHandle); } }}
这段代码首先获取当前光标位置,然后尝试找到光标所在的窗口句柄。如果找到,它将尝试将焦点设置到该控件。请注意,这个示例仅适用于WinForms应用程序,并且可能需要根据您的具体应用程序进行适当的调整。
我需要问题 “How to get cursor position on a control?”问的相反信息.给定当前光标位置,我如何找到表单(在我的应用程序中)和光标当前的控件?我需要它的句柄,以便我可以使用Windows.SetFocus(句柄).
作为参考,我正在使用Delphi 2009.
我认为FindVCLWindow将满足您的需求.在光标下有窗口控件后,您可以遍历父链以查找窗口所在的窗体.本文共计480个文字,预计阅读时间需要2分钟。
为了在控件上获取光标位置,您可以使用以下步骤:
1. 确定当前光标位置。
2.找到光标所在的控件。
3.使用 `Windows.SetFocus` 方法将焦点设置到该控件。
具体操作如下:
- 使用 `GetCursorPos` 函数获取当前光标位置。
- 使用 `WindowFromPoint` 函数找到光标所在的窗口。- 如果是您的应用程序中的控件,您可能需要递归检查或使用特定的方法来确定确切的控制。示例代码(假设使用的是C#和WinForms):
csharpusing System;using System.Drawing;using System.Runtime.InteropServices;using System.Windows.Forms;
public class CursorPositionExample{ [DllImport(user32.dll)] private static extern bool GetCursorPos(out Point lpPoint);
[DllImport(user32.dll)] private static extern IntPtr WindowFromPoint(Point lpPoint);
[DllImport(user32.dll)] private static extern IntPtr SetFocus(IntPtr hWnd);
public static void Main() { Point cursorPos; GetCursorPos(out cursorPos);
IntPtr controlHandle=WindowFromPoint(cursorPos); if (controlHandle !=IntPtr.Zero) { // 获取控件的具体信息,例如名称或类型 // ...
// 将焦点设置到该控件 SetFocus(controlHandle); } }}
这段代码首先获取当前光标位置,然后尝试找到光标所在的窗口句柄。如果找到,它将尝试将焦点设置到该控件。请注意,这个示例仅适用于WinForms应用程序,并且可能需要根据您的具体应用程序进行适当的调整。
我需要问题 “How to get cursor position on a control?”问的相反信息.给定当前光标位置,我如何找到表单(在我的应用程序中)和光标当前的控件?我需要它的句柄,以便我可以使用Windows.SetFocus(句柄).
作为参考,我正在使用Delphi 2009.
我认为FindVCLWindow将满足您的需求.在光标下有窗口控件后,您可以遍历父链以查找窗口所在的窗体.
