公司搞了一個電視牆,要顯示不一樣內容,因而買了一個多接口顯卡(如今看來這個方案不是太好,但很是省錢)chrome
要打開的就是幾個網頁,但要本身手工拖到不一樣電視上,很是麻煩c#
因而查了一下資料,發現能夠用代碼實現,說幾個關鍵點,因爲是用chrome打開的,須要多窗口打開,而不是一個窗口多個tabui
這個蠻難搞的,最主要是chrome這個程序經過進程的MainWindowHandle 取不到,並且啓動屢次時,仍然指向的是同一個進程,有點麻煩,只能另外寫程序了this
關鍵代碼以下:spa
public static List<IntPtr> GetMainWindowHandle(int processId) { IntPtr MainWindowHandle = IntPtr.Zero; List<IntPtr> hs = new List<IntPtr>(); NativeMethods.EnumWindows(new NativeMethods.EnumWindowsProc((hWnd, lParam) => { IntPtr PID; NativeMethods.GetWindowThreadProcessId(hWnd, out PID); if (PID == lParam && NativeMethods.IsWindowVisible(hWnd) && NativeMethods.GetWindow(hWnd, NativeMethods.GW_OWNER) == IntPtr.Zero) { MainWindowHandle = hWnd; hs.Add(MainWindowHandle); //return false; } return true; }), new IntPtr(processId)); return hs; }
另外多屏的問題,在c#里居然只是簡單的座標問題,這個有點出我意料以外code
foreach (var screen in System.Windows.Forms.Screen.AllScreens) { if (screen.Primary) { this.Location = screen.WorkingArea.Location; } if (screen.DeviceName == @"\\.\DISPLAY7") { Rectangle rc = screen.WorkingArea; Process proc = Process.Start(@"C:\Program Files (x86)\Windows Media Player\wmplayer.exe", @"D:\g.mp4"); Thread.Sleep(3000); Console.WriteLine(proc.MainWindowHandle.ToString()); SetWindowPos(proc.MainWindowHandle, IntPtr.Zero, rc.X, rc.Y, 0, 0, (uint)1); SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND2, new IntPtr(SC_MAXIMIZE2), IntPtr.Zero); } }
弄了一天,總算弄好了orm