Skip to content

Commit

Permalink
Arreglado error de sobrepasar la altura máxima de la consola
Browse files Browse the repository at this point in the history
  • Loading branch information
josago97 committed Nov 15, 2022
1 parent f9b4fc7 commit 4b6f9d1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
19 changes: 7 additions & 12 deletions ConsoleMediaPlayer.Common/ConsoleHelper.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace ConsoleMediaPlayer.Common
{
Expand Down Expand Up @@ -63,9 +58,9 @@ public static FontInfo[] SetCurrentFont(short fontSize = 0)
// Get some settings from current font.
if (!SetCurrentConsoleFontEx(ConsoleOutputHandle, false, ref set))
{
var ex = Marshal.GetLastWin32Error();
Console.WriteLine("Set error " + ex);
throw new System.ComponentModel.Win32Exception(ex);
int error = Marshal.GetLastWin32Error();
Console.WriteLine("Set error " + error);
throw new System.ComponentModel.Win32Exception(error);
}

FontInfo after = new FontInfo
Expand All @@ -78,9 +73,9 @@ public static FontInfo[] SetCurrentFont(short fontSize = 0)
}
else
{
var er = Marshal.GetLastWin32Error();
Console.WriteLine("Get error " + er);
throw new System.ComponentModel.Win32Exception(er);
int error = Marshal.GetLastWin32Error();
Console.WriteLine("Get error " + error);
throw new System.ComponentModel.Win32Exception(error);
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions ConsoleMediaPlayer.Common/MediaPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace ConsoleMediaPlayer.Common
{
public abstract class MediaPlayer
{
static readonly Size MARGIN = new Size(1, 2);
protected abstract short FontSize { get; }
public string FilePath { get; init; }
public Size Resolution { get; protected set; }
Expand All @@ -20,16 +21,19 @@ public MediaPlayer(string filePath)
public void ResizeConsoleScreen()
{
ConsoleHelper.SetCurrentFont(FontSize);
Console.SetWindowSize(Resolution.Width + 1, Resolution.Height + 2);
int width = Resolution.Width + MARGIN.Width;
int height = Resolution.Height + MARGIN.Height;
Console.SetWindowSize(width, height);
}

protected Size CalculateResolution(Size originalResolution, int desiredHeight)
{
double aspectRatio = originalResolution.Width / (double)originalResolution.Height;
int width = (int)(desiredHeight * aspectRatio);
int height = desiredHeight / 2;
int maxWindowHeight = Console.LargestWindowHeight - MARGIN.Height;
int height = Math.Min(maxWindowHeight * 2, desiredHeight);
int width = (int)(height * aspectRatio);

return new Size(width, height);
return new Size(width, height / 2);
}

}
Expand Down

0 comments on commit 4b6f9d1

Please sign in to comment.