在 C#/.Net 中创建/切换桌面

发布于 2024-10-28 04:37:59 字数 2086 浏览 1 评论 0原文

我当前正在使用 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);

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的影子我的梦 2024-11-04 04:37:59

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

这是使用本机 Windows API 的桌面切换示例。

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

这是另一个方法略有不同的示例: Windows 中的多桌面支持

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文