Link to home
Start Free TrialLog in
Avatar of cophi
cophi

asked on

Flicker free window for ticker

Hi,

I have a custom activex that is basically a ticker(i.e. news ticker)  But the word's keep flashing on the screen.  Anyway to prevent this?

void CTickerAtxCtrl::OnDraw(
                  CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{

      CFont *tFont = new CFont();
      tFont->CreateFont(36, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, 0, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_ROMAN, "Times New Roman");

      CFont* pOldFont = (CFont*)pdc->SelectObject(tFont);

      if (m_bNeedToInitRenderer == true) {
            
            m_textLenInPixels = pdc->GetTextExtent(m_ticker).cx;
            m_xPosOfText = rcBounds.right;
            width = rcBounds.right;

            m_bNeedToInitRenderer = false;
      }
      if (m_bCtrlResized == true) {
            Reset();
            m_bCtrlResized = false;
      }

      CRect r = rcBounds;

      if (m_textLenInPixels + m_xPosOfText == 0) {
            Reset();
      }

      r.left = m_xPosOfText--;
      pdc->SetBkColor(white);
      pdc->FillSolidRect(rcBounds, white);
      pdc->SetTextColor(black);
      pdc->DrawText(m_ticker, -1, r, DT_SINGLELINE | DT_VCENTER);

      pdc->SelectObject(pOldFont);
}
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

One thing that can help is to stop the default  background painting.
Trap the WM_ERASEBKGND and in the stub just return true.
eg.
BOOL CMyWnd::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}


NOTE - you must repaint the complete control in the OnDraw function
eg.
void CTickerAtxCtrl::OnDraw(
               CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
CRect rc;
GetClientRect(&rc);
pdc->FillSolidRect(&rc, RGB(255, 0, 0));   <<----------insert colour you want here
Why - Windows does drawing in two stages.  First the background then the foreground.  Between the two *could* be a delay - hence the flicker.  Returning true from the WM_ERASEBKGND message handler stops windows doing the first step, you are instructing the OP system that you will do it yourself / have done it yourself.
Avatar of cophi
cophi

ASKER

The text is still flashing....

I captured EraseBkgnd and I am passing back True.  

I am not doing

>>NOTE - you must repaint the complete control in the OnDraw function
>>eg.
>>void CTickerAtxCtrl::OnDraw(
>>               CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
>>{
>>CRect rc;
>>GetClientRect(&rc);
>>pdc->FillSolidRect(&rc, RGB(255, 0, 0));   <<----------insert colour you want here

because I assumed my code already took care of that

     pdc->SetBkColor(white);
     pdc->FillSolidRect(rcBounds, white);
     pdc->SetTextColor(black);
     pdc->DrawText(m_ticker, -1, r, DT_SINGLELINE | DT_VCENTER);
This will reduce the flashing.  
If you are calling OnDraw 10 times per second it will flash just because of that - make certain you only call OnDraw when you do need to change the display.
Avatar of cophi

ASKER

Well since its a ticker... yeah its changing probably 10 times per second, but  I just thought there might be a way to eliminate the flashing.  
Avatar of cophi

ASKER

I was able to get rid of flashing by using the following code in the Timer event, incase anyone else out there is trying to create a ticker control/app.

            CPoint ptScroll;
            ptScroll.x = -1;
            ptScroll.y = 0;

            CDC* pDC = GetDC();
            LPtoDP(pDC->m_hDC, &ptScroll, 1);
            pDC->DPtoLP(&ptScroll, 1);
            ReleaseDC(pDC);

            ScrollWindow(ptScroll.x, ptScroll.y, m_rcBounds, m_rcBounds);
Avatar of cophi

ASKER

Could I have my pts refunded since I solved it myself.  Thanks!
To refund put a question into community support with a link to this question asking for a PAQ/refund.
ASKER CERTIFIED SOLUTION
Avatar of GranMod
GranMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial