分享

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

 simplelam 2014-11-18

在做项目时候,曾经遇到一个问题,就是用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";//网页

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多