private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void SetHotKey_Load(object sender, EventArgs e)
{
comboBox1.Text = Settings.Default.HotKey.ToString();
}
修改 Program.cs,使用 Mutex 或處理序檢查確保單一執行個體:
using System;
using System.Reflection;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ScreenCutter
{
static class Program
{
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
private const int WS_SHOWNORMAL = 1;
[STAThread]
static void Main()
{
Process instance = RunningInstance();
if (instance == null)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
else
{
HandleRunningInstance(instance);
}
}
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
foreach (Process process in processes)
{
if (process.Id != current.Id)
{
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == process.MainModule.FileName)
return process;
}
}
return null;
}
public static void HandleRunningInstance(Process instance)
{
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
SetForegroundWindow(instance.MainWindowHandle);
}
}
}
至此截圖程式基本完成,預設熱鍵 Ctrl+Alt+A。
注意: 程式需用到游標檔 (.cur)、圖示 (.ico) 與圖片,請自行複製至對應目錄或修改程式碼路徑。