2017-02-26 19:23:31 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Windows.Forms;
|
2020-11-22 07:49:37 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System;
|
2017-02-26 19:23:31 +00:00
|
|
|
|
using SharpShell.Attributes;
|
|
|
|
|
using SharpShell.SharpContextMenu;
|
2020-11-22 07:49:37 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using Microsoft.Win32;
|
2020-12-09 08:42:04 +00:00
|
|
|
|
using System.Drawing;
|
2017-02-26 19:23:31 +00:00
|
|
|
|
|
2020-12-20 07:42:04 +00:00
|
|
|
|
namespace DisplayMagicianShellExtension
|
2017-02-26 19:23:31 +00:00
|
|
|
|
{
|
2020-12-09 08:42:04 +00:00
|
|
|
|
|
2017-02-26 19:23:31 +00:00
|
|
|
|
[ComVisible(true)]
|
2020-12-06 08:19:03 +00:00
|
|
|
|
[Guid("de271cd7-fa82-439f-b128-202d473bb51e")]
|
|
|
|
|
[RegistrationName("DisplayMagician.ShellExtension")]
|
2020-11-22 07:49:37 +00:00
|
|
|
|
[COMServerAssociation(AssociationType.DesktopBackground)]
|
2020-12-06 08:19:03 +00:00
|
|
|
|
public class DisplayMagicianDesktopMenuExtension : SharpContextMenu
|
2017-02-26 19:23:31 +00:00
|
|
|
|
{
|
2020-11-22 07:49:37 +00:00
|
|
|
|
// Other constants that are useful
|
2021-09-09 09:23:34 +00:00
|
|
|
|
internal static Version _version = new Version(0, 2, 0);
|
2020-12-02 08:11:23 +00:00
|
|
|
|
internal static string AlternateAppHomePath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "DisplayMagician");
|
|
|
|
|
internal static string AppDataPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "DisplayMagician");
|
2020-11-22 07:49:37 +00:00
|
|
|
|
private static string AppProfileStoragePath = System.IO.Path.Combine(AppDataPath, $"Profiles");
|
2021-09-09 09:23:34 +00:00
|
|
|
|
private static string _profileStorageJsonFileName = System.IO.Path.Combine(AppProfileStoragePath, $"DisplayProfiles_2.0.json");
|
2020-12-02 08:11:23 +00:00
|
|
|
|
internal static string registryDisplayMagician = @"SOFTWARE\DisplayMagician";
|
|
|
|
|
string DisplayMagicianFullname = "";
|
|
|
|
|
string DisplayMagicianInstallDir = "";
|
|
|
|
|
Process DisplayMagicianProcess = null;
|
2018-10-20 00:27:25 +00:00
|
|
|
|
|
2020-12-09 08:42:04 +00:00
|
|
|
|
private struct ProfileData
|
|
|
|
|
{
|
|
|
|
|
public string UUID;
|
|
|
|
|
public string Name;
|
|
|
|
|
public Bitmap Bitmap;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-06 08:19:03 +00:00
|
|
|
|
public DisplayMagicianDesktopMenuExtension()
|
2020-11-22 07:49:37 +00:00
|
|
|
|
{ }
|
2017-02-26 19:23:31 +00:00
|
|
|
|
|
|
|
|
|
protected override bool CanShowMenu()
|
|
|
|
|
{
|
2020-12-02 08:11:23 +00:00
|
|
|
|
//Logging.Log($"Starting CanShowMenu");
|
|
|
|
|
// Only show this menu if DisplayMagician is installed
|
|
|
|
|
DisplayMagicianInstallDir = "";
|
2020-11-22 07:49:37 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2020-12-02 08:11:23 +00:00
|
|
|
|
RegistryKey DisplayMagicianKey = Registry.LocalMachine.OpenSubKey(registryDisplayMagician, RegistryKeyPermissionCheck.ReadSubTree);
|
|
|
|
|
DisplayMagicianInstallDir = DisplayMagicianKey.GetValue("InstallDir", AlternateAppHomePath).ToString();
|
2020-11-22 07:49:37 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
2020-12-02 08:11:23 +00:00
|
|
|
|
DisplayMagicianInstallDir = AlternateAppHomePath;
|
2020-11-22 07:49:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 08:11:23 +00:00
|
|
|
|
DisplayMagicianFullname = Path.Combine(DisplayMagicianInstallDir, "DisplayMagician.exe");
|
2020-11-22 07:49:37 +00:00
|
|
|
|
|
2020-12-02 08:11:23 +00:00
|
|
|
|
//Logging.Log($"DisplayMagician is installed in {DisplayMagicianFullname}");
|
2020-11-30 09:25:58 +00:00
|
|
|
|
|
2020-11-22 07:49:37 +00:00
|
|
|
|
|
2020-12-02 08:11:23 +00:00
|
|
|
|
if (File.Exists(DisplayMagicianFullname))
|
2020-11-30 09:25:58 +00:00
|
|
|
|
{
|
2020-12-02 08:11:23 +00:00
|
|
|
|
//Logging.Log($"CanShowMenu is returning true (can show menu)");
|
2020-11-22 07:49:37 +00:00
|
|
|
|
return true;
|
2020-11-30 09:25:58 +00:00
|
|
|
|
}
|
2020-11-22 07:49:37 +00:00
|
|
|
|
else
|
2020-11-30 09:25:58 +00:00
|
|
|
|
{
|
2020-12-02 08:11:23 +00:00
|
|
|
|
//Logging.Log($"CanShowMenu is returning false (cannot show menu)");
|
2020-11-22 07:49:37 +00:00
|
|
|
|
return false;
|
2020-11-30 09:25:58 +00:00
|
|
|
|
}
|
2017-02-26 19:23:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override ContextMenuStrip CreateMenu()
|
|
|
|
|
{
|
2020-12-02 08:11:23 +00:00
|
|
|
|
//Logging.Log($"Starting CreateMenu");
|
2020-11-22 07:49:37 +00:00
|
|
|
|
var explorerMenuStrip = new ContextMenuStrip();
|
|
|
|
|
|
2020-12-09 08:42:04 +00:00
|
|
|
|
List<ProfileData> profiles = new List<ProfileData>();
|
2020-11-22 07:49:37 +00:00
|
|
|
|
|
2020-12-06 08:19:03 +00:00
|
|
|
|
if (File.Exists(_profileStorageJsonFileName))
|
|
|
|
|
{
|
|
|
|
|
//Logging.Log($"{_profileStorageJsonFileName} file exists");
|
|
|
|
|
MatchCollection mc;
|
|
|
|
|
string uuid = "";
|
|
|
|
|
string profileName = "";
|
2020-12-09 08:42:04 +00:00
|
|
|
|
string profileBitmapData = "";
|
2020-11-22 07:49:37 +00:00
|
|
|
|
|
2020-12-06 08:19:03 +00:00
|
|
|
|
foreach (string aLine in File.ReadLines(_profileStorageJsonFileName, Encoding.Unicode))
|
2018-10-20 00:27:25 +00:00
|
|
|
|
{
|
2020-12-06 08:19:03 +00:00
|
|
|
|
//Logging.Log($"Processing line: {_profileStorageJsonFileName}");
|
|
|
|
|
string lineToProcess = aLine;
|
|
|
|
|
if (lineToProcess.StartsWith(" \"UUID\""))
|
2018-10-20 00:27:25 +00:00
|
|
|
|
{
|
2020-12-06 08:19:03 +00:00
|
|
|
|
//Logging.Log($"Line starts with 4 spaces and UUID");
|
|
|
|
|
mc = Regex.Matches(lineToProcess, " \"UUID\": \"(.*)\"");
|
|
|
|
|
uuid = mc[0].Groups[1].ToString();
|
|
|
|
|
}
|
|
|
|
|
else if (lineToProcess.StartsWith(" \"Name\""))
|
|
|
|
|
{
|
|
|
|
|
//Logging.Log($"Line starts with 4 spaces and Name");
|
|
|
|
|
mc = Regex.Matches(lineToProcess, " \"Name\": \"(.*)\"");
|
|
|
|
|
profileName = mc[0].Groups[1].ToString();
|
2020-11-22 07:49:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-09 08:42:04 +00:00
|
|
|
|
|
|
|
|
|
else if (lineToProcess.StartsWith(" \"ProfileBitmap\""))
|
|
|
|
|
{
|
|
|
|
|
//Logging.Log($"Line starts with 4 spaces and Name");
|
|
|
|
|
mc = Regex.Matches(lineToProcess, " \"ProfileBitmap\": \"(.*)\"");
|
|
|
|
|
profileBitmapData = mc[0].Groups[1].ToString();
|
|
|
|
|
if (!String.IsNullOrEmpty(uuid) && !String.IsNullOrEmpty(profileName))
|
|
|
|
|
{
|
|
|
|
|
var bytes = Convert.FromBase64String(profileBitmapData);
|
|
|
|
|
Bitmap profileBitmap;
|
|
|
|
|
using (var ms = new MemoryStream(bytes))
|
|
|
|
|
profileBitmap = new Bitmap(Bitmap.FromStream(ms),16,16);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ProfileData newProfile = new ProfileData();
|
|
|
|
|
newProfile.UUID = uuid;
|
|
|
|
|
newProfile.Name = profileName;
|
|
|
|
|
if (profileBitmap is Bitmap)
|
|
|
|
|
{
|
|
|
|
|
newProfile.Bitmap = profileBitmap;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
newProfile.Bitmap = null;
|
|
|
|
|
|
|
|
|
|
profiles.Add(newProfile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-22 07:49:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-06 08:19:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-20 07:42:04 +00:00
|
|
|
|
var extensionMenu = new ToolStripMenuItem("DisplayMagician: Change display profiles...", Properties.Resources.MenuImage);
|
2020-12-06 08:19:03 +00:00
|
|
|
|
explorerMenuStrip.Items.Add(extensionMenu);
|
|
|
|
|
|
2020-12-09 08:42:04 +00:00
|
|
|
|
// Add the first menu to create a new Display Profile
|
2020-12-06 08:19:03 +00:00
|
|
|
|
extensionMenu.DropDownItems.Add(new ToolStripMenuItem("Create a new display profile", null,
|
|
|
|
|
(sender, args) =>
|
2020-11-22 07:49:37 +00:00
|
|
|
|
{
|
2020-12-06 08:19:03 +00:00
|
|
|
|
//Logging.Log(DisplayMagicianFullname + $" CreateProfile");
|
|
|
|
|
DisplayMagicianProcess = Process.Start(DisplayMagicianFullname, $"CreateProfile");
|
|
|
|
|
//Logging.Log(DisplayMagicianProcess.ToString());
|
|
|
|
|
}
|
|
|
|
|
));
|
2020-11-22 07:49:37 +00:00
|
|
|
|
|
2020-12-06 08:19:03 +00:00
|
|
|
|
if (profiles.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
extensionMenu.DropDownItems.Add(new ToolStripSeparator());
|
|
|
|
|
|
2020-12-09 08:42:04 +00:00
|
|
|
|
foreach (ProfileData profile in profiles.OrderBy(p => p.Name))
|
2020-12-06 08:19:03 +00:00
|
|
|
|
{
|
2020-12-09 08:42:04 +00:00
|
|
|
|
extensionMenu.DropDownItems.Add(new ToolStripMenuItem(profile.Name, profile.Bitmap,
|
2020-12-06 08:19:03 +00:00
|
|
|
|
(sender, args) =>
|
|
|
|
|
{
|
2020-12-09 08:42:04 +00:00
|
|
|
|
DisplayMagicianProcess = Process.Start(DisplayMagicianFullname,$"ChangeProfile \"{profile.UUID}\"");
|
2020-12-06 08:19:03 +00:00
|
|
|
|
}
|
|
|
|
|
));
|
2020-11-22 07:49:37 +00:00
|
|
|
|
}
|
2017-08-10 14:05:43 +00:00
|
|
|
|
}
|
2018-10-20 00:27:25 +00:00
|
|
|
|
|
2020-11-22 07:49:37 +00:00
|
|
|
|
return explorerMenuStrip;
|
2017-02-26 19:23:31 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|