1,105,391 Community Members

Moving the cursor and clicking the mouse button (native win32)

Member Avatar
morelve
Newbie Poster
4 posts since Jun 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I want to make a native win32 program that simply moves the cursor to x=100 y=200 on the screen, and then clicks the left mouse button once. Where do I start? For once, Google got me nowhere. I use VC++ 2010 to compile.

Member Avatar
nbaztec
Posting Pro in Training
473 posts since May 2010
Reputation Points: 45 [?]
Q&As Helped to Solve: 62 [?]
Skill Endorsements: 0 [?]
 
0
 

>>Google got me nowhere
Then you didn't do enough of it. There are tons of tutorials on web. Some are here at DaniWeb & I've posted to at least 5 such queries in past 3 days. Search properly.

Member Avatar
morelve
Newbie Poster
4 posts since Jun 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

They all use either #import or #using, which yields "error C2773: #import and #using available only in C++ compiler" ...

Member Avatar
nbaztec
Posting Pro in Training
473 posts since May 2010
Reputation Points: 45 [?]
Q&As Helped to Solve: 62 [?]
Skill Endorsements: 0 [?]
 
0
 

You'll have to use API specific functions.

Member Avatar
morelve
Newbie Poster
4 posts since Jun 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Ok, so I'm trying to do this in native, not managed C++. Which means I can't use code such as this

using namespace System;
using namespace System::Runtime::InteropServices;
[DllImport("user32.dll")]

Right?

I'm running this C program through the Java Native Interface, and it doesn't seem to handle managed code... Is there another way?

Member Avatar
nbaztec
Posting Pro in Training
473 posts since May 2010
Reputation Points: 45 [?]
Q&As Helped to Solve: 62 [?]
Skill Endorsements: 0 [?]
 
0
 

Use SetCursorPos or better mouse_event() or best SendInput() for Windows.

Member Avatar
morelve
Newbie Poster
4 posts since Jun 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks a lot. I couldn't get the other examples to work, so for anyone else who might stumble upon this thread with the same problems ... here's what i did:

#define _WIN32_WINNT 0x0501
#include "C:\Program Files\Java\jdk1.6.0_20\include\jni.h"
#include <stdio.h>
#include <windows.h>
#include <winuser.h>
#include "hio.h"
#pragma comment(lib, "User32.lib" ) // Probably compiler (VC++) specific

void move(int x, int y);
void press(void);
void release(void);

int ix,iy;
INPUT *click;

JNIEXPORT void JNICALL Java_hio_click(JNIEnv *env, jobject obj) {
	printf("it works!\n");
	click = new INPUT;
	ix = GetSystemMetrics(SM_CXSCREEN);
	iy = GetSystemMetrics(SM_CYSCREEN);
	move(100,200);
	press();
	release();
}
void move(int x, int y) {
	click->type = INPUT_MOUSE;
	click->mi.dx = x*65535/ix;
	click->mi.dy = y*65535/iy;
	click->mi.dwFlags = (MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE);
	SendInput(1,click,sizeof(INPUT));
}
void press(void) {
	click->type = INPUT_MOUSE;
	click->mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
	SendInput(1,click,sizeof(INPUT));
}
void release(void) {
	click->type = INPUT_MOUSE;
	click->mi.dwFlags = MOUSEEVENTF_LEFTUP;
	SendInput(1,click,sizeof(INPUT));
}

Probably not good code, but it works. Compiled with VC++ 2010. Also, thread should be moved to the C++ forum.

Question Answered as of 3 Years Ago by nbaztec
Member Avatar
Adak
Posting Virtuoso
1,711 posts since Jun 2008
Reputation Points: 419 [?]
Q&As Helped to Solve: 207 [?]
Skill Endorsements: 10 [?]
 
0
 

This is a bit of code showing how to move the cursor, using one API, in the console window.

//you need to include <windows.h>

void Gotoxy(int x, int y) {
   COORD coord;
   coord.X = x;
   coord.Y = y;
   SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void function Where I want to move the console cursor(void)  {
  Gotoxy(1,5);
}
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: