When the cursor is controlled by another thread, you cannot use the GetCursor() application interface to obtain the cursor handle. This article explains how to obtain a cursor handle when any thread has control of the cursor.
================================================== =======
{
When the cursor is controlled by another thread, you cannot use the GetCursor() application interface to obtain the cursor handle.
This article explains how to obtain a cursor handle when any thread has control of the cursor.
For example, what to do when you want to place the cursor in a screen capture program.
}
function GetCursorHandle: HCURSOR;
var
hWindow: HWND;
pt: TPoint;
pIconInfo: TIconInfo;
dwThreadID, dwCurrentThreadID: DWord;
begin
// Check which form has the cursor
GetCursorPos(pt);
hWindow := WindowFromPoint(pt);
// Get the thread ID of the cursor owner
dwThreadID := GetWindowThreadPRocessId(hWindow, nil);
// Get the ID of the current thread
dwCurrentThreadID := GetCurrentThreadId;
// If the thread of the cursor owner is not the current thread, the thread of the cursor owner must be assigned to the current thread.
//Then call GetCursor() to get the correct cursor handle (hCursor).
if (dwCurrentThreadID <> dwThreadID) then
begin
if AttachThreadInput(dwCurrentThreadID, dwThreadID, True) then
begin
//Get cursor handle
Result := GetCursor;
AttachThreadInput(dwCurrentThreadID, dwThreadID, False);
end;
end else
begin
Result := GetCursor;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
CurPosX, CurPoxY: Integer;
MyCursor: TIcon;
pIconInfo: TIconInfo;
begin
MyCursor := TIcon.Create;
try
MyCursor.Handle := GetCursorHandle;
// Get cursor position
GetIconInfo(MyCursor.Handle, pIconInfo);
CurPosX := pIconInfo.xHotspot;
CurPoxY := pIconInfo.yHotspot;
//Draw the cursor on the form
Canvas.Draw(CurPoxY, CurPoxY, MyCursor);
finally
When the cursor is controlled by another thread, you cannot use the GetCursor() application interface to obtain the cursor handle. This article explains how to obtain a cursor handle when any thread has control of the cursor.
================================================== =======
{
When the cursor is controlled by another thread, you cannot use the GetCursor() application interface to obtain the cursor handle.
This article explains how to obtain a cursor handle when any thread has control of the cursor.
For example, what to do when you want to place the cursor in a screen capture program.
}
function GetCursorHandle: HCURSOR;
var
hWindow: HWND;
pt: TPoint;
pIconInfo: TIconInfo;
dwThreadID, dwCurrentThreadID: DWORD;
begin
// Check which form has the cursor
GetCursorPos(pt);
hWindow := WindowFromPoint(pt);
// Get the thread ID of the cursor owner
dwThreadID := GetWindowThreadProcessId(hWindow, nil);
// Get the ID of the current thread
dwCurrentThreadID := GetCurrentThreadId;
// If the cursor owner's thread is not the current thread, the cursor owner's thread must be assigned to the current thread.
//Then call GetCursor() to get the correct cursor handle (hCursor).
if (dwCurrentThreadID <> dwThreadID) then
begin
if AttachThreadInput(dwCurrentThreadID, dwThreadID, True) then
begin
//Get cursor handle
Result := GetCursor;
AttachThreadInput(dwCurrentThreadID, dwThreadID, False);
end;
end else
begin
Result := GetCursor;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
CurPosX, CurPoxY: Integer;
MyCursor: TIcon;
pIconInfo: TIconInfo;
begin
MyCursor := TIcon.Create;
try
MyCursor.Handle := GetCursorHandle;
// Get cursor position
GetIconInfo(MyCursor.Handle, pIconInfo);
CurPosX := pIconInfo.xHotspot;
CurPoxY := pIconInfo.yHotspot;
//Draw the cursor on the form
Canvas.Draw(CurPoxY, CurPoxY, MyCursor);
finally
MyCursor.ReleaseHandle;
MyCursor.Free;
end;
end;
//Another solution:
procedure TForm1.Timer1Timer(Sender: TObject);
var
CI:TCursorInfo;
begin
CI.cbSize := SizeOf(CI);
GetCursorInfo(CI);
Image1.Picture.Icon.Handle := CI.hCursor;
end;
MyCursor.ReleaseHandle;
MyCursor.Free;
end;
end;
//Another solution:
procedure TForm1.Timer1Timer(Sender: TObject);
var
CI:TCursorInfo;
begin
CI.cbSize := SizeOf(CI);
GetCursorInfo(CI);
Image1.Picture.Icon.Handle := CI.hCursor;
end;