在休眠模式之前切换虚拟桌面

时间:2018-10-30 01:17:53

标签: c# virtual-desktop

我已经在C#winform应用中使用以下代码制作了虚拟桌面。

#region win32
    [DllImport("user32.dll")]
    public static extern IntPtr CreateDesktop(string lpszDesktop, IntPtr lpszDevice, IntPtr pDevmode, int dwFlags, uint dwDesiredAccess, IntPtr lpsa);
    [DllImport("user32.dll")]
    public static extern bool SwitchDesktop(IntPtr hDesktop);
    [DllImport("user32.dll")]
    public static extern bool CloseDesktop(IntPtr handle);
    [DllImport("user32.dll")]
    public static extern bool SetThreadDesktop(IntPtr hDesktop);
    [DllImport("user32.dll")]
    public static extern IntPtr GetThreadDesktop(int dwThreadId);
    [DllImport("kernel32.dll")]
    public static extern int GetCurrentThreadId();

    enum DESKTOP_ACCESS : uint
    {
        DESKTOP_NONE = 0,
        DESKTOP_READOBJECTS = 0x0001,
        DESKTOP_CREATEWINDOW = 0x0002,
        DESKTOP_CREATEMENU = 0x0004,
        DESKTOP_HOOKCONTROL = 0x0008,
        DESKTOP_JOURNALRECORD = 0x0010,
        DESKTOP_JOURNALPLAYBACK = 0x0020,
        DESKTOP_ENUMERATE = 0x0040,
        DESKTOP_WRITEOBJECTS = 0x0080,
        DESKTOP_SWITCHDESKTOP = 0x0100,

        GENERIC_ALL = (DESKTOP_READOBJECTS | DESKTOP_CREATEWINDOW | DESKTOP_CREATEMENU |
                        DESKTOP_HOOKCONTROL | DESKTOP_JOURNALRECORD | DESKTOP_JOURNALPLAYBACK |
                        DESKTOP_ENUMERATE | DESKTOP_WRITEOBJECTS | DESKTOP_SWITCHDESKTOP),
    }
    #endregion

    IntPtr hOldDesktop;
    IntPtr hNewDesktop;

    hOldDesktop = GetThreadDesktop(GetCurrentThreadId());
    hNewDesktop = CreateDesktop("ScreenLock", IntPtr.Zero, IntPtr.Zero, 0, (uint)DESKTOP_ACCESS.GENERIC_ALL, IntPtr.Zero);
                SwitchDesktop(hNewDesktop);
                SetThreadDesktop(hNewDesktop);

我要制作的应用程序就像一个屏幕保护程序,但是仅在按下按钮时该窗体才会关闭。此表单是全屏表单。现在,我遇到的问题是,在虚拟桌面上运行此全屏表单后,计算机进入了休眠模式,该表单将始终位于顶部,而登录屏幕将位于背面,这使我无法直接按下按钮在我再次打开计算机直到按Ctrl + Alt + Del并登录后。此问题有解决方案吗?

从休眠方式打开计算机时,是否可以使登录屏幕显示在顶部?

我在下面尝试了以下代码,但未完成工作...

SystemEvents.SessionSwitch += new SessionSwitchEventHandler(SystemEvents_SessionSwitch);
    //Event definition
    void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
    {
        //Use switch case to identify the session switch reason.
        //Code accordingly.
        switch (e.Reason)
        {
            case SessionSwitchReason.SessionLock:
                this.Hide();
                break;
            case SessionSwitchReason.SessionUnlock:
                break;
            default:
                break;
        }
    }

0 个答案:

没有答案