StockTicker Sample: Displays Web Information in an ATL Control
The StockTicker sample 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 four parts:
Shared ATL component (stockquotes).
ATL control (stocktickeratl).
ISAPI Extension DLL (stocksourcemfc).
MFC container app (containermfc). You can rename this .exe file.
Security Note: |
---|
This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended. |
To get samples and instructions for installing them:
On the Visual Studio Help menu, click Samples.
For more information, see Visual Studio Samples.
The most recent version and complete list of samples is available online from the Visual Studio 2008 Samples page.
You can also locate samples on your computer's hard disk. By default, samples and a Readme file are copied into a folder under \Program Files\Visual Studio 9.0\Samples\. For Express editions of Visual Studio, all samples are located online.
Building and Running the Sample
To build and run the StockTicker sample
Open the solution StockTicker.sln.
On the Build menu, click Build.
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 Services [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.
Run containermfc.exe. Right-click in the client area of the application to display a shortcut menu. Use this menu to change the behavior and properties of StockTicker, as described in a following section.
How to Add Stock Source Web Sites
Follow these steps to add your own stock source Web site.
Edit stockquotes\stockquotes_.h. Examine the CSQSISAPIDll class. You can use the class as an example for creating new stock quote classes.
Create a new class, derived from CStockQuoteSource.
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.
The format of the HTML returned from the Web site may require that you override SetStockInfo and/or ParseStockInfo.
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.
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 three 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.
Ticker Speed Move the slider to increase or decrease the rate at which the stock information scrolls from right to left.
Update Interval Time in minutes between updates.
ISAPI Extension DLL URL Enter the full URL to the ISAPI Extension DLL. Append the following text to the end of the URL: ?QueryForStock?Symbol=.
When specifying the URL for the ISAPI Extension DLL, you need to include the component that is responsible for returning the HTML that contains the stock information.
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
ATL COM object
ATL Dialog object
ATL control
MFC Internet (WinInet) classes
MFC ISAPI Extension DLL
Connection points
Persistence
Custom enumerators
Standard C++ Library
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 Project Application Wizard. 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 Internet. 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 two 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 Application Wizard. 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.
Keywords
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
Note
Some samples, such as this one, have not been modified to reflect the changes in the Visual C++ wizards, libraries, and compiler, but still demonstrate how to complete your desired task.