我在C#Form应用程序中编写了以下代码,试图从Leap Motion设备获取x,y
和z
坐标。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//controller.EventContext = WindowsFormsSynchronizationContext.Current;
button1.Click += new EventHandler(button1_Click);
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button1_Click(object sender, EventArgs e)
{
// Initialize the Controller object which connects to the Leap motion service
// and captures the hand tracking data
Controller controller = new Controller();
//Get the most recent tracking data using the Frame object
Frame frame = controller.Frame();
for (int h = 0; h < frame.Hands.Count; h++)
{
// Initialize the Hand in the given frame
Hand leapHand = frame.Hands[h];
// Get the "Pointer" finger of current hand which refers to where a person is pointing
Finger leapFinger = leapHand.Fingers[1];
// Prepare a vector which will store the co-ordinate values of the tip of the pointer
Vector currentPosition = leapFinger.StabilizedTipPosition;
textBox1.Text = Convert.ToString(currentPosition.x);
textBox2.Text = Convert.ToString(currentPosition.y);
textBox3.Text = Convert.ToString(currentPosition.z);
}
}
}
但是,我需要明确单击button1
来显示。
你知道怎么了吗?
答案 0 :(得分:0)
这是一种实现方法的过于简单的示例。我并不是说这是最好的方法,但是您应该能够使用您的代码修改此示例代码。
在此示例中,有一个简单的“ HelloWorld”方法,在初始化表单以及每当您按下按钮时都会调用该方法。让我知道这是否使您接近所追求的目标。
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.Click += new EventHandler(button1_Click);
HelloWorldTest();
}
private void button1_Click(object sender, EventArgs e)
{
HelloWorldTest();
}
private void HelloWorldTest()
{
MessageBox.Show("Hello World!");
}
}
}
关于它的价值,我没有使用您的代码,因为我没有安装Leap库,而且如果有错字,我也将无济于事。尽管如此,希望它能使您走上正确的道路。