在.NET中交换鼠标左键和右键

在.NET中交换鼠标左键和右键,.net,mouse,user32,.net,Mouse,User32,如何在.NET(最好是C#)中交换鼠标左键和右键?基本上,结果应该与用户通过控制面板选中鼠标属性中的“切换主按钮和次按钮”复选框时的结果相同。我正在处理Windows XP,以防出现问题。您可以使用Windows API调用SwapMouseButton: using System.Runtime.InteropServices; // ... [DllImport("user32.dll")] public static extern Int32 SwapMouseButton(Int32

如何在.NET(最好是C#)中交换鼠标左键和右键?基本上,结果应该与用户通过控制面板选中鼠标属性中的“切换主按钮和次按钮”复选框时的结果相同。我正在处理Windows XP,以防出现问题。

您可以使用Windows API调用
SwapMouseButton

using System.Runtime.InteropServices;

// ...

[DllImport("user32.dll")]
public static extern Int32 SwapMouseButton(Int32 bSwap);

// ...

// Swap it.
SwapMouseButton(1); 

// Back to normal.
SwapMouseButton(0); 
就是这样。

类似这样的东西:

using Microsoft.Win32;

var key = Registry.CurrentUser.CreateSubKey("Control Panel\\Mouse\\");
var newValue = key.GetValue("SwapMouseButtons");

if (newValue == null) newValue = "1";
else                  newValue = Int32.Parse(newValue) == 1 ? "0" : "1";

key.SetValue("SwapMouseButtons", newValue, RegistryValueKind.String);

你说的交换是什么意思。。。您想为自己的应用程序进行系统级交换还是交换?谢谢。可能想补充一点,您需要“使用System.Runtime.InteropServices;”哎呀;我确实没有提到这一点。我将添加它以供将来参考。这可以工作,但在用户注销后不会保存状态。要记住交换状态,您必须将Porges的解决方案用于注册表。