title: Sending Click with user32.dll, messages appear in spy++ but button isn't clicked
body: <p>I have problems with sending a Click to an application with the user32.dll. the button does not be clicked, but in spy++ the message do appear.
  i'm using win7 x64</p>
  
  <p>The code is written in c#:</p>
  
  <pre><code>    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
      public static extern IntPtr FindWindow(string strClassName, string strWindowName);
  
      [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
      public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lpClassName, string lpWindowTitle);
  
      [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
      private static extern IntPtr SendMessage(IntPtr hwnd, uint Msg, IntPtr wParam, IntPtr lParam);
  
      [DllImport("user32.dll")]
      static extern IntPtr GetWindow(IntPtr hWnd, uint wCmd);
  
      //
      // Finds a window in the whole tree of childs for a parent window.
      //
      static IntPtr FindWindowRecursive(IntPtr hParent, string szClass, string szCaption)
      {
          IntPtr hResult = FindWindowEx(hParent, IntPtr.Zero, szClass, szCaption);
          if (hResult != IntPtr.Zero)
              return hResult; // found it
  
          // enumerate all childs and if found one that has childs go in
          IntPtr hChild = FindWindowEx(hParent, IntPtr.Zero, null, null); // first child
          if (hChild != IntPtr.Zero)
          {
              // let's enumerate
              do
              {
                  hResult = FindWindowRecursive(hChild, szClass, szCaption);
                  if (hResult != IntPtr.Zero)
                      return hResult;  // found it
              } while ((hChild = GetWindow(hChild, GW_HWNDNEXT)) != IntPtr.Zero);
          }
  
          return IntPtr.Zero; // no childs, so no window was found
      }
  
      static void Main(string[] args)
      {
          IntPtr win = FindWindow("Omnipage17_MainWnd_Class", "Unbenanntes OmniPage-Dokument 1 - OmniPage");
                  SetForegroundWindow(win);
          ShowWindowAsync(win, SW_RESTORE);
          IntPtr ButtonHandle = FindWindowRecursive(win, "BarButton", "c");
          SetActiveWindow(win);
          //sEND Lbuttondown
          IntPtr ptr = SendMessage(ButtonHandle, 0x0201, new IntPtr(0x0001), MakeLParam(81,28));
          //Thread.Sleep(10);
          //Mousemove
          ptr = SendMessage(ButtonHandle, 0x0200, new IntPtr(0x0001), MakeLParam(86,24));
          //lbuttonup
          ptr = SendMessage(ButtonHandle, 0x0202, new IntPtr(0x0001), MakeLParam(81, 28));
  
          //SendMessage(ButtonHandle, BM_CLICK, IntPtr.Zero, IntPtr.Zero);
      }
  </code></pre>
  
  <p>Here are the messages of spy++ of that button:
  <img src="https://i.stack.imgur.com/RYIh5.jpg" alt="enter image description here"></p>
  
  <p>If i send the the messages i get following: 
  i don't if that is that problem but the lbuttondown, buttoup appears 2 times ( S + R ) and if i'm clicking it manuelly it gets 1 message (P) 
  also tried to do it with WM_CLICK but then i have the same problem with it</p>
  
  <p><em>Edit: 
  Using now PostMessage so spy++ shows the same messages as i click it manually, but still the button seems not to be clicked</em></p>
  
  <p><img src="https://i.stack.imgur.com/Tutyl.jpg" alt="enter image description here"></p>
  
  <hr>
  
  <p>With this library I have the same problem.</p>
  
  <p>code:</p>
  
  <pre><code>SetForegroundWindow(win);
  Rectangle re;
  GetWindowRect(ButtonHandle, out re);
  
  Cursor.Position = new Point((re.X + re.Width)/2, (re.Y + re.Height)/2);
  WindowsInput.InputSimulator.SimulateKeyDown(WindowsInput.VirtualKeyCode.LBUTTON);
  WindowsInput.InputSimulator.SimulateKeyUp(WindowsInput.VirtualKeyCode.LBUTTON);
  </code></pre>
  
  <p>Message are sent, but button isn't be clicked
  <img src="https://i.stack.imgur.com/9p5Z3.jpg" alt="enter image description here"></p>
  
  <p><strong>Edit:</strong></p>
  
  <p>thanks for that link (<a href="http://www.hanselman.com/blog/IntroducingLync2010SuperSimpleAutoAnswerVideoKioskWithFullScreen.aspx" rel="nofollow noreferrer">http://www.hanselman.com/blog/IntroducingLync2010SuperSimpleAutoAnswerVideoKioskWithFullScreen.aspx</a>), but also with this library i have the same problem :/</p>
  
  <p>code:</p>
  
  <pre><code>SetForegroundWindow(win);
  Rectangle re;
  GetWindowRect(ButtonHandle, out re);
  
  Cursor.Position = new Point((re.X + re.Width)/2, (re.Y + re.Height)/2);
  WindowsInput.InputSimulator.SimulateKeyDown(WindowsInput.VirtualKeyCode.LBUTTON);
  WindowsInput.InputSimulator.SimulateKeyUp(WindowsInput.VirtualKeyCode.LBUTTON);
  </code></pre>
  
  <p>Message are sent, but button isn't be clicked
  <img src="https://i.stack.imgur.com/9p5Z3.jpg" alt="enter image description here"></p>
  
  <p><strong>Edit2:</strong> </p>
  
  <p>Answer of user was deleted, because i posted my comment as an answer:</p>
  
  <blockquote>
    <p>This is not an answer, this belongs in your question. It doesn't match
    your code, clearly you are still posting BM_CLICK. Which is wrong, it
    should be sent and you should either send BM_CLICK or post the mouse
    messages. And you are looking at the wrong window, it is button's
    parent that gets the BN_CLICK notification and acts on it. Having the
    processs' keyboard state wrong would be a typical failure mode. – Hans
    Passant 18 hours ago</p>
  </blockquote>
  
  <p>Regarding to that, why should it be the parents windows? bcs in spy++ (screenshot below i serached for that button (Class: BarButton) and the handle i get from user32.dll is also the same as that one in spy++
  <img src="https://i.stack.imgur.com/KC0t8.png" alt="enter image description here"></p>