Поэтому я пытаюсь получить пользовательские входы, когда кто-то нажимает горячие клавиши, также за пределами формы (в данном случае SHIFT + A). Теперь, так как я хотел добавить вкладки в свое приложение форм, я решил go с usercontrols, теперь проблема в том, что я не могу получить доступ к событию formclosing (из form1) в usercontrol, то есть мне придется каким-то образом анализировать все Я хотел выполнить в форме закрытия мероприятия.
Usercontrol (по имени home)
public partial class Home : UserControl
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
enum KeyModifier
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
WinKey = 8
}
public Home()
{
InitializeComponent();
int id = 0; // The id of the hotkey.
RegisterHotKey(this.Handle, id, (int)KeyModifier.Shift, Keys.A.GetHashCode()); //Register Shift + A as global hotkey.
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0x0312)
{
/* Note that the three lines below are not needed if you only want to register one hotkey.
* The below lines are useful in case you want to register multiple keys, which you can use a switch with the id as argument, or if you want to know which key/modifier was pressed for some particular reason. */
Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF); // The key of the hotkey that was pressed.
KeyModifier modifier = (KeyModifier)((int)m.LParam & 0xFFFF); // The modifier of the hotkey that was pressed.
int id = m.WParam.ToInt32(); // The id of the hotkey that was pressed.
MessageBox.Show("Hotkey has been pressed!");
// do something
}
}
}
Теперь я могу sh проанализировать метод unregisterHotKey, чтобы очистить все горячие клавиши после закрытия программы (таким образом вы не сможете нажимать SHIFT-A, когда приложение закрыто)
UnregisterHotKey(this.Handle, 0);
Теперь мой вопрос: почему вы анализируете вышеизложенное из usercontrol для события formclosing в моей основной форме, чтобы все ключи будут успешно очищены, если это имеет смысл ...