为什么我无法捕获进程窗口并将其显示在pictureBox中?

时间:2017-07-08 23:18:17

标签: c# .net winforms

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);

但是它没有在pictureBox中显示窗口。 我没有检查列表中的所有进程,但我检查了20,并且它们在MainWindowTitle中都是空的&#34;&#34;

我现在检查了索引112中列表中的记事本,它确实有mainwindowtitle:MainWindowTitle =&#34; New Text Document(11) - Notepad&#34;但它仍未在图片框中显示任何内容。

也许有办法使用id号来捕获进程的窗口?

0 个答案:

没有答案