Thread: SetTimer().. WM_TIMER window flickers

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    SetTimer().. WM_TIMER window flickers

    How do i avoid this?

    Code:
    	case WM_CREATE:
    		{
    			/* Create timer to refresh screen */ 
    			SetTimer(hwnd, IDT_REFRESH, (UINT)0.005,(TIMERPROC) NULL); 
    
            ............
            ............
    
    	case WM_TIMER: 
    		{
    			// Refresh screen
    			InvalidateRect (hwnd, NULL, TRUE);
    			UpdateWindow(hwnd);
    		}
    		break;
    My whole window is flickering and when selecting menu, sometimes menu is black (wont see text on menu) until i move mouse over menu then text clears out.. Lets just say the window operations would not function properly if i use timer as shown... I just wanna refresh screen every milliseconds

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Are you SURE that you want t a time of 0? (UINT)0.005 will turn into zero, as UINT is an integer, and 0.005 is less than 1.0. And although it gets changed to USER_TIMER_MINIMUM, you probably want to do something else in the first place.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Are you SURE that you want t a time of 0? (UINT)0.005 will turn into zero, as UINT is an integer, and 0.005 is less than 1.0. And although it gets changed to USER_TIMER_MINIMUM, you probably want to do something else in the first place.

    --
    Mats
    increasing the value doesn't really make much difference... made it 5 with no difference

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I wouldn't be surprised. You're going to have to expect havoc if you set it to refresh every 5 ms.
    Not to mention you are eating significant CPU time and thereby wasting it by doing so.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    increasing the value doesn't really make much difference... made it 5 with no difference
    5 milliseconds is 200 times a second - do you REALLY need to update your screen that often (by the way, I _THINK_ USER_TIMER_MINIMUM is 10, so setting it to less than that would clamp it to 10).

    Try something like 20 or 50, and see if that makes a difference.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    5 milliseconds is 200 times a second - do you REALLY need to update your screen that often (by the way, I _THINK_ USER_TIMER_MINIMUM is 10, so setting it to less than that would clamp it to 10).

    Try something like 20 or 50, and see if that makes a difference.

    --
    Mats
    made it 1000, well much better but can see there's something happening on the screen every second... I'm sure there must be something to smooth out the flickering completely.. Anywayz, suppose i can leave with that minimal flickering

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    1000 milliseconds is 1 second, which is quite often. I suggested a value of about 20-50 (50-20 Hz, which happens to be roughly the range where things stop flickering and start being smooth - perhaps 35 is a good value -> ~28Hz update frequency ?)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    1000 milliseconds is 1 second, which is quite often. I suggested a value of about 20-50 (50-20 Hz, which happens to be roughly the range where things stop flickering and start being smooth - perhaps 35 is a good value -> ~28Hz update frequency ?)

    --
    Mats
    lost you now... 35ms or 35s (35000)

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    35 milliseconds. 35 seconds would definitely make the updates "jump" (unless the user got bored and looked away before the screen got updated).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    35 milliseconds. 35 seconds would definitely make the updates "jump" (unless the user got bored and looked away before the screen got updated).

    --
    Mats
    Anything less than 500 doesn't look good. In fact since i'm displaying a lot of text with different font levels, after a little while with the flickering suddenly the font changes all text to bold, and removes any italics existing on text.. Some stages aligns text incorrectly!

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I suspect you are entering your WM_PAINT twice within a single draw.

    So if less than 500 is not good, then you probably better stick with the jerky 1000 - not much difference if it's jerky twice as often.

    Another thought would of course be to not update as much of the screen each time - perhaps you can draw just parts of it?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    I suspect you are entering your WM_PAINT twice within a single draw.

    So if less than 500 is not good, then you probably better stick with the jerky 1000 - not much difference if it's jerky twice as often.

    Another thought would of course be to not update as much of the screen each time - perhaps you can draw just parts of it?

    --
    Mats
    This is my messages switch statement

    Code:
    	switch (uMsg)
    	{
    	case WM_CREATE:
    		{
    			/* Create timer to refresh screen */ 
    			SetTimer(hwnd, IDT_REFRESH, REFRESHTIME,(TIMERPROC) NULL); 
    
    			/* Create edit controls */
    			EditBoxes(hwnd, cs);
    
    			/* Create radio buttons */
    			RadioButtons(hwnd, cs);
    		}
    		return 0;
    
    	case WM_PAINT:
    		{
    			hdc=BeginPaint(hwnd, &pntS);
    
    			/* *** User Interface *** */
    			DrawArea(hwnd, hdc, colors);
    			RadioButtonsText(hwnd, hdc, polarLbl, stdFont);
    			TargetText(hwnd, targetLbl, sqm, mtrs, hdc, stdFont);
    			GenParams(hwnd, genPara, fareRng, dpRng, hdc, stdFont);
    			RadaAntennaText(hwnd, rdrAntenaLbl, deg, db, halfMin, mtrs, hdc, stdFont);
    			DisplayResultsBox(hwnd, hdc, rdrTrans_recv, targetLbl, rsltFont);
    			RadarReceiverText(hwnd, rdrTrans_recv, hz, mhz, mtrs, kw, usec, db, wvlnthFig, hdc, stdFont);
    			
    			// Calculate & show results
    			output = ProcessData(hwnd);
    			DisplayResults(hwnd, hdc, &output, rsltFont);
    
    			EndPaint(hwnd, &pntS);
    		}
    		return 0;
    
    	case WM_COMMAND:
    		{
    			// Listen to the input commands
    			switch( LOWORD(wParam) )
    			{
    			case ID_FILE_EXIT:
    				{
    					PostMessage(hwnd, WM_CLOSE, 0, 0);
    					break;
    				}
    
    			case ID_EDIT_CLEAR01:
    				{
    					ClearEditControls(hwnd, indata);
    					break;
    				}
    			case ID_FILE_OPEN01:
    				{
    					// Load data from file to edit controls
    					DoFileOpen(hwnd);
    					SetValues(hwnd, &indata);
    					break;
    				}
    			case ID_FILE_SAVEAS:
    				{
    					// Save data on edit controls to file
     					DoFileSaveAs(hwnd);
    					break;
    				}
    			case ID_FILE_SAVE01:
    				{
    					// Save data on edit controls to file
    					WriteToFile(hwnd, "RFSDataFile.dat");
    					break;
    				}
    			}
    		}
    		return 0;
    
    	case WM_GETDLGCODE:
    		{
    			break;
    		}
    	case WM_TIMER: 
    		{
    			// Refresh screen
    			InvalidateRect (hwnd, NULL, TRUE);
    			UpdateWindow(hwnd);
    		}
    		break;
    
    	case WM_DESTROY:
    		KillTimer(hwnd, IDT_REFRESH);
    		PostQuitMessage(0);    
    		return 0;
    	default:
    		//let system deal with msg
    		return DefWindowProc(hwnd,uMsg,wParam,lParam);  
    	}

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That's not much help - I'm just asking if you need to redraw everything. I can't say from that what actually happens in your functions that do the actual drawing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    That's not much help - I'm just asking if you need to redraw everything. I can't say from that what actually happens in your functions that do the actual drawing.

    --
    Mats
    Actually i need to redraw a portion of the window. I have five rectangles (4 have edit boxes, 1 has results table) in one window. Really i only need to update the results (the Fifth rectangle) every time thew user changes the values on the other 4 rectangles... Any suggestions on how to do this?

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    Actually i need to redraw a portion of the window. I have five rectangles (4 have edit boxes, 1 has results table) in one window. Really i only need to update the results (the Fifth rectangle) every time thew user changes the values on the other 4 rectangles... Any suggestions on how to do this?
    Perhaps call InvalidateRect() at the time when you need to update?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  2. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  3. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  4. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  5. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM