window won't bounce

This is a discussion on window won't bounce within the Windows Programming forums, part of the Platform Specific Boards category; I am writting a code in which the window is supposed to bounce on the screen from top to bottom ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    window won't bounce

    I am writting a code in which the window is supposed to bounce on the screen from top to bottom and left to right as soon as the user clicks the left botton in the client area, but its getting stuck as soon as it reaches the bottom. Help..

    Code:
    #include <windows.h>
    #include <tchar.h>  
    #include <stdio.h> 
    #include<time.h>
    
    #undef WS_OVERLAPPEDWINDOW
    #define WS_OVERLAPPEDWINDOW (WS_OVERLAPPED     | \
    						 WS_SYSMENU        | \
                                 WS_THICKFRAME   )
    
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
         static TCHAR szAppName[] = TEXT ("Connect") ;
         HWND         hwnd ;
         MSG          msg ;
         WNDCLASS     wndclass ;
         
         wndclass.style         = WS_EX_DLGMODALFRAME ;
         wndclass.lpfnWndProc   = WndProc ;
         wndclass.cbClsExtra    = 0 ;
         wndclass.cbWndExtra    = 0 ;
         wndclass.hInstance     = hInstance ;
         wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
         wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
         wndclass.lpszMenuName  = NULL ;
         wndclass.lpszClassName = szAppName ;
         
         if (!RegisterClass (&wndclass))
         {
              MessageBox (NULL, TEXT ("Program requires Windows NT!"), 
                          szAppName, MB_ICONERROR) ;
              return 0 ;
         }
         
         hwnd = CreateWindow (szAppName, TEXT ("C"),
                              WS_OVERLAPPEDWINDOW,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              100, 100,
                              NULL, NULL, hInstance, NULL) ;
         
         ShowWindow (hwnd, iCmdShow) ;
         UpdateWindow (hwnd) ;
         
         while (GetMessage (&msg, NULL, 0, 0))
         {
              TranslateMessage (&msg) ;
              DispatchMessage (&msg) ;
         }
    
    return msg.wParam ;
    }
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
         TCHAR s[100];
         static int   iCount ;
         HDC          hdc ;
         int          i, j, x, y, sx = 1, sy = 1, bottom, right, top, left  ;
         PAINTSTRUCT  ps ;
    	 RECT r,win;
    
    	 top = left = 0;
    	 right = GetSystemMetrics(SM_CXSCREEN) - 100;
    	 bottom = GetSystemMetrics(SM_CYSCREEN) - 100;
         switch (message)
         {
    	 case WM_LBUTTONDOWN:
    		 srand(time(NULL));
    		 GetWindowRect(hwnd,&r);
    		 while(1)
    		 {
    			 x = rand() % 5 + 1;
    			 y = rand() % 5 + 1;
    			 while((r.bottom < bottom) && (r.right < right) && (r.top > top) && (r.left > left))
    			 {
    				 r.left += (x*sx);
    				 r.top += (y*sy);
    				 MoveWindow(hwnd,r.left,r.top,100,100, TRUE);
    				 GetWindowRect(hwnd,&r);
    			 }
    
    			 
    			 if(r.bottom>= bottom)
    				 sy = -1;
    			 else if(r.right>= right)
    				 sx = -1;
    			 else if(r.top <= top)
    				 sy = 1;
    			 else if(r.left<=left)
    				 sx = 1;
    		 }
    		 return 0;
    		 
    	 case WM_PAINT:
    		 hdc = BeginPaint(hwnd, &ps);
    		 TextOut (hdc, 0, 0, TEXT("Click Me"),4) ;
    		 EndPaint(hwnd, &ps);
    		 return 0;
                   
         case WM_DESTROY:
              PostQuitMessage (0) ;
              return 0 ;
         }
         return DefWindowProc (hwnd, message, wParam, lParam) ;
    }
    Last edited by juice; 02-19-2012 at 02:00 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > while(1)
    Well it's not going anywhere after it gets into this loop.

    IIRC, you're going to get a whole series of WM_LBUTTONDOWN messages, so try just moving a few pixels and then exiting the wndproc


    > srand(time(NULL));
    This you do ONCE when the program starts.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by Salem View Post
    so try just moving a few pixels and then exiting the wndproc
    Am not being lazy, but how is that done??

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    I tried this such that the window gets displaced when the mouse pointer is brought on the client area, but the same problem exists..

    Code:
    #include <windows.h>
    #include <tchar.h>  
    #include <stdio.h> 
    #include<time.h>
    
    #undef WS_OVERLAPPEDWINDOW
    #define WS_OVERLAPPEDWINDOW (WS_OVERLAPPED     | \
    							 WS_SYSMENU        | \
                                 WS_THICKFRAME   )
    
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
         static TCHAR szAppName[] = TEXT ("Connect") ;
         HWND         hwnd ;
         MSG          msg ;
         WNDCLASS     wndclass ;
         
         wndclass.style         = WS_EX_DLGMODALFRAME ;
         wndclass.lpfnWndProc   = WndProc ;
         wndclass.cbClsExtra    = 0 ;
         wndclass.cbWndExtra    = 0 ;
         wndclass.hInstance     = hInstance ;
         wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
         wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
         wndclass.lpszMenuName  = NULL ;
         wndclass.lpszClassName = szAppName ;
         
         if (!RegisterClass (&wndclass))
         {
              MessageBox (NULL, TEXT ("Program requires Windows NT!"), 
                          szAppName, MB_ICONERROR) ;
              return 0 ;
         }
         
         hwnd = CreateWindow (szAppName, TEXT ("C"),
                              WS_OVERLAPPEDWINDOW,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              100, 100,
                              NULL, NULL, hInstance, NULL) ;
         
         ShowWindow (hwnd, iCmdShow) ;
         UpdateWindow (hwnd) ;
         
    	 srand(time(NULL));
         while (GetMessage (&msg, NULL, 0, 0))
         {
              TranslateMessage (&msg) ;
              DispatchMessage (&msg) ;
         }
    
    return msg.wParam ;
    }
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
         TCHAR s[100];
         static int   iCount,bottom, right, top, left, sx = 1, sy = 1   ;
         HDC          hdc ;
         int          i, j, x, y;
         PAINTSTRUCT  ps ;
    	 RECT r,win;
    
    	 top = left = 0;
    	 right = GetSystemMetrics(SM_CXSCREEN) - 100;
    	 bottom = GetSystemMetrics(SM_CYSCREEN) - 100;
         switch (message)
         {
    	 case WM_LBUTTONDOWN:
    		 
    		 GetWindowRect(hwnd,&r);
    			 x = rand() % 5 + 1;
    			 y = rand() % 5 + 1;
    			 while((r.bottom < bottom) && (r.right < right) && (r.top > top) && (r.left > left))
    			 {
    				 r.left += (x*sx);
    				 r.top += (y*sy);
    				 MoveWindow(hwnd,r.left,r.top,100,100, TRUE);
    				 GetWindowRect(hwnd,&r);
    			 }
    
    			 
    			 if(r.bottom>= bottom)
    				 sy = -1;
    			 else if(r.right>= right)
    				 sx = -1;
    			 else if(r.top <= top)
    				 sy = 1;
    			 else if(r.left<=left)
    				 sx = 1;
    		 
    			
    		 return 0;
    		 
    	 case WM_MOUSEMOVE:
    			 SendMessage(hwnd, WM_LBUTTONDOWN,wParam,lParam);
    			 return 0;
    	 
    	 case WM_PAINT:
    		 hdc = BeginPaint(hwnd, &ps);
    		 TextOut (hdc, 0, 0, TEXT("Click Me"),8) ;
    		 EndPaint(hwnd, &ps);
    		 return 0;
                   
         case WM_DESTROY:
              PostQuitMessage (0) ;
              return 0 ;
         }
         return DefWindowProc (hwnd, message, wParam, lParam) ;
    }

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Is there some way to debug a windows program line by line, by stepping into the WndProc, just like we do in the console programs?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > while((r.bottom < bottom) && (r.right < right) && (r.top > top) && (r.left > left))
    Change this to an if statement as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Is there some way to debug a windows program line by line, by stepping into the WndProc, just like we do in the console programs?

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by juice View Post
    Is there some way to debug a windows program line by line, by stepping into the WndProc, just like we do in the console programs?
    What IDE are you using?

    Cheap animation can be created with a timer. I have 4 of your little windows bouncing around my screen right now.
    Code:
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	static int bottom, right, sx = 1, sy = 1, x, y;
    	static BOOL timerOn = FALSE;
    
    	HDC hdc ;
    	PAINTSTRUCT ps ;
    	RECT r;
    
    	switch (message)
    	{
    	case WM_CREATE:
    		right  = GetSystemMetrics(SM_CXSCREEN) - 10;
    		bottom = GetSystemMetrics(SM_CYSCREEN) - 10;
    		return 0;
    
    	case WM_TIMER:
    		GetWindowRect(hwnd, &r);
    
    		if      (r.bottom >= bottom) sy = -1;
    		else if (r.top    <= 0     ) sy =  1;
    
    		if      (r.right  >= right)  sx = -1;
    		else if (r.left   <= 0    )  sx =  1;
    
    		r.left += x * sx;
    		r.top  += y * sy;
    
    		MoveWindow(hwnd, r.left, r.top, 100, 100, TRUE);
    
    		return 0;
    
    	 case WM_LBUTTONDOWN:
    		if (timerOn) {
    			KillTimer(hwnd, 1);
    			timerOn = FALSE;
    			return 0;
    		}
    		x = rand() % 10 + 5;
    		y = rand() % 10 + 5;
    		sx = rand() % 2 ? 1 : -1;
    		sy = rand() % 2 ? 1 : -1;
    		SetTimer(hwnd, 1, 50, 0);
    		timerOn = TRUE;
    		return 0;

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    385
    Quote Originally Posted by oogabooga View Post
    what ide are you using?
    msvc 2010

  10. #10
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    >>Is there some way to debug a windows program line by line, by stepping into the WndProc, just like we do in the console programs?

    Set a break point (F9) on the line you want to debug (ie in the LB down) and run/start [can not be a 'release' build but MSVC builds 'debug' by default].

    When the line of code is hit the app will pause. Use F10 to step line by line. F11 to step into functions. F5 to run to next break point.

    Beware of setting break points in things like paint, which would generate a paint msg, which would generate a paint msg, which would generate a paint msg, which would generate a paint msg (ie infinite loop)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bounce - Dealing With It
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-25-2006, 02:50 PM
  2. need help making a dot bounce up and down y axis in this prog
    By redwing26 in forum Game Programming
    Replies: 10
    Last Post: 08-05-2006, 01:48 PM
  3. Retrieving the window handle of a window a control resides in
    By -leech- in forum Windows Programming
    Replies: 7
    Last Post: 08-09-2003, 09:55 AM
  4. bounce angles
    By canine in forum Windows Programming
    Replies: 1
    Last Post: 09-04-2002, 10:47 PM
  5. Bounce around screen
    By Akilla in forum C++ Programming
    Replies: 1
    Last Post: 07-19-2002, 11:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21