使弹出窗体相对控件的位置固定的方法!

在点击Button弹出一个新窗体时,希望窗体位于Button的上面,改变Button位置点击新窗体的位置始终相对固定。

           BlankFrm m_listViewUI = new BlankFrm();        
this.listBox1.Items.Clear();
m_listViewUI.StartPosition = FormStartPosition.Manual;
m_listViewUI.Location = new Point(Control.MousePosition.X, Control.MousePosition.Y - 250 - 28);
//m_listViewUI.Location = pt;
m_listViewUI.Name = "listViewUI1";
m_listViewUI.Padding = new System.Windows.Forms.Padding(4);
m_listViewUI.Size = new System.Drawing.Size(200, 250);

开始用这种方法,好像有些繁琐!

            m_listViewUI = new BlankFrm();
int x, y;
int Screen_x = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Width;
int Screen_y = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Height;
this.listBox1.Items.Clear();
m_listViewUI.StartPosition = FormStartPosition.Manual;
Control control = (Control)sender;
//MessageBox.Show(this.PointToClient(this.btnAdd.Location).X.ToString() + " " + this.PointToClient(this.btnAdd.Location).Y.ToString());
Point pt1 = control.PointToScreen((sender as Button).Location);
x = this.PointToScreen((sender as Button).Location).X;
y = pt1.Y - 250 - 28;
m_listViewUI.Location = new Point(x, y);
m_listViewUI.Name = "listViewUI1";
m_listViewUI.Padding = new System.Windows.Forms.Padding(4);
m_listViewUI.Size = new System.Drawing.Size(200, 250);

效果一样,如下:

文章未经说明均属原创,学习笔记可能有大段的引用,一般会注明参考文献。 欢迎大家留言交流,转载请注明出处。
原文地址:https://www.cnblogs.com/yhlx125/p/2316944.html