View Full Version : updating window coordinates in real time

09-15-2004, 08:39 PM
Hi to everyone, today I stumbled upon the following problem, using glut and Windows:
I have a window on screen for which I store the screen coordinates of the window, like: left, top, right, bottom. I show these coordinates on screen. I'd like to update these coordinates pixel by pixel as the user reshapes or moves the window.
For reshaping, there is the reshape callback, so no problemo there. But what about moving the window? I tried doing the update in the display callback, but the coordinates update as soon as I release the mouse button.
Has anyone stumbled upon this before? I thought after that I could use glutMotionFunc or PassiveMotionFunc callbacks, but these require that the mouse is within the window (not in the window border). Thanks for your time.

09-15-2004, 11:28 PM
This is pretty easy to do when using the win32 api, but there is no way to have glut do it for you, so as long as you use glut the answer is no.


09-15-2004, 11:49 PM
well, there is a sneaky way to do it under win32, using window sub-classing. use this code at your own risk :)

#include <windows.h>
#include "glut.h"

// for glut's window proc
WNDPROC oldWndProc;

// our new window proc
LRESULT CALLBACK newWndProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
RECT* rect;
switch( uMsg )
// watch out, moving rect!
rect = (RECT*)lParam;
return CallWindowProc( oldWndProc, hwnd, uMsg, wParam, lParam );

int main( int argc, char** argv )
// typical glut setup here, nothing unusual
glutInit( &amp;argc, argv );
glutInitDisplayMode( ... );
glutInitWindowSize( ... );
glutInitWindowPosition( ... );
glutCreateWindow( argv[0] );

// !!! sneaky part !!!
// find our window
// note the name matches the window title
HWND hWnd = FindWindow( NULL, argv[0] );
if( hWnd )
// sub-class the window proc
oldWndProc = (WNDPROC)GetWindowLongPtr( hWnd, GWL_WNDPROC );
if( oldWndProc )
SetWindowLongPtr( hWnd, GWL_WNDPROC, (LONG)newWndProc );

// and the rest is completely as usual
// ...

09-16-2004, 03:48 AM
Thanks guys. I guess I have to think about it because in the end this is supposed to be a beginners program, talking about viewports and windows, so I don't know how much I want to put in win32 api calls in there (it might scare people off). I'll think about it but thanks for the options :)