Use an external Form

0

I have my application in C # , I need to fill in three fields in another application, I'm using this code to pick up 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 well, with the code you can lift and do focus in the other application, the problem is that the other application has two different windows, and always makes me focus in window 1 and I need you to focus on window 2

    
asked by Alejandro Ricotti 22.11.2016 в 16:32
source

3 answers

0

That's how I stay

Class

class TraerVentana
{
    static private string nombreVentana;

    public static string NombreVentana
    {
        get { return TraerVentana.nombreVentana; }
        set { TraerVentana.nombreVentana = value; }
    }

    public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    private static extern int GetWindowText(IntPtr hWnd, StringBuilder strText, int maxCount);

    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    private static extern int GetWindowTextLength(IntPtr hWnd);

    [DllImport("user32.dll")]
    public static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam);

    [DllImport("user32.dll")]
    private static extern bool IsWindowVisible(IntPtr hWnd);

    [DllImport("user32.dll")]
    private static extern int SetForegroundWindow(IntPtr hwnd);

    //Antes de utilizar asignar un valor a nombreVentana
    public static bool EnumTheWindows(IntPtr hWnd, IntPtr lParam)
    {
        int size = GetWindowTextLength(hWnd);
        if (size++ > 0 && IsWindowVisible(hWnd))
        {
            StringBuilder sb = new StringBuilder(size);
            GetWindowText(hWnd, sb, size);
            if (sb.ToString() == nombreVentana)
            {
                SetForegroundWindow(hWnd);
            }
        }
        return true;
    }

}         

Implementation

TraerVentana.NombreVentana = "Nombre de ventana";
TraerVentana.EnumWindows(new TraerVentana.EnumWindowsProc(TraerVentana.EnumTheWindows), IntPtr.Zero);
    
answered by 03.02.2017 / 20:22
source
1

If that second window is "daughter" of the first one, you can use FindWindowEx to search using the first pointer that returns FindWindow .

Another method that can help you is EnumWindows , which returns all the windows.

    
answered by 22.11.2016 в 16:37
0

I got this code that unlike using FindWindow and FindWindowEx which work by process uses the name that appears in the taskbar

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace EnumWindows { class Program { protected delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

[DllImport("user32.dll")] private static extern int SetForegroundWindow(IntPtr hwnd); [DllImport("user32.dll", CharSet = CharSet.Unicode)] protected static extern int GetWindowText(IntPtr hWnd, StringBuilder strText, int maxCount); [DllImport("user32.dll", CharSet = CharSet.Unicode)] protected static extern int GetWindowTextLength(IntPtr hWnd); [DllImport("user32.dll")] protected static extern bool EnumWindows(EnumWindowsProc enumProc, IntPtr lParam); [DllImport("user32.dll")] protected static extern bool IsWindowVisible(IntPtr hWnd); protected static bool EnumTheWindows(IntPtr hWnd, IntPtr lParam) { int size = GetWindowTextLength(hWnd); if (size++ > 0 && IsWindowVisible(hWnd)) { StringBuilder sb = new StringBuilder(size); GetWindowText(hWnd, sb, size); Console.WriteLine(sb.ToString()); if (sb.ToString() == "NOMBRE DE CARPETA O APLICACION") { SetForegroundWindow(hWnd); } } return true; } static void Main(string[] args) { EnumWindows(new EnumWindowsProc(EnumTheWindows), IntPtr.Zero); Console.ReadKey(); } }

This what it does is list all the elements of the toolbar, and focus on what we enter, I need to adapt it in a class to be able to implement it in my application of forms

    
answered by 22.11.2016 в 21:52