利用c# 多屏显示

公司搞了一个电视墙,要显示不同内容,于是买了一个多接口显卡(现在看来这个方案不是太好,但非常省钱)

要打开的就是几个网页,但要自己手工拖到不同电视上,非常麻烦

于是查了一下资料,发现可以用代码实现,说几个关键点,由于是用chrome打开的,需要多窗口打开,而不是一个窗口多个tab

这个蛮难搞的,最主要是chrome这个程序通过进程的MainWindowHandle 取不到,而且启动多次时,仍然指向的是同一个进程,有点麻烦,只能另外写程序了

关键代码如下:

        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#里竟然只是简单的坐标问题,这个有点出我意料之外

            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 Playerwmplayer.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);                    


                }

            }

弄了一天,总算弄好了

原文地址:https://www.cnblogs.com/szyicol/p/10895404.html