【发布时间】:2014-10-03 22:01:25
【问题描述】:
我正在尝试从头开始创建一个复选框,但遇到了一些问题。
在我的 control.h 文件中我初始化了
public : int checked = 0;
所以每当鼠标按下正确的区域时,checked 就会变成一个。
而drawCheckBox 方法会检查它是1 还是0,并在方框上打勾。
程序运行,但是当我按下方框区域并检查 checked 的值时,它一直显示 0。我不知道为什么。
绘制复选框/检查用户是否选中
// Function to generate food selection box in left selection area.
void Control::drawCheckBox(string food, double x1, double y1, double x2, double y2)
{
glColor3f(0, 1, 1);
//placement of the check box
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glRectf(x1, y1, x2,y2);
// Draw black boundary.
glColor3f(0.0, 0.0, 0.0);
glLineWidth(5);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glRectf(x1, y1, x2, y2);
if (checked == 1)checker(x1, y1, x2, y2); // checks the check box.
else glColor3f(1, 1, 1);
cout << checked;
}
鼠标回调例程
// The mouse callback routine.
void mouseControl(int button, int state, int x, int y)
{
Control check;
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
// Store the clicked point in the points array after correcting
// from event to OpenGL co-ordinates.
//points.push_back(Point(x, height - y));
if ((x >= 5 && x <= 10) && (y <= 85 && y >= 80))
{
if (check.checked == 1)
{
check.checked = check.checked - 1;
}
else check.checked++;
}
if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) exit(0);
glutPostRedisplay();
}
【问题讨论】:
-
mouseControl()是否只在鼠标状态改变时被调用,还是每帧都会被调用(即在用户按住鼠标时不断调用)。您可以考虑为所有 if 语句使用大括号,否则很容易运行您不期望的代码。 -
从你的代码来看,
Control check;永远不会被实例化,只存在于你的 mouseControl 函数的范围内。
标签: c++ oop opengl graphics logic