Click here to Skip to main content
15,352,570 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i want to click the abcd button of an application from my c# application and also click the ok button of pop-up messagebox appears when abcd button is clicked.
but when i click the abcd button from my application then My c# application stop working after pop-up window is appeared.

What I have tried:

[DllImport("user32.dll", SetLastError = true)]
      private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
      [DllImport("user32.dll")]
      static public extern bool GetWindowRect(IntPtr hWnd, out Rectangle lpRect);

      [DllImport("user32.dll")]
      private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);

      [DllImport("user32.dll")]
      private static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);


//code to click abcd button
IntPtr maindHwnd = FindWindow(null, "apps");
    if (maindHwnd != IntPtr.Zero)
     {
       IntPtr panel = FindWindowEx(maindHwnd, IntPtr.Zero, "MDIClient", null);
       IntPtr panel1 = FindWindowEx(panel, IntPtr.Zero, "TAveForm", null);
       IntPtr panel2 = FindWindowEx(panel1, IntPtr.Zero, "TPanel", "Panel5");
       IntPtr panel3 = FindWindowEx(panel2, IntPtr.Zero, "TPanel", null);
       IntPtr childHwnd = FindWindowEx(panel3, IntPtr.Zero, "TBitBtn", "abcd");


               if (childHwnd != IntPtr.Zero)
               {

                   SendMessage(childHwnd, BM_CLICK, IntPtr.Zero, IntPtr.Zero);

               }


               else
               {
                   textBox3.BackColor = Color.Yellow;
                   textBox3.Text = "apps is not opened";
               }

//code for clicking OK button of pop-up message

IntPtr hWnd = FindWindow(null, "pop");

      if (hWnd != IntPtr.Zero)
       {

        IntPtr childHwnd = FindWindowEx(hWnd, IntPtr.Zero, "Button", "Ok");
        if (childHwnd != IntPtr.Zero)
         {
          SendMessage(childHwnd, BM_CLICK, IntPtr.Zero, IntPtr.Zero);

             }
             else
             {
                 textBox3.BackColor = Color.Yellow;
                 textBox3.Text = "error";
             }
         }
Posted
Updated 27-Feb-22 18:24pm

1 solution

Check the article to achieve through Windows APIs:
Using Windows APIs from C#, again![^]
   

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