I have my application in C# , I need to be able to fill three fields in another application, I am using this code to raise the other application and send sendkeys
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string className, string windowTitle);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool ShowWindow(IntPtr hWnd, ShowWindowEnum flags);
[DllImport("user32.dll")]
private static extern int SetForegroundWindow(IntPtr hwnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowPlacement(IntPtr hWnd, ref Windowplacement lpwndpl);
private enum ShowWindowEnum
{
Hide = 0,
ShowNormal = 1, ShowMinimized = 2, ShowMaximized = 3,
Maximize = 3, ShowNormalNoActivate = 4, Show = 5,
Minimize = 6, ShowMinNoActivate = 7, ShowNoActivate = 8,
Restore = 9, ShowDefault = 10, ForceMinimized = 11
};
private struct Windowplacement
{
public int length;
public int flags;
public int showCmd;
public System.Drawing.Point ptMinPosition;
public System.Drawing.Point ptMaxPosition;
public System.Drawing.Rectangle rcNormalPosition;
}
public void TraerVentanaAlFrente(string proceso)
{
IntPtr wdwIntPtr = FindWindow(null, proceso);
//Obtener el hWnd del proceso
Windowplacement placement = new Windowplacement();
GetWindowPlacement(wdwIntPtr, ref placement);
// Verificar si la ventana esta minimizada
if (placement.showCmd == 2)
{
//Si la ventana esta minimizada restaurarla
ShowWindow(wdwIntPtr, ShowWindowEnum.Restore);
}
//Hacer foco en la ventana
SetForegroundWindow(wdwIntPtr);
}
This part works fine, with the code I can raise and focus on the other application, the problem is that the other application has two different windows, and it always focuses on window 1 and I need it to focus on window 2
If that second window is a "child" of the first, you can use FindWindowEx to search using the first pointer returned to you
FindWindow
.Another method that may work for you is EnumWindows , which returns all windows.
I got this code that unlike using FindWindow and FindWindowEx which work by process uses the name that appears in the task bar
What this does is list all the elements of the toolbar, and focus on the one we enter, I need to adapt it in a class to be able to implement it in my forms application
so i stay
Class
Implementation