Thomas
Thomas

Reputation: 34188

How to calculate mouse coordinate based on resolution c#

i am trying to develop a remote desktop apps with c#. so i have couple of question regarding mouse coordinate calculation based on picture box

  1. suppose i have picture box and i want to capture mouse coordinate when i will move my mouse on that picture box in c#?

  2. if i click at location (200, 300) on my picture box. then how can i determine
    programmatically resolution of picture box and convert that (200,300) coordinate based on that resolution.

  3. when i will send (x, y) coordinate to other machine and if that pc has resolution have like 1024x768 then what logic i need to use to convert (x, y) according to that pc resolution

if possible help me with small sample code for my question. thanks

Upvotes: 0

Views: 3634

Answers (3)

Tomiha
Tomiha

Reputation: 141

Simple, the easiest way is to transform your coordinates to a normalized form (ranging from 0 to 1). Then you can use these normalized coordinates to calculate the mousePosition on another resolution. This way the devices don't need to know the other devices resolution.

So:

//First Normalize the clickPosition using the current resolution
//clickPos(200,300) and resolution(800,600) => normalized(0.25,0.5)
var normalized = clickPos/resolution;

//Now you can send this information to the other device
//The other device uses the normalized parameter to calculate the mouseClick with his  resolution
//normalized(0.25,0.5) and otherResolution(1280,720) => otherDeviceClickPos(320, 360)
var otherDeviceClickPos = normalized * otherResolution;

Upvotes: 1

TeamWild
TeamWild

Reputation: 2550

If you know the resolution of the remote screen and you know the size of the picture box then it's simply ratios rounded off to an integer.

Upvotes: 0

Mario
Mario

Reputation: 36487

This sounds like a trivial question, if it's something homework related, add a tag homework.

int remote_x = local_x * remote_width / local_width;
int remote_y = local_y * remote_height / local_height;

The dimensions of the picture box (local_width and local_height) can be determined e.g. by using pictureBox.Width and pictureBox.Height. The cursor coordinates, local_x and local_y can be requested or are part of the event data (e.g. of the MouseMove event).

Upvotes: 3

Related Questions