微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用C#设置窗口的高度?

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

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

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来设置窗口的高度吗?

在VB.Net中从二进制文件提取string

从cmd分析.NET应用程序:对于x64,缺lessvsperf.exe

PowerShell 3:每个命令执行结果在“types初始值设定项抛出一个exception”错误

如何从Web服务器打印格式化的文本并确认打印成功?

WM_DESTROY,WM_CLOSE绕过IMessageFilter

AnonymousPipeServerStream.Read()偶尔挂起在客户端退出

Environment.UserName为同一用户(套pipe)提供不同的结果:替代/转换?

什么types的内存在32位Windows上有2 GB的限制?

如何通过Windows应用程序或Web应用程序调用代码来识别代码

如何在用户login之前运行Windows应用程序?

这是我将如何做到这一点:

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只是更容易调用

您应该能够使用Win32 SetwindowPos函数(用于位置和大小)。 这里有一个如何在C#中完成的链接

这是一个快速示例。 这将移动记事本到(10,10)在屏幕上,并调整到(450,450):

class Program { [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool SetwindowPos(IntPtr hWnd,IntPtr hWndInsertAfter,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,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,}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐