设为首页 加入收藏

TOP

C#操作移动其他程序窗口
2014-11-24 11:52:53 来源: 作者: 【 】 浏览:1
Tags:操作 移动 其他 程序 窗口

在做项目时候,曾经遇到一个问题,就是用C#的WinForm,来打开一个使用C++编写的软件,并控制打开窗体位置和大小。


在这里使用了Win32 API来做的。可以使用C#根据窗体的路径,启动一个进程,然后使用Win32 API控制打开窗口的位置和大小。


主要代码如下:


public class A


{


//调用Win32 API
[System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "MoveWindow")]
public static extern bool MoveWindow(System.IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);


//打开窗体方法,fileName是C++的窗体名称,包含路径


private void OpenAndSetWindow(String fileName)
{
Process p = new Process();//新建进程
p.StartInfo.FileName = fileName;//设置进程名字
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
p.Start();
MoveWindow(p.MainWindowHandle, 200, 300, 500, 400, true);


//p.MainWindowHandle是你要移动的窗口的句柄;200,300是移动后窗口左上角的横纵坐标;500,400是移动后窗口的宽度和高度;true表示移动后的窗口是需要重画


}


}


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇让 WordPress 使用 Redis 缓存来.. 下一篇利用shell脚本监控LXC应用容器内..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis on AWS:Elast (2025-12-27 04:19:30)
·在 Spring Boot 项目 (2025-12-27 04:19:27)
·使用华为开发者空间 (2025-12-27 04:19:24)
·Getting Started wit (2025-12-27 03:49:24)
·Ubuntu 上最好用的中 (2025-12-27 03:49:20)