【问题标题】:How to set the height of a window using c#?如何使用 c# 设置窗口的高度?
【发布时间】:2014-08-29 13:35:01
【问题描述】:

是否可以使用窗口句柄或进程句柄来设置窗口的高度?

到目前为止,我有以下内容,假设有问题的应用程序是记事本。

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

}

我可以使用handlep来设置窗口的高度吗?

【问题讨论】:

  • 使用窗口句柄,pinvoke GetWindowRect得到rect,修改高度然后pinvoke MoveWindow。

标签: c# .net windows


【解决方案1】:

我会这样做:

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 是更新且功能更强大的 API,但 MoveWindow 更易于调用。

【讨论】:

  • 太棒了!在 Silverlight OOB 中工作(也可能在浏览器中提升)。当然它需要 FindWindow 来处理。
  • 我认为 Rect 结构是错误的。它应该是左、上、右、下
【解决方案2】:

您应该能够使用 Win32 SetWindowPos 函数(用于位置和大小)。这是一个link,了解如何在 C# 中执行此操作。

这是一个快速示例。这会将记事本移动到屏幕上的 (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,
}

【讨论】:

  • 太棒了!我想将窗口的宽度保留为原始宽度我该怎么做?
  • 调用 GetWindowRect。 MoveWindow 比 SetWindowPos 更容易使用。
  • @David - 我不熟悉,也许你可以添加一个例子?
  • @Abs:使用 GetWindowRect 函数获取 David 建议的大小,并使用其中的宽度。见这里:pinvoke.net/default.aspx/user32/getwindowrect.html
  • 你忘记了明确的 SpecialWindowHandles 到 int
猜你喜欢
  • 2015-01-15
  • 2013-01-08
  • 2021-01-20
  • 2015-02-26
  • 2012-01-18
  • 2017-05-11
  • 2018-03-08
  • 2011-08-21
  • 1970-01-01
相关资源
最近更新 更多