2016-12-18 3 views
0

このコードを使用して2番目のモニタを選択すると、マウスカーソルが最初のモニタディスプレイに表示されている場合に、2番目のモニタを選択してフォームを開く

private void showOnMonitor(int showOnMonitor) 
     { 
      Screen[] sc; 
      sc = Screen.AllScreens; 
      if (showOnMonitor >= sc.Length) 
      { 
       showOnMonitor = 0; 
      } 

      this.StartPosition = FormStartPosition.CenterParent; 
      this.Location = new Point(sc[showOnMonitor].Bounds.Left, sc[showOnMonitor].Bounds.Top); 
      this.WindowState = FormWindowState.Normal; 

     } 
+0

として関数を呼び出します。 –

+0

現在のところ、私のコードは、2番目のモニタに表示するフォームを起動すると、マウスカーソルが2番目のモニタにプライベートvoid Form1_OnLoad(オブジェクト送信者、EventArgs e)を使用して表示されている場合にのみフォームを2番目のモニタに表示します { showOnMonitor 1)。 } –

答えて

0

質問が不明です。プライマリモニタにマウスポインタがある場合は、プライマリモニタ以外の他のモニタでフォームを開きたいとします。コメントを追加したので、コードを微調整できます。 は、私は、マウスポインタが第1のモニタに表示されている場合でもセカンドモニタ上でフォームを開きたい

showOnMonitor(5, ConvertMousePointToScreenIndex(System.Windows.Forms.Cursor.Position));

private void showOnMonitor(int showOnMonitor, Point mousePoint) 
{ 
    Screen[] sc; 
    System.Drawing.Rectangle rect; 
    int mouseOnMonitor = 0; 

    for (int i = 1; i <= Screen.AllScreens.Count(); i++) 
    { 
     rect = Screen.AllScreens[i - 1].Bounds; 
     if (rect.Contains(mousePoint)) 
     { 
      mouseOnMonitor = i - 1; 
      break; 
     }   
    } 

    sc = Screen.AllScreens; 
    if (showOnMonitor >= sc.Length) 
    { 
     showOnMonitor = 0; 
    } 

    if (showOnMonitor == mouseOnMonitor && showOnMonitor == 0) 
    { 
     //do your logic here if monitor with mouse and showOnMonitor are same i.e. disable the form opening on the monitor with mouse, 
     //if such monitor should be always 0, then add the check to if condition showOnMonitor == 0 else remove this check 

     //open the form on next monitor 
     //Check if more than 1 monitor available 
     if (sc.Length > 1) 
     { 
      showOnMonitor = showOnMonitor + 1; 
      if (showOnMonitor >= sc.Length) 
       showOnMonitor = 1; 
     } 
     else 
     { 
      //throw exception as only 1 monitor and form should not be allowed to open on this monitor 
     }   
    } 
    else 
    { 
     this.StartPosition = FormStartPosition.CenterParent; 
     this.Location = new Point(sc[showOnMonitor].Bounds.Left, sc[showOnMonitor].Bounds.Top); 
     this.WindowState = FormWindowState.Normal; 
    } 
} 
関連する問題