How to get middlemouse click event in C++

2019-03-05 17:19发布

问题:

How can I respond to a middle mouse click in C++/OpenGL?

I know it may have to do with the WM_MBUTTONDOWN event however I am completely clueless with using it. I am also unfamiliar with callback functions so if it has to be used, can it be thoroughly explained? Can anyone show me how to implement the code for middle mouse click event?

回答1:

You can try and implement the oldschool version with the WM_ commands, but I think it'll be a lot easier to use GLUT (since it really makes life with OpenGL easier).

#include <GL/glut.h>
void myMouseHandleFunction(int button, int state, int x, int y){
   if(button==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN) std::cout << "Pressed middle mouse button!";
}


int main(int argc, char *argv[]){
   glutInit(&argc, argv);
   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
   glutInitWindowSize(500, 500);
   glutInitWindowPosition(300, 200);
   glutCreateWindow("Hello World!");
   glutMouseFunc(myMouseHandleFunction);
   glutMainLoop();
   return 0;
}

If you're doing a simple app, GLUT will be sufficient. If you wanna do something more complicated, try freeglut or openglut. The old, basic GLUT doesn't handle the mouse wheel, so if you want to check for that - you'll need one of those two.