WPF识别远程访问(不是Winforms)

发布于 2024-12-19 03:13:18 字数 373 浏览 6 评论 0原文

WPF 应用程序如何知道它是否正在远程操作(通过 VNC 或远程桌面)?

在 winforms 中,有 System.Windows.Forms.SystemInformation.TerminalServerSession 根据 检测远程桌面连接,但是WPF 中有一个简单的方法吗?

我想现在的黑客可能是在 WPF 上拥有一个不可见的 Winforms 主机,并使用它自己的能力来托管可以识别相同内容的虚拟 win 表单...但这对我来说看起来很蹩脚!

任何意见将不胜感激!

谢谢

How can a WPF app know if its getting remotely operated (via VNC or remote desktop)?

In winforms there is System.Windows.Forms.SystemInformation.TerminalServerSession as per Detecting remote desktop connection but is there a strightforward way for this in WPF?

I guess the hack for now could be to have an invisible Winforms host on WPF and use its own capacity to host dummy win form that can identify the same... but that looks lame to me!

Any inputs would be appreciated!

Thx

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

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

发布评论

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

评论(2

黑色毁心梦 2024-12-26 03:13:18

我猜现在的黑客可能是在 WPF 上拥有一个不可见的 Winforms 主机,并使用它自己的能力来托管可以识别相同内容的虚拟 win 表单...但这对我来说看起来很蹩脚!

您不需要不可见的 WinForms 主机...您只需添加对 System.Windows.Forms 程序集的引用,并使用 SystemInformation.TerminalServerSession 静态属性即可。

如果您不想依赖 WinForms,可以使用 GetSystemMetrics Win32 API:

const int SM_REMOTESESSION = 0x1000;

[DllImport("user32")]
static extern int GetSystemMetrics(int nIndex);


public static bool IsTerminalServerSession()
{
    return (GetSystemMetrics(SM_REMOTESESSION) & 1) != 0;
}

I guess the hack for now could be to have an invisible Winforms host on WPF and use its own capacity to host dummy win form that can identify the same... but that looks lame to me!

You don't need an invisible WinForms host... you can just add a reference to the System.Windows.Forms assembly, and use the SystemInformation.TerminalServerSession static property.

If you don't want a dependency on WinForms, you can use the GetSystemMetrics Win32 API:

const int SM_REMOTESESSION = 0x1000;

[DllImport("user32")]
static extern int GetSystemMetrics(int nIndex);


public static bool IsTerminalServerSession()
{
    return (GetSystemMetrics(SM_REMOTESESSION) & 1) != 0;
}
你げ笑在眉眼 2024-12-26 03:13:18

此方法不需要 Windows 窗体窗口,只需要对 DLL 的引用。

如果不想引用this,可以自己调用该方法来检查this,实现如下(我将其包装在一个类中):

static class SystemInformation
{
    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    private static extern int GetSystemMetrics(int nIndex);
    public static bool IsTerminalServerSession
    {
        get
        {
            //copied the Windows Forms implementation
            return (GetSystemMetrics(0x1000) & 1) != 0;
        }
    }
}

This method does not require a Windows Forms Window, only a reference to the DLL.

If you don't want to reference this, you can call the method to check this yourself, the implementation is as follows (I've wrapped it in a class):

static class SystemInformation
{
    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    private static extern int GetSystemMetrics(int nIndex);
    public static bool IsTerminalServerSession
    {
        get
        {
            //copied the Windows Forms implementation
            return (GetSystemMetrics(0x1000) & 1) != 0;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文