Mega Code Archive

 
Categories / Delphi / Graphic
 

How to obtain a handle to the current Cursor

Title: How to obtain a handle to the current Cursor function GetCursorHandle: HCURSOR; var hWindow: HWND; pt: TPoint; pIconInfo: TIconInfo; dwThreadID, dwCurrentThreadID: DWORD; begin // Find out which window owns the cursor // Das zum Mauszeiger zugeh rige Fenster finden GetCursorPos(pt); hWindow := WindowFromPoint(pt); // Get the thread ID for the cursor owner. // Thread ID des Fensters ermitteln dwThreadID := GetWindowThreadProcessId(hWindow, nil); // Get the thread ID for the current thread // Thread ID f r den aktuellen Thread ermitteln 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 // Wenn der Mauszeiger zu einem anderen Thread geh rt, m ssen wir // an den anderen Thread anh ngen. if (dwCurrentThreadID dwThreadID) then begin if AttachThreadInput(dwCurrentThreadID, dwThreadID, True) then begin // Das Handle des Mauszeigers ermitteln // 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. // Informationen ber den Mauszeiger auslesen GetIconInfo(MyCursor.Handle, pIconInfo); CurPosX := pIconInfo.xHotspot; CurPoxY := pIconInfo.yHotspot; // Draw the Cursor on the form // Den Mauszeiger auf die Form zeichnen Canvas.Draw(CurPoxY, CurPoxY, MyCursor); finally MyCursor.ReleaseHandle; MyCursor.Free; end; end; // Another Solution: // Andere M glichkeit: procedure TForm1.Timer1Timer(Sender: TObject); var CI: TCursorInfo; begin CI.cbSize := SizeOf(CI); GetCursorInfo(CI); Image1.Picture.Icon.Handle := CI.hCursor; end;