C # how to show a shape at a specific mouse position on the screen?

I have two forms, my main form is Form1, and my secondary form is displayed on request, as the Form2 dialog box. Now, if I call Form2, it always displays in the upper left corner of the screen. The first time I thought that my form was gone, but then I saw that it was hanging in the upper corner of the screen. I would like to show my form at the current position of the mouse, where the user clicks the context menu to display a modal dialog box. I already tried different things and looked for code samples. But I did not find anything but a thousand different codes on how to get the actual mouse position in different ways, which I already know. But this position is always in any case relative to the screen, the main form, the control, or any other context. Here is my code (the location of the desktop that I also tried doesn't work, and the center-screen only centers the form, so I left the property in Windows.Default.Position):

Form2 frm2 = new Form2(); frm2.textBox1.Text = listView1.ToString(); frm2.textBox1.Tag = RenameFile; DialogResult dlgres=frm2.ShowDialog(this); frm2.SetDesktopLocation(Cursor.Position.X, Cursor.Position.Y); 
+6
source share
2 answers

Your problem is that your first call is: frm2.ShowDialog(this); and then call frm2.SetDesktopLocation , which is actually only called after the form (frm2) is already closed.

ShowDialog is a blocking call - this means that it returns only when the form that you call ShowDialog closes. Therefore, you will need a different approach to adjust the position of the form.

Probably the easiest way to achieve this would be to create a second constructor on your Form2 (which you want to place) that takes two parameters for the X and Y coordinates.

 public class Form2 { // add this code after the class' default constructor private int desiredStartLocationX; private int desiredStartLocationY; public Form2(int x, int y) : this() { // here store the value for x & y into instance variables this.desiredStartLocationX = x; this.desiredStartLocationY = y; Load += new EventHandler(Form2_Load); } private void Form2_Load(object sender, System.EventArgs e) { this.SetDesktopLocation(desiredStartLocationX, desiredStartLocationY); } 

Then, when you create a form to display it, use this constructor instead of the standard one:

 Form2 frm2 = new Form2(Cursor.Position.X, Cursor.Position.Y); frm2.textBox1.Text = listView1.ToString(); frm2.textBox1.Tag = RenameFile; DialogResult dlgres=frm2.ShowDialog(this); 

You can also try using this.Move(...)' instead of 'this.SetDesktopLocation in a load handler.

+9
source

You need to call SetDesktopLocation before the ShowDialog () method, for example:

 using(Form2 frm2 = new Form2()) { frm2.textBox1.Text = listView1.ToString(); frm2.textBox1.Tag = RenameFile; frm2.SetDesktopLocation(Cursor.Position.X, Cursor.Position.Y); DialogResult dlgres=frm2.ShowDialog(this); } 

Use the use of staters that he fixed. Good luck;)

+2
source

Source: https://habr.com/ru/post/920848/


All Articles