這可能是一個有點難看,我張貼的代碼是沒有考慮非常漂亮,(它的粗糙來形容的解決方案),但你可以從註冊表拉動當前光標再出再次保存他們回來。
public class UserCursors
{
[Serializable]
internal enum ImageType
{
Bitmap = 0,
Icon = 1,
Cursor = 2,
EnhMetafile = 3,
}
[Serializable, Flags]
internal enum LoadImageFlags
{
DefaultColor = 0x0,
Monochrome = 0x1,
Color = 0x2,
CopyReturnOriginal = 0x4,
CopyDeleteOriginal = 0x8,
LoadFromFile = 0x10,
LoadTransparent = 0x20,
DefaultSize = 0x40,
VgaColor = 0x80,
LoadMap3DColors = 0x1000,
CreateDibSection = 0x2000,
CopyFromResource = 0x4000,
Shared = 0x8000,
}
[DllImport("user32.dll")]
static extern IntPtr LoadImage(IntPtr hinst, String lpszName, ImageType uType, Int32 cxDesired, Int32 cyDesired, LoadImageFlags fuLoad);
public IntPtr hInst = IntPtr.Zero;
public String lpszName;
public Int32 width = 0;
public Int32 height = 0;
public string regKeyName = String.Empty;
public bool Changed = false;
public UserCursors()
{
}
public UserCursors(string cursorLocation, string keyName)
{
hInst = LoadImage(IntPtr.Zero, cursorLocation, ImageType.Cursor, width, height, LoadImageFlags.LoadFromFile);
lpszName = cursorLocation;
regKeyName = keyName;
}
}
然後創建例如列表
public List<UserCursors> systemCursors;
最多可以裝入
[DllImport("user32.dll", SetLastError = true)]
static extern bool SetSystemCursor(IntPtr hcur, uint id);
const uint OCR_NORMAL = 32512;
const uint OCR_HAND = 32649;
const uint OCR_IBEAM = 32513;
IntPtr hArrow = LoadImage(IntPtr.Zero, "<my custom cursor file>", ImageType.Cursor, width, height, LoadImageFlags.LoadFromFile);
IntPtr hHand = LoadImage(IntPtr.Zero, "<my custom cursor file>", ImageType.Cursor, width, height, LoadImageFlags.LoadFromFile);
IntPtr hBeam = LoadImage(IntPtr.Zero, "<my custom cursor file>", ImageType.Cursor, width, height, LoadImageFlags.LoadFromFile);
RegistryKey myCursors = Registry.CurrentUser.OpenSubKey(defaultCursors);
string[] keyCursors = myCursors.GetValueNames();
bool beamFound = false;
int lastError = 0;
foreach (string cursorKey in keyCursors)
{
RegistryValueKind rvk = myCursors.GetValueKind(cursorKey);
switch (rvk)
{
case RegistryValueKind.ExpandString:
string cursorValue = myCursors.GetValue(cursorKey) as string;
if (!String.IsNullOrEmpty(cursorValue))
{
UserCursors currentSystemCursor = new UserCursors(cursorValue, cursorKey);
switch (cursorKey)
{
case "Arrow":
currentSystemCursor.Changed = SetSystemCursor(hArrow, OCR_NORMAL);
break;
case "Hand":
currentSystemCursor.Changed = SetSystemCursor(hHand, OCR_HAND);
if (!currentSystemCursor.Changed)
{
lastError = Marshal.GetLastWin32Error();
Win32Exception ex = new Win32Exception(lastError);
}
break;
case "IBeam":
beamFound = true;
currentSystemCursor.Changed = SetSystemCursor(hBeam, OCR_IBEAM);
break;
default:
break;
}
systemCursors.Add(currentSystemCursor);
}
break;
default:
break;
}
}
// if a user hasn't customised the IBeam then it doesn't appear in the registry so we still change it
// and then clear the value to remove it.
if (!beamFound)
{
UserCursors currentSystemCursor = new UserCursors("C:\\Windows\\Cursors\\beam_i.cur", "IBeam");
currentSystemCursor.Changed = SetSystemCursor(hBeam, OCR_IBEAM);
systemCursors.Add(currentSystemCursor);
}
,然後在析構函數或完成或類似
~MainWindow()
{
int lastError = 0;
bool changed = false;
foreach (UserCursors savedCursor in systemCursors)
{
if (savedCursor.Changed)
{
switch (savedCursor.regKeyName)
{
case "Arrow":
changed = SetSystemCursor(savedCursor.hInst, OCR_NORMAL);
if (!changed)
{
lastError = Marshal.GetLastWin32Error();
Win32Exception ex = new Win32Exception(lastError);
}
break;
case "Hand":
changed = SetSystemCursor(savedCursor.hInst, OCR_HAND);
if (!changed)
{
lastError = Marshal.GetLastWin32Error();
Win32Exception ex = new Win32Exception(lastError);
}
break;
case "IBeam":
changed = SetSystemCursor(savedCursor.hInst, OCR_IBEAM);
if (!changed)
{
lastError = Marshal.GetLastWin32Error();
Win32Exception ex = new Win32Exception(lastError);
}
break;
default:
break;
}
}
}
}
我想知道名單你爲什麼需要?光標是用戶首選項,而不是用戶未經許可擅自更改的內容。 – 2012-04-24 14:23:07
我會在我的應用程序中添加選項以供用戶切換他們喜歡的任何光標。所以我需要知道如何使用WPF中的c#切換回系統默認光標。 – 2012-04-25 02:46:49
我得到的是:你需要設置系統範圍還是隻爲當前的應用程序?如果僅適用於當前應用程序,則會更容易。 – 2012-04-25 12:37:11