Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want to clicked the save button of another application from my c# application.if there is data in that application then save dialogbox will appear while in absence of data message box will appear.After i want to give the filename as date and time similarly i want to click the ok button for messagebox.

i did it using 3 button save,enter filename button and ok button but the problem is when i clicked the save button window is changed to either save dialogbox or message box after that my c# application freezed. if i restart c# application it will work.how to solve this?
if possible i want to do it using single button.

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);
[DllImport("user32.dll")]
 private static extern int SendNotifyMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);
 [DllImport("User32.dll", CharSet = CharSet.Auto)]
 private static extern int SetForegroundWindow(IntPtr points);
 [DllImport("user32.dll")]
 private static extern bool PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
//save button
private void saveBtn_Click(object sender, EventArgs e)
  {
    IntPtr maindHwnd = FindWindow(null, "app1");
    IntPtr maindHwnd1 = FindWindow(null, "Error");
    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", "Save");
  if (childHwnd != IntPtr.Zero)
     {
      SendMessage(childHwnd, BM_CLICK, IntPtr.Zero, IntPtr.Zero);
     }
   }
  }

//click messagebox
private void button4_Click(object sender, EventArgs e)
  {
    IntPtr hWnd = FindWindow(null, "Error");
    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;
                }
         }
         else
         {
          textBox3.BackColor = Color.Yellow;
        textBox3.Text = "hmd is zero";
         }
        }


 //save dialogbox
String textBox = DateTime.Now.ToString("yyyyMMdd_HH-mm-ss");
  IntPtr maindHwnd = FindWindow(null, "save");
    IntPtr hWnd = FindWindow(null, "Error");
   if (maindHwnd != IntPtr.Zero)
    {
   {
     IntPtr panel = FindWindowEx(maindHwnd, IntPtr.Zero, "ComboBoxEx32", null);
      IntPtr panel1 = FindWindowEx(panel, IntPtr.Zero, "ComboBox", null);
      IntPtr panel2 = FindWindowEx(panel1, IntPtr.Zero, "Edit", null);
       if (panel2 != IntPtr.Zero)
        {

          SendKeys.Send(textBox);
                       
         }

                }
Posted
Updated 28-Feb-22 14:54pm
v2

1 solution

Hi,

SendMessage
waits for a reply by the addressee, which is OK for most purposes; however it is the wrong choice for sending a "stop all activity" command, as the destination will obey and not reply, causing your sending app to hang forever.

Solution: a quit command, such as clicking the close box of another app's main form, should be sent with
PostMessage()


:)

PS: an alternative and more complex approach, which I don't recommend in general, is to make sure the destination app's main window is frontmost, fully visible, and then issuing a Mouse command on the close box, using SendInput().
 
Share this answer
 
v2

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