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 */); }
protected override void WndProc(ref Message m) { switch (m.Msg) { case 0x0201: // WM_LBUTTONDOWN case 0x0202: // WM_LBUTTONUP case 0x0100: // WM_KEYDOWN case 0x0101: // WM_KEYUP case 0x020A: // WM_MOUSEWHEEL if (mpv.MpvWindowHandle != IntPtr.Zero) { Native.SendMessage(mpv.MpvWindowHandle, m.Msg, m.WParam, m.LParam); } break; case 0x203: // Native.WM.LBUTTONDBLCLK if (!IsMouseInOSC()) { mpv.CommandString("cycle fullscreen"); } 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; float aspect = mpv.VideoSize.Width / (float)mpv.VideoSize.Height; int d_w = (int)(c_h * aspect - c_w); int d_h = (int)(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; } base.WndProc(ref m); }
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 */); }
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 { Point pos = PointToClient(Cursor.Position); mp.command($"mouse {pos.X} {pos.Y}"); if (CursorHelp.IsPosDifferent(LastCursorPosChanged)) { CursorHelp.Show(); } } break; case 0x2a3: // WM_MOUSELEAVE mp.command("mouse 1 1"); // osc won't always auto hide break; case 0x203: // Native.WM.LBUTTONDBLCLK { Point pos = PointToClient(Cursor.Position); mp.command($"mouse {pos.X} {pos.Y} 0 double"); } break; case 0x02E0: // WM_DPICHANGED if (!WasShown) { break; } var r2 = Marshal.PtrToStructure <Native.RECT>(m.LParam); Native.SetWindowPos(Handle, IntPtr.Zero, r2.Left, r2.Top, r2.Width, r2.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); }
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 */); }
protected override void WndProc(ref Message m) { //Debug.WriteLine(m); switch (m.Msg) { case 0x201: // WM_LBUTTONDOWN case 0x202: // WM_LBUTTONUP case 0x207: // WM_MBUTTONDOWN case 0x208: // WM_MBUTTONUP case 0x20b: // WM_XBUTTONDOWN case 0x20c: // WM_XBUTTONUP case 0x20A: // WM_MOUSEWHEEL case 0x100: // WM_KEYDOWN case 0x101: // WM_KEYUP case 0x104: // WM_SYSKEYDOWN case 0x105: // WM_SYSKEYUP { bool skip = m.Msg == 0x100 && LastAppCommand != 0 && (Environment.TickCount - LastAppCommand) < 1000; if (core.WindowHandle != IntPtr.Zero && !skip) { m.Result = WinAPI.SendMessage(core.WindowHandle, m.Msg, m.WParam, m.LParam); } } break; case 0x319: // WM_APPCOMMAND { string value = mpvHelp.WM_APPCOMMAND_to_mpv_key((int)(m.LParam.ToInt64() >> 16 & ~0xf000)); if (value != null) { core.command("keypress " + value); m.Result = new IntPtr(1); LastAppCommand = Environment.TickCount; return; } } break; case 0x0200: // WM_MOUSEMOVE if (Environment.TickCount - LastCycleFullscreen > 500) { Point pos = PointToClient(Cursor.Position); core.command($"mouse {pos.X} {pos.Y}"); } if (CursorHelp.IsPosDifferent(LastCursorPosition)) { CursorHelp.Show(); } break; case 0x2a3: // WM_MOUSELEAVE //osc won't auto hide after mouse left window in borderless mode core.command($"mouse {ClientSize.Width / 2} {ClientSize.Height / 3}"); break; case 0x203: // WM_LBUTTONDBLCLK { Point pos = PointToClient(Cursor.Position); core.command($"mouse {pos.X} {pos.Y} 0 double"); } break; case 0x02E0: // WM_DPICHANGED { if (!WasShown()) { break; } WinAPI.RECT rect = Marshal.PtrToStructure <WinAPI.RECT>(m.LParam); WinAPI.SetWindowPos(Handle, IntPtr.Zero, rect.Left, rect.Top, rect.Width, rect.Height, 0); } break; case 0x0214: // WM_SIZING { var rc = Marshal.PtrToStructure <WinAPI.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 = core.VideoSize; if (s == Size.Empty) { s = new Size(16, 9); } float aspect = s.Width / (float)s.Height; int d_w = (int)(c_h * aspect - c_w); int d_h = (int)(c_w / aspect - c_h); Debug.WriteLine(d_w); Debug.WriteLine(d_h); int d_w2 = (int)(c_h * aspect - c_w); int d_h2 = (int)(c_w / aspect - c_h); Debug.WriteLine(d_w2); Debug.WriteLine(d_h2); 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 <WinAPI.RECT>(new WinAPI.RECT(corners[0], corners[1], corners[2], corners[3]), m.LParam, false); m.Result = new IntPtr(1); } return; case 0x004A: // WM_COPYDATA { var copyData = (WinAPI.COPYDATASTRUCT)m.GetLParam(typeof(WinAPI.COPYDATASTRUCT)); string[] files = copyData.lpData.Split('\n'); string mode = files[0]; files = files.Skip(1).ToArray(); switch (mode) { case "single": core.LoadFiles(files, true, Control.ModifierKeys.HasFlag(Keys.Control)); break; case "queue": foreach (string file in files) { core.commandv("loadfile", file, "append"); } break; } Activate(); } return; } if (m.Msg == TaskbarButtonCreatedMessage && core.TaskbarProgress) { Taskbar = new Taskbar(Handle); ProgressTimer.Start(); } // beep sound when closed using taskbar due to exception if (!IsDisposed) { base.WndProc(ref m); } }
void SetFormPosAndSize(double scale = 1, bool force = false) { if (!force) { if (WindowState != FormWindowState.Normal) { return; } if (core.Fullscreen) { CycleFullscreen(true); return; } } Screen screen = Screen.FromControl(this); int autoFitHeight = Convert.ToInt32(screen.WorkingArea.Height * core.Autofit); if (core.VideoSize.Height == 0 || core.VideoSize.Width == 0 || core.VideoSize.Width / (float)core.VideoSize.Height < App.MinimumAspectRatio) { core.VideoSize = new Size((int)(autoFitHeight * (16 / 9f)), autoFitHeight); } Size videoSize = core.VideoSize; int height = videoSize.Height; if (core.WasInitialSizeSet || scale != 1) { height = ClientSize.Height; } else { int savedHeight = RegistryHelp.GetInt(App.RegPath, "Height"); if (App.StartSize == "always" && savedHeight != 0) { height = savedHeight; } else if (App.StartSize != "video") { height = autoFitHeight; } core.WasInitialSizeSet = true; } height = Convert.ToInt32(height * scale); int width = height * videoSize.Width / videoSize.Height; int maxHeight = screen.WorkingArea.Height - (Height - ClientSize.Height); int maxWidth = screen.WorkingArea.Width - (Width - ClientSize.Width); if (height < maxHeight * core.AutofitSmaller) { height = Convert.ToInt32(maxHeight * core.AutofitSmaller); width = Convert.ToInt32(height * videoSize.Width / videoSize.Height); } if (height > maxHeight * core.AutofitLarger) { height = Convert.ToInt32(maxHeight * core.AutofitLarger); width = Convert.ToInt32(height * videoSize.Width / videoSize.Height); } if (width > maxWidth) { width = maxWidth; height = (int)Math.Ceiling(width * videoSize.Height / (double)videoSize.Width); } Point middlePos = new Point(Left + Width / 2, Top + Height / 2); var rect = new WinAPI.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; } WinAPI.SetWindowPos(Handle, IntPtr.Zero /* HWND_TOP */, left, top, rect.Width, rect.Height, 4 /* SWP_NOZORDER */); }
protected override void WndProc(ref Message m) { //Debug.WriteLine(m); switch (m.Msg) { case 0x0201: // WM_LBUTTONDOWN case 0x0202: // WM_LBUTTONUP case 0x0100: // WM_KEYDOWN case 0x0101: // WM_KEYUP case 0x0104: // WM_SYSKEYDOWN case 0x0105: // WM_SYSKEYUP case 0x020A: // WM_MOUSEWHEEL if (mp.WindowHandle != IntPtr.Zero) { Native.SendMessage(mp.WindowHandle, m.Msg, m.WParam, m.LParam); } break; case 0x0200: // WM_MOUSEMOVE Point pos = PointToClient(Cursor.Position); mp.command_string($"mouse {pos.X} {pos.Y}"); if (CursorHelp.IsPosDifferent(LastCursorPosChanged)) { CursorHelp.Show(); } break; case 0x319: // WM_APPCOMMAND if (mp.WindowHandle != IntPtr.Zero) { Native.PostMessage(mp.WindowHandle, m.Msg, m.WParam, m.LParam); } break; case 0x203: // Native.WM.LBUTTONDBLCLK if (!IsMouseInOSC()) { mp.command_string("cycle fullscreen"); } break; case 0x02E0: // WM_DPICHANGED if (IgnoreDpiChanged) { break; } var r2 = Marshal.PtrToStructure <Native.RECT>(m.LParam); Native.SetWindowPos(Handle, IntPtr.Zero, r2.Left, r2.Top, r2.Width, r2.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; float aspect = mp.VideoSize.Width / (float)mp.VideoSize.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; } if (m.Msg == SingleProcess.Message) { object filesObject = RegHelp.GetObject(App.RegPath, "ShellFiles"); if (filesObject is string[] files) { switch (RegHelp.GetString(App.RegPath, "ProcessInstanceMode")) { 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; } } RegHelp.RemoveValue(App.RegPath, "ShellFiles"); Activate(); return; } base.WndProc(ref m); }
void SetSize(Size size, Size videoSize, Screen screen, bool checkAutofitSmaller = true, bool checkAutofitLarger = true) { int height = size.Height; int width = size.Height * videoSize.Width / videoSize.Height; int maxHeight = screen.WorkingArea.Height - (Height - ClientSize.Height); int maxWidth = screen.WorkingArea.Width - (Width - ClientSize.Width); if (checkAutofitSmaller && (height < maxHeight * core.AutofitSmaller)) { height = Convert.ToInt32(maxHeight * core.AutofitSmaller); width = Convert.ToInt32(height * videoSize.Width / videoSize.Height); } float autofitLarger = checkAutofitLarger ? core.AutofitLarger : 1; if (height > maxHeight * autofitLarger) { height = Convert.ToInt32(maxHeight * autofitLarger); width = Convert.ToInt32(height * videoSize.Width / videoSize.Height); } if (width > maxWidth) { width = maxWidth; height = (int)Math.Ceiling(width * videoSize.Height / (double)videoSize.Width); } Point middlePos = new Point(Left + Width / 2, Top + Height / 2); var rect = new 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; } SetWindowPos(Handle, IntPtr.Zero, left, top, rect.Width, rect.Height, 4); }