Thread: SetTimer().. WM_TIMER window flickers

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    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.

  5. #5
    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

  6. #6
    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.

  7. #7
    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.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Are these rectangles windows? If not, it's best to make them labels.
    Then just invalidate those labels and it will redraw.
    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.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    Are these rectangles windows? If not, it's best to make them labels.
    Then just invalidate those labels and it will redraw.
    No they not, they are drawn on one window... How do you make them labels?

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You can also actually try to draw what you need on the invisible dc - and then just copy it - when whole buffer is ready - onto the screen dc
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by vart View Post
    You can also actually try to draw what you need on the invisible dc - and then just copy it - when whole buffer is ready - onto the screen dc
    Just read through some tutorial... Is this Double buffering you talking about? If so, i'm busy adding some code... Not sure how it works but will update you on my progress...

    The idea is...

    1. Create a DC on buffer
    2. Add/draw rectangles on Buffer DC
    3. Copy back to DC to client area using BitBlt()...

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by csonx_p View Post
    No they not, they are drawn on one window... How do you make them labels?
    You would just add labels to your window. They're called "static," I think. Create them just like any other windows. Labels are made to contain text and appears invisible to the dialog.

    So you could make labels and seeing as they are windows, draw upon them and invalidate them.

    Quote Originally Posted by csonx_p View Post
    Just read through some tutorial... Is this Double buffering you talking about? If so, i'm busy adding some code... Not sure how it works but will update you on my progress...

    The idea is...

    1. Create a DC on buffer
    2. Add/draw rectangles on Buffer DC
    3. Copy back to DC to client area using BitBlt()...
    Yep, it's double buffer and the general idea you have is correct.
    Basically, it's to avoid drawing anything on the window until you've finished drawing at which time you copy the data over to the window.
    It's similar to games which also tend to use some double buffering technique.
    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.

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    You would just add labels to your window. They're called "static," I think. Create them just like any other windows. Labels are made to contain text and appears invisible to the dialog.

    So you could make labels and seeing as they are windows, draw upon them and invalidate them.
    Will check this later.. now am busy wif double buffering....

    Yep, it's double buffer and the general idea you have is correct.
    Basically, it's to avoid drawing anything on the window until you've finished drawing at which time you copy the data over to the window.
    It's similar to games which also tend to use some double buffering technique.
    Q. Is it essential to first delete the DC on buffer only after you copied it to client area DC? Thing is i have about 5 rectangles. Currently i draw each on DC created by BegingPaint(), then delete after drawing it, draw next Rec, then delete until i finish drawing all... The example i have draws object(s) on Memory DC on a buffer, then copies the buffer DC to Client DC, then only after copying deletes the Buffer DC.... To clear my question, is it safe to draw all five rectangles without deleting each separately on Buffer DC, then delete all after copying back to Client DC... (Hope this question is clear)

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    You might try overriding WM_ERASEBKGRND and just return non zero.

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by DaveH View Post
    You might try overriding WM_ERASEBKGRND and just return non zero.
    The examples of this i've seen only in MFC

    Code:
     
      BOOL CRecordList::OnEraseBkgnd(CDC* pDC)
       {
          return FALSE;
       }
    How would one do this in WinAPI C? how do u override functions in C

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