為按鈕btnCancel添加click事件
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
為SetHotkey窗口添加load事件
private void SetHotKey_Load(object sender, EventArgs e)
{
comboBox1.Text = Settings.Default.HotKey.ToString();
}
6,防止程序多次運行
同樣,網上有許多這方面的資料,本部分代碼基本來自互聯網,如有版權問題請給我留言,我將立即刪除
為防止程序多次運行,修改Program.cs文件內容如下:
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;
/// <summary>
/// The main entry point for the application.
/// </summary>
[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);
//Loop through the running processes in with the same name
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.Replace("/", """) ==
current.MainModule.FileName)
{
//Return the other process instance.
return process;
}
}
}
//No other instance was found, return null.
return null;
}
public static void HandleRunningInstance(Process instance)
{
//Make sure the window is not minimized or maximized
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
//Set the real intance to foreground window
SetForegroundWindow(instance.MainWindowHandle);
}
}
}
至此,該截圖程序基本完成,實現了類似QQ截圖的功能。(默認熱鍵為Ctrl+Alt+A)
注意:程序中用到了一些圖片,Icon文件和cur文件,請復制系統目錄(C:"Windows"Cursors)下的hcross.cur、 move_m.cur、size1_m.cur、size2_m.cur、size3_m.cur、size4_m.cur文件到.." ScreenCutter"ScreenCutter"Cursors目錄下,在.."ScreenCutter"ScreenCutter"Icons 目錄下添加相應圖標,在.."ScreenCutter"ScreenCutter"Images目錄下添加相應圖片。如路徑不同,請在代碼中自行更改。