首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何在C#中找到运行应用程序的屏幕

在C#中,您可以使用Windows API(Win32 API)来找到运行应用程序的屏幕。以下是一个简单的示例,演示如何使用Win32 API在C#中找到运行应用程序的屏幕。

首先,您需要在项目中添加以下命名空间:

代码语言:csharp
复制
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

然后,您可以使用以下代码来找到运行应用程序的屏幕:

代码语言:csharp
复制
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll")]
private static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);

private static int GetWindowProcessId(IntPtr hwnd)
{
    int processId = 0;
    GetWindowThreadProcessId(hwnd, out processId);
    return processId;
}

private static string GetWindowTitle(IntPtr hwnd)
{
    int length = GetWindowTextLength(hwnd);
    StringBuilder sb = new StringBuilder(length + 1);
    GetWindowText(hwnd, sb, sb.Capacity);
    return sb.ToString();
}

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowTextLength(IntPtr hWnd);

private static void Main()
{
    IntPtr hwnd = GetForegroundWindow();
    int processId = GetWindowProcessId(hwnd);
    string windowTitle = GetWindowTitle(hwnd);

    Console.WriteLine($"Window Title: {windowTitle}");
    Console.WriteLine($"Process ID: {processId}");
}

这个示例将打印出当前运行应用程序的屏幕的标题和进程ID。

请注意,这个示例仅适用于Windows操作系统。如果您需要在其他平台上找到运行应用程序的屏幕,您需要使用其他API或库。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的视频

领券