是的,看起来有一种方法——至少在 C# 中。您指定了 .NET,所以我也尝试在 VB 中执行此操作,但无法使其工作。以下代码中最重要的部分是MoveWindow(IntPtr hwnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
参数有:句柄、窗口 x 位置、窗口 y 位置、窗口宽度、窗口高度和一个布尔值,表示是否要重新绘制窗口。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.Title = "Test";
IntPtr handle = FindWindowByCaption(IntPtr.Zero, Console.Title);
MoveWindow(handle, 100, 300, 200, 200, true);
Console.WriteLine("Hi");
RECT thisRect = new RECT();
GetWindowRect(handle, ref thisRect);
Console.WriteLine(thisRect.left);
Console.WriteLine(thisRect.top);
Console.ReadKey();
return;
}
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
public static extern IntPtr FindWindowByCaption(IntPtr zeroOnly, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hwnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
public static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
}
}