access controls on other windows

后端 未结 3 665
無奈伤痛
無奈伤痛 2021-01-15 12:41

How can I access to the controls on another application\'s window?
I need to change the value of that controls (like textboxes) or click on them (like buttons).
I th

相关标签:
3条回答
  • 2021-01-15 13:04

    See the SendKeys class and read this article, here is an example from the article:

    // Get a handle to an application window.
    [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
    public static extern IntPtr FindWindow(string lpClassName,
        string lpWindowName);
    
    // Activate an application window.
    [DllImport("USER32.DLL")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);
    
    // Send a series of key presses to the Calculator application.
    private void button1_Click(object sender, EventArgs e)
    {
        // Get a handle to the Calculator application. The window class
        // and window name were obtained using the Spy++ tool.
        IntPtr calculatorHandle = FindWindow("CalcFrame","Calculator");
    
        // Verify that Calculator is a running process.
        if (calculatorHandle == IntPtr.Zero)
        {
            MessageBox.Show("Calculator is not running.");
            return;
        }
    
        // Make Calculator the foreground application and send it 
        // a set of calculations.
        SetForegroundWindow(calculatorHandle);
        SendKeys.SendWait("111");
        SendKeys.SendWait("*");
        SendKeys.SendWait("11");
        SendKeys.SendWait("=");
    }
    
    0 讨论(0)
  • 2021-01-15 13:14

    Look for "Spy++": http://msdn.microsoft.com/en-us/library/dd460756.aspx

    You can access controls on other windows with it.

    0 讨论(0)
  • 2021-01-15 13:30

    you should use Windows APIs like EnumWindow or FindWindow then use EnumChildWindows API to find the controls in the target window, like the textbox you are looking for, then the API SetWindowText

    have a look here for some ideas: Why is EnumChildWindows skipping children? also search for these APIs names here in Stack Overflow and you will find many examples...

    0 讨论(0)
提交回复
热议问题