使用C#/ .Net创建/切换桌面

时间:2011-03-30 10:10:20

标签: c# dll import pinvoke desktop

我目前正在使用CreateDesktop本机C函数并在我的C#代码中调用它来在桌面之间创建和切换。有没有办法使用Process类或任何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 个答案:

答案 0 :(得分:2)

.net框架中没有内置的桌面切换类/方法。

以下是使用原生Windows API的Desktop Switching示例。

如果有任何用于桌面切换的.net框架类/方法,他们将使用/包装与我提到的codeproject中的示例或示例相同的API。

以下是另一个采用不同方法的示例:Multiple desktop support in Windows