在当前按钮位置弹出窗体的代码。自动计算弹出方位。

来源:互联网 发布:注册中文域名的好处 编辑:程序博客网 时间:2024/06/06 01:08

  private void button_Click(object sender, System.EventArgs e)
  {
   string data = "data"
   Form f = new Form();
   try
   {
    int topx = this.Parent.PointToScreen(this.Location).X;
    int topy = this.Parent.PointToScreen(this.Location).Y;
    int bottomx = topx + this.Width;
    int bottomy = topy + this.Height;
    if (System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width - topx >= f.Width)
     f.Left = topx;
    else
     f.Left = bottomx - f.Width;
    if (System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height - bottomy >= f.Height)
     f.Top = bottomy;
    else
     f.Top = topy - f.Height;
    f.Data = data;
    if (f.ShowDialog() == System.Windows.Forms.DialogResult.Yes)
    {
     }
   }
   catch  (Exception ex)
   {
    throw new Exception(this.Name,ex);
   }
   finally
   {
    f.Dispose();
   }
  } 

原创粉丝点击