title: How to move mouse with c++
body: <p>I want to move the mouse cursor with a c++ script. I am using Visual C++ 2010 Express in a Windows 7 inside Parallels and I created a console application.</p>
  
  <p>I know SetCursorPos method but it is just not working (it does nothing).</p>
  
  <p>I managed to simulate clicks with SendInput but it does not actually move the mouse.</p>
  
  <p>This is my code:</p>
  
  <pre><code>#include &lt;Windows.h&gt;
  #include &lt;Tlhelp32.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;sstream&gt;
  #include &lt;time.h&gt;
  
  void mouseLeftClick(const int x, const int y);
  
  // window
  HWND hWindow;
  
  int main()
  {
      // find window
      hWindow = FindWindow(NULL, "Calculadora");
  
      if (NULL == hWindow) {
          OutputDebugStringA("Couldn't find application.");
      }else{
  
          if (!SetForegroundWindow(hWindow)) {
              OutputDebugStringA("Couldn't set application to foreground.");
          }else{
              // click on 1
              mouseLeftClick(20 265));
              Sleep(500);
              // click on 2
              mouseLeftClick(60, 265);
              Sleep(500);
          }
      }
      return 0;
  }
  
  void mouseLeftClick(const int x, const int y)
  { 
      // get the window position
      RECT rect;
      GetWindowRect(hWindow, &amp;rect);
  
      // calculate scale factor
      const double XSCALEFACTOR = 65535 / (GetSystemMetrics(SM_CXSCREEN) - 1);
      const double YSCALEFACTOR = 65535 / (GetSystemMetrics(SM_CYSCREEN) - 1);
  
      // get current position
      POINT cursorPos;
      GetCursorPos(&amp;cursorPos);
      double cx = cursorPos.x * XSCALEFACTOR;
      double cy = cursorPos.y * YSCALEFACTOR;
  
      // calculate target position relative to application
      double nx = (x + rect.left) * XSCALEFACTOR;
      double ny = (y + rect.top) * YSCALEFACTOR;
  
      INPUT Input={0};
      Input.type = INPUT_MOUSE;
  
      Input.mi.dx = (LONG)nx;
      Input.mi.dy = (LONG)ny;
  
      // set move cursor directly and left click
      Input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP;
  
      SendInput(1,&amp;Input,sizeof(INPUT));
  }
  </code></pre>