using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WinFormsApp4
{
public partial class Form1 : Form
{
private static System.Threading.Timer _timer;
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
private const int GWL_EXSTYLE = -20;
private const uint SWP_NOZORDER = 0x0004;
private const uint SWP_NOACTIVATE = 0x0010;
private const int WS_EX_TOPMOST = 0x00000008;
private const int WS_EX_NOTOPMOST = 0x00000004;
private const int SWP_NOMOVE = 0x0002;
private const int SWP_NOSIZE = 0x0001;
private IntPtr _targetWindowHandle;
public Form1()
{
InitializeComponent();
this.Resize += MainForm_Resize;
this.Move += MainForm_Move;
_timer = new System.Threading.Timer(TimerCallback, null, 0, 500);
}
private void TimerCallback(Object o)
{
string className = "WeChatMainWndForPC";
string windowName = "微信";
_targetWindowHandle = FindWindow(className, windowName);
if (_targetWindowHandle == IntPtr.Zero)
{
MessageBox.Show("未找到目标窗口!");
return;
}
int x = 50;
int y = 50;
int width = 1920;
int height = 1000;
Size clientSize = this.ClientSize;
Console.WriteLine($"内容区域宽度: {clientSize.Width}, 内容区域高度: {clientSize.Height}");
Size formSize = this.Size;
Console.WriteLine($"窗体宽度: {formSize.Width}, 窗体高度: {formSize.Height}");
if(clientSize.Width != width || clientSize.Height != height)
{
SetWindowPos(_targetWindowHandle, new IntPtr(-1), this.Location.X + 100, this.Location.Y + 100, (int)(clientSize.Width * 0.8), (int)(clientSize.Height * 0.8), SWP_NOZORDER | SWP_NOACTIVATE);
}
}
private void MainForm_Resize(object sender, EventArgs e)
{
Size clientSize = this.ClientSize;
Console.WriteLine($"内容区域宽度: {clientSize.Width}, 内容区域高度: {clientSize.Height}");
Size formSize = this.Size;
Console.WriteLine($"窗体宽度: {formSize.Width}, 窗体高度: {formSize.Height}");
if (_targetWindowHandle != IntPtr.Zero)
{
int width = this.Width;
int height = this.Height;
SetWindowPos(_targetWindowHandle, new IntPtr(-1), this.Location.X + 100, this.Location.Y +100, (int)(clientSize.Width * 0.8), (int)(clientSize.Height * 0.8), SWP_NOZORDER | SWP_NOACTIVATE);
}
}
private void MainForm_Move(object sender, EventArgs e)
{
Size clientSize = this.ClientSize;
Console.WriteLine($"内容区域宽度: {clientSize.Width}, 内容区域高度: {clientSize.Height}");
Size formSize = this.Size;
Console.WriteLine($"窗体宽度: {formSize.Width}, 窗体高度: {formSize.Height}");
if (_targetWindowHandle != IntPtr.Zero)
{
SetWindowPos(_targetWindowHandle, new IntPtr(-1), this.Location.X + 100, this.Location.Y + 100, (int)(clientSize.Width * 0.8), (int)(clientSize.Height * 0.8), SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
Size clientSize = this.ClientSize;
Console.WriteLine($"内容区域宽度: {clientSize.Width}, 内容区域高度: {clientSize.Height}");
Size formSize = this.Size;
Console.WriteLine($"窗体宽度: {formSize.Width}, 窗体高度: {formSize.Height}");
}
}
}