We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,464 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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

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.

3
Contributors
7
Replies
6 Hours
Discussion Span
2 Years Ago
Last Updated
9
Views
Question
Answered
morelve
Newbie Poster
4 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

nbaztec
Posting Pro in Training
475 posts since May 2010
Reputation Points: 57
Solved Threads: 60
Skill Endorsements: 0

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

morelve
Newbie Poster
4 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

You'll have to use API specific functions.

nbaztec
Posting Pro in Training
475 posts since May 2010
Reputation Points: 57
Solved Threads: 60
Skill Endorsements: 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?

morelve
Newbie Poster
4 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

nbaztec
Posting Pro in Training
475 posts since May 2010
Reputation Points: 57
Solved Threads: 60
Skill Endorsements: 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.

morelve
Newbie Poster
4 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 2 Years Ago by nbaztec

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);
}
Adak
Posting Virtuoso
1,641 posts since Jun 2008
Reputation Points: 456
Solved Threads: 196
Skill Endorsements: 7

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0745 seconds using 2.67MB