Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A Ticker control

0.00/5 (No votes)
3 Sep 2000 2  
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) 
      { 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)

      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:

    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