如何为非表单程序设置热键
How to set a hotkey to a Non-Form Program
好的,我正在使用位图等制作这个简单的屏幕截图程序,但是当我尝试制作像 f12 这样的热键时,例如,没有任何反应,我编码它只是为了显示一个消息框,但它没有甚至那样做。所以我将其设置回去以同时执行消息框和截屏,但仍然无法正常工作。
私有静态 NotifyIcon notifyIcon;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
// We need to dispose here, or the icon will not remove until the
// system tray is updated.
System.Windows.Forms.Application.ApplicationExit += delegate
{
notifyIcon.Dispose();
};
CreateNotifyIcon();
System.Windows.Forms.Application.Run();
}
/// <summary>
/// Creates the icon that sits in the system tray.
/// </summary>
///
static void Program_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.F12)
{
MessageBox.Show("ScreenShot Taken");
TakeFullScreenShot();
}
else if(e.KeyCode == Keys.F11)
{
Application.Exit();
}
}
private static void CreateNotifyIcon()
{
notifyIcon = new NotifyIcon
{
Icon = Resources.AppIcon, ContextMenu = GetContextMenu()
};
notifyIcon.Visible = true;
}
private static ContextMenu GetContextMenu()
{
string myPath = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
System.Diagnostics.Process prc = new System.Diagnostics.Process();
prc.StartInfo.FileName = myPath;
ContextMenu menu = new ContextMenu();
menu.MenuItems.Add("Take Screenshot (F12)", delegate { TakeFullScreenShot(); });
menu.MenuItems.Add("Open Folder", delegate { prc.Start(); });
menu.MenuItems.Add("Exit", delegate { System.Windows.Forms.Application.Exit(); });
return menu;
}
private static void TakeFullScreenShot()
{
int width = Screen.PrimaryScreen.Bounds.Width;
int height = Screen.PrimaryScreen.Bounds.Height;
using (Bitmap screenshot = new Bitmap(width, height, PixelFormat.Format32bppArgb))
{
using (Graphics graphics = Graphics.FromImage(screenshot))
{
Point origin = new Point(0, 0);
Size screenSize = Screen.PrimaryScreen.Bounds.Size;
//Copy Entire screen to entire bitmap.
graphics.CopyFromScreen(origin, origin, screenSize);
}
//Check to see if the file exists, if it does, append.
int append = 1;
while (File.Exists($"Screenshot{append}.jpg"))
append++;
string fileName = $"Screenshot{append}.jpg";
screenshot.Save(fileName, ImageFormat.Jpeg);
}
}
您可能不需要所有这些,但这只是为了确保我在尝试构建它时没有搞砸任何事情。此外,该程序没有任何形式,它只是任务栏中的一个图标,如果您右键单击它并单击“截屏”(F12),它将毫无问题地截取屏幕截图。谢谢!
除非程序有焦点,否则热键将不起作用。关键事件仅在您的应用程序获得焦点时才会发送到您的应用程序。
好的,我正在使用位图等制作这个简单的屏幕截图程序,但是当我尝试制作像 f12 这样的热键时,例如,没有任何反应,我编码它只是为了显示一个消息框,但它没有甚至那样做。所以我将其设置回去以同时执行消息框和截屏,但仍然无法正常工作。 私有静态 NotifyIcon notifyIcon;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
// We need to dispose here, or the icon will not remove until the
// system tray is updated.
System.Windows.Forms.Application.ApplicationExit += delegate
{
notifyIcon.Dispose();
};
CreateNotifyIcon();
System.Windows.Forms.Application.Run();
}
/// <summary>
/// Creates the icon that sits in the system tray.
/// </summary>
///
static void Program_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.F12)
{
MessageBox.Show("ScreenShot Taken");
TakeFullScreenShot();
}
else if(e.KeyCode == Keys.F11)
{
Application.Exit();
}
}
private static void CreateNotifyIcon()
{
notifyIcon = new NotifyIcon
{
Icon = Resources.AppIcon, ContextMenu = GetContextMenu()
};
notifyIcon.Visible = true;
}
private static ContextMenu GetContextMenu()
{
string myPath = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
System.Diagnostics.Process prc = new System.Diagnostics.Process();
prc.StartInfo.FileName = myPath;
ContextMenu menu = new ContextMenu();
menu.MenuItems.Add("Take Screenshot (F12)", delegate { TakeFullScreenShot(); });
menu.MenuItems.Add("Open Folder", delegate { prc.Start(); });
menu.MenuItems.Add("Exit", delegate { System.Windows.Forms.Application.Exit(); });
return menu;
}
private static void TakeFullScreenShot()
{
int width = Screen.PrimaryScreen.Bounds.Width;
int height = Screen.PrimaryScreen.Bounds.Height;
using (Bitmap screenshot = new Bitmap(width, height, PixelFormat.Format32bppArgb))
{
using (Graphics graphics = Graphics.FromImage(screenshot))
{
Point origin = new Point(0, 0);
Size screenSize = Screen.PrimaryScreen.Bounds.Size;
//Copy Entire screen to entire bitmap.
graphics.CopyFromScreen(origin, origin, screenSize);
}
//Check to see if the file exists, if it does, append.
int append = 1;
while (File.Exists($"Screenshot{append}.jpg"))
append++;
string fileName = $"Screenshot{append}.jpg";
screenshot.Save(fileName, ImageFormat.Jpeg);
}
}
您可能不需要所有这些,但这只是为了确保我在尝试构建它时没有搞砸任何事情。此外,该程序没有任何形式,它只是任务栏中的一个图标,如果您右键单击它并单击“截屏”(F12),它将毫无问题地截取屏幕截图。谢谢!
除非程序有焦点,否则热键将不起作用。关键事件仅在您的应用程序获得焦点时才会发送到您的应用程序。