如何獲取當前GLOBAL鼠標光標類型(沙漏/箭頭/ ..)?在Windows中。獲取當前鼠標光標類型
全局 - 我需要它即使鼠標在我的應用程序的外部或即使我的程序是無風的。
在C#,Delphi或純WINAPI,請不要介意...
非常感謝你提前!
如何獲取當前GLOBAL鼠標光標類型(沙漏/箭頭/ ..)?在Windows中。獲取當前鼠標光標類型
全局 - 我需要它即使鼠標在我的應用程序的外部或即使我的程序是無風的。
在C#,Delphi或純WINAPI,請不要介意...
非常感謝你提前!
經過多年的時間來回答我自己的問題。這裏是你如何檢查,如果當前的全球光標沙漏在C#(爲你自己的需求,如果你需要擴展的代碼):
private static bool IsWaitCursor()
{
var h = Cursors.WaitCursor.Handle;
CURSORINFO pci;
pci.cbSize = Marshal.SizeOf(typeof(CURSORINFO));
GetCursorInfo(out pci);
return pci.hCursor == h;
}
[StructLayout(LayoutKind.Sequential)]
struct POINT
{
public Int32 x;
public Int32 y;
}
[StructLayout(LayoutKind.Sequential)]
struct CURSORINFO
{
public Int32 cbSize; // Specifies the size, in bytes, of the structure.
// The caller must set this to Marshal.SizeOf(typeof(CURSORINFO)).
public Int32 flags; // Specifies the cursor state. This parameter can be one of the following values:
// 0 The cursor is hidden.
// CURSOR_SHOWING The cursor is showing.
public IntPtr hCursor; // Handle to the cursor.
public POINT ptScreenPos; // A POINT structure that receives the screen coordinates of the cursor.
}
[DllImport("user32.dll")]
static extern bool GetCursorInfo(out CURSORINFO pci);
使用(在Delphi)
Screen.MouseCursor.
對於當前的鼠標光標。
一般的Win32(USER32)給出:
function GetCursor: HCURSOR; stdcall;
這應該可用於其他Win32語言。
這是不對的。正如我在另一個問題中學到的,GetCursor不再適用於其他程序的遊標:http:// stackoverflow。com/questions/358527/how-to-tell-if-mouse-pointer-icon-change – 2009-01-19 23:58:48
編輯:在Delphi
在大多數可視對象可以使用光標財產,否則使用Screen.Cursor屬性格式。 將其重置爲crDefault取消您對之前設置的更改。
這並不能讓你在程序外面顯示光標。 – 2009-01-20 00:00:29
OEM遊標是共享資源,所以請求特定遊標的所有進程都將檢索相同的句柄。應用程序可以在啓動時緩存標準系統光標句柄,然後它可以使用GetCursorInfo獲取全局光標句柄,並在緩存中查找該句柄以檢索其類別 - 如果它是系統光標的話。
下面的Delphi示例代碼演示。光標手柄通過在表單創建時使用LoadImage填充到數組中。計時器輪詢全球光標通過定期使用GetCursorInfo,代碼看起來式數組中的句柄從名稱的常量數組檢索遊標的名稱:
const
HighCursor = 13;
type
TForm1 = class(TForm)
Timer1: TTimer;
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
FCursorHandles: array [0..HighCursor] of HCURSOR;
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
const
OEMCursors: array [0..HighCursor] of Integer = (OCR_NORMAL, OCR_IBEAM,
OCR_WAIT, OCR_CROSS, OCR_UP, OCR_SIZENWSE, OCR_SIZENESW, OCR_SIZEWE,
OCR_SIZENS, OCR_SIZEALL, OCR_NO, OCR_HAND, OCR_APPSTARTING,
32651 {OCR_HELP?});
CursorNames: array [0..HighCursor] of string = ('OCR_NORMAL', 'OCR_IBEAM',
'OCR_WAIT', 'OCR_CROSS', 'OCR_UP', 'OCR_SIZENWSE', 'OCR_SIZENESW',
'OCR_SIZEWE', 'OCR_SIZENS', 'OCR_SIZEALL', 'OCR_NO', 'OCR_HAND',
'OCR_APPSTARTING', 'OCR_HELP');
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
begin
for i := 0 to HighCursor do
FCursorHandles[i] := LoadImage(0, MakeIntResource(OEMCursors[i]),
IMAGE_CURSOR, 0, 0, LR_DEFAULTCOLOR or LR_DEFAULTSIZE or LR_SHARED);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
function GetCursorName(Cursor: HCURSOR): string;
var
i: Integer;
begin
for i := 0 to HighCursor do
if Cursor = FCursorHandles[i] then begin
Result := CursorNames[i];
Exit;
end;
Result := 'Unknown Cursor'; // A custom cursor.
end;
var
CursorInfo: TCursorInfo;
begin
CursorInfo.cbSize := SizeOf(CursorInfo);
if GetCursorInfo(CursorInfo) then
Label1.Caption := GetCursorName(CursorInfo.hCursor)
else
Label1.Caption := 'Fail: ' + SysErrorMessage(GetLastError);
end;
注意,使用Delphi的一個時,不必須緩存遊標句柄,因爲Delphi通過它的Screen.Cursors列表來完成它。示例代碼沒有使用它來具有更好的可移植性。
還要注意,'winuser.h'中沒有'OCR_HELP',但提供的對應於'IDC_HELP'的常量似乎工作正常(儘管我無法在W7中找到一個使用「Help選擇「光標)。
看起來像它不可能:( – Alex 2010-08-19 18:14:53
TNX 3年後回答你的問題 - 真的幫我:) – barakcaf 2015-04-05 14:59:15