1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-04-23 12:15:48 +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{
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,
RedirectStandardOutput = true
})) != null){

View File

@ -13,6 +13,7 @@ sealed partial class FormPlayer : Form{
protected override bool ShowWithoutActivation => true;
private readonly IntPtr ownerHandle;
private readonly float ownerDpi;
private readonly string videoUrl;
private readonly DuplexPipe pipe;
@ -23,10 +24,11 @@ sealed partial class FormPlayer : Form{
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();
this.ownerHandle = handle;
this.ownerDpi = dpi / 100F;
this.videoUrl = url;
this.pipe = DuplexPipe.CreateClient(token);
this.pipe.DataIn += pipe_DataIn;
@ -78,6 +80,10 @@ public FormPlayer(IntPtr handle, int volume, string url, string token){
Application.AddMessageFilter(new MessageFilter(this));
}
private int DpiScaled(int value){
return (int)Math.Round(value*ownerDpi);
}
// Events
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;
// roughly matches MinimumSize for client bounds
const int minWidth = 334;
const int minHeight = 388;
int minWidth = DpiScaled(332);
int minHeight = DpiScaled(386);
int maxWidth = Math.Min(media.imageSourceWidth, ownerWidth*3/4);
int maxHeight = Math.Min(media.imageSourceHeight, ownerHeight*3/4);
if (NativeMethods.GetClientRect(ownerHandle, out NativeMethods.RECT clientSize)){
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));
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);
if (ClientSize != newSize || Location != newLocation){

View File

@ -9,6 +9,10 @@ static class NativeMethods{
[return: MarshalAs(UnmanagedType.Bool)]
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")]
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;
IntPtr ownerHandle;
int ownerDpi;
int defaultVolume;
string videoUrl;
string pipeToken;
try{
ownerHandle = new IntPtr(int.Parse(args[0], NumberStyles.Integer));
defaultVolume = int.Parse(args[1], NumberStyles.Integer);
videoUrl = new Uri(args[2], UriKind.Absolute).AbsoluteUri;
pipeToken = args[3];
ownerDpi = int.Parse(args[1], NumberStyles.Integer);
defaultVolume = int.Parse(args[2], NumberStyles.Integer);
videoUrl = new Uri(args[3], UriKind.Absolute).AbsoluteUri;
pipeToken = args[4];
}catch{
return CODE_INVALID_ARGS;
}
try{
Application.Run(new FormPlayer(ownerHandle, defaultVolume, videoUrl, pipeToken));
Application.Run(new FormPlayer(ownerHandle, ownerDpi, defaultVolume, videoUrl, pipeToken));
}catch(Exception e){
Console.Out.WriteLine(e);
return CODE_LAUNCH_FAIL;