光标跟随
Posted swtool
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了光标跟随相关的知识,希望对你有一定的参考价值。
#region 得到光标在屏幕上的位置
[DllImport(
"user32"
)]
public
static
extern
bool
GetCaretPos(
out
Point lpPoint);
[DllImport(
"user32.dll"
)]
private
static
extern
IntPtr GetForegroundWindow();
[DllImport(
"user32.dll"
)]
private
static
extern
IntPtr GetFocus();
[DllImport(
"user32.dll"
)]
private
static
extern
IntPtr AttachThreadInput(IntPtr idAttach, IntPtr idAttachTo,
int
fAttach);
[DllImport(
"user32.dll"
)]
private
static
extern
IntPtr GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
[DllImport(
"kernel32.dll"
)]
private
static
extern
IntPtr GetCurrentThreadId();
[DllImport(
"user32.dll"
)]
private
static
extern
void
ClientToScreen(IntPtr hWnd,
ref
Point p);
private
Point CaretPos()
{
IntPtr ptr = GetForegroundWindow();
Point p =
new
Point();
//得到Caret在屏幕上的位置
if
(ptr.ToInt32() != 0)
{
IntPtr targetThreadID = GetWindowThreadProcessId(ptr, IntPtr.Zero);
IntPtr localThreadID = GetCurrentThreadId();
if
(localThreadID != targetThreadID)
{
AttachThreadInput(localThreadID, targetThreadID, 1);
ptr = GetFocus();
if
(ptr.ToInt32() != 0)
{
GetCaretPos(
out
p);
ClientToScreen(ptr,
ref
p);
}
AttachThreadInput(localThreadID, targetThreadID, 0);
}
}
return
p;
}
#endregion
以上是关于光标跟随的主要内容,如果未能解决你的问题,请参考以下文章