[英]After button was pressed, get pixel properties from a picturebox only after mouse was clicked c#
[英]C# how to get mouse position after INSERT key if pressed, after button click?
當我按下一個鍵(插入)時,我需要知道如何獲得鼠標位置。
這就是我想要做的:
我有一個帶有一個buuton的form1,當你按下那個按鈕時它會調用另一個表格。 但在調用form2之前,我需要從外部應用程序獲取鼠標位置。 為此,用戶必須將光標懸停在請求的位置上並按“插入”。
public partial class _CalibrateGeneralStep2 : Form
{
public _CalibrateGeneralStep2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Application.Restart();
}
private void button2_Click(object sender, EventArgs e)
{
this.Hide();
///// HERE I NEED TO WAIT UNTIL USER PRESS 'INSERT' KEY BEFORE CALL _CalibrateGeneralStep3 /////
_CalibrateGeneralStep3 frm = new _CalibrateGeneralStep3();
frm.Show();
}
}
我嘗試使用按鍵和鍵盤,但我不知道使用它。
謝謝......抱歉,如果我的英語不好......
您可以使用表單的KeyDown
事件(您可以從Designer添加它以確保它正確連接)
由於你不能只等待button2_Click中的按鍵事件,我已經使用私有字段來存儲按鈕被按下的事實。 現在,每次用戶按Insert時,都會檢查按鈕是否被按下以及光標位置。 如果兩者都正確,則生成新表單。
我已經在類的頂部用2個常量定義了所需的光標位置,你還應該為“hasButton2BeenClicked”選擇一個更好的名稱,具體取決於你的業務環境哈哈。
public partial class _CalibrateGeneralStep2 : Form
{
private const int NEEDED_X_POSITION = 0;
private const int NEEDED_Y_POSITION = 0;
private bool hasButton2BeenClicked = false;
public _CalibrateGeneralStep2()
{
InitializeComponent();
KeyPreview = true;
}
private void button1_Click(object sender, EventArgs e)
{
Application.Restart();
}
private void button2_Click(object sender, EventArgs e)
{
hasButton2BeenClicked = true;
}
private void OnKeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Insert && IsCursorAtTheCorrectPosition() && hasButton2BeenClicked)
{
GoToNextStep();
}
}
private bool IsCursorAtTheCorrectPosition()
{
return Cursor.Position.X == NEEDED_X_POSITION && Cursor.Position.Y == NEEDED_Y_POSITION;
}
private void GoToNextStep()
{
this.Hide();
new _CalibrateGeneralStep3().Show();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.