2014-06-30 13:55:51 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.Net;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
|
|
2018-02-11 15:43:58 +01:00
|
|
|
|
namespace MinecraftClient.WinAPI
|
2014-06-30 13:55:51 +02:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Allow to set the player skin as console icon, on Windows only.
|
|
|
|
|
|
/// See StackOverflow no. 2986853
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static class ConsoleIcon
|
|
|
|
|
|
{
|
|
|
|
|
|
[DllImport("kernel32.dll", SetLastError = true)]
|
|
|
|
|
|
private static extern bool SetConsoleIcon(IntPtr hIcon);
|
|
|
|
|
|
|
2020-06-07 23:38:09 +02:00
|
|
|
|
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
|
|
|
|
|
|
private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, IntPtr lParam);
|
2022-07-03 22:34:07 +08:00
|
|
|
|
|
2020-06-07 16:16:49 +05:00
|
|
|
|
/// <summary>
|
2020-06-07 23:38:09 +02:00
|
|
|
|
/// An application sends the WM_SETICON message to associate a new large or small icon with a window.
|
|
|
|
|
|
/// The system displays the large icon in the ALT+TAB dialog box, and the small icon in the window caption.
|
2020-06-07 16:16:49 +05:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public enum WinMessages : uint
|
|
|
|
|
|
{
|
|
|
|
|
|
SETICON = 0x0080,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-07-03 22:34:07 +08:00
|
|
|
|
private static void SetWindowIcon(System.Drawing.Icon icon)
|
2020-06-07 16:16:49 +05:00
|
|
|
|
{
|
2022-07-03 22:34:07 +08:00
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
|
|
|
|
{
|
|
|
|
|
|
IntPtr mwHandle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
|
|
|
|
|
|
IntPtr result01 = SendMessage(mwHandle, (int)WinMessages.SETICON, 0, icon.Handle);
|
|
|
|
|
|
IntPtr result02 = SendMessage(mwHandle, (int)WinMessages.SETICON, 1, icon.Handle);
|
|
|
|
|
|
}
|
2020-06-07 16:16:49 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-06-30 13:55:51 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Asynchronously download the player's skin and set the head as console icon
|
|
|
|
|
|
/// </summary>
|
2022-07-03 22:34:07 +08:00
|
|
|
|
public static void setPlayerIconAsync(string playerName) {
|
|
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
2014-06-30 13:55:51 +02:00
|
|
|
|
{
|
2022-07-03 22:34:07 +08:00
|
|
|
|
Thread t = new Thread(new ThreadStart(delegate
|
2014-06-30 13:55:51 +02:00
|
|
|
|
{
|
2022-07-03 22:34:07 +08:00
|
|
|
|
HttpWebRequest httpWebRequest = (HttpWebRequest) HttpWebRequest.Create("https://minotar.net/helm/" + playerName + "/100.png");
|
|
|
|
|
|
try
|
2014-06-30 13:55:51 +02:00
|
|
|
|
{
|
2022-07-03 22:34:07 +08:00
|
|
|
|
using (HttpWebResponse httpWebReponse = (HttpWebResponse) httpWebRequest.GetResponse()) {
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
Bitmap skin = new Bitmap(Image.FromStream(httpWebReponse.GetResponseStream())); //Read skin from network
|
|
|
|
|
|
SetWindowIcon(Icon.FromHandle(skin.GetHicon())); // Windows 10+ (New console)
|
|
|
|
|
|
SetConsoleIcon(skin.GetHicon()); // Windows 8 and lower (Older console)
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (ArgumentException)
|
|
|
|
|
|
{
|
|
|
|
|
|
/* Invalid image in HTTP response */
|
|
|
|
|
|
}
|
2015-11-27 16:52:41 +01:00
|
|
|
|
}
|
2014-06-30 13:55:51 +02:00
|
|
|
|
}
|
2022-07-03 22:34:07 +08:00
|
|
|
|
catch (WebException) //Skin not found? Reset to default icon
|
|
|
|
|
|
{
|
|
|
|
|
|
revertToMCCIcon();
|
|
|
|
|
|
}
|
2014-06-30 13:55:51 +02:00
|
|
|
|
}
|
2014-07-27 16:43:45 +02:00
|
|
|
|
));
|
|
|
|
|
|
t.Name = "Player skin icon setter";
|
|
|
|
|
|
t.Start();
|
2022-07-03 22:34:07 +08:00
|
|
|
|
|
2014-07-27 16:43:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-06-09 23:31:37 +02:00
|
|
|
|
/// Set the icon back to the default MCC icon
|
2014-07-27 16:43:45 +02:00
|
|
|
|
/// </summary>
|
2020-06-09 23:31:37 +02:00
|
|
|
|
public static void revertToMCCIcon()
|
2014-07-27 16:43:45 +02:00
|
|
|
|
{
|
2022-07-03 22:34:07 +08:00
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) //Windows Only
|
2014-07-27 16:43:45 +02:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2022-07-03 22:34:07 +08:00
|
|
|
|
Icon defaultIcon = Icon.ExtractAssociatedIcon(System.Reflection.Assembly.GetExecutingAssembly().Location);
|
2020-06-09 23:31:37 +02:00
|
|
|
|
SetWindowIcon(Icon.FromHandle(defaultIcon.Handle)); // Windows 10+ (New console)
|
|
|
|
|
|
SetConsoleIcon(defaultIcon.Handle); // Windows 8 and lower (Older console)
|
2014-07-27 16:43:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
catch { }
|
2014-06-30 13:55:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|