Thread: SetTimer().. WM_TIMER window flickers

  1. #31
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by csonx_p View Post
    @Elysia, i have a feeling that putting the lines in blue outside WM_PAINT rather in WM_CREATE may improve the code efficiency (or reduce flickers as well)... In fact ASAIC the only thing that should now be within WM_PAINT is the bitblt function....
    Then what's the point of doing it at all?
    You must draw the new information you want to display on the screen on the memory DC.
    The whole point of the code is that you needed some part of the GUI to show updated information, was it not?

    Do you know what the following language in red is/means
    I'm afraid not. It seems to be german or something such.
    But between BeginPaint and EndPaint is where you put your drawing code for drawing on the window. That's probably where you'll want to blit your memory DC to the window.
    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.

  2. #32
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    @Elysia, i have a feeling that putting the lines in blue outside WM_PAINT rather in WM_CREATE may improve the code efficiency (or reduce flickers as well)... In fact ASAIC the only thing that should now be within WM_PAINT is the bitblt function.... Do you know what the following language in red is/means

    Code:
    	case WM_PAINT:
    		hdc = BeginPaint(hWnd, &ps);
    		// TODO: Fügen Sie hier den Zeichnungscode hinzu...
    		EndPaint(hWnd, &ps);
    		break;
    I have no idea what the red comment, but babelfish.yahoo.com says:
    Add the design code here in addition…
    I'd paraphrase this to
    Add your drawing code here...

    You need to put the drawing to the memory DC so that it's being done when you need to update what's on the display. Then you do the bitblt in the WM_PAINT section.

    --
    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. #33
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Btw, google says "/ / TODO: Add here the drawing code ..." so I'd say it's pretty accurate
    Better than both yahoo and altavista =)
    But I also suspect mats is right.
    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.

  4. #34
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    Then what's the point of doing it at all?
    You must draw the new information you want to display on the screen on the memory DC.
    The whole point of the code is that you needed some part of the GUI to show updated information, was it not?

    You need to put the drawing to the memory DC so that it's being done when you need to update what's on the display. Then you do the bitblt in the WM_PAINT section.

    In fact ASAIC the only thing that should now be within WM_PAINT is the bitblt function
    aren't we all saying the same thing? What i said is the creation of MemDC and buffering need not to be on WM_PAINT, but only when i do the bitblt...like i've done here

    Code:
    case WM_PAINT:
    
    hdc=BeginPaint(hwnd, &pntS);
    
    // Draw on screen from Memory DC
    BitBlt(hdc,0,0,dc_rect[which].right,dc_rect[which].bottom,my_DC_Buffer[which],0,0,SRCCOPY);
    
    EndPaint(hwnd, &pntS);
    perhaps the question is where exactly do i do the drawing of rectangles and text in MemDC, my answer is within WM_CREATE... i may be wrong

  5. #35
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by csonx_p View Post
    perhaps the question is where exactly do i do the drawing of rectangles and text in MemDC, my answer is within WM_CREATE... i may be wrong
    That's the thing we're telling you. If you draw everything in WM_CREATE, then the contents will only be drawn once and so there is no need for a memory DC at all.
    We may be thinking wrong because I'm not sure how your dialog looks like or if you're writing some text or information on the dialog either. Perhaps you should clarify that a bit.

    Do you write some sort of information inside some rectangles (ie text)?
    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.

  6. #36
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    That's the thing we're telling you. If you draw everything in WM_CREATE, then the contents will only be drawn once and so there is no need for a memory DC at all
    Make sense, i draw text on rectangles... So i guess have to put everything on WM_PAINT...

  7. #37
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not true. You need to think about this.
    WHERE would you update your GUI with new information? THIS is where you draw on the memory DC.
    In WM_PAINT, you simply blit the memory dc to the window dc (ie display the information on the window).
    It's logical, really.
    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. #38
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    Not true. You need to think about this.
    WHERE would you update your GUI with new information? THIS is where you draw on the memory DC.
    In WM_PAINT, you simply blit the memory dc to the window dc (ie display the information on the window).
    It's logical, really.
    AFAIC there are two places where things can get updated... Within WM_PAINT & WM_TIMER... so i can put code in either...

  9. #39
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then the logical place would be WM_TIMER, since you never know where WM_PAINT might be called from and hence would do unnecessary processing.
    Be sure to try it all out first. I don't know how all this will work out!
    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.

  10. #40
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    Then the logical place would be WM_TIMER, since you never know where WM_PAINT might be called from and hence would do unnecessary processing.
    Be sure to try it all out first. I don't know how all this will work out!
    do i still need to call these two lines int WM_TMER?
    Code:
    	InvalidateRect (hwnd, NULL, TRUE);
    	UpdateWindow(hwnd);

  11. #41
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    do i still need to call these two lines int WM_TMER?
    Code:
    	InvalidateRect (hwnd, NULL, TRUE);
    	UpdateWindow(hwnd);
    If you want to redraw at every WM_TIMER, yes. If you don't have anything that has changed, then you don't.

    --
    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. #42
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    My first compilation of the program...

    After made these major changes... Non of the rectangles are drawn and am getting a black background... Only thing drawn are edit controls... Bad start but will spend sometime checking what's happening! maybe now it's time to sit back and walk through my code and evaluate these new changes...

  13. #43
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    After made these major changes... Non of the rectangles are drawn and am getting a black background... Only thing drawn are edit controls... Bad start but will spend sometime checking what's happening! maybe now it's time to sit back and walk through my code and evaluate these new changes...
    Well, you must call Invalidate at some point if you want your application to draw onto an existing window. There will only be one call to WM_PAINT if there's no Invalidate calls (and your window isn't being hidden by some other window that is later removed).

    --
    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. #44
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by csonx_p View Post
    After made these major changes... Non of the rectangles are drawn and am getting a black background... Only thing drawn are edit controls... Bad start but will spend sometime checking what's happening! maybe now it's time to sit back and walk through my code and evaluate these new changes...
    This is partially why I suggested you make those ractangles labels instead.
    Then you can simply invalidate those labels instead of the whole window. It's much easier.
    Plus labels are made for text, so you can set the text on them with a WM_SETTEXT message (IIRC).
    If you get flicker in those labels, then it means you'll have to do double buffering like you're doing now.
    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.

  15. #45
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Well, you must call Invalidate at some point if you want your application to draw onto an existing window. There will only be one call to WM_PAINT if there's no Invalidate calls (and your window isn't being hidden by some other window that is later removed).

    --
    Mats
    Ok, let me show you what i've done

    1. In WM_PAINT

    Code:
    	case WM_PAINT:
    		{
    			hdc=BeginPaint(hwnd, &pntS);
    
    			//.... Function shown below
    			CreateBuffer(0, hdc);
    			
    			/* *** User Interface *** */
    			// In this area,  i draw 5 rectangles and display text (i call about 7 functions here)...
    
                            // .................
                            // .................
    
    
    			// Here i calculate & display results
    			output = ProcessData(hwnd);
    			DisplayResults(hwnd, my_DC_Buffer[0], &output, rsltFont);
    			
                            // Here, i Bitblt (function shown below)
    			CopyToScreen(hdc, 0);
    
    			EndPaint(hwnd, &pntS);
    		}
    		return 0;
    Function CreateBuffer

    Code:
    /* Create Device Context on buffer for client window */
    void CreateBuffer(int which, HDC hDc)
    {
    	GetClientRect(my_handle,&dc_rect[which]);
    //	my_DC = GetDC(my_handle);
    	my_DC_Buffer[which] = CreateCompatibleDC(hDc);
    	hbm_Buffer[which] = CreateCompatibleBitmap(hDc,dc_rect[which].right ,
    		dc_rect[which].bottom );
    	hbm_oldBuffer[which] = (HBITMAP) SelectObject(my_DC_Buffer[which],
    		hbm_Buffer[which]);
    }
    Function CopyToScreen()

    Code:
    /* Copy Device Context from buffer to screen */
    void CopyToScreen(HDC hdc, int which)
    {
    BitBlt(hdc,0,0,dc_rect[which].right,dc_rect[which].bottom,my_DC_Buffer[which],0,0,SRCCOPY);
    }
    Problems...

    1. The background is now black
    2. When i add InvalidateRect() in WM_TIMER, flickering is worse
    3. At first, i tried moving the drawing section to WM_TIMER, got problems there, then moved it back to WM_PAINT

    Concerns...

    When creating a CompatibleDC, should i use hdc from BeginPaint() or should i rather GetDC() of the ClientArea (note the commented GetDC()), then create a compatible DC from it?

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