DisplayMagician/DisplayMagicianShellExtension/DisplayMagicianDesktopMenuExtension.cs

173 lines
7.1 KiB
C#
Raw Normal View History

2017-02-26 19:23:31 +00:00
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.IO;
using System;
2017-02-26 19:23:31 +00:00
using SharpShell.Attributes;
using SharpShell.SharpContextMenu;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Win32;
using System.Drawing;
2017-02-26 19:23:31 +00:00
namespace DisplayMagicianShellExtension
2017-02-26 19:23:31 +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")]
[COMServerAssociation(AssociationType.DesktopBackground)]
2020-12-06 08:19:03 +00:00
public class DisplayMagicianDesktopMenuExtension : SharpContextMenu
2017-02-26 19:23:31 +00:00
{
// Other constants that are useful
2020-12-06 08:19:03 +00:00
internal static Version _version = new Version(0, 1, 0);
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");
private static string AppProfileStoragePath = System.IO.Path.Combine(AppDataPath, $"Profiles");
2020-12-06 08:19:03 +00:00
private static string _profileStorageJsonFileName = System.IO.Path.Combine(AppProfileStoragePath, $"DisplayProfiles_1.0.json");
internal static string registryDisplayMagician = @"SOFTWARE\DisplayMagician";
string DisplayMagicianFullname = "";
string DisplayMagicianInstallDir = "";
Process DisplayMagicianProcess = null;
2018-10-20 00:27:25 +00:00
private struct ProfileData
{
public string UUID;
public string Name;
public Bitmap Bitmap;
}
2020-12-06 08:19:03 +00:00
public DisplayMagicianDesktopMenuExtension()
{ }
2017-02-26 19:23:31 +00:00
protected override bool CanShowMenu()
{
//Logging.Log($"Starting CanShowMenu");
// Only show this menu if DisplayMagician is installed
DisplayMagicianInstallDir = "";
try
{
RegistryKey DisplayMagicianKey = Registry.LocalMachine.OpenSubKey(registryDisplayMagician, RegistryKeyPermissionCheck.ReadSubTree);
DisplayMagicianInstallDir = DisplayMagicianKey.GetValue("InstallDir", AlternateAppHomePath).ToString();
}
catch (Exception)
{
DisplayMagicianInstallDir = AlternateAppHomePath;
}
DisplayMagicianFullname = Path.Combine(DisplayMagicianInstallDir, "DisplayMagician.exe");
//Logging.Log($"DisplayMagician is installed in {DisplayMagicianFullname}");
if (File.Exists(DisplayMagicianFullname))
{
//Logging.Log($"CanShowMenu is returning true (can show menu)");
return true;
}
else
{
//Logging.Log($"CanShowMenu is returning false (cannot show menu)");
return false;
}
2017-02-26 19:23:31 +00:00
}
protected override ContextMenuStrip CreateMenu()
{
//Logging.Log($"Starting CreateMenu");
var explorerMenuStrip = new ContextMenuStrip();
List<ProfileData> profiles = new List<ProfileData>();
2020-12-06 08:19:03 +00:00
if (File.Exists(_profileStorageJsonFileName))
{
//Logging.Log($"{_profileStorageJsonFileName} file exists");
MatchCollection mc;
string uuid = "";
string profileName = "";
string profileBitmapData = "";
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();
}
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-12-06 08:19:03 +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);
// 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-12-06 08:19:03 +00:00
//Logging.Log(DisplayMagicianFullname + $" CreateProfile");
DisplayMagicianProcess = Process.Start(DisplayMagicianFullname, $"CreateProfile");
//Logging.Log(DisplayMagicianProcess.ToString());
}
));
2020-12-06 08:19:03 +00:00
if (profiles.Count > 0)
{
extensionMenu.DropDownItems.Add(new ToolStripSeparator());
foreach (ProfileData profile in profiles.OrderBy(p => p.Name))
2020-12-06 08:19:03 +00:00
{
extensionMenu.DropDownItems.Add(new ToolStripMenuItem(profile.Name, profile.Bitmap,
2020-12-06 08:19:03 +00:00
(sender, args) =>
{
DisplayMagicianProcess = Process.Start(DisplayMagicianFullname,$"ChangeProfile \"{profile.UUID}\"");
2020-12-06 08:19:03 +00:00
}
));
}
}
2018-10-20 00:27:25 +00:00
return explorerMenuStrip;
2017-02-26 19:23:31 +00:00
}
}
}