2011-06-26 12 views
12

क्या विंडो हैंडल या प्रोसेस हैंडल का उपयोग कर विंडो की ऊंचाई सेट करना संभव है?सी # का उपयोग कर खिड़की की ऊंचाई कैसे सेट करें?

मेरे पास अब तक निम्न है, मान लीजिए कि प्रश्न में आवेदन नोटपैड है।

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; 
    } 

} 

मैं खिड़की की ऊंचाई निर्धारित करने के लिए handle या p के उपयोग कर सकते हैं?

+2

विंडो हैंडल का उपयोग करके, पिनवोक GetWindowRect को प्राप्त करने के लिए, ऊंचाई संशोधित करें और फिर MoveWindow को पिनवोक करें। –

उत्तर

12

यह कैसे मैं यह कर देगी:

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); 
      } 
     } 
    } 
} 

आप के साथ ऐसा कर सकते हैं SetWindowPos, और SetWindowPos नया और अधिक सक्षम एपीआई है, MoveWindow कॉल करना बस आसान है।

+0

उत्कृष्ट भूल गए हैं! सिल्वरलाइट ओओबी में काम करता है (और संभवतः ब्राउज़र में भी उन्नत)। बेशक इसे हैंडल प्राप्त करने के लिए FindWindow की आवश्यकता होगी। –

+0

मुझे लगता है कि रेक्ट संरचना गलत है। यह बाएं, शीर्ष, दाएं, नीचे – atomaras

+0

@atom धन्यवाद होना चाहिए, आप सही हैं, मैंने संपादित किया –

7

आप Win32 SetWindowPos फ़ंक्शन (स्थिति और आकार दोनों के लिए उपयोग) का उपयोग करने में सक्षम होना चाहिए। C# में इसे कैसे करें के लिए link यहां दिया गया है।

यहां एक त्वरित नमूना है। इस स्क्रीन पर करने के लिए (10,10) नोटपैड को बदल देगा और करने के लिए इसे दोबारा आकार (450,450):

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

यह कमाल है! मैं खिड़की की चौड़ाई को मूल चौड़ाई के रूप में छोड़ना चाहता हूं मैं यह कैसे कर सकता हूं? – Abs

+1

कॉल GetWindowRect पर कॉल करें। SetWindowPos की तुलना में MoveWindow का उपयोग करना आसान है। –

+0

@ डेविड - मैं उससे परिचित नहीं हूं, शायद आप एक उदाहरण जोड़ सकते हैं? – Abs

संबंधित मुद्दे