C# 为什么我能';捕获流程窗口并将其显示在pictureBox中?
但它没有在图片框中显示窗口。 我没有检查列表中的所有进程,但我检查了20个进程,它们在MainWindowTitle中都是空的“” 我现在检查了记事本,它在索引112的列表中,它确实有mainwindowtitle:mainwindowtitle=“新文本文档(11)-记事本”,但它仍然没有在图片框中显示任何内容C# 为什么我能';捕获流程窗口并将其显示在pictureBox中?,c#,.net,winforms,C#,.net,Winforms,但它没有在图片框中显示窗口。 我没有检查列表中的所有进程,但我检查了20个进程,它们在MainWindowTitle中都是空的“” 我现在检查了记事本,它在索引112的列表中,它确实有mainwindowtitle:mainwindowtitle=“新文本文档(11)-记事本”,但它仍然没有在图片框中显示任何内容 也许有一种方法可以使用id号捕获进程的窗口?我在代码中没有看到您正在为图片设置图像box@AlexanderHiggins纠正我的错误,我认为行IntPtr value=SetPare
也许有一种方法可以使用id号捕获进程的窗口?我在代码中没有看到您正在为图片设置图像box@AlexanderHiggins纠正我的错误,我认为行IntPtr value=SetParent(\u wndConsole,this.pictureBox1.Handle);在CaptureApplication内部处理pictureBox(this.pictureBox1.handle)。那么我应该在何处以及如何将图像分配给pictureBox?在具有控制台窗口“Blender”的其他窗口中,它将在pictureBox中显示控制台窗口。但如果它是“记事本”或其他,它将不会显示。也许因为WindowClass只用于控制台?字符串_wndcls=“ConsoleWindowClass”;右…将FindWindow()的第一个参数留空,它将只在窗口标题上匹配。另外,如果应用程序已经在运行,为什么要使用CreateProcess?@Idle\u Mind我将FindWindow的行更改为:\u wndConsole=FindWindow(“,\u title”);并删除了带有CreateProcess的行。我仍然没有在图片盒中看到记事本窗口。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Drawing.Imaging;
namespace CaptureProcessWindow
{
public partial class Form1 : Form
{
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
public static extern long SetWindowPos(IntPtr hwnd, IntPtr hWndInsertAfter, long x, long y, long cx, long cy,
long wFlags);
[DllImport("user32.dll", SetLastError = true)]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
[DllImport("kernel32.dll")]
static extern bool CreateProcess(string lpApplicationName,
string lpCommandLine,
IntPtr lpProcessAttributes,
IntPtr lpThreadAttributes,
bool bInheritHandles,
uint dwCreationFlags,
IntPtr lpEnvironment,
string lpCurrentDirectory,
ref STARTUPINFO lpStartupInfo,
out PROCESS_INFORMATION lpProcessInformation);
public const int WM_SYSCOMMAND = 0x112;
public const int SC_MINIMIZE = 0xf020;
public const int SC_MAXIMIZE = 0xf030;
public struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public uint dwProcessId;
public uint dwThreadId;
}
public struct STARTUPINFO
{
public uint cb;
public string lpReserved;
public string lpDesktop;
public string lpTitle;
public uint dwX;
public uint dwY;
public uint dwXSize;
public uint dwYSize;
public uint dwXCountChars;
public uint dwYCountChars;
public uint dwFillAttribute;
public uint dwFlags;
public short wShowWindow;
public short cbReserved2;
public IntPtr lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}
public struct SECURITY_ATTRIBUTES
{
public int length;
public IntPtr lpSecurityDescriptor;
public bool bInheritHandle;
}
public Form1()
{
InitializeComponent();
}
public void CaptureApplication(string _title)
{
string _wndcls = "ConsoleWindowClass";
STARTUPINFO si = new STARTUPINFO();
PROCESS_INFORMATION pi = new PROCESS_INFORMATION();
CreateProcess(_title, null, IntPtr.Zero, IntPtr.Zero, false, 0, IntPtr.Zero, null, ref si, out pi);
IntPtr _wndConsole = IntPtr.Zero;
for (int i = 0; i < 30; i++)
{
_wndConsole = FindWindow(_wndcls, _title);
if (_wndConsole == IntPtr.Zero)
{
System.Threading.Thread.Sleep(10);
continue;
}
break;
}
IntPtr value = SetParent(_wndConsole, this.pictureBox1.Handle);
int style = GetWindowLong(_wndConsole, -16);
style &= -12582913;
SetWindowLong(_wndConsole, -16, style);
SendMessage(_wndConsole, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Process[] processlist = Process.GetProcesses();
List<string> names = new List<string>();
foreach (Process process in processlist)
{
if (process.MainWindowTitle.Contains("Notepad"))
{
CaptureApplication(process.MainWindowTitle);
break;
}
else
{
string t = "";
}
}
}
}
}
CaptureApplication(process.MainWindowTitle);