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.

Dani AI

Generated

As hinted, the clean, native way is to synthesize real mouse input with SendInput. It does not require managed code or #import, and it is the supported successor to mouse_event. One gotcha: when you use absolute mouse moves, dx/dy are NOT pixels; they are normalized to 0..65535, so you must scale your pixel target into that range. Also note that injected input is subject to UIPI, so it will not drive higher‑integrity targets (e.g., UAC prompts). See SendInput and the deprecation note on mouse_event.

Here is a minimal, robust pattern that moves to (100,200) on the virtual desktop and clicks once. It zero‑initializes INPUTs, normalizes coordinates using width-1/height-1, and submits the whole sequence in one SendInput call.

void MoveAndLeftClick(int px, int py) {
    int vx = GetSystemMetrics(SM_XVIRTUALSCREEN);
    int vy = GetSystemMetrics(SM_YVIRTUALSCREEN);
    int vw = GetSystemMetrics(SM_CXVIRTUALSCREEN);
    int vh = GetSystemMetrics(SM_CYVIRTUALSCREEN);

    LONG nx = MulDiv(px - vx, 65535, vw - 1);
    LONG ny = MulDiv(py - vy, 65535, vh - 1);

    INPUT in[3] = {};
    in[0].type = INPUT_MOUSE;
    in[0].mi.dx = nx; in[0].mi.dy = ny;
    in[0].mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_VIRTUALDESK;
    in[1].type = INPUT_MOUSE; in[1].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
    in[2].type = INPUT_MOUSE; in[2].mi.dwFlags = MOUSEEVENTF_LEFTUP;

    SendInput(3, in, sizeof(INPUT));
}

Notes:

  • Use MOUSEEVENTF_VIRTUALDESK with absolute moves so your math targets the entire multi‑monitor desktop, not just the primary. The flags and behavior are documented under MOUSEINPUT; the virtual‑desktop bounds come from GetSystemMetrics (SM_X/Y/CXVIRTUALSCREEN/ CYVIRTUALSCREEN). See MOUSEINPUT flags and GetSystemMetrics.
  • If you truly want to set pixels directly (not normalized), SetCursorPos works, and on newer Windows you can use SetPhysicalCursorPos to bypass DPI scaling nuances. See SetPhysicalCursorPos.
  • ’s snippet is for the console caret; it does not move the system mouse pointer.

Recommended Answers

All 7 Replies

>>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.

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

You'll have to use API specific functions.

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?

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

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.

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);
}
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.