如何获取当前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