1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-05-02 11:34:08 +02:00

Fix video player size with a small window on high DPI

This commit is contained in:
chylex 2018-11-23 00:45:02 +01:00
parent b0f9de67cf
commit 9414f372d7
4 changed files with 28 additions and 11 deletions

View File

@ -40,7 +40,7 @@ public void Launch(string url, string username){
if ((process = Process.Start(new ProcessStartInfo{ if ((process = Process.Start(new ProcessStartInfo{
FileName = Path.Combine(Program.ProgramPath, "TweetDuck.Video.exe"), FileName = Path.Combine(Program.ProgramPath, "TweetDuck.Video.exe"),
Arguments = $"{owner.Handle} {Config.VideoPlayerVolume} \"{url}\" \"{pipe.GenerateToken()}\"", Arguments = $"{owner.Handle} {(int)Math.Floor(100F*owner.GetDPIScale())} {Config.VideoPlayerVolume} \"{url}\" \"{pipe.GenerateToken()}\"",
UseShellExecute = false, UseShellExecute = false,
RedirectStandardOutput = true RedirectStandardOutput = true
})) != null){ })) != null){

View File

@ -13,6 +13,7 @@ sealed partial class FormPlayer : Form{
protected override bool ShowWithoutActivation => true; protected override bool ShowWithoutActivation => true;
private readonly IntPtr ownerHandle; private readonly IntPtr ownerHandle;
private readonly float ownerDpi;
private readonly string videoUrl; private readonly string videoUrl;
private readonly DuplexPipe pipe; private readonly DuplexPipe pipe;
@ -23,10 +24,11 @@ sealed partial class FormPlayer : Form{
private WindowsMediaPlayer Player => player.Ocx; private WindowsMediaPlayer Player => player.Ocx;
public FormPlayer(IntPtr handle, int volume, string url, string token){ public FormPlayer(IntPtr handle, int dpi, int volume, string url, string token){
InitializeComponent(); InitializeComponent();
this.ownerHandle = handle; this.ownerHandle = handle;
this.ownerDpi = dpi / 100F;
this.videoUrl = url; this.videoUrl = url;
this.pipe = DuplexPipe.CreateClient(token); this.pipe = DuplexPipe.CreateClient(token);
this.pipe.DataIn += pipe_DataIn; this.pipe.DataIn += pipe_DataIn;
@ -78,6 +80,10 @@ public FormPlayer(IntPtr handle, int volume, string url, string token){
Application.AddMessageFilter(new MessageFilter(this)); Application.AddMessageFilter(new MessageFilter(this));
} }
private int DpiScaled(int value){
return (int)Math.Round(value*ownerDpi);
}
// Events // Events
private void FormPlayer_Load(object sender, EventArgs e){ private void FormPlayer_Load(object sender, EventArgs e){
@ -139,15 +145,20 @@ private void timerSync_Tick(object sender, EventArgs e){
int ownerHeight = rect.Bottom-rect.Top+1; int ownerHeight = rect.Bottom-rect.Top+1;
// roughly matches MinimumSize for client bounds // roughly matches MinimumSize for client bounds
const int minWidth = 334; int minWidth = DpiScaled(332);
const int minHeight = 388; int minHeight = DpiScaled(386);
int maxWidth = Math.Min(media.imageSourceWidth, ownerWidth*3/4); if (NativeMethods.GetClientRect(ownerHandle, out NativeMethods.RECT clientSize)){
int maxHeight = Math.Min(media.imageSourceHeight, ownerHeight*3/4); minWidth = Math.Min(minWidth, clientSize.Right);
minHeight = Math.Min(minHeight, clientSize.Bottom);
}
int maxWidth = Math.Min(DpiScaled(media.imageSourceWidth), ownerWidth*3/4);
int maxHeight = Math.Min(DpiScaled(media.imageSourceHeight), ownerHeight*3/4);
bool isCursorInside = ClientRectangle.Contains(PointToClient(Cursor.Position)); bool isCursorInside = ClientRectangle.Contains(PointToClient(Cursor.Position));
Size newSize = new Size(Math.Max(minWidth, maxWidth), Math.Max(minHeight, maxHeight)); Size newSize = new Size(Math.Max(minWidth+2, maxWidth), Math.Max(minHeight+2, maxHeight));
Point newLocation = new Point(ownerLeft+(ownerWidth-newSize.Width)/2, ownerTop+(ownerHeight-newSize.Height+SystemInformation.CaptionHeight)/2); Point newLocation = new Point(ownerLeft+(ownerWidth-newSize.Width)/2, ownerTop+(ownerHeight-newSize.Height+SystemInformation.CaptionHeight)/2);
if (ClientSize != newSize || Location != newLocation){ if (ClientSize != newSize || Location != newLocation){

View File

@ -9,6 +9,10 @@ static class NativeMethods{
[return: MarshalAs(UnmanagedType.Bool)] [return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
[DllImport("user32.dll")] [DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong); private static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);

View File

@ -25,21 +25,23 @@ private static int Main(string[] args){
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture; CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
IntPtr ownerHandle; IntPtr ownerHandle;
int ownerDpi;
int defaultVolume; int defaultVolume;
string videoUrl; string videoUrl;
string pipeToken; string pipeToken;
try{ try{
ownerHandle = new IntPtr(int.Parse(args[0], NumberStyles.Integer)); ownerHandle = new IntPtr(int.Parse(args[0], NumberStyles.Integer));
defaultVolume = int.Parse(args[1], NumberStyles.Integer); ownerDpi = int.Parse(args[1], NumberStyles.Integer);
videoUrl = new Uri(args[2], UriKind.Absolute).AbsoluteUri; defaultVolume = int.Parse(args[2], NumberStyles.Integer);
pipeToken = args[3]; videoUrl = new Uri(args[3], UriKind.Absolute).AbsoluteUri;
pipeToken = args[4];
}catch{ }catch{
return CODE_INVALID_ARGS; return CODE_INVALID_ARGS;
} }
try{ try{
Application.Run(new FormPlayer(ownerHandle, defaultVolume, videoUrl, pipeToken)); Application.Run(new FormPlayer(ownerHandle, ownerDpi, defaultVolume, videoUrl, pipeToken));
}catch(Exception e){ }catch(Exception e){
Console.Out.WriteLine(e); Console.Out.WriteLine(e);
return CODE_LAUNCH_FAIL; return CODE_LAUNCH_FAIL;