install to context menu

This commit is contained in:
LouisLam 2020-05-09 22:36:14 +08:00
parent 73843b9a03
commit ba529ca6da
3 changed files with 135 additions and 7 deletions

34
Form1.Designer.cs generated
View File

@ -28,6 +28,8 @@
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.linkLabel1 = new System.Windows.Forms.LinkLabel();
this.buttonContextMenu = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
@ -58,22 +60,46 @@
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(206, 41);
this.button2.TabIndex = 2;
this.button2.Text = "Compress to H265";
this.button2.Text = "Compress to H.265";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// linkLabel1
//
this.linkLabel1.Location = new System.Drawing.Point(314, 93);
this.linkLabel1.Name = "linkLabel1";
this.linkLabel1.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
this.linkLabel1.Size = new System.Drawing.Size(284, 23);
this.linkLabel1.TabIndex = 4;
this.linkLabel1.TabStop = true;
this.linkLabel1.Text = "https://github.com/louislam";
//
// buttonContextMenu
//
this.buttonContextMenu.Location = new System.Drawing.Point(364, 40);
this.buttonContextMenu.Name = "buttonContextMenu";
this.buttonContextMenu.Size = new System.Drawing.Size(234, 41);
this.buttonContextMenu.TabIndex = 5;
this.buttonContextMenu.Text = "Install to Context Menu";
this.buttonContextMenu.UseVisualStyleBackColor = true;
this.buttonContextMenu.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AllowDrop = true;
this.AutoScaleDimensions = new System.Drawing.SizeF(120F, 120F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.ClientSize = new System.Drawing.Size(610, 96);
this.ClientSize = new System.Drawing.Size(610, 125);
this.Controls.Add(this.buttonContextMenu);
this.Controls.Add(this.linkLabel1);
this.Controls.Add(this.button2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Name = "Form1";
this.Text = "Form1";
this.Text = "Lazy Compress H.265 (HEVC)";
this.Load += new System.EventHandler(this.Form1_Load);
this.Shown += new System.EventHandler(this.Form1_Shown);
this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form1_DragDrop);
this.DragEnter += new System.Windows.Forms.DragEventHandler(this.Form1_DragEnter);
this.ResumeLayout(false);
@ -82,6 +108,8 @@
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button buttonContextMenu;
private System.Windows.Forms.LinkLabel linkLabel1;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.TextBox textBox1;

View File

@ -6,16 +6,37 @@ using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
namespace CompressH265 {
public partial class Form1 : Form {
private bool isFromContextMenu = false;
public Form1() {
InitializeComponent();
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
}
private void Form1_Load(object sender, EventArgs e) {
AddShieldToButton(buttonContextMenu);
string[] args = Environment.GetCommandLineArgs();
var isInputFilePath = false;
foreach (var arg in args) {
if (arg == "-i") {
isInputFilePath = true;
} else if (isInputFilePath) {
isFromContextMenu = true;
textBox1.Text = arg;
isInputFilePath = false;
button2_Click(null, null);
}
}
}
private void button1_Click(object sender, EventArgs e) {
@ -52,10 +73,14 @@ namespace CompressH265 {
var outputFilename = textBox1.Text + ".h265.mp4";
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = $"/K ffmpeg.exe -i \"{textBox1.Text}\" -vcodec hevc \"{outputFilename}\"";
process.StartInfo.Arguments = $"/K ffmpeg.exe -i \"{textBox1.Text}\" -vcodec hevc -map_metadata 0 \"{outputFilename}\"";
process.StartInfo.UseShellExecute = false;
// process.StartInfo.CreateNoWindow = true;
process.Exited += (sender2, e2) => {
System.Environment.Exit(1);
};
// process.OutputDataReceived += OnProcessOutput;
// process.ErrorDataReceived += OnProcessOutput;
process.Start();
@ -67,5 +92,46 @@ namespace CompressH265 {
private void OnProcessOutput(object send, DataReceivedEventArgs args) {
// textBox2.Text += args.Data;
}
private void button3_Click(object sender, EventArgs e) {
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = System.Reflection.Assembly.GetExecutingAssembly().Location;
info.UseShellExecute = true;
info.Verb = "runas"; // Provides Run as Administrator
info.Arguments = "-contextmenu";
try {
if (Process.Start(info) != null) {
MessageBox.Show("Installed successfully.");
} else {
MessageBox.Show("Error");
}
} catch (Exception exception) {
MessageBox.Show("Error: You have to allow UAC in order to install to the context menu.");
}
}
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd,
uint Msg, int wParam, int lParam);
// Make the button display the UAC shield.
public static void AddShieldToButton(Button btn)
{
const Int32 BCM_SETSHIELD = 0x160C;
// Give the button the flat style and make it
// display the UAC shield.
btn.FlatStyle = System.Windows.Forms.FlatStyle.System;
SendMessage(btn.Handle, BCM_SETSHIELD, 0, 1);
}
private void Form1_Shown(object sender, EventArgs e) {
if (isFromContextMenu) {
Hide();
}
}
}
}

View File

@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
namespace CompressH265 {
static class Program {
@ -10,7 +12,19 @@ namespace CompressH265 {
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
static void Main(string[] args) {
foreach(var item in args)
{
Console.WriteLine(item);
if (item == "-contextmenu") {
installContextMenu();
System.Environment.Exit(1);
}
}
// ***this line is added***
if (Environment.OSVersion.Version.Major >= 6)
SetProcessDPIAware();
@ -18,10 +32,30 @@ namespace CompressH265 {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
// ***also dllimport of that function***
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool SetProcessDPIAware();
public static void installContextMenu() {
RegistryKey item = null;
try {
item = Registry.ClassesRoot.CreateSubKey("*\\shell\\Compress to H.265 (HEVC)\\command");
item.SetValue("", System.Reflection.Assembly.GetExecutingAssembly().Location + " -i \"%1\"");
}
catch(Exception ex)
{
}
finally
{
if(item != null)
item.Close();
}
}
}
}