0
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 = "";
                }
            } 
        }
    }
}

When using a break point it stop on the line:

CaptureApplication(process.MainWindowTitle);

But it's not showing the window in the pictureBox. I didn't check all the processes in the list but i checked like 20 and they are all empty in the MainWindowTitle ""

I checked now the notepad it's in the list in index 112 and it does have mainwindowtitle: MainWindowTitle = "New Text Document (11) - Notepad" but still it's not showing anything in the picturebox.

Maybe there is a way to capture the window of the process/s using the id number ?

testman tm
  • 89
  • 1
  • 11
  • I don't see anywhere in your code that you are setting an image for the picture box – Alexander Higgins Jul 08 '17 at 23:34
  • @AlexanderHiggins right my mistake i thought the line IntPtr value = SetParent(_wndConsole, this.pictureBox1.Handle); inside the CaptureApplication handle the pictureBox(this.pictureBox1.Handle). Then where and how should i assign the image to the pictureBox ? – testman tm Jul 09 '17 at 00:10
  • In other window that have a console window "Blender" then it will show in the pictureBox the console window. But if it's "Notepad" or others it will not show. Maybe since the WindowClass is only for console ? string _wndcls = "ConsoleWindowClass"; – testman tm Jul 09 '17 at 00:18
  • RIght...leave the first parameter to FindWindow() blank and it will match only on the window title. Also, why are you using CreateProcess if that app is already running? – Idle_Mind Jul 09 '17 at 00:50
  • @Idle_Mind i changed the line with the FindWindow to: _wndConsole = FindWindow("", _title); and removed the line with the CreateProcess. Still i don't see the Notepad window in the pictureBox. – testman tm Jul 09 '17 at 00:59
  • Did you see [this](https://stackoverflow.com/questions/891345/get-a-screenshot-of-a-specific-application)? – TaW Jul 09 '17 at 07:20

0 Answers0