StockTicker: Displays Web Information in an ATL Control

Click to open or copy the STOCKTICKER project files.

StockTicker downloads stock information from the Web and displays it in an ATL control that scrolls the data from right to left. The control updates the data at set intervals, which the user sets.

StockTicker contains 4 parts:

Installing and Running the Sample

To use StockTicker, first build all of the components and then use regsvr32 to register two DLLs: stockquotes.dll and stocktickeratl.dll. You may need to register ATL.dll, also.

The ISAPI Extension DLL mimics a stock data source. It should be installed on a computer where Web server software (such as Microsoft Internet Information Server (IIS)) has been installed. You must tell StockTicker where this DLL is located. For instructions on how to use the ISAPI Extension DLL, see below.

How to Use the Sample

Run containermfc.exe. Right-click in the client area of the application to display a popup menu. Use this menu to change the following behavior and properties of StockTicker:

Stocks

Displays a dialog box through which you can add or remove the stock symbols to monitor. The dialog box is initialized with the current list of stock symbols.

Update Now

Updates the stock information from the Web.

Properties

The Properties dialog box has 3 property pages: color, font, and custom. If the stock color and font property pages do not display in the Properties dialog box, make sure you have registered msstkprp.dll.

You can change the following properties.

About Stock Ticker

Opens the About dialog box.

Stay On Top

StockTicker will stay on top of all other applications; otherwise, it can be hidden by other applications.

Features Used by StockTicker

StockTicker Components

This section provides more detail on the StockTicker components.

Stock Quotes Component

The shared component, the ATL object, is created with the ATL COM AppWizard. It uses MFC WinInet support and other utility classes. Stock Quotes keeps track of the stocks to be monitored and updates the stock information from the Web. An application can use this component for either synchronous or asynchronous updates. If the StartUpdating method is called, a child thread is created and the function returns immediately. The child thread creates a timer and waits until the update interval has elapsed. Then it requests an update from the Stock Quotes component. Subsequent calls to Update post a thread message to the child thread indicating that an update should occur, and Update returns immediately. In synchronous mode, Update blocks until the download is complete.

The Stock Quotes component keeps track of stock data source objects that represent the Web sites from which the stock information is downloaded. When StockTicker updates the stock information, the Stock Quotes component goes through the list of stock data sources and attempts to download the information. If the attempt fails, the component tries to download stock data from the next source. If all of the stock data sources fail to download the information, <NA> will appear in the ticker window after the stock symbol.

Stock data comes from the Web site in HTML form. The data must be parsed to find the current price and the price change since the last update. The Stock Quotes component handles this parsing by searching for 2 keywords: for example, Current: or Change:. The component reads the data following the keywords if that data is either a fractional or decimal number (fractional or decimal).

Because stock Web sites may change the way they deliver data, you may have to change the algorithm that Stock Quotes uses to get the stock information.

See below for instructions on how to add your own stock data sources.

ISAPI Extension DLL

The default stock data source is the local ISAPI Extension DLL. The ISAPI Extension DLL does not return real stock data. Instead, when you request data for a stock, the DLL checks whether the stock symbol is in its list of known stocks. If the stock symbol is on the list, the DLL retrieves the data and randomly adjusts the current stock price up or down. If the stock symbol is new, it is added to the list and an initial stock price is set.

Stock Ticker Control

This control was created with the ATL Object Wizard. It uses MFC to make some things easier.

You can use this control outside of StockTicker.

The Stock Ticker control uses the Stock Quotes component to handle the stock information and updates. When it comes time to save itself to a stream, the control will also ask the Stock Quotes component to save itself.

On startup, the Stock Ticker control creates the Stock Quotes component and asks it to start updating stock information. The control then starts a timer that it uses for the draw/render loop. When stock data has been updated, the Stock Quotes component notifies the control (through a connection point). The control then gets all the stock information from the Stock Quotes component and creates a string out of it. This string is drawn in the control and scrolls from right to left.

Stock Ticker Container

The Stock Ticker container is an MFC Active Control container, created with the MFC AppWizard. It saves its size and position upon quitting, and it restores its previous state when it is restarted.

The container also handles the StockTicker menus.

How to Add Stock Source Web Sites

Follow these steps to add your own stock source Web site.

  1. Edit stockquotes\stockquotes_.h. Examine the CSQSISAPIDll class. You can use the class as an example for creating new stock quote classes.

  2. Create a new class, derived from CStockQuoteSource.

  3. In the new class constructor, set m_strURL to the full URL of the Web site from where to retrieve data. Be sure to include the command to retrieve the stock data.

  4. The format of the HTML returned from the Web site may require that you override SetStockInfo and/or ParseStockInfo.

  5. Rebuild the component and register it.

If your selected site returns data resembling CONAME: <NA>, where CONAME is the stock symbol, either the site could not be accessed, or the source class you created could not find the stock information from the HTML.

Note   The format, either decimal or fractional, of the stock data may differ among the stocks you choose to monitor. StockTicker makes no attempt to alter the data to make it consistent.

This sample demonstrates the following keywords:

IEnumXXX, CWinThread, std::vector, IConnectionPointContainer, CInternetSession, CInternetException, AfxParseURL, CHttpConnection, CHttpFile, COleClientItem, COleDocument, COleDispatchDriver, CreateILockBytesOnHGlobal, StgCreateDocfileOnILockBytes, OleSave, StgIsStorageILockBytes, GetHGlobalFromILockBytes, COleFont, std::basic_istream, std::basic_ostream, CHttpServer, CComPtr, CDialogImpl, std::list, IPersistStreamInit, IFontDisp, IViewObjectEx, CComControl, IOleControl, IPropertyPageImpl, CBitmap, CDC, IPersistStreamInit_Load, IPersistStreamInit_Save, IConnectionPointImpl