2009-09-01 8 views
23

Sto cercando di impostare un Windows Form sul monitor secondario, come segue:Visualizzazione di un modulo Windows su un monitor secondario?

private void button1_Click(object sender, EventArgs e) 
{ 
    MatrixView n = new MatrixView(); 
    Screen[] screens = Screen.AllScreens; 
    setFormLocation(n, screens[1]); 
    n.Show(); 
} 

private void setFormLocation(Form form, Screen screen) 
{ 
    // first method 
    Rectangle bounds = screen.Bounds; 
    form.SetBounds(bounds.X, bounds.Y, bounds.Width, bounds.Height); 

    // second method 
    //Point location = screen.Bounds.Location; 
    //Size size = screen.Bounds.Size; 

    //form.Left = location.X; 
    //form.Top = location.Y; 
    //form.Width = size.Width; 
    //form.Height = size.Height; 
} 

Le proprietà dei limiti sembrano corrette, ma in entrambi i metodi che ho provato, questo massimizza la forma sul monitor principale. Qualche idea?

+0

Solo per essere sicuro, il WindowState su MatrixView non è * Massimizzato *, vero? –

+0

@Austin No, WindowState è normale. –

risposta

21

Provare a impostare il parametro WindowStartUpLocation come "manuale" all'interno del metodo SetFormLocation.

+2

Sì, facendo form.StartPosition = FormStartPosition.Manual; ha fatto il trucco Qualche idea del perché? –

+1

@Henk No, è Windows Form. Ecco un link ad esso: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.startposition(VS.80).aspx –

+3

Da MSDN: "L'impostazione di WindowStartupLocation su Manual fa sì che una finestra venga posizionata in base ai valori di proprietà Left e Top. Se le proprietà Left o Top non sono specificate, i loro valori sono determinati da Windows." http://msdn.microsoft.com/en-us/library/system.windows.window.windowstartuplocation.aspx – Sesh

2

Sei sicuro che screens[1] è il tuo secondario? Dai una prova a screens[0]. Il tuo codice è fondamentalmente corretto.


Ok, ho controllato, si dovrà farlo dopo lo spettacolo():

n.Show(); 
setFormLocation(n, screens[1]); 

che dà un certo tremolio indesiderato. Ma probabilmente si può fare:

n.SetBounds(-100, -100, 10, 10); // or similar 
n.Show(); 
setFormLocation(n, screens[1]); 
+0

Relativamente sicuro - l'uso degli schermi [0] e degli schermi [1] dà lo stesso risultato. –

30
this.Location = Screen.AllScreens[1].WorkingArea.Location; 

questo è il riferimento modulo.

+1

(+1) Questa dovrebbe essere la risposta approvata. Questa linea di codice ha funzionato per me, ma gli altri no: D, continua il lavoro @Gengi – jkell311

+2

Tuttavia sembra che sia ordinato da alcuni altri criteri rispetto alla configurazione del monitor di Windows. Il mio primario, ad esempio, era in posizione [1] mentre il mio secondo monitor era a [0]. – cvsguimaraes

1

Ho usato questo per un'applicazione XNA 4 Dual Screen (Schermo intero XNA Game Finestra + WinForm)

Nel metodo Form_Load(), inserire il seguente codice:

var primaryDisplay = Screen.AllScreens.ElementAtOrDefault(0); 
var extendedDisplay = Screen.AllScreens.FirstOrDefault(s => s != primaryDisplay) ?? primaryDisplay; 

this.Left = extendedDisplay.WorkingArea.Left + (extendedDisplay.Bounds.Size.Width/2) - (this.Size.Width/2); 
this.Top = extendedDisplay.WorkingArea.Top + (extendedDisplay.Bounds.Size.Height/2) - (this.Size.Height/2); 
+0

Fantastico uomo fantastico ...! –

0

Set modulo di avvio Posizione struttura al Manuale

public void MoveWindowToProjector() 
    { 
      Rectangle rectMonitor; 

      // Create New Process 
      Process objProcess = new Process(); 

      //Get All the screens associated with this Monitor 
      Screen[] screens = Screen.AllScreens; 

      // Get Monitor Count 
      int iMonitorCount = Screen.AllScreens.Length; 

      // Get Parameters of Current Project 
      string[] parametros = Environment.GetCommandLineArgs(); 
      // if (parametros.Length > 0) 
      // { 
       //objProcess.StartInfo.FileName = parametros[0]; 
       // objProcess.Start(); 
      // } 
      // Get Window Handle of this Form 
      IntPtr hWnd = this.Handle; 

      Thread.Sleep(1000); 


      if (IsProjectorMode) 
      { 
       if (iMonitorCount > 1) // If monitor Count 2 or more 
       { 
        //Get the Dimension of the monitor 
        rectMonitor = Screen.AllScreens[1].WorkingArea; 
       } 
       else 
       { 
        //Get the Dimension of the monitor 
        rectMonitor = Screen.AllScreens[0].WorkingArea; 
       } 

      } 
      else 
      { 
       rectMonitor = Screen.AllScreens[0].WorkingArea; 

      } 
      if (hWnd != IntPtr.Zero) 
      { 
       SetWindowPos(hWnd, 0, 
        rectMonitor.Left, rectMonitor.Top, rectMonitor.Width, 
        rectMonitor.Height, SWP_SHOWWINDOW); 
      } 

     } 
2

per visualizzare forma sullo schermo secondario:

Screen primaryFormScreen = Screen.FromControl(primaryForm); 
    //Use this if you are looking for secondary screen that is not primary 
    Screen secondaryFormScreen = Screen.AllScreens.FirstOrDefault(s => !s.Primary) ?? primaryFormScreen; 
    //Use this if you are looking for screen that is not being used by specific form 
    Screen secondaryFormScreen = Screen.AllScreens.FirstOrDefault(s => !s.Equals(primaryFormScreen)) ?? primaryFormScreen; 
    //Putting the form on the other screen 
    secondaryForm.Left = secondaryFormScreen.Bounds.Width; 
    secondaryForm.Top = secondaryFormScreen.Bounds.Height; 
    //Recommended to use, You can change it back later to the settings you wish 
    secondaryForm.StartPosition = FormStartPosition.Manual; 
    secondaryForm.Location = secondaryFormScreen.Bounds.Location; 
    Point p = new Point(secondaryFormScreen.Bounds.Location.X, secondaryFormScreen.Bounds.Location.Y); 
    secondaryForm.Location = p; 
    secondaryForm.Show(); 

Se si prevede una schermata specifica, è possibile eseguire il loop su "Screen.AllScreens" e utilizzare la procedura precedente.

7

@ La risposta di Gengi è concisa e funziona bene. Se la finestra è ingrandita, non sposta la finestra. Questo frammento risolve che (anche se sospetto finestre dimensioni "normali" devono essere inferiori alle nuove dimensioni dello schermo per questo lavoro):

void showOnScreen(int screenNumber) 
    { 
     Screen[] screens = Screen.AllScreens; 

     if (screenNumber >= 0 && screenNumber < screens.Length) 
     { 
      bool maximised = false; 
      if (WindowState == FormWindowState.Maximized) 
      { 
       WindowState = FormWindowState.Normal; 
       maximised = true; 
      } 
      Location = screens[screenNumber].WorkingArea.Location; 
      if (maximised) 
      { 
       WindowState = FormWindowState.Maximized; 
      } 
     } 
    } 
-1
Screen[] screens = Screen.AllScreens; 
       sc aoc = new sc(); 
       aoc.Show(); 
    aoc.Location = Screen.AllScreens[INDEX OF YOUR AVAILABLE SCREENS TARGET].WorkingArea.Location; 

massimizzando FINESTRA STATO

aoc.WindowState = FormWindowState.Maximized; 

PER DANNI X, Y POSIZIONE

aoc.Location = new Point(TARGET X POSITION, TARGET Y POSITION); 
Problemi correlati