4

OnClickそのボタンをクリックするたびにトリガーするボタンがあります。どのマウスボタンがそのボタンをクリックしたか知りたいですか?

Mouse.LeftButtonまたはを使用すると、どちらもクリック後の状態である「実現Mouse.RightButton」を教えてくれます。

どれが私のボタンをクリックしたか知りたいだけです。に変更EventArgsするとMouseEventArgs、エラーが発生します。

XAML: <Button Name="myButton" Click="OnClick">

private void OnClick(object sender, EventArgs e)
{
//do certain thing. 
}
4

3 に答える 3

6

以下のようにキャストできます:

MouseEventArgs myArgs = (MouseEventArgs) e;

そして、次の情報を取得します。

if (myArgs.Button == System.Windows.Forms.MouseButtons.Left)
{
    // do sth
}

このソリューションはVS2013で機能し、MouseClickイベントを使用する必要はありません;)

于 2014-09-18T12:20:42.637 に答える
2

ButtonのClickイベントを使用しているだけの場合、それを起動するマウスボタンはプライマリマウスボタンのみです。

それでも左ボタンか右ボタンかを具体的に知る必要がある場合は、SystemInformationを使用して取得できます。

void OnClick(object sender, RoutedEventArgs e)
    {
        if (SystemParameters.SwapButtons) // Or use SystemInformation.MouseButtonsSwapped
        {
            // It's the right button.
        }
        else
        {
            // It's the standard left button.
        }
    }

編集: SystemInformationに相当するWPFはSystemParametersであり、代わりに使用できます。ただし、アプリケーションに悪影響を与えることなくSystemInformationを取得するための参照として、System.Windows.Formsを含めることができます。

于 2009-06-10T23:18:24.897 に答える
0

そうです、ホセ、それはMouseClickイベントです。ただし、少しデリゲートを追加する必要があります。

this.button1.MouseDown + = new System.Windows.Forms.MouseEventHandler(this.MyMouseDouwn);

そして、あなたのフォームでこの方法を使用してください:

    private void MyMouseDouwn(object sender, MouseEventArgs e) 
    {
        if (e.Button == MouseButtons.Right)
           this.Text = "Right";

        if (e.Button == MouseButtons.Left)
            this.Text = "Left";
    }
于 2009-06-10T19:23:38.833 に答える