C# winfrom实现微信(其他应用)自动跟随C#窗口,拖动页面自动恢复到固定位置

csharp 复制代码
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;
        // WinAPI 函数声明
        [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}");

            // 注意:在OnLoad事件中,如果窗体还未完全显示,
            // 那么在某些情况下,Size属性可能不是最终的显示大小。
            // 如果您需要确保获取到最终的显示大小,
            // 可以考虑在窗体的Shown事件中进行这些操作。
        }
    }
}
相关推荐
APItesterCris3 分钟前
TypeScript 与淘宝 API:构建类型安全的商品数据查询前端 / Node.js 服务
开发语言·php
ftpeak7 分钟前
《Cargo 参考手册》第二十一章:Cargo 包命令
开发语言·rust
陈一Tender13 分钟前
JavaWeb后端实战(登录认证 & 令牌技术 & 拦截器 & 过滤器)
java·开发语言·spring boot·mysql
Camel卡蒙13 分钟前
红黑树详细介绍(五大规则、保持平衡操作、Java实现)
java·开发语言·算法
jerryinwuhan17 分钟前
机器人模拟器(python)
开发语言·python·机器人
孤廖39 分钟前
吃透 C++ 栈和队列:stack/queue/priority_queue 用法 + 模拟 + STL 标准实现对比
java·开发语言·数据结构·c++·人工智能·深度学习·算法
嵌入式学习和实践1 小时前
C# WinForms 多窗口交互通信的示例-主窗口子窗口交互通信
c#·交互·主窗口-子窗口通信
驰羽1 小时前
[GO]GORM中的Tag映射规则
开发语言·golang
专注VB编程开发20年1 小时前
C#,VB.NET数组去重复,提取键名和重复键和非重复键
c#·.net·linq·取唯一键·去重复·重复数量
YuanlongWang1 小时前
Entity Framework Core和SqlSugar的区别,详细介绍
c#