【WindowsAPI之MoveWindow】 C#调整目标窗体的位置、大小
首先查看一下WindowsAPI给我们的解释
函数功能:该函数改变指定窗口的位置和尺寸。对于顶层窗口,位置和尺寸是相对于屏幕的左上角的:对于子窗口,位置和尺寸是相对于父窗口客户区的左上角坐标的。
函数原型:bool MoveWindow(HWND hWnd,int x,int y,int nWidth,int nHeight,bool BRePaint);
参数:
hWnd:窗口句柄。
x:指定窗口的新位置的左边界。
Y:指定窗口的新位置的顶部边界。
nWidth:指定窗口的新的宽度。
nHaight:指定窗口的新的高度。
所在位置:user32.dll
需要命名空间
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Runtime.InteropServices;
首先是获取目标窗体的句柄(什么是句柄?我不知道,但是百度知道 ^_^)
/// <summary>
/// 获取窗体句柄
/// </summary>
/// <param name="lpClassName"></param>
/// <param name="lpWindowName"></param>
/// <returns></returns>
[DllImport("User32.dll", EntryPoint = "FindWindow")]
public extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
/// 获取窗体句柄
/// </summary>
/// <param name="lpClassName"></param>
/// <param name="lpWindowName"></param>
/// <returns></returns>
[DllImport("User32.dll", EntryPoint = "FindWindow")]
public extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
然后就是我们的主角登场啦(MoveWindow)
/// <summary>
/// 设置目标窗体大小,位置
/// </summary>
/// <param name="hWnd">目标句柄</param>
/// <param name="x">目标窗体新位置X轴坐标</param>
/// <param name="y">目标窗体新位置Y轴坐标</param>
/// <param name="nWidth">目标窗体新宽度</param>
/// <param name="nHeight">目标窗体新高度</param>
/// <param name="BRePaint">是否刷新窗体</param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool BRePaint);
/// 设置目标窗体大小,位置
/// </summary>
/// <param name="hWnd">目标句柄</param>
/// <param name="x">目标窗体新位置X轴坐标</param>
/// <param name="y">目标窗体新位置Y轴坐标</param>
/// <param name="nWidth">目标窗体新宽度</param>
/// <param name="nHeight">目标窗体新高度</param>
/// <param name="BRePaint">是否刷新窗体</param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool BRePaint);
这里说一下,API里面第一个参数写的类型是HWND ,到C#里面就要用IntPtr 类表示,指针呢,据说是用委托实现(我也刚接触,没试过)
有了这两段代码,就已经完成一半了
设计窗体是这个样子滴。。
在按钮的Click事件里添加如下代码
null, "新编WIN32API大全");
int x = this.GetInt(txtP_x.Text.Trim(), 0);
int y = this.GetInt(txtP_y.Text.Trim(), 0);
int nWidth = this.GetInt(txtNWidth.Text.Trim(), 0);
int nHeight = this.GetInt(txtNHeight.Text.Trim(), 0);
//调用API
MoveWindow(intptr, x, y, nWidth, nHeight, true);
IntPtr intptr = FindWindow(int x = this.GetInt(txtP_x.Text.Trim(), 0);
int y = this.GetInt(txtP_y.Text.Trim(), 0);
int nWidth = this.GetInt(txtNWidth.Text.Trim(), 0);
int nHeight = this.GetInt(txtNHeight.Text.Trim(), 0);
//调用API
MoveWindow(intptr, x, y, nWidth, nHeight, true);
GetInt方法是简单处理一下非数字的字符串
/// <summary>
/// 将字string转成int类型
/// </summary>
/// <param name="value">要转换的字符串</param>
/// <param name="_default">默认值</param>
/// <returns></returns>
public int GetInt(string value, int _default)
{
if (int.TryParse(value, out _default))
return Convert.ToInt32(value);
else
return _default;
}
大功告成!! 运行一下试试吧。哦对了,忘了说了,改一下句柄名称再试哦~ 可以改成QQ2012之类的,我试过了,可以的。哈哈
分类:
WindowsAPI
标签:
MoveWindow
, C# 调整目标窗体位置大小
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?