可能重复:
在winforms中实现键盘快捷键的最佳方法?
我对 KeyDown 或 KeyPress 或 KeyUp 有疑问。我为表单 ( ) 创建了一个新的 KeyDown private void Form1_KeyDown(object sender, KeyEventArgs e)
,我在其中插入了以下代码:
if (e.KeyCode == Keys.A && e.Modifiers == Keys.Control)
MessageBox.Show("yes");
如果您按下 Ctrl + A 将显示一个带有 Yes 消息的 MessageBox。
Windows form application
问题是这样的:如果我创建一个新项目(我不知道在这种情况下有什么问题。
也许是因为图书馆:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
using System.Media;
using System.Diagnostics;
using Microsoft.Win32;
using System.Net.Mail;
using System.Net;
或者我在 Program.cs 中添加的代码,让人们只启动一次应用程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
namespace Programari
{
static class Program
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
[return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
bool createdNew = true;
using (Mutex mutex = new Mutex(true, "MyApplicationName", out createdNew))
{
if (createdNew)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
Process current = Process.GetCurrentProcess();
if(Settings1.Default.rosaueng == 0)
MessageBox.Show("smartAppointment este deja deschis, ii gasesti iconita in System Icons !","EROARE DESCHIDERE APLICATIE");
else
MessageBox.Show("smartAppointment is already open, you can find him at System Icons !", "ERROR OPEN APPLICATION");
foreach (Process process in Process.GetProcessesByName(current.ProcessName))
{
if (process.Id != current.Id)
{
SetForegroundWindow(process.MainWindowHandle);
break;
}
}
}
}
}
}
}
如果你知道,请告诉我。谢谢 !