2011-06-26 16 views
12

¿Es posible configurar la altura de una ventana usando el identificador de ventana o el identificador de proceso?Cómo establecer la altura de una ventana usando C#?

Tengo hasta ahora, supongo que la aplicación en cuestión es bloc de notas.

Process[] processes = Process.GetProcessesByName("notepad"); 

foreach (Process p in processes) 
{ 

    if (p.MainWindowTitle == title) 
    { 

     handle = p.MainWindowHandle; 

     while ((handle = p.MainWindowHandle) == IntPtr.Zero) 
     { 
      Thread.Sleep(1000); 
      p.Refresh(); 
     } 

     break; 
    } 

} 

¿Puedo hacer uso de handle o p para ajustar la altura de la ventana?

+2

Usando el identificador de ventana, pinvoke GetWindowRect para obtener rect, modifique la altura y luego pinvoke MoveWindow. –

Respuesta

12

Ésta es la forma en que lo haría:

using System; 
using System.Diagnostics; 
using System.Runtime.InteropServices; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     [StructLayout(LayoutKind.Sequential)] 
     public struct RECT 
     { 
      public int left; 
      public int top; 
      public int right; 
      public int bottom; 
     } 

     [DllImport("user32.dll", SetLastError = true)] 
     static extern bool GetWindowRect(IntPtr hWnd, ref RECT Rect); 

     [DllImport("user32.dll", SetLastError = true)] 
     static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int Width, int Height, bool Repaint); 

     static void Main(string[] args) 
     { 
      Process[] processes = Process.GetProcessesByName("notepad"); 
      foreach (Process p in processes) 
      { 
       IntPtr handle = p.MainWindowHandle; 
       RECT Rect = new RECT(); 
       if (GetWindowRect(handle, ref Rect)) 
        MoveWindow(handle, Rect.left, Rect.right, Rect.right-Rect.left, Rect.bottom-Rect.top + 50, true); 
      } 
     } 
    } 
} 

Mientras que usted puede hacerlo con SetWindowPos y SetWindowPos es la API más nueva y más capaz, MoveWindow es simplemente más fácil de llamar.

+0

¡Excelente! Funciona en Silverlight OOB (y probablemente también elevado en el navegador). Por supuesto, se necesitará FindWindow para obtener el control. –

+0

Creo que la estructura Rect es incorrecta. Debería ser Izquierda, Arriba, Derecha, Abajo – atomaras

+0

@atom gracias, tienes razón, he editado –

7

Debería poder usar la función Win32 SetWindowPos (usar tanto para posición como para tamaño). Aquí hay un link para saber cómo hacerlo en C#.

Aquí hay una muestra rápida. Esto moverá el bloc de notas de (10,10) en la pantalla, y cambiar su tamaño a (450450):

class Program 
{ 
    [DllImport("user32.dll")] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, SetWindowPosFlags uFlags); 

    static void Main(string[] args) 
    { 
     Console.WriteLine("Start notepad and hit any key..."); 
     Console.ReadKey(true); 
     Process[] processes = Process.GetProcessesByName("notepad"); 

     foreach (Process p in processes) 
     { 
      var handle = p.MainWindowHandle; 

      SetWindowPos(handle, new IntPtr(SpecialWindowHandles.HWND_TOP), 10,10,450,450,SetWindowPosFlags.SWP_SHOWWINDOW); 

      break; 

     } 

    } 
} 

public enum SpecialWindowHandles 
{ 
    HWND_TOP = 0, 
    HWND_BOTTOM = 1, 
    HWND_TOPMOST = -1, 
    HWND_NOTOPMOST = -2 
} 

[Flags] 
public enum SetWindowPosFlags : uint 
{ 
    SWP_ASYNCWINDOWPOS = 0x4000, 

    SWP_DEFERERASE = 0x2000, 

    SWP_DRAWFRAME = 0x0020, 

    SWP_FRAMECHANGED = 0x0020, 

    SWP_HIDEWINDOW = 0x0080, 

    SWP_NOACTIVATE = 0x0010, 

    SWP_NOCOPYBITS = 0x0100, 

    SWP_NOMOVE = 0x0002, 

    SWP_NOOWNERZORDER = 0x0200, 

    SWP_NOREDRAW = 0x0008, 

    SWP_NOREPOSITION = 0x0200, 

    SWP_NOSENDCHANGING = 0x0400, 

    SWP_NOSIZE = 0x0001, 

    SWP_NOZORDER = 0x0004, 

    SWP_SHOWWINDOW = 0x0040, 
} 
+0

¡Eso es asombroso! Me gustaría dejar el ancho de la ventana como el ancho original, ¿cómo puedo hacer esto? – Abs

+1

Llamar a GetWindowRect. MoveWindow es más fácil de usar que SetWindowPos. –

+0

@David - No estoy familiarizado con eso, ¿quizás puedes agregar un ejemplo? – Abs

Cuestiones relacionadas