I have the following code:
The using statements are
using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
Before the Main I have
// import the function in your
[DllImport("User32.dll")]
static extern int SetForegroundWindow(IntPtr point);
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
static extern IntPtr SetActiveWindow(IntPtr hWnd);
[STAThreadAttribute]
static void **Main**(string[] args)
Within the Main I have
// Get the Processes
Process[] p = Process.GetProcesses();
string processName = "";
// Loop through the processes list
for (int i = 0; i < p.Length; i++)
{
// Find myProcessName
if (p[i].ProcessName == "myProcessName")
{
Console.WriteLine("PID: " + p[i].Id);
processName = p[i].ProcessName;
}
}
// Set the process/window to the foreground
Process p = Process.GetProcessesByName(processName)[0];
if (p != null)
{
// Get the process handle
IntPtr h = p.MainWindowHandle;
// Set the window to the foreground
SetForegroundWindow(h);
// Wait to get idle
p.WaitForInputIdle();
// Set the Active window
SetActiveWindow(h);
// Wait to get idle
p.WaitForInputIdle();
}
// Select the Text
MouseOperations.SetCursorPosition(1269, 218);
MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.LeftDown);
MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.LeftUp);
MouseOperations.SetCursorPosition(123, 310);
MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.LeftDown);
MouseOperations.SetCursorPosition(133, 502);
// Send the Ctrl+C
SendKeys.SendWait("^c");
// Application.DoEvents();
// Print the clipboard
if (Clipboard.ContainsText())
{
Console.WriteLine("##########################################");
Console.WriteLine(Clipboard.GetText());
Console.WriteLine("##########################################");
}
// Done, lift the left mouse key
MouseOperations.MouseEvent(MouseOperations.MouseEventFlags.LeftUp);
The MouseOperations was taken from here (https://stackoverflow.com/a/7121314/1933657)
I run the program from within VisualStudio 2019, the CommandWindow shows up, the program I want comes up above the CommandWindow, the Text is selected, but nothing is ever copied to the Clipboard.
Any ideas what I am missing here?