抓取当前鼠标形状

I

import

Unregistered / Unconfirmed
GUEST, unregistred user!
这样抓取当前鼠标形状: 先定义一个cursor变量:
var GlobalCur:TIcon;
windowhld:hwnd;
threadld:dword;
begin
windowhld:=GetForegroundWindow;
threadld:=GetWindowThreadProcessId(Windowhld,nil);
AttachThreadInput(GetCurrentThreadId,threadld,true);
GlobalCur:=TIcon.Create;
GlobalCur.handle:=GetCursor;
AttachThreadInput(GetCurrentThreadId,threadld,false);
然后
bitmap.canvas.brush.Style:=bsclear;
bitmap.canvas.draw(x,y,GlobalCur); //x,y的值可以通过GetCursorPos得到
****************************
下面是两个演示:
{
The GetCursor() API is limited in that it does not, by default, return a handle to the current
cursor when that cursor is owned by another thread. This article demonstrates a way to retrieve
the current cursor regardless of what thread owns it.
For example, when you wish to include the image of the cursor in a screen capture.
}
function GetCursorHandle: HCURSOR;
var
hWindow : HWND;
pt : TPoint;
pIconInfo : TIconInfo;
dwThreadID, dwCurrentThreadID: DWORD;
begin
// Find out which window owns the cursor
GetCursorPos(pt);
hWindow := WindowFromPoint(pt);
// Get the thread ID for the cursor owner.
dwThreadID := GetWindowThreadProcessId(hWindow, nil);
// Get the thread ID for the current thread
dwCurrentThreadID := GetCurrentThreadId;
// If the cursor owner is not us then we must attach to
// the other thread in so that we can use GetCursor() to
// return the correct hCursor
if (dwCurrentThreadID <> dwThreadID) then
begin
if AttachThreadInput(dwCurrentThreadID, dwThreadID, True) then
begin
// Get the handle to the cursor
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;
// Retrieves information about the specified cursor.
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;
 
顶部