2013-03-03 14 views
9

Używam programu C# WinForm do opracowania aplikacji do powiadamiania smoga. Chciałbym umieścić główny formularz w prawym dolnym rogu obszaru roboczego ekranu. W przypadku wielu ekranów istnieje sposób, aby znaleźć ekran z prawej strony, gdzie umieścić aplikację, lub przynajmniej zapamiętać ostatnio używany ekran i umieścić formularz w prawym dolnym rogu?Położenie formularza w prawym dolnym rogu ekranu

Odpowiedz

21

I obecnie nie mają wielu wyświetlaczy, aby sprawdzić, ale powinno to być coś podobnego

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

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

+0

@GertArnold Wiem, że MoreLINQ ma 'MaxBy 'być najbardziej efektywnym, ale jeśli muszę zaryzykować odgadnięcie' OrderByDescending.First' powinno być bardziej wydajne niż 'OrderBy.Last'. – nawfal

8

Przestawianie Formularza Onload i ustaw nową lokalizację:

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); 
Powiązane problemy