What I am trying to do is simply get a coordinate system in which to run mouse commands in a window I have the handle to.
What I am trying to do is macro my mouse. The catch is my mouse pointer has to move along with commands such as LBUTTONDOWN\LBUTTON UP.

I am trying to automate a drag and drop sequence. So when my program finds what its supposed to it will move the mouse to that area of the window and click the left mouse button down then drag the item to a specified zone and drop it.

This is far more trivial than I thought it would be, and I am begging for help on this one after hours of googling, and no code I can find is relating to what I am trying to do. Im not trying to make a drag and drop file program. It's as simple as in this programs window I have to drag and drop icons from point A to point B. So I have loops and using getpixel(); to find what I am looking for I am half way there. Once the icon is found I tried sending WM_LBUTTONDOWN to the found coordinates, and using math to logically drag the icon however it did not work nor even dragged the icon. So I am assuming my actual mouse icon for this applications window must be over the icon for this to work properly.

I am working and reworking code, but the outcome is always the same. My mouse pointer goes to the top left of my desktop screen and not of the hwnd window I am wanting this to work within.

int AutoMateMouseMacro()
{             //code for goldpickup hotkey being pressed.
      POINT point;
      RECT rect;
      int ytop = rect.top;
      int ybottom = rect.bottom;
      int xleft = rect.left;
      int xright = rect.right;
      while(ytop <= ybottom)
        point.y = ytop;
        if(ytop == ybottom)
           point.y = ytop - ybottom;
           ytop = point.y;
      while(xleft <= xright)
                  point.x = xleft;
                  if(xleft == xright)
                  point.x = xleft - xright;   
                  xleft = point.x;
      if(point.x >= xleft && point.x <= xright && point.y >= ytop && point.y <= ybottom)
      { // Your inside the client rect area 
      MessageBox(hwndDC,"Your Are INSIDE the Client Zone","WOOO HOOO", MB_OK|MB_ICONINFORMATION);    
          // Your out of the client rect
          MessageBox(hwndDC,"Your Are Outside the Client Zone","Warning Box", MB_OK|MB_ICONINFORMATION);    

      return 0;

What am I doing wrong here?
The mouse pointer keeps getting set to the top left corner of the screen.
However I know I have the proper HWND, because all the other functions work properly, and my error check for the HWND to the applications window is not failing.
I am simply doing something wrong, and dont know exactly what.
Ive been on this problem all morning with the same outcome.
Help would be very appreciated.

5 Years
Discussion Span
Last Post by CodyOebel

After much thought and thinking this over, and after finally accomplishing close to what I wanted, but not exactly. I think I might need to ask my question over again.


POINT point;
  //RECT rect;
  char text[200];
  sprintf(text,"this is the number %i",point.x);
  char text2[200];
  sprintf(text2,"this is the number %i",point.y);q

The above code returns the coordinates of wherever the mouse is on the entire screen, but it is not returning the coordinates of the mouse in the client screen or window I am trying to automate a mouse drag and drop.

So ... is there a way that if I use SendMessage(hwnd,WM_LBUTTONDOWN,0,MAKELPARAM(x,y));
I can return the equivalent screen coordinates?

E.g SendMessage(hwnd,WM_LBUTTONDOWN,0,MAKELPARAM(50,50)); would send a LBUTTONDOWN at THAT WINDOWS 50x and 50y coordinate. However those coordinates will NOT be the same as the screens coordinates of my entire desktop\computer screen. How can I get those converted to full screen coordinates?


Hmm this is odd that Im the only one answer all your threads as I've ran into the same problems for some reason :S I've also been programming for a game.. A bot really.. that Automates stuff, Finds colours on the screen and clicks it.. etc.. The solution is below of course.

RECT rcClient;
			 GetClientRect(hQuery, &rcClient);
			 POINT ptClientUL;              // client upper left corner 
			 POINT ptClientLR;              // client lower right corner 

			 POINT ptClientUR;              // client upper right corner 
			 POINT ptClientLL;              // client lower left corner

            ptClientUR.x = rcClient.right; 
            ptClientUR.y = rcClient.top;
            ptClientLL.x = rcClient.left - 1; 
            ptClientLL.y = rcClient.bottom - 1;

	    ptClientUL.x = rcClient.left; 
            ptClientUL.y = rcClient.top;
            ptClientLR.x = rcClient.right + 1; 
            ptClientLR.y = rcClient.bottom + 1;
            ClientToScreen(hQuery, &ptClientUL); 
            ClientToScreen(hQuery, &ptClientLR);
            ClientToScreen(hQuery, &ptClientUR); 
            ClientToScreen(hQuery, &ptClientLL);
            //Clips the cursor to the specified client window..
            SetRect(&rcClient, ptClientUL.x, ptClientUL.y,
            ptClientLR.x, ptClientLR.y);                       //From Upper Left to the Lower right..
			SetCursorPos(ptClientUR.x - 119 , ptClientUR.y + 38);   //Set CursorPos from client size. Should be easy to figure out..
			POINT cursorPos;
			int h = (int) cursorPos.x;
			int k = (int) cursorPos.y;
			mouse_event(MOUSEEVENTF_LEFTDOWN, h, k, 0, 0);   //Click the mouse at the position set.. :) U have a messagebox here
			mouse_event(MOUSEEVENTF_LEFTUP, h, k, 0, 0);

Edited by triumphost: n/a

struct WINDOW {
      HWND WndHandle;      
      int ID;
      WINDOW () {}
      WINDOW (HWND Owner,int ID_) : ID(ID_) {
        // WndHandle=CreateWIndow ("edit","Name",WS_CHILD,posx,posy,length,width,Owner,(HMENU) ID,NULL,NULL);
      ~WINDOW () {}
      int posx,posy,length,width;
      WINDOW MatchWIndow (const POINT&);
      operator HWND ()  { return WndHandle; }

   WINDOW WINDOW::MatchWindow (const POINT& p)  {
      RECT rc;

      bool IsInRect=(p.x > posx) & (p.x < posx+length);
      IsInRect&=(p.y > posy) & (p.y < posy+width);

      return IsInRect ? this : NULL;
//in main 
WINDOW b; //declare a window somewhere in the main procedure
//further in the switch block
         switch (LOWORD(wParam))  {
            case WM_RBUTTONDOWN:
               POINT p;
               p.x=LOWORD(lParam);            p.y=HIWORD(lParam);
               if (b.MatchWindow(p)) MessageBox (hwnd,NULL,"OK",MB_OK);

Edited by caut_baia: n/a


Great example codes. Yes triumph you have been superior advantage for me in learning and I greatly greatly appreciate the time in your day you spent to help me understand.

Im pretty brain burnt and tired, and think I will avoid this today until I get some mind rest and better sleep. Then im going to tackle this problem, and your code was exactly what I was looking for. I had found similar, but not as well commented, and after long hours of coding I got scrambler headed.

Super thanks for all that you have done once again!!!!!!!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.