您的位置:首页 > 其它

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

2012-01-09 11:48 337 查看
在点击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);


效果一样,如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: