您的位置:首页 > 编程语言 > C#

C#操作移动其他程序窗口

2012-07-06 10:09 281 查看

在做项目时候,曾经遇到一个问题,就是用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表示移动后的窗口是需要重画

}

}

如果打开IE网页,可以成下面语句一句

p.StartInfo.FileName = "iexplore";
p.StartInfo.Arguments = "www.baidu.com";//网页
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: