2011-03-30 76 views
2

我目前使用CreateDesktop原生C函數,並在我的C#代碼中調用它來在桌面之間創建和切換。有沒有辦法使用Process類或任何c#/ .Net類來做到這一點?在C#中創建/切換桌面。Net

這是我現在用於桌面切換的示例代碼。

[Flags] 
    public enum AccessRight : uint 
    { 
     DESKTOP_READOBJECTS = 0x00000001, 
     DESKTOP_CREATEWINDOW = 0x00000002, 
     DESKTOP_CREATEMENU = 0x00000004, 
     DESKTOP_HOOKCONTROL = 0x00000008, 
     DESKTOP_JOURNALRECORD = 0x00000010, 
     DESKTOP_JOURNALPLAYBACK = 0x00000020, 
     DESKTOP_ENUMERATE = 0x00000040, 
     DESKTOP_WRITEOBJECTS = 0x00000080, 
     DESKTOP_SWITCHDESKTOP = 0x00000100, 

     GENERIC_ALL = (DESKTOP_READOBJECTS | DESKTOP_CREATEWINDOW | DESKTOP_CREATEMENU | 
      DESKTOP_HOOKCONTROL | DESKTOP_JOURNALRECORD | DESKTOP_JOURNALPLAYBACK | 
      DESKTOP_ENUMERATE | DESKTOP_WRITEOBJECTS | DESKTOP_SWITCHDESKTOP) 
    }; 

    [Flags] 
    public enum AccountHook 
    { 
     Allow = 1, 
     Disallow = 0 
    }; 

    public enum HandleInheritance 
    { 
     Inherit, 
     None 
    }; 

    [StructLayout(LayoutKind.Sequential)] 
    public struct SecAttrib 
    { 
     public int nLength; 
     public IntPtr lpSecurityDescriptor; 
     public int bInheritHandle; 
    } 

    [DllImport("user32.dll")] 
    public static extern IntPtr OpenDesktop(string lpszDesktop, 
     uint dwFlags, 
     bool fInherit, 
     uint dwDesiredAccess); 

    [DllImport("user32.dll")] 
    public static extern bool SwitchDesktop(IntPtr hDesktop); 

    [DllImport("user32.dll")] 
    public static extern IntPtr CreateDesktop(string lpszDesktop, 
     IntPtr lpszDevice, 
     IntPtr pDevmode, 
     int dwFlags, 
     uint dwDesiredAccess, 
     IntPtr lpsa); 

    [DllImport("user32.dll", SetLastError = true)] 
    public static extern IntPtr OpenInputDesktop(uint dwFlags, bool fInherit, 
     uint dwDesiredAccess); 



    [DllImport("user32.dll", EntryPoint = "CloseDesktop", CharSet = CharSet.Unicode, SetLastError = true)] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    public static extern bool CloseDesktop(IntPtr handle); 
+1

挑剔:本地Windows API(其中CreateDesktop是的一部分)是C API,而不是一個C++一個。 – 2011-03-30 10:13:01

回答

2

在.net框架中沒有內置桌面切換類/方法。

這裏是Desktop Switching使用本地windows API的例子。

如果有任何用於桌面切換的.net框架類/方法,它們將使用/包裝一個相同的API作爲您的示例或來自我提到的codeproject的示例。

下面是一個例子很少不同的方法:Multiple desktop support in Windows