windows 如何使用c#设置窗口的高度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6484567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set the height of a window using c#?
提问by Abs
Is it possible to set the height of a window using the window handle or process handle?
是否可以使用窗口句柄或进程句柄设置窗口的高度?
I have the following so far, assume the application in question is notepad.
到目前为止,我有以下内容,假设有问题的应用程序是记事本。
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;
}
}
Can I make use of handle
or p
to set the height of the window?
我可以使用handle
或p
设置窗口的高度吗?
回答by David Heffernan
This is how I would do it:
这就是我将如何做到的:
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);
}
}
}
}
While you can do it with SetWindowPos
, and SetWindowPos
is the newer and more capable API, MoveWindow
is just easier to call.
虽然您可以使用SetWindowPos
, 并且SetWindowPos
是更新且功能更强大的 API,但MoveWindow
更易于调用。
回答by BFree
You should be able to use the Win32 SetWindowPosfunction (use for both position and size). Here's a linkfor how to do it in C#.
您应该能够使用 Win32 SetWindowPos函数(用于位置和大小)。这是有关如何在 C# 中执行此操作的链接。
Here's a quick sample. This will move notepad to (10,10) on the screen, and resize it to (450,450):
这是一个快速示例。这会将记事本移动到屏幕上的 (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,
}