1

First of all, have in mind that Im a beginner.

With that said, Im trying to press a button in an external application with user32.dll. I have tried a lot of solutions but cant find anything that works.

I am using 3 DllImport because I want to bring the window to the front aswell (This works fine)

It looks like this:

[DllImportAttribute("User32.dll")]
private static extern int FindWindow(String ClassName, String 
WindowName);

[DllImportAttribute("User32.dll")]
private static extern IntPtr SetForegroundWindow(int fgr);

[DllImport("user32.dll")]
static extern bool SendMessage(IntPtr hWnd, uint Msg, int wParam, int 
lParam);
const int BN_CLICKED = 245;

When I click a button, it will bring the application to the front, like this:

private void button1_Click(object sender, EventArgs e)
{
    int fgr = FindWindow("TAssistMainFrm", null);
    if (fgr > 0) //If found
    {
        MessageBox.Show("Window found!");
        SetForegroundWindow(fgr);

    }
    else //Not Found
    {
        MessageBox.Show("Window Not Found!");
    }

}

As I said, this works absolutly fine! Heres the problem. I want to press a button with ClassNN TButton10

I have tried:

 SendMessage(TButton10, BN_CLICKED, 0, 0);

And a lot of other things, but Im guessing that I need to find TButton10 and declare it first. How should I do that?

Anyone that can help me out with code that works, and an explanation of the code? Thanks a lot!

  • So did I understand you right that you want to send a message that a specific button on the external application gets clicked? – L. Guthardt Mar 07 '18 at 08:25
  • Do you have control over that "external application"'s code, too? – Fildor Mar 07 '18 at 08:28
  • 2
    this might help you: https://www.codeproject.com/Articles/14519/Using-Windows-APIs-from-C-again – slow Mar 07 '18 at 08:30
  • So it looks like you're trying to press a button in a delphi or cbuilder app, you would probably do better to send mouse events, such as mouse move to (x,y) as hopefully the button is fixed from some part of the form so you can guess where it is, and then send mouse down, mouse up. – BugFinder Mar 07 '18 at 08:31
  • Ist that external application a .net application? – Timo Mar 07 '18 at 08:31
  • Yes, I want to send a message to a specific button on the external application so it gets clicked. The external application is written in Delphi and I dont want to use cordinates. Its simply too risky. I can look at the code on the external application, but Im not going to change anything in there. – Daniel Strömberg Mar 07 '18 at 09:57
  • Possible duplicate of [Simulating Key Press c#](https://stackoverflow.com/questions/3047375/simulating-key-press-c-sharp) – Deniz Jun 21 '18 at 12:39

0 Answers0