How to put background window/process on top

725 views Asked by At

I want to create a software like a virtualkeyboard, you have a AlwaysTop Window and use this to put some data on another process/windows. In this case I will record all data on clipboard and compare if this data is compatible with a pattern (A### is the patern and A123 is compatible with the patern), if yes the application will put it in a listbox and the user can paste it on another process/windows (already open) clicking on item on list.

My question is about how to put this information on the last application/process used, I already started a prototype of code but the line indicated is wrong, on my code it's the currentprocess and need to be the last used before click on my form.

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }

    [DllImport("user32.dll")]
    static extern IntPtr SetForegroundWindow(IntPtr hWnd);

    [DllImport("user32.dll")]
    internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    [DllImport("User32.dll")]
    static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);


    private void button2_Click(object sender, EventArgs e)
    {
    Process currentProcess = Process.GetCurrentProcess();   //this line is wrong
    IntPtr hWnd = currentProcess.MainWindowHandle;          //this line is wrong
    if (hWnd != IntPtr.Zero)
    {
        SetForegroundWindow(hWnd);
        ShowWindow(hWnd, 9);
    SendKeys.Send("A123");
    }
    }

}
}
1

There are 1 answers

0
Paixão On

I get on simple solution, instead of get the process I just send the combination ALT+TAB and work for all cases that I need. Below the solution if anyone need in the future:

string old_clipboard = Clipboard.GetText();
Clipboard.SetText("A123");
SendKeys.SendWait("%{Tab}");
SendKeys.SendWait("^V");
Thread.Sleep(100);
Clipboard.SetText(old_clipboard);

Ps.: I put one delay because the SendWait works only on caller windows, as the target of ^V is another process it´s don´t work well.

Best regards. =)