开发者

Creating/Switching Desktop in C#/.Net

开发者 https://www.devze.com 2023-02-20 14:03 出处:网络
I\'m currently using the CreateDesktop native C function and calling it in my C# code to create and switch between desktops.Is there any way to do this using the Process class or any c#/.Net class for

I'm currently using the CreateDesktop native C function and calling it in my C# code to create and switch between desktops. Is there any way to do this using the Process class or any c#/.Net class for that matter?

This is the sample code i'm using in my class now for Desktop switching.

    [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);


There is no built-in desktop switching class/methods in .net framework.

Here is Desktop Switching example which uses native windows API's.

If there is any .net framework class/method for desktop switching, they would use/wrap a same API's as your example or example from codeproject i mentioned.

Here is one more example with little different approach: Multiple desktop support in Windows

0

精彩评论

暂无评论...
验证码 换一张
取 消