improve and uninstall from context menu

This commit is contained in:
LouisLam 2020-05-10 00:46:44 +08:00
parent b232647c78
commit cbe0525228
5 changed files with 58 additions and 7 deletions

View File

@ -7,7 +7,7 @@
<ProjectGuid>{880679DC-D0C5-4B1A-ADF4-8AA06B738BB5}</ProjectGuid>
<OutputType>WinExe</OutputType>
<RootNamespace>CompressH265</RootNamespace>
<AssemblyName>CompressH265</AssemblyName>
<AssemblyName>LazyCompressH265</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>

2
Form1.Designer.cs generated
View File

@ -73,6 +73,7 @@
this.linkLabel1.TabIndex = 4;
this.linkLabel1.TabStop = true;
this.linkLabel1.Text = "https://github.com/louislam";
this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
//
// buttonContextMenu
//
@ -97,6 +98,7 @@
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Lazy Compress H.265 (HEVC)";
this.Load += new System.EventHandler(this.Form1_Load);
this.Shown += new System.EventHandler(this.Form1_Shown);

View File

@ -6,6 +6,7 @@ using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
@ -16,12 +17,16 @@ namespace CompressH265 {
public partial class Form1 : Form {
private bool isFromContextMenu = false;
private bool installed = false;
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
var version = Assembly.GetExecutingAssembly().GetName().Version;
Text = Text + " v" + version.Major + "." + version.Minor;
AddShieldToButton(buttonContextMenu);
string[] args = Environment.GetCommandLineArgs();
@ -37,6 +42,20 @@ namespace CompressH265 {
button2_Click(null, null);
}
}
UpdateContextMenuButton();
}
public void UpdateContextMenuButton() {
var a = Registry.ClassesRoot.OpenSubKey("*\\shell\\Compress to H.265 (HEVC)");
if (a == null) {
buttonContextMenu.Text = "Install to Context Menu";
installed = false;
} else {
buttonContextMenu.Text = "Uninstall from Context Menu";
installed = true;
}
}
private void button1_Click(object sender, EventArgs e) {
@ -65,6 +84,17 @@ namespace CompressH265 {
}
private void button2_Click(object sender, EventArgs e) {
if (textBox1.Text.Trim() == "") {
MessageBox.Show("Please select or drag-and-drop a file.");
return;
}
if (!File.Exists(textBox1.Text)) {
MessageBox.Show("File Not Found");
return;
}
Process process = new Process();
process.EnableRaisingEvents = true;
// process.StartInfo.RedirectStandardOutput = true;
@ -98,11 +128,17 @@ namespace CompressH265 {
info.FileName = System.Reflection.Assembly.GetExecutingAssembly().Location;
info.UseShellExecute = true;
info.Verb = "runas"; // Provides Run as Administrator
info.Arguments = "-contextmenu";
if (installed) {
info.Arguments = "-uninstallcontextmenu";
} else {
info.Arguments = "-contextmenu";
}
try {
if (Process.Start(info) != null) {
MessageBox.Show("Installed successfully.");
UpdateContextMenuButton();
} else {
MessageBox.Show("Error");
}
@ -133,5 +169,9 @@ namespace CompressH265 {
Hide();
}
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
System.Diagnostics.Process.Start("https://github.com/louislam/lazy-compress-h265");
}
}
}

View File

@ -8,6 +8,8 @@ using Microsoft.Win32;
namespace CompressH265 {
static class Program {
private static string subkey = "*\\shell\\Compress to H.265 (HEVC)";
/// <summary>
/// The main entry point for the application.
/// </summary>
@ -21,6 +23,9 @@ namespace CompressH265 {
if (item == "-contextmenu") {
installContextMenu();
System.Environment.Exit(1);
} else if (item == "-uninstallcontextmenu") {
uninstallContextMenu();
System.Environment.Exit(1);
}
}
@ -44,7 +49,7 @@ namespace CompressH265 {
public static void installContextMenu() {
RegistryKey item = null;
try {
item = Registry.ClassesRoot.CreateSubKey("*\\shell\\Compress to H.265 (HEVC)\\command");
item = Registry.ClassesRoot.CreateSubKey(subkey + "\\command");
item.SetValue("", System.Reflection.Assembly.GetExecutingAssembly().Location + " -i \"%1\"");
}
catch(Exception ex)
@ -57,5 +62,9 @@ namespace CompressH265 {
item.Close();
}
}
public static void uninstallContextMenu() {
Registry.ClassesRoot.DeleteSubKeyTree(subkey);
}
}
}

View File

@ -5,12 +5,12 @@ using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("CompressH265")]
[assembly: AssemblyTitle("Lazy Compress H.265 (HEVC)")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CompressH265")]
[assembly: AssemblyCopyright("Copyright © 2020")]
[assembly: AssemblyCompany("WhatSticker.online")]
[assembly: AssemblyProduct("Lazy Compress H.265 (HEVC)")]
[assembly: AssemblyCopyright("WhatSticker.online © 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]