0

I want to show my form on the screen I'm clicking on.

For this I use a globalhookMouse to get the coordinates of my cursor outside my form. I've two screen (1280x1024 and 1680x1050).

I've tried to put my form if my Cursor.Position.X is lower than one of my resolution but I get higher value.

This is how I hook my form to show

private void GlobalHookMouseDownExt(object sender, MouseEventArgs e)
{
     if (e.Button == MouseButtons.Middle)
     {
          this.Show();
          this.TopLevel = true;
     }
}  

How can I know from which screen I clicked to show my form on the right screen ?

  • Read the notes here: [Using SetWindowPos with multiple monitors](https://stackoverflow.com/a/53026765/7444103) -- Don't skip the DpiAwareness part. – Jimi May 17 '22 at 10:00
  • 1
    https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.screen.frompoint?view=windowsdesktop-6.0 – Hans Passant May 17 '22 at 12:09

1 Answers1

0

System.Windows.Forms.Screen.AllScreens gives you a lot of information about your screens.

You will see that the X can go negative.

int x = System.Windows.Forms.Cursor.Position.X; int y = System.Windows.Forms.Cursor.Position.Y;

these will always be the coords of where your cursor is at that moment.

the end result would be something like:

if (Cursor.Position.X > 0)
{
   this.Location = new System.Drawing.Point(0,0);
}

else
{
    this.Location = new System.Drawing.Point(-1920,0);
}

I hope it helps.

  • But if I've two screen, how can I know which screen am I on if my X is like 2342 ? – LeandroCamilo May 17 '22 at 09:35
  • You could also do the following: this.Location = new System.Drawing.Point(Cursor.Position.X, Cursor.Position.Y); Then the form will start wherever your cursor is at that moment. Please let me know if it works for you. – John Spuls May 17 '22 at 10:51