【问题标题】:Creating/Switching Desktop in C#/.Net在 C#/.Net 中创建/切换桌面
【发布时间】:2011-07-25 23:44:19
【问题描述】:

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

【问题讨论】:

  • Nitpick:本机 Windows API(CreateDesktop 是其中的一部分)是 C API,而不是 C++ API。

标签: c# dll import pinvoke desktop


【解决方案1】:

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

这里是Desktop Switching 使用本机 Windows API 的示例。

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

这里还有一个示例,方法略有不同:Multiple desktop support in Windows

【讨论】:

  • 最后一个链接是404
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
相关资源
最近更新 更多