2013-03-03 18 views
9

Sto usando C# WinForm per sviluppare un'app di notifica sman. Vorrei posizionare il modulo principale nell'angolo in basso a destra dell'area di lavoro dello schermo. In caso di più schermi, c'è un modo per trovare lo schermo più a destra dove posizionare l'app, o almeno ricordare l'ultima schermata utilizzata e palce il modulo nell'angolo in basso a destra?Posizione modulo nell'angolo in basso a destra dello schermo

risposta

21

Io attualmente non hanno più display per controllare, ma dovrebbe essere qualcosa di simile a

public partial class LowerRightForm : Form 
    { 
     public LowerRightForm() 
     { 
      InitializeComponent(); 
     } 

     protected override void OnLoad(EventArgs e) 
     { 
      PlaceLowerRight(); 
      base.OnLoad(e); 
     } 

     private void PlaceLowerRight() 
     { 
      //Determine "rightmost" screen 
      Screen rightmost = Screen.AllScreens[0]; 
      foreach (Screen screen in Screen.AllScreens) 
      { 
       if (screen.WorkingArea.Right > rightmost.WorkingArea.Right) 
        rightmost = screen; 
      } 

      this.Left = rightmost.WorkingArea.Right - this.Width; 
      this.Top = rightmost.WorkingArea.Bottom - this.Height; 
     } 
    } 
+1

Oppure 'var rightmost = Screen.AllScreens.OrderBy (s => s.WorkingArea.Right) .Last();' –

+0

@GertArnold So che MoreLINQ ha 'MaxBy 'Per essere più efficiente ma se devo azzardare un'ipotesi,' OrderByDescending.First' dovrebbe essere più efficiente di 'OrderBy.Last'. – nawfal

8

sovrascrivere il modulo Onload e impostare la nuova posizione:

protected override void OnLoad(EventArgs e) 
{ 
    var screen = Screen.FromPoint(this.Location); 
    this.Location = new Point(screen.WorkingArea.Right - this.Width, screen.WorkingArea.Bottom - this.Height); 
    base.OnLoad(e); 
} 
2
//Get screen resolution 
Rectangle res = Screen.PrimaryScreen.Bounds; 

// Calculate location (etc. 1366 Width - form size...) 
this.Location = new Point(res.Width - Size.Width, res.Height - Size.Height); 
0
int x = Screen.PrimaryScreen.WorkingArea.Right - this.Width; 
    int y = Screen.PrimaryScreen.WorkingArea.Bottom - this.Height; 

    // Add this for the real edge of the screen: 
    x = 0; // for Left Border or Get the screen Dimension to set it on the Right 

    this.Location = new Point(x, y); 
Problemi correlati