2012-04-24 14 views
0

私はxxx.curファイルからシステムカーソルを設定する方法を知っていますが、現在のシステムカーソルをファイルに保存する方法を知りたいと思います。 WPFでは現在のシステムカーソルを取得してxxx.curファイルに保存する方法は?

、設定されたシステムカーソルが次のコードを使用することができます:

[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

のためのリストを作成しますなぜあなたはする必要がありますか?カーソルはユーザーの好みであり、ユーザーが無断で変更するものではありません。 –

+0

私は自分のアプリケーションにオプションを追加して、好きなカーソルを切り替えることができます。ですから、WPFでc#を使ってシステムのデフォルトカーソルに戻す方法を知る必要があります。 –

+0

私は何を得ているのですか:それをシステム全体に設定するのか、現在のアプリだけに設定する必要がありますか?現在のアプリだけの方がはるかに簡単です。 –

答えて

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; 
       } 
      } 
     } 
    } 
関連する問題