winform打开浏览器,并定位浏览器显示位置

打开浏览器直接使用,process.start就能打开,重新定位浏览器显示的位置需要用到windows api中的MoveWindow接口;

string url = "www.baidu.com";
//Process.Start("iexplore.exe", url);
ProcessStartInfo ps = new ProcessStartInfo();
ps.FileName = "chrome.exe";
ps.Arguments = string.Format("--new-window \"{0}\"", url);//一直重新打开浏览器
ps.WindowStyle = ProcessWindowStyle.Minimized;
//ps.Arguments = url;
p = Process.Start(ps);
System.Threading.Thread.Sleep(1000);//这里休眠是看其他博客上这么写的,说是为了等待页面加载完成
MoveWindow(p.MainWindowHandle, -200, -200, 600, 400, false);

 

但目前这个方法,还有问题

1、ps.WindowStyle = ProcessWindowStyle.Minimized;这句话不起作用,如果上次打开的浏览器时最大化,用这个方法打开还会是最大化,这段代码全完无用,最大化的话,定位效果就没用了;

2、如果已经打开的有浏览器,有时候会报错,说进程已退出;

以上两个问题,没具体研究,本来这个是个新需求要做的,刚研究就又不做了,没再研究下去,这里简单写下研究的结果,后续有时间再研究吧,如果哪位研究出了结果,望留言说明,感谢

 

windows api:

[DllImportAttribute("user32.dll", EntryPoint = "MoveWindow")]
public static extern bool MoveWindow(System.IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

posted @ 2022-02-28 17:42  卡萨丁·周  阅读(646)  评论(0编辑  收藏  举报