none
Open an external application, set Focus to that window and move mouse Inside that window

    Question

  • Hi, i'm having trouble developing one program that needs to do the following:

    -Open an external application by using one button on my form
    -Set permanent focus on that application window even if i click outside the window
    -Move the mouse inside that window (or sending keystrokes) using code to know where to move (by doing a pixel compare)

    I'm using C# using .NET 2008, i'm mainly a C++ .net user but i'm now learning how to use what I know with C#

    Right now i'm able to open one window by using this:


    using System.Diagnostics;
    button click event
            private void buttonOPEN_Click(object sender, EventArgs e)
            {
                Process runExe = new Process();
                runExe.StartInfo.FileName = "xxxxx";
                Process.Start("xxxxx");
            }
    this is what i have when pressing the button "GO"

            private void buttonGO_Click(object sender, EventArgs e)
            {
                Process[] procs = Process.GetProcesses();
                foreach (Process proc in procs)
                {
    
                    if (proc.MainWindowTitle == "xxx")
                        //MessageBox.Show("App found!");  //works and shows if the app is opened
    
                        Cursor.Position = new Point(500, 500);  //works and puts the mouse where i want to
                        
    
                }
                
            }
    all this is currently working but i would like to know what can i do so maintain focus on the application that i opened even if i click somewhere else, also i would like to know how to put the cursor.position on a Relative position of the application that i opened and NOT the main form or even the windows client position.

    thx in advance and let me know what i can change in the above code and help me out if you can
    Sunday, January 31, 2010 11:19 PM

Answers

  • Hi,SketchUp

    Here is my code ,supposing the process to be "calc.exe":

            private void buttonOPEN_Click(object sender, EventArgs e)
            {
               
                Process runExe = new Process();
                runExe.StartInfo.FileName = "calc.exe";
                Process.Start("calc.exe");
               
            }

            private void buttonGO_Click(object sender, EventArgs e)
            {
               
                iHandle = FindWindow(null, "Calculator");
                if (iHandle != IntPtr.Zero)
                {
                    SetForegroundWindow(iHandle);

                    Rectangle rect = new Rectangle();
                    GetWindowRect(iHandle, ref rect);

                    Cursor.Position = new Point(rect.Left + 140, rect.Top + 240); 
                }
            }


    Hope this helps!

    Sincerely,
    Helen Zhou

    • Marked as answer by SketchUp Wednesday, February 3, 2010 4:35 PM
    Tuesday, February 2, 2010 8:16 AM

All replies

  • Hi,SketchUp

    Here is my code ,supposing the process to be "calc.exe":

            private void buttonOPEN_Click(object sender, EventArgs e)
            {
               
                Process runExe = new Process();
                runExe.StartInfo.FileName = "calc.exe";
                Process.Start("calc.exe");
               
            }

            private void buttonGO_Click(object sender, EventArgs e)
            {
               
                iHandle = FindWindow(null, "Calculator");
                if (iHandle != IntPtr.Zero)
                {
                    SetForegroundWindow(iHandle);

                    Rectangle rect = new Rectangle();
                    GetWindowRect(iHandle, ref rect);

                    Cursor.Position = new Point(rect.Left + 140, rect.Top + 240); 
                }
            }


    Hope this helps!

    Sincerely,
    Helen Zhou

    • Marked as answer by SketchUp Wednesday, February 3, 2010 4:35 PM
    Tuesday, February 2, 2010 8:16 AM
  • Tyvm for your help, i was having problems with Handle, and that solved my problem.

    I now need to work on the "maintain focus" if i click somewhere else, so i guess i need to work on the buttonGO_Click part now to make sure that the program that i'm opening sill have high priority even if the mouse moves outside.


    Do i need to work with threads for this (because threads is something that I never worked with)?


    If i don't require threads for this  do I need to manually code so that the FindWindow allways checks for that window and works inside it? or there's an alternate or propper way to do this?

    Thank you again for your help.
    Wednesday, February 3, 2010 4:47 PM
  • Hi,SketchUp

    I think you don't need to work with threads.I managered this by using Timer.

    Let the " SetForegroundWindow(iHandle);" take place in timer1_Tick() event. Oh,especially remember to set the timer's interval to 300!
    Here is my modified codes:

            private void buttonOK_Click(object sender, EventArgs e)
            {
                Process runExe = new Process();
                runExe.StartInfo.FileName = "calc.exe";
                Process.Start("calc.exe");

                timer1.Enabled = true;
            }

            private void buttonGO_Click(object sender, EventArgs e)
            {
                iHandle = FindWindow(null, "Calculator");
                if (iHandle != IntPtr.Zero)
                {
                    SetForegroundWindow(iHandle);

                    Rectangle rect = new Rectangle();
                    GetWindowRect(iHandle, ref rect);

                    Cursor.Position = new Point(rect.Left + 140, rect.Top + 240);
                }
            }

            private void timer1_Tick(object sender, EventArgs e)
            {
                iHandle = FindWindow(null, "Calculator");
                if (iHandle != IntPtr.Zero)
                {
                    SetForegroundWindow(iHandle);
                }
            }

    Hope this helps.!

    Best wishes,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Friday, February 5, 2010 3:46 AM
  • Hi Helen Zhou,

    I read all above post, i feel you can help me to solve my issue.

    I want to place cursor on specific textbox in a webpage which it is opened externally.

     

    I tried your sample, its just moving the mouse over the webpage.  Its not placing the cursor to the specific textbox in the webpage, where i want to send message.

    int iHandle = FindWindow(null, "Google - Windows Internet Explorer");
    
            int dat = SetForegroundWindow(iHandle);
            if (iHandle != 0)
            {
              
    
              //Rectangle rect = new Rectangle();
    
    RECT rect;
    
              NativeWin32.GetWindowRect(iHandle, out rect);
    
    
              Cursor.Position = new Point(rect.left + 140, rect.top + 130);
    
    
              SendKeys.SendWait("RAM");
    
              SendKeys.SendWait("{ENTER}");
            }

    The webpage might have multiple textbox's, but always i want to send message to the particular textbox.

     

    Is it possible ?  Please give me suggestion

     

    Thanks & Regards,

    S. Ramkumar

     


    Smiley
    Tuesday, May 25, 2010 4:30 PM
  • Ask your question in a new thread, please.
    • Proposed as answer by Rudedog2 Tuesday, May 25, 2010 6:24 PM
    Tuesday, May 25, 2010 5:18 PM