我正在使用 Visual Studio 使用 ScreenRecorderLib 庫(但它與我的問題無關)和 c# 制作螢屏錄像機。這是我的第一個應用程式,盡管我使用 c# 統一制作了一些游戲。我想在錄制按鈕單擊或鍵盤快捷鍵(例如 crtl a)時最小化任務欄中的視窗。我設法使用
private void RecordButton_Click(object sender, EventArgs e)
{
ActiveControl = null;
try
{
if (_IsRecording)
{
//some actions to stop recording (not interresting for my question)
WindowState = FormWindowState.Normal;
}
UpdateProgress();
if (_rec == null)
{
// some actions to make settings active and launch the recording
WindowState = FormWindowState.Minimized;
}
}}
和我的快捷方式代碼
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode.ToString() == "A")
{
RecordButton.PerformClick();
}
}
這不是我真正的代碼,但我試圖為我的問題選擇一些有趣的部分。我設法使用快捷方式或按鈕將任務欄中的按鈕最小化,但在最小化時不使用快捷方式停止錄制。我希望在錄制和視窗最小化時能夠單擊 crtl a 停止錄制
有任何想法嗎???
uj5u.com熱心網友回復:
API:MouseKeyHook 中收集了“應用程式處于非活動狀態時如何使用快捷鍵”。
您只需要在適當的地方修改您需要的操作。

您也可以自己撰寫全域熱鍵。
只實作縮小視窗的功能:
private void Form1_Load(object sender, EventArgs e) {
this.KeyPreview = true;
}
private void Form1_KeyDown(object sender, KeyEventArgs e) {
if (e.Control && e.KeyCode == Keys.A) {
this.WindowState = FormWindowState.Minimized;
}
}
uj5u.com熱心網友回復:
https://www.fluxbytes.com/csharp/how-to-register-a-global-hotkey-for-your-application-in-c/#comments
這完美地回答了我的問題。對于想要擁有多個快捷方式的人,我會在下面放一個代碼。我是初學者,所以可能我的解釋不正確,但我嘗試以最簡單的方式和我理解的方式解釋它們
public partial class Form1 : Form
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id1, int fsModifiers, int vk);
//for creating another hotKey
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool RegisterHotKey1(IntPtr hWnd, int id2, int fsModifiers, int vk);
//here you can create even more shortcuts
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id1);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool UnregisterHotKey1(IntPtr hWnd, int id2);
//remember unreg them
enum KeyModifier
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
WinKey = 8
}
public Form1()
{
InitializeComponent();
int id1 = 0; // The id of the hotkey.
RegisterHotKey(this.Handle, id1, (int)KeyModifier.Shift, Keys.F1.GetHashCode());
int id2 = 1; // The 2nd id of the hotkey.
RegisterHotKey(this.Handle, id2, (int)KeyModifier.Shift, Keys.F2.GetHashCode());// Register Shift A as global hotkey.
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0x0312)
{
if (m.WParam.ToInt32() == 0)
{
RecordButton.PerformClick();
}
if(m.WParam.ToInt32() == 1)
{
PauseButton.PerformClick();
}
//add the other shortcuts just replace == 1 by the id value
}
}
private void ExampleForm_FormClosing(object sender, FormClosingEventArgs e)
{
UnregisterHotKey(this.Handle, 0); // Unregister hotkey with id 0 before closing the form. You might want to call this more than once with different id values if you are planning to register more than one hotkey.
UnregisterHotKey(this.Handle, 1);
}
//delete them with the id
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/335370.html
