Click here to Skip to main content
15,889,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a new Chrome window with Google Translate website, I want the window to appear unmaximized for the user to type the text and exit the new window. Also how to make the new window alone unmaximized.

What I have tried:

private void button1_Click(object sender, EventArgs e)
{
    var prs = new ProcessStartInfo("chrome.exe");
    prs.Arguments = "http://translate.google.com" + " --new-window";
    Process p = Process.Start(prs);
    Program.setFocusToProcess(p);


    //setFocusToThisProcessChromeName("chrome");
}






public static void setFocusToThisProcessChromeName(string name)
{
    Process[] Processes = Process.GetProcessesByName(name);

    //if (Processes.Length == 0)
    //{
    //    MessageBox.Show("The program: '" + name + "' isn't running and can't be focused on.", "TranslateProgram");
    //    return;
    //}



    foreach (Process process in Processes)
    {
        setFocusToProcess(process);
    }
}


static public void setFocusToProcess(Process process)
{
    //if (process.MainWindowHandle == (IntPtr)0x00000000)
    setFocusToHandleAndUnmaximize(process.MainWindowHandle);
    setFocusToHandleAndUnmaximize(process.Handle);

}

private static void setFocusToHandleAndUnmaximize(IntPtr i)
{
    if (i == IntPtr.Zero)
    {
        return;
    }

    int SWP_SHOWWINDOW = 0x0040;

    const int SW_RESTORE = 9;

    int HWND_TOPMOST = -1;

    //const int SW_SHOWNORMAL = 1;


    //if (IsIconic(process.MainWindowHandle) != 0)
    //{
    //    ShowWindow(process.MainWindowHandle, SW_RESTORE);
    //}

    ShowWindow(i, SW_RESTORE);

    SetForegroundWindow(i);

    BringWindowToTop(i);

    SetFocus(new HandleRef(null, i));

    SetWindowPos(i, 0, Cursor.Position.X - 100, Cursor.Position.Y - 100, 0, 0, SWP_SHOWWINDOW | HWND_TOPMOST| SW_RESTORE);
}

[DllImport("User32.dll")]
static extern int SetForegroundWindow(IntPtr point);

[DllImport("user32.dll")]
private static extern int IsIconic(IntPtr hWnd);

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr SetFocus(HandleRef hWnd);


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


[DllImport("user32.dll")]
private static extern int BringWindowToTop(IntPtr hWnd);


[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
Posted
Updated 21-Dec-18 7:14am
Comments
Dave Kreskowiak 20-Dec-18 13:56pm    
Why would you want the handles of all processes?

Also, how are you going to figure out which chrome.exe process is the one you can send a message to? Just launching Chrome starts nine processes.
john1990_1 20-Dec-18 14:02pm    
I open a new Chrome window as seen in my code, I want only this window to be brought to front and only this window being unmaximized and brought near the mouse position for the user to type on it. How to distinguish the processes of the newly opened window alone and make it unmaximized? (and not all Chrome windows)
Dave Kreskowiak 20-Dec-18 14:55pm    
Have you looked at the WindowStyle property of the ProcessStartInfo object you created? Set that to Normal before you launch the process.
john1990_1 20-Dec-18 15:15pm    
Thanks, sorry, I tried, it was supposed to work but it didn't!
mehr.ah 20-Dec-18 14:56pm    
Think about better scenarios : you can use google translator for query base results. you need to send queries in url and retrieve them from url . in this scenario you can do it with ajax in your main page or you can opening a new page and doing what you want.

1 solution

See article here and stop doing things the hard way: Google Translator[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900