C# 和 win32 API.在两个桌面之间切换.如何回到原来的桌面? [英] C# and win32 API. Switch between two desktops. How to get back to the original desktop?

查看:37
本文介绍了C# 和 win32 API.在两个桌面之间切换.如何回到原来的桌面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个小程序,用于创建一个新的 Windows 桌面,切换到该桌面并等待几秒钟,然后切换回原始桌面.我已经设法创建并切换到新桌面,但没有再回到原来的桌面.我在 C# 中使用 win32 API 并导入 DLL.我以为 GetDesktopWindow() 可以让我获得原始桌面的句柄,但它对我不起作用.

I'm writing a small program that creates a new Windows desktop, switches to it and waits a couple of seconds, and then switches back to the original desktop. I've managed to create and switch to the new desktop, but not back again to the original. I'm using the win32 API in C# and importing the DLLs. I thought GetDesktopWindow() would get me the handle to the original desktop, but it does not work for me.

这是我要开始工作的代码片段.

Here is the code snippet I'm trying to get to work.

    public IntPtr createDesktop(string name)
    {
        return CreateDesktop(name, IntPtr.Zero, IntPtr.Zero, 0, (long)DESKTOP_ACCESS_MASK.GENERIC_ALL, IntPtr.Zero);
    }

    public IntPtr getCurrentDesktop()
    {
        return GetDesktopWindow();
    }

    public void switchDesktop(IntPtr desktop)
    {
        SwitchDesktop(desktop);
    }

    main()
    {
      IntPtr newDesktop = createDesktop("Test");
      IntPtr oldDesktop = getCurrentDesktop();
      switchDesktop(newDesktop);
      switchDesktop(oldDesktop);
    }

推荐答案

桌面窗口是构成桌面背景的窗口.它与桌面本身不同.您需要更改 getCurrentDesktop() 以获取桌面本身的句柄,而不是桌面上的窗口(尽管是特殊窗口).

The Desktop Window is the Window that makes out the background of the desktop. It is not the same as the Desktop itself. You need to change getCurrentDesktop() to fetch the handle of the desktop itself, not a window on the desktop (although a special window).

我认为你可以使用 GetThreadDesktop() 在切换到新桌面之前获取当前桌面.

I think that you can use GetThreadDesktop() to get the current desktop before switching to the new one.

这篇关于C# 和 win32 API.在两个桌面之间切换.如何回到原来的桌面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆