Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Rephrasing my flickering problem

Sonx_hvn7Sonx_hvn7 Member Posts: 54

How about i rephrase my problem, perhaps may bring some light

I have a single window which i draw 5 rectangles with lots of text. 4 of these rectangles have at least each 2 edit controls for user input. The fifth rectangle is to display results calculated from the edited controls. I use the timer to update the window that whenever the user enters new data on edit controls, the results are immediately calculated.

Problem: Well, the initial compilation of the program produced terrible window flickering (could see a huge flickering from bottom up) but managed to remove that by setting wcx.hbrBackground to NULL when creating the window and also returning non-zero to WM_ERASEBKGND... The flickering is now happening on the edit controls only, not the entire window. And after a minute or so, the text display start loosing the font i used (non-bold & italics), every text suddenly becomes bold and no-italics. The borders of the rectangle also become thiner, noticed i also loose one rectangle...Also, if i leave this for a little longer, the window itself repaints badly and looses the title bar (or fades the entire blue bar), Infact, my entire screen fades some icons on the windows Start Menu bar if i let this run forever... To close my application i would have to do CNTRL+ALT+DEL. To me this seems more like memory issues (overloading buffers or something). As i said, i display a lot of text (declared as TCHAR) using TextOut() function and use CreateFont() to set my text fonts. If i comment out all my displaying of text leaving only the rectangles & edit controls to paint, i still do get flickering but not the changing of fonts and borders, and the title bar stays for longer.

All of this is after i have attempted doing the off-screen DC with bitblt... Am really stuck as to what the problem may be... Here's some of the code. It's a larger program so can't post everything here!

[code]
case WM_CREATE:
{
GetClientRect(hwnd, &clientRECT);

[color=Green]// Client area width & height[/color]
cScreenX=GetSystemMetrics(SM_CXSCREEN);
cScreenY=GetSystemMetrics(SM_CYSCREEN);

[color=Green]// Client Device Context[/color]
hDC=GetDC(hwnd);
memDC = CreateCompatibleDC(hDC);

[color=Green]// Memory Device Context[/color]
hBitmap = CreateCompatibleBitmap(hDC,cScreenX,cScreenY);
hOldBitmap = SelectObject(memDC,hBitmap);

[color=Green]// Paint screen background on mem DC grey[/color]
hBrush = CreateSolidBrush( RGB(0xD3, 0xD3, 0xD3) );
FillRect(memDC, &clientRECT, hBrush);
DeleteObject(hBrush);

ReleaseDC(hwnd,hDC);

[color=Green]/* Screen refresh time */[/color]
SetTimer(hwnd, IDT_REFRESH, REFRESHTIME, NULL);

[color=Green]/* Create edit controls */[/color]
EditBoxes(hwnd, cs);

[color=Green]/* Create radio buttons */[/color]
RadioButtons(hwnd, cs);
}
return 0;

case WM_PAINT:
{
hdc = BeginPaint(hwnd, &pntS);
BitBlt(hdc, 0, 0, cScreenX, cScreenY, memDC, 0, 0, SRCCOPY);
EndPaint(hwnd, &pntS);
}
return 0;

case WM_TIMER:
{
[color=Green]/* Begin rendering Data on Mem DC */[/color]


[color=Green]// Displaying text[/color]
PrintText(hwnd, rdrTrans_recv[FREQ], initRecArea(395,45,0,0), hdc,
fntClr[1].boldStyle, fntClr[0].bkgrngColor[0], fntClr[0].txtColor, fntClr[1].italicStyle);

[color=Green]// Retrieve data, calculate & display[/color]
output = ProcessData(hwnd);
DisplayResults(hwnd, memDC, &output, rsltFont);

InvalidateRect (hwnd, NULL, FALSE);
UpdateWindow(hwnd);
}
break;


[color=Green]// PrintText function[/color]
[color=Green]// Display text on screen[/color]
void PrintText(HWND hWnd, TCHAR txt[], RECT rc, HDC hDC, DWORD fnWeight,
COLORREF bkgrndClr, COLORREF txtColor, BOOL italics)
{
long lfHeight=-MulDiv(12, GetDeviceCaps(hDC, LOGPIXELSY), 72);
HFONT font=CreateFont(lfHeight, 0, 0, 0, fnWeight, italics,
0, 0, 0, 0, 0, 0, 0, _T("Times New Roman"));

HGDIOBJ hDefFont=SelectObject(hDC, font);

SetBkColor(hDC, bkgrndClr);
SetTextColor(hDC, txtColor);

TextOut(hDC, rc.left, rc.top, txt,(int)_tcslen(txt));

SelectObject(hDC, hDefFont);
DeleteObject(hDefFont);
}


As you may noticed, this code attempts to use Memory DC

Comments

  • AsmGuru62AsmGuru62 Member Posts: 6,519
    [color=Blue]The DC font was leaked - see RED in PrintText() function.

    WM_TIMER only gets calculations done and triggers WM_PAINT - you should remove the drawings from there.

    Also, did you use WS_CLIPCHILDREN style for your main window? This can reduce flicker too.[/color]
    :
    : How about i rephrase my problem, perhaps may bring some light
    :
    : I have a single window which i draw 5 rectangles with lots of text.
    : 4 of these rectangles have at least each 2 edit controls for user
    : input. The fifth rectangle is to display results calculated from the
    : edited controls. I use the timer to update the window that whenever
    : the user enters new data on edit controls, the results are
    : immediately calculated.
    :
    : Problem: Well, the initial compilation of the program produced
    : terrible window flickering (could see a huge flickering from bottom
    : up) but managed to remove that by setting wcx.hbrBackground to NULL
    : when creating the window and also returning non-zero to
    : WM_ERASEBKGND... The flickering is now happening on the edit
    : controls only, not the entire window. And after a minute or so, the
    : text display start loosing the font i used (non-bold & italics),
    : every text suddenly becomes bold and no-italics. The borders of the
    : rectangle also become thiner, noticed i also loose one
    : rectangle...Also, if i leave this for a little longer, the window
    : itself repaints badly and looses the title bar (or fades the entire
    : blue bar), Infact, my entire screen fades some icons on the windows
    : Start Menu bar if i let this run forever... To close my application
    : i would have to do CNTRL+ALT+DEL. To me this seems more like memory
    : issues (overloading buffers or something). As i said, i display a
    : lot of text (declared as TCHAR) using TextOut() function and use
    : CreateFont() to set my text fonts. If i comment out all my
    : displaying of text leaving only the rectangles & edit controls to
    : paint, i still do get flickering but not the changing of fonts and
    : borders, and the title bar stays for longer.
    :
    : All of this is after i have attempted doing the off-screen DC with
    : bitblt... Am really stuck as to what the problem may be... Here's
    : some of the code. It's a larger program so can't post everything
    : here!
    :
    : [code]:
    : case WM_CREATE:
    : {
    : GetClientRect(hwnd, &clientRECT);
    :
    : [color=Green]// Client area width & height[/color]
    : cScreenX=GetSystemMetrics(SM_CXSCREEN);
    : cScreenY=GetSystemMetrics(SM_CYSCREEN);
    :
    : [color=Green]// Client Device Context[/color]
    : hDC=GetDC(hwnd);
    : memDC = CreateCompatibleDC(hDC);
    :
    : [color=Green]// Memory Device Context[/color]
    : hBitmap = CreateCompatibleBitmap(hDC,cScreenX,cScreenY);
    : hOldBitmap = SelectObject(memDC,hBitmap);
    :
    : [color=Green]// Paint screen background on mem DC grey[/color]
    : hBrush = CreateSolidBrush( RGB(0xD3, 0xD3, 0xD3) );
    : FillRect(memDC, &clientRECT, hBrush);
    : DeleteObject(hBrush);
    :
    : ReleaseDC(hwnd,hDC);
    :
    : [color=Green]/* Screen refresh time */[/color]
    : SetTimer(hwnd, IDT_REFRESH, REFRESHTIME, NULL);
    :
    : [color=Green]/* Create edit controls */[/color]
    : EditBoxes(hwnd, cs);
    :
    : [color=Green]/* Create radio buttons */[/color]
    : RadioButtons(hwnd, cs);
    : }
    : return 0;
    :
    : case WM_PAINT:
    : {
    : hdc = BeginPaint(hwnd, &pntS);
    : BitBlt(hdc, 0, 0, cScreenX, cScreenY, memDC, 0, 0, SRCCOPY);
    : EndPaint(hwnd, &pntS);
    : }
    : return 0;
    :
    : case WM_TIMER:
    : {
    : [color=Green]/* Begin rendering Data on Mem DC */[/color]
    :
    :
    : [color=Green]// Displaying text[/color]
    : PrintText(hwnd, rdrTrans_recv[FREQ], initRecArea(395,45,0,0), hdc,
    : fntClr[1].boldStyle, fntClr[0].bkgrngColor[0], fntClr[0].txtColor, fntClr[1].italicStyle);
    : [color=Red]// This ^^^ block should be moved to WM_PAINT[/color]
    :
    : [color=Green]// Retrieve data, calculate & display[/color]
    : output = ProcessData(hwnd);

    : DisplayResults(hwnd, memDC, &output, rsltFont);
    : [color=Red]// This ^^^ line should be moved to WM_PAINT[/color]
    :
    : InvalidateRect (hwnd, NULL, FALSE);
    : UpdateWindow(hwnd);
    : }
    : break;
    :
    :
    : [color=Green]// PrintText function[/color]
    : [color=Green]// Display text on screen[/color]
    : void PrintText(HWND hWnd, TCHAR txt[], RECT rc, HDC hDC, DWORD fnWeight,
    : COLORREF bkgrndClr, COLORREF txtColor, BOOL italics)
    : {
    : long lfHeight=-MulDiv(12, GetDeviceCaps(hDC, LOGPIXELSY), 72);
    : HFONT font=CreateFont(lfHeight, 0, 0, 0, fnWeight, italics,
    : 0, 0, 0, 0, 0, 0, 0, _T("Times New Roman"));
    :
    : HGDIOBJ hDefFont=SelectObject(hDC, font);
    :
    : SetBkColor(hDC, bkgrndClr);
    : SetTextColor(hDC, txtColor);
    :
    : TextOut(hDC, rc.left, rc.top, txt,(int)_tcslen(txt));
    :
    : SelectObject(hDC, hDefFont);
    : [color=Red]//DeleteObject(hDefFont);
    : DeleteObject(font);[/color]
    : }
    :
    :
    : As you may noticed, this code attempts to use Memory DC
    :
  • Sonx_hvn7Sonx_hvn7 Member Posts: 54
    : The DC font was leaked - see RED in PrintText() function.
    :
    : WM_TIMER only gets calculations done and triggers WM_PAINT - you
    : should remove the drawings from there.
    :
    : Also, did you use WS_CLIPCHILDREN style for your main window? This
    : can reduce flicker too.

    [color=Blue]Thanks, Asmguru... No more memory leakages... The WS_CLIPCHILDREN gives "Failed to register wnd class" message... I have managed to solve the flickering problem completely by introducing clipped regions.. So all my rectangles are selected into the off-screen buffer as clipped regions.. I then only updated one clipped region (the results window) using InvalidateRgn instead of InvalidateRect... Thanks both to you and MT for your extensive help[/color]
Sign In or Register to comment.