void SetFormPosSize() { if (IsFullscreen || mpv.VideoSize.Width == 0) { return; } var wa = Screen.GetWorkingArea(this); int h = (int)(wa.Height * 0.6); int w = (int)(h * mpv.VideoSize.Width / (float)mpv.VideoSize.Height); Point middlePos = new Point(Left + Width / 2, Top + Height / 2); var r = new Native.RECT(new Rectangle(0, 0, w, h)); NativeHelp.AddWindowBorders(Handle, ref r); int l = middlePos.X - r.Width / 2; int t = middlePos.Y - r.Height / 2; if (l < 0) { l = 0; } if (t < 0) { t = 0; } if (l + r.Width > wa.Width) { l = wa.Width - r.Width; } if (t + r.Height > wa.Height) { t = wa.Height - r.Height; } Native.SetWindowPos(Handle, IntPtr.Zero /* HWND_TOP */, l, t, r.Width, r.Height, 4 /* SWP_NOZORDER */); }
void SetFormPositionAndSizeKeepHeight() { if (IsFullscreen || mp.VideoSize.Width == 0) { return; } Screen screen = Screen.FromControl(this); int height = ClientSize.Height; int width = Convert.ToInt32(height * mp.VideoSize.Width / (double)mp.VideoSize.Height); Point middlePos = new Point(Left + Width / 2, Top + Height / 2); var rect = new Native.RECT(new Rectangle(screen.Bounds.X, screen.Bounds.Y, width, height)); NativeHelp.AddWindowBorders(Handle, ref rect); int left = middlePos.X - rect.Width / 2; int top = middlePos.Y - rect.Height / 2; Screen[] screens = Screen.AllScreens; if (left < screens[0].Bounds.Left) { left = screens[0].Bounds.Left; } int maxLeft = screens[0].Bounds.Left + screens.Select((sc) => sc.Bounds.Width).Sum() - rect.Width - SystemInformation.CaptionHeight; if (left > maxLeft) { left = maxLeft; } Native.SetWindowPos(Handle, IntPtr.Zero /* HWND_TOP */, left, top, rect.Width, rect.Height, 4 /* SWP_NOZORDER */); }
public static void SubtractWindowBorders(IntPtr hwnd, ref Native.RECT rc) { var b = new Native.RECT(0, 0, 0, 0); AddWindowBorders(hwnd, ref b); rc.Left -= b.Left; rc.Top -= b.Top; rc.Right -= b.Right; rc.Bottom -= b.Bottom; }
static void subtract_window_borders(IntPtr hwnd, ref Native.RECT rc) { var b = new Native.RECT(0, 0, 0, 0); add_window_borders(hwnd, ref b); rc.Left -= b.Left; rc.Top -= b.Top; rc.Right -= b.Right; rc.Bottom -= b.Bottom; }
void SetStartFormPositionAndSize() { if (IsFullscreen || mp.VideoSize.Width == 0) { return; } Screen screen = Screen.FromControl(this); int height = Convert.ToInt32(screen.Bounds.Height * 0.6); int width = Convert.ToInt32(height * mp.VideoSize.Width / (double)mp.VideoSize.Height); Point middlePos = new Point(Left + Width / 2, Top + Height / 2); var rect = new Native.RECT(new Rectangle(screen.Bounds.X, screen.Bounds.Y, width, height)); NativeHelp.AddWindowBorders(Handle, ref rect); int left = middlePos.X - rect.Width / 2; int top = middlePos.Y - rect.Height / 2; Native.SetWindowPos(Handle, IntPtr.Zero /* HWND_TOP */, left, top, rect.Width, rect.Height, 4 /* SWP_NOZORDER */); }
void SetFormPosAndSize() { if (WindowState == FormWindowState.Maximized) { return; } if (mp.Fullscreen) { CycleFullscreen(true); return; } Screen screen = Screen.FromControl(this); int autoFitHeight = Convert.ToInt32(screen.WorkingArea.Height * mp.Autofit); if (mp.VideoSize.Height == 0 || mp.VideoSize.Width == 0 || mp.VideoSize.Width / (float)mp.VideoSize.Height < App.MinimumAspectRatio) { mp.VideoSize = new Size((int)(autoFitHeight * (16 / 9.0)), autoFitHeight); } Size size = mp.VideoSize; int height = size.Height; if (App.RememberHeight) { if (WasInitialSizeSet) { height = ClientSize.Height; } else { height = autoFitHeight; WasInitialSizeSet = true; } } int width = Convert.ToInt32(height * size.Width / (double)size.Height); if (height > screen.WorkingArea.Height * 0.9) { height = Convert.ToInt32(screen.WorkingArea.Height * 0.9); width = Convert.ToInt32(height * size.Width / (double)size.Height); } if (width > screen.WorkingArea.Width * 0.9) { width = Convert.ToInt32(screen.WorkingArea.Width * 0.9); height = Convert.ToInt32(width * size.Height / (double)size.Width); } if (height < screen.WorkingArea.Height * mp.AutofitSmaller) { height = Convert.ToInt32(screen.WorkingArea.Height * mp.AutofitSmaller); width = Convert.ToInt32(height * size.Width / (double)size.Height); } if (height > screen.WorkingArea.Height * mp.AutofitLarger) { height = Convert.ToInt32(screen.WorkingArea.Height * mp.AutofitLarger); width = Convert.ToInt32(height * size.Width / (double)size.Height); } Point middlePos = new Point(Left + Width / 2, Top + Height / 2); var rect = new Native.RECT(new Rectangle(screen.Bounds.X, screen.Bounds.Y, width, height)); NativeHelp.AddWindowBorders(Handle, ref rect); int left = middlePos.X - rect.Width / 2; int top = middlePos.Y - rect.Height / 2; Screen[] screens = Screen.AllScreens; int minLeft = screens.Select(val => val.WorkingArea.X).Min(); int maxRight = screens.Select(val => val.WorkingArea.Right).Max(); int minTop = screens.Select(val => val.WorkingArea.Y).Min(); int maxBottom = screens.Select(val => val.WorkingArea.Bottom).Max(); if (left < minLeft) { left = minLeft; } if (left + rect.Width > maxRight) { left = maxRight - rect.Width; } if (top < minTop) { top = minTop; } if (top + rect.Height > maxBottom) { top = maxBottom - rect.Height; } Native.SetWindowPos(Handle, IntPtr.Zero /* HWND_TOP */, left, top, rect.Width, rect.Height, 4 /* SWP_NOZORDER */); }
public static void AddWindowBorders(IntPtr hwnd, ref Native.RECT rc) { Native.AdjustWindowRect(ref rc, (uint)Native.GetWindowLongPtrW(hwnd, -16 /* GWL_STYLE */), false); }
protected override void WndProc(ref Message m) { //Debug.WriteLine(m); switch (m.Msg) { case 0x0201: // WM_LBUTTONDOWN case 0x0202: // WM_LBUTTONUP case 0x0207: // WM_MBUTTONDOWN case 0x0208: // WM_MBUTTONUP case 0x020b: // WM_XBUTTONDOWN case 0x020c: // WM_XBUTTONUP case 0x020A: // WM_MOUSEWHEEL case 0x0100: // WM_KEYDOWN case 0x0101: // WM_KEYUP case 0x0104: // WM_SYSKEYDOWN case 0x0105: // WM_SYSKEYUP case 0x319: // WM_APPCOMMAND if (mp.WindowHandle != IntPtr.Zero) { Native.SendMessage(mp.WindowHandle, m.Msg, m.WParam, m.LParam); } break; case 0x0200: // WM_MOUSEMOVE { if ((DateTime.Now - LastCycleFullscreen).TotalMilliseconds > 500) { Point pos = PointToClient(Cursor.Position); mp.command($"mouse {pos.X} {pos.Y}"); } if (CursorHelp.IsPosDifferent(LastCursorPosChanged)) { CursorHelp.Show(); } } break; case 0x2a3: // WM_MOUSELEAVE // osc won't always auto hide mp.command("mouse 1 1"); break; case 0x203: // WM_LBUTTONDBLCLK { Point pos = PointToClient(Cursor.Position); mp.command($"mouse {pos.X} {pos.Y} 0 double"); } break; case 0x02E0: // WM_DPICHANGED { if (!WasShown) { break; } Native.RECT rect = Marshal.PtrToStructure <Native.RECT>(m.LParam); Native.SetWindowPos(Handle, IntPtr.Zero, rect.Left, rect.Top, rect.Width, rect.Height, 0); } break; case 0x0214: // WM_SIZING { var rc = Marshal.PtrToStructure <Native.RECT>(m.LParam); var r = rc; NativeHelp.SubtractWindowBorders(Handle, ref r); int c_w = r.Right - r.Left, c_h = r.Bottom - r.Top; Size s = mp.VideoSize; if (s == Size.Empty) { s = new Size(16, 9); } float aspect = s.Width / (float)s.Height; int d_w = Convert.ToInt32(c_h * aspect - c_w); int d_h = Convert.ToInt32(c_w / aspect - c_h); int[] d_corners = { d_w, d_h, -d_w, -d_h }; int[] corners = { rc.Left, rc.Top, rc.Right, rc.Bottom }; int corner = NativeHelp.GetResizeBorder(m.WParam.ToInt32()); if (corner >= 0) { corners[corner] -= d_corners[corner]; } Marshal.StructureToPtr <Native.RECT>(new Native.RECT(corners[0], corners[1], corners[2], corners[3]), m.LParam, false); m.Result = new IntPtr(1); } return; case 0x004A: // WM_COPYDATA { var copyData = (Native.COPYDATASTRUCT)m.GetLParam(typeof(Native.COPYDATASTRUCT)); string[] files = copyData.lpData.Split('\n'); string mode = files[0]; files = files.Skip(1).ToArray(); switch (mode) { case "single": mp.Load(files, true, Control.ModifierKeys.HasFlag(Keys.Control)); break; case "queue": foreach (string file in files) { mp.commandv("loadfile", file, "append"); } break; } Activate(); } return; } if (m.Msg == TaskbarButtonCreatedMessage && mp.TaskbarProgress) { Taskbar = new Taskbar(Handle); ProgressTimer.Start(); } base.WndProc(ref m); }