2012-04-24 42 views
0

我知道如何从xxx.cur文件设置系统光标,但我想知道如何将当前系统光标保存到文件。如何获取当前系统光标并将其保存到文件xxx.cur?

在WPF中,设置系统光标可以使用下面的代码:

[DllImport("user32.dll")] 
internal static extern IntPtr LoadCursorFromFile(string lpFileName); 
[DllImport("user32.dll")] 
internal static extern bool SetSystemCursor(IntPtr hcur, uint id); 
internal const uint OCR_NORMAL = 32512; 
IntPtr hAni = Win32Api.LoadCursorFromFile("file.cur"); 
bool b = SetSystemCursor(hAni, Win32Api.OCR_NORMAL); 

但我不知道如何将当前系统光标保存到磁盘。

有谁能告诉我,谢谢。

+0

我想知道名单你为什么需要?光标是用户首选项,而不是用户未经许可擅自更改的内容。 – 2012-04-24 14:23:07

+0

我会在我的应用程序中添加选项以供用户切换他们喜欢的任何光标。所以我需要知道如何使用WPF中的c#切换回系统默认光标。 – 2012-04-25 02:46:49

+0

我得到的是:你需要设置系统范围还是只为当前的应用程序?如果仅适用于当前应用程序,则会更容易。 – 2012-04-25 12:37:11

回答

0

这可能是一个有点难看,我张贴的代码是没有考虑非常漂亮,(它的粗糙来形容的解决方案),但你可以从注册表拉动当前光标再出再次保存他们回来。

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; 
       } 
      } 
     } 
    } 
相关问题