Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / MFC
Article

A Ticker control

Rate me:
Please Sign up or sign in to vote.
4.94/5 (12 votes)
3 Sep 2000 133.5K   3.8K   51   9
A class that provides a news/stock ticker for your MFC applications
  • Download source files - 7 Kb
  • Download demo project - 34 Kb
  • Sample Image - AppPic.jpg

    Introduction

    An MFC class CTicker that could be used to provide a news/stock ticker for your MFC applications.

    Imagine there is a requirement in your project where you need to display scrolling information in an application that delivers timely information on stocks, news, sports scores, and weather.

    A 'Mouse Over' event stops the ticker and highlights the border, and clicking on it would open a predefined URL in your favourite browser with more detailed information in the html files. There are similar applications available on the net, for example EntryPoint.

    The ticker should continuously display changing data and download the URL specified at regular intervals and display that data. The data can be in text format.

    I always wished there was a readymade class that enabled my application to display a scrolling ticker. I had to resort to Third party ActiveX controls which added considerable size to my applications and versioning headaches. So I wrote one myself and in the spirit of MFC I call it CTicker.

    I have done GDI with MFC's wrapper classes and used the CAsyncMonikerFile class to achieve HTTP.

    The sample code is a Dialog based app called TickerTest. One could try out other combinations of MFC applications.

    Usage

    To add the ticker to TickerTest you need to do the following:

    1. Drop a Static/label control on the dialog resource IDD_TICKERTEST_DIALOG. Give it an appropriate resource id IDC_STATIC1 (say). Do not select the Notify property for the static control.

    2. Add a member variable for the particular static control. Give it the name m_sTicker & select a category of type Control. This would make the variable type CStatic automatically.

    3. Open the class TickerTestDlg.h and add the line #include "Ticker.h" on top to include the declarations of the CTicker class. Replace the class CStatic with CTicker.

    4. Add a member variable m_TickerRect to the class CTickerTestDlg. This variable holds the window coordinates for the ticker.

    5. Override the OnInitDialog function for the class CTickerTestDlg

    6. Call the function m_sTicker.GetWindowRect (&m_TickerRect) to set the ticker's window coordinates.

    7. Call the one stop function ShowRates or the ShowRatesWithBkgBmp passing it the following relevant parameters:

      • Name of the file containing the quotes,
      • Text color on ticker,
      • Color of text of rates(RATES mode),
      • FontName,
      • FontHeight,
      • TickerSpeed,
      • TickerMode (Regular mode which displays only information OR the Rates mode which displays information in the format CompanyName followed by CompanyRate slightly below in height and so on...),
      • Bitmap Resource ID

      eg:

      m_sTicker.ShowRates("Ticker.txt", 
                          RGB(255,255,255),
                          RGB(255,255,43),
                          "Comic Sans MS",
                          17,
                          4, 
                          MODE_RATES_TICKER);

      or

      m_sTicker2.ShowRatesWithBkgBmp("Ticker2.txt", 
                                     RGB(255,255,255),
                                     RGB(255,255,43),
                                     "Arial",
                                     17,
                                     4,
                                     MODE_REGULAR_TICKER, 
                                     IDB_TICKER_BKG);
    8. For the MouseOver override WM_MOUSEMOVE message, and add a handler to the class CTickerTestDlg and add the following code
      void CTickerTestDlg::OnMouseMove(UINT nFlags, CPoint point) <br>
      {      
      	CDialog::OnMouseMove(nFlags, point);
      	m_sTicker.GetWindowRect (&m_TickerRect);
      	ClientToScreen (&point);
                 
      	TRACK_TICKER_MOUSEMOVE(m_sTicker, m_TickerRect)
                 
      	m_sTicker.ResumeTicker();
      }

      Take a look at the TRACK_TICKER_MOUSEMOVE macro. This Macro takes the CTicker based object name the CRect object name representing the window coordinates. This macro changes two properties of the CTicker class based on which it draws a Rectangular border around the ticker on mouseover.

    9. For the Click-go-to-URL feature handle the WM_LBUTTONUP message by adding a handler to the class CTickerTestDlg and add the following code
      void CTickerTestDlg::OnLButtonUp(UINT nFlags, CPoint point)
      {         
      	CDialog::OnLButtonUp(nFlags, point);
                 
      	ClientToScreen(&point);
      	if (m_TickerRect.PtInRect (point))
      	{
      		AfxMessageBox("Will open http://finance.yahoo.com/ in the browser now");
      		ShellExecute(0 ,"open", "http://whatever.com/", NULL, NULL, SW_MAXIMIZE);
      	}
    10. For the Fetch-via-HTTP feature do the following: (This feature uses Asynchronous Monikers) Add a new Class through the class wizard(CTRL-W)

      Image 2

      Image 3

      Override the OnDataAvailable and OnProgress (If progress indication is required) messages and add the following code to OnDataAvailable

      void CMyAsyncMon::OnDataAvailable(DWORD dwSize, DWORD bscfFlag)
      {
          if ((bscfFlag & BSCF_LASTDATANOTIFICATION) != 0)
          {
      		char* m_buffer;
      		m_buffer = (char*)malloc(dwSize*sizeof(char) + 1);
      		try {
      			UINT nBytesRead = Read(m_buffer,dwSize);
      			m_buffer[nBytesRead] = '\0';
      			CFile f1;
      			if( f1.Open( "Ticker2.txt", CFile::modeCreate | CFile::modeWrite ) ) 
      			{
      				f1.Write(m_buffer, strlen(m_buffer));
      				f1.Close();
      				//Retrive a pointer to the main window 
      				//Access the public(eeeks) variable m_sTicker2
      				//and set the text to be displayed in the ticker
      				CTickerTestDlg* pDlgMain = (CTickerTestDlg*)AfxGetMainWnd();
      				pDlgMain->m_sTicker2.SetTickerText (m_buffer);
      			}
      			free(m_buffer);
      		} 
      		catch(CFileException* pe)
      		{
      			/*	ALL THE ERROR HANDLING GOES HERE  */
      			pe->Delete ();
      		}
      	}
      	CAsyncMonikerFile::OnDataAvailable(dwSize, bscfFlag);
      }

      This is how a typical TextFile containing CompanyName/Quotes with delimiters are:

      Image 4

    Authors Note

    I made the class CTicker for open use. If it works with you please give credit where I deserve. Please send me some feedback .

    There are more articles coming on server side MTS Components which generates stock exchange feed at runtime from various quote providers on the net. These server side components along with the client side ticker(as a custom app or an activeX control) help in creation of a great Online Stock Portfolio WITHOUT subscribing to expensive stock feed from quote providers.

    Special thanks to Fabian Toader for providing The CJumpyDraw class
    I have borrowed the DrawLinearWash function from his class to provide the gradient look.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    Web Developer
    United States United States
    I am originally from Mumbai, India, Currently living in Los Angeles doing Software Development. If there is something I miss here in LA, its the crazy Mumbai monsoon and delicious "wadapavs".
    Apart from spending time writing software, I spend most of my time with XBox riding the Warthog and killing covenants in Halo.

    Comments and Discussions

     
    Generalwin32 C Pin
    Lord_Chiru2-Nov-06 18:50
    Lord_Chiru2-Nov-06 18:50 
    GeneralNice,but.. Pin
    ucc8011-Feb-06 16:06
    ucc8011-Feb-06 16:06 
    GeneralSerious problem with CAsyncMonikerFile in VC++ 7.0 Pin
    PetoG11-Nov-04 9:42
    PetoG11-Nov-04 9:42 
    QuestionCan i add a small logo alongwith text Pin
    Atif Mushtaq28-Jun-04 0:40
    Atif Mushtaq28-Jun-04 0:40 
    GeneralFlicker Pin
    waelahmed12-Jan-04 3:08
    waelahmed12-Jan-04 3:08 
    GeneralLess Jerky Pin
    Wayne Munslow1-Sep-03 5:27
    Wayne Munslow1-Sep-03 5:27 
    Generalhelp me with code Pin
    no_body6916-Jun-03 19:51
    no_body6916-Jun-03 19:51 
    GeneralHyperlinks Pin
    allanmcd29-Mar-03 0:32
    allanmcd29-Mar-03 0:32 
    GeneralRe: Hyperlinks Pin
    Brian Delahunty30-Apr-03 8:11
    Brian Delahunty30-Apr-03 8:11 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.