Ok ... I have a handle to a game window in which inside this game my little character picks up items off the ground and drops them onto himself.

It is done with drag and drop. I left click down drag the item to the character and release the leftclick or let it up.

Using getpixel my program is locating the stuff to pickup of interest. I tried using WM_LBUTTONDOWN, and to a for loop to incriment the x,y coordinates in the SendMessage() function. I was hoping this would simulate a drag and drop. Here is some code for example.

//code for goldpickup hotkey being pressed.
                   /////Side Top Gold Check
                   int gmy1 = 160;
                   for(int gmx1 = 170; gmx1 <= 173; gmx1++)
                 {
                           
           cr = GetPixel(hdcDC,gmx1,gmy1);
           if(cr == crGold)
           {   gmx1+3;
               for(int i = 0; i > 20; i++)
               {
                       
               SendMessage(hwndDC,WM_LBUTTONDOWN,gmx1,gmy1);
               Sleep(500);
               gmy1++;
               SetPixel(hdcDC,173,gmy1,RedDot);
               };
               SendMessage(hwndDC,WM_LBUTTONUP,173,gmy1);//Character location
               
               //Drops Gold on Character  
                
           };
                gmy1 = 160;
                SetPixel(hdcDC,gmx1,gmy1,RedDot);
                 };

This above is what I thought would simulate holding down the mouse and moving it on x,y axis of the games screen I have the window to. However its not working. I added some Sleep() functions thinking maybe messages were being dropped to slow it down a bit, but it still doesnt work. Im not sure if this is the correct way to simulate\automate a drag and drop using code. The mouse icon on the game does not move when this is happening as I am sure its not supposed to. Is there a function I could use to set the cursor to the position I am wanting to automate the moves?
Im guessing here, but I am thinking the code in the game is requiring the mouse icon to be over the gold my character is trying to autonomously pick up, and if thats the case.
Then SendMessage() is sending the messages alright, but without the mouse actually being at those coordinates of sendmessage it means nothing.

Any help would be greatly appreciated.


-Cody

I tried this to see if I could get the mouse to set equal to the coordinates relevant to the games screen but it fails to work properly, and my mouse pointer keeps getting shot to the top right out of the game window.

/*
    //code for goldpickup hotkey being pressed.
                   /////Side Top Gold Check
                   POINT point;
                   GetCursorPos(&point);
                   ScreenToClient(hwndDC,&point);
                   RECT rect;
                   GetClientRect(hwndDC,&rect);
     if(point.x >= rect.left && point.x <= rect.right && point.y >= rect.top && point.y <= rect.bottom)
     {              
                   int gmy1 = 160;
                   for(int gmx1 = 170; gmx1 <= 175; gmx1++)
                 {
                 point.x=gmx1;
                 point.y=gmy1;
                 
                           
           cr = GetPixel(hdcDC,gmx1,gmy1);
           if(cr == crGold)
           {   
               for(int i = 0; i > 15; i++)
               {
                       
               SetCursorPos(point.x,point.y);        
               SendMessage(hwndDC,WM_LBUTTONDOWN,gmx1,gmy1);
               Sleep(100);
               gmy1++;
               SetPixel(hdcDC,173,gmy1,RedDot);
               };
               SetCursorPos(point.x,point.y);
               SendMessage(hwndDC,WM_LBUTTONUP,173,gmy1);//Character location
               
               //Drops Gold on Character  
                
           };
                 
                SetPixel(hdcDC,gmx1,gmy1,RedDot);
                 };
}; // end if IF statement to check for rect area of client screen hwndDC
    
   */

Ive looked around online and am doubting myself quite a bit here.
So just to clarify I am trying to set the mouse pointer to be inside my games window, and the mouse clicks, or drag and drop I am trying to acheive must be within the games window. The code I have above is not working. It is setting the mouse outside the games window. When I send a click using SendMessage() to a given coordinates of the hwndDC\handle to games window I need the mouse cursor to be relevant to those coordinates so I can process a drag and drop via the mouse automated. I am done frustrating over the code and will wait for a reply to get me in the right direction and re-motivated.

Thank you,

Cody Oebel

mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
SetCursorPos(x,y);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);

That should drag the item wherever.. as it sends mousedown then moves it, then sends mouse up..

You can always try:

SendMessage(hwndDC,WM_LBUTTONDOWN,UL.x + x,UL.y + y);

That will move the cursor in reference to the client.. Check out the other post that I have replied on.. and u will see where the UL comes from aka Upper Left..

Use GetRect, SetRect, ClipCursor (Not Necessary), then finally u set mouse position relative to either the top left of the client, bottom left, top right, bottom right.. either one will work.. That way the cursor will stay inside the client.. especially if u clip it.. which i must say again, is not necessary. No NEED to clip it.

Example from my antivirus installer:

#define REPEAT do{
#define UNTIL( condition ) }while(!(condition));
#define _WIN32_WINNT 0x0500


#pragma comment(lib, "advapi32.lib")
#include <windows.h>
#include <tlhelp32.h>
#include <iostream>
#include <conio.h>
#include <string.h>
#include <sstream>
#include <cstdlib>
#include <winable.h>
#include <Scar++.h>

using namespace std;

int main()
{
Scar setup;
HWND avast;
avast = FindWindow(0, "avast! Internet Security");
BlockInput(true);
if (avast != 0)
{
	cout << "Found the Avast Window!" << endl;
	ShowWindow(avast, SW_RESTORE);
	SetForegroundWindow(avast);
	MoveWindow(avast, 0, 0, 500, 500, true);
	Sleep(3000);
	
    RECT rcClient;                 // client area rectangle 
    POINT ptClientUL;              // client upper left corner 
    POINT ptClientLR;              // client lower right corner 

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

SetCursorPos(ptClientUR.x - 50, ptClientUR.y + 68);
   
POINT cursorPos;
GetCursorPos(&cursorPos);
int x, y;

if(FindColorTolerance(x, y, 10342864, ptClientUR.x, ptClientUR.y, ptClientLL.x, ptClientLL.y, 15))
      {
         cout<<"Found Settings Button Colour at: "<<x<<" , "<<y<<"\n\n";
         MoveMouseSmooth(x, y);
         Sleep(5000);
         
         int x = (int) cursorPos.x;
         int y = (int) cursorPos.y;

         cout<<"Settings Button Found at: ("<<x<<", "<<y<<")\n\n";

         mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
         mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}

OHHH man I really like that :)

Super nice code for me to refference. Great stuff Triumph I did check and reply on the other post. Im going to tackle this pretty soon using the example code you have provided and see how things play out!

Once more for the time youll never get back in your life ever. I thank you for this help, and your knowledge.

-Cody Oebel

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.