【发布时间】:2011-02-02 16:53:13
【问题描述】:
我想使用 Mono 来允许我目前在 Windows 中运行的 C# 程序(目前是 WPF,但将其更改为 Windows Forms 或 Silverlight 以便 Mono 可以运行)在 Mac 上运行。
我使用原生 windows 代码来检测鼠标何时被点击,也可以用来点击鼠标,如下所示:
[DllImport("user32.dll")]
static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
[DllImport("User32.dll")]
public static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey);
为了让它在 Mac 上运行,我首先需要能够检测程序是在 Mac 上运行还是在 Windows 上运行,然后运行适当的代码来检测是否单击了鼠标,或者单击了老鼠。比如:
if (runningInWindows())
{
// Windows mouse click...
System.Windows.Forms.Cursor.Position = new System.Drawing.Point(X, Y);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
// Or if I want to detect a mouse click...
bool mouseClicked = GetAsyncKeyState(System.Windows.Forms.Keys.LButton) != 0;
}
else
{
// Running on a Mac, so do Mac mouse click...
// Or detect a mouse click on a Mac...
}
基本上我有 3 个问题:
- 如何在 Mac 上单击鼠标? (相当于 mouse_event)
- 如何检测 Mac 上的鼠标点击? (相当于 GetAsyncKeyState)
- 如何检测您的应用程序在哪个操作系统中运行?
【问题讨论】: