如何在 OpenTK 中制作可点击的按钮?
How can I make a clickable button in OpenTK?
我正在尝试为 OpenTK 中的按钮制作一个 class。
有什么制作按钮的好方法吗?
如果鼠标悬停在上方时能显示说明,将不胜感激。
你有两个选择。
使用 GLControl
在 Windows 表单中嵌入 OpenTK 上下文。这里的缺点是不能在 OpenTK 中放置任何按钮 window,但根据您的目标,这可能不是问题。
自己渲染一个按钮并测试该区域内的鼠标按下情况。
选项 2 的工作量更大,但显然更通用,如果您刚开始,将会学到很多东西。
获取光标位置...
int mouseX = System.Windows.Forms.Cursor.Position.X;
int mouseY = System.Windows.Forms.Cursor.Position.Y;
检查鼠标按下...
MouseState mouseState = OpenTK.Input.Mouse.GetState();
bool leftMouseDown = mouseState.IsButtonDown(MouseButton.Left);
bool rightMouseDown = mouseState.IsButtonDown(MouseButton.Right);
并在每个像素坐标处绘制四边形...
float x1 = (float)x * 2 / screenWidth - 1;
float x2 = (float)(x + buttonWidth) * 2 / screenWidth - 1;
float y1 = (float)y * 2 / screenHeight - 1;
float y2 = (float)(y + buttonHeight) * 2 / screenHeight - 1;
朋友,剩下的就交给你了。
我正在尝试为 OpenTK 中的按钮制作一个 class。
有什么制作按钮的好方法吗?
如果鼠标悬停在上方时能显示说明,将不胜感激。
你有两个选择。
使用
GLControl
在 Windows 表单中嵌入 OpenTK 上下文。这里的缺点是不能在 OpenTK 中放置任何按钮 window,但根据您的目标,这可能不是问题。自己渲染一个按钮并测试该区域内的鼠标按下情况。
选项 2 的工作量更大,但显然更通用,如果您刚开始,将会学到很多东西。
获取光标位置...
int mouseX = System.Windows.Forms.Cursor.Position.X;
int mouseY = System.Windows.Forms.Cursor.Position.Y;
检查鼠标按下...
MouseState mouseState = OpenTK.Input.Mouse.GetState();
bool leftMouseDown = mouseState.IsButtonDown(MouseButton.Left);
bool rightMouseDown = mouseState.IsButtonDown(MouseButton.Right);
并在每个像素坐标处绘制四边形...
float x1 = (float)x * 2 / screenWidth - 1;
float x2 = (float)(x + buttonWidth) * 2 / screenWidth - 1;
float y1 = (float)y * 2 / screenHeight - 1;
float y2 = (float)(y + buttonHeight) * 2 / screenHeight - 1;
朋友,剩下的就交给你了。