Borland Online And The Cobb Group Present:


June, 1994 - Vol. 1 No. 6

Windows programming tip - Using cout from a Windows program

If you've written DOS applications that use the standard output stream cout to print diagnostic information, you may want to continue to display these types of messages from a Windows program as well. Unfortunately, printing text to a window using the traditional Windows interface functions isn't a trivial matter.

In the article Borland C++ 3.1 Windows Programming - Running a DOS program as a Windows program, we show you how to use the EasyWin feature of Borland C++ to create a Windows interface for your DOS applications. In this article, we'll show how you can use the EasyWin code from a true Windows program to display messages you want to send to cout.

Enabling the EasyWin code

In a normal EasyWin program, the Borland C++ compiler detects that you're building a Windows program that doesn't contain a WinMain() function. If it finds a main() function instead, it automatically adds the necessary EasyWin code to translate the standard input and standard output calls to display in a window.

However, since a true Windows program will have a WinMain() function, you'll have to tell the compiler explicitly to add this special code. You'll use the _InitEasyWin() function to add the EasyWin code.

To use the _InitEasyWin() function, you'll do two things. First, you'll need to add a #include directive for either the STDIO.H or the IOSTREAM.H header file. Both of these files contain the prototype for the function _InitEasyWin().

Second, you'll add a call to the function _InitEasyWin() at the beginning of the function WinMain(). Any code the program calls after this function will be able to use the standard output stream cout just as in a DOS program.

Now let's look at an example of how you can use the EasyWin code from a true Windows program. To demonstrate this, we'll modify the Windows version of the Hello World program from the Borland C++ examples to allow us to view the message identification numbers for different Windows events.

Displaying Windows messages

To begin modifying the WHELLO.PRJ project, launch the Borland C++ 3.1 Integrated Development Environment (IDE) for Windows. When the IDE's main window appears, choose Open Project... from the Project menu.

In the Open Project dialog box, enter the following path and filename in the File Name entry field:

c:\borlandc\examples\win30\whello.prj

Then, click the OK button to open the example project.

When the Project:whello window appears, double-click on the name WHELLO.CPP to open an editing window for that file. Locate the line that contains

#include <string.h>

and add the following line immediately after it:

#include <iostream.h>

Next, locate the MainWindow::WndProc() function and add the following line at the beginning of the function:

cout << "iMessage = " << iMessage << endl;

This will display the value of the iMessage argument to this function. This value is the message identification number for the current message.

Finally, locate the beginning of the WinMain() function (near the end of the file). Add the following line as the first line in the WinMain() function:

_InitEasyWin();

Save the changes to WHELLO.CPP by choosing Save from the File menu. Now you're ready to test your new version of the WHELLO program.

Trying out WHELLO.EXE

To compile and run the program, choose Run from the Run menu. When the compiler finishes building and linking the program, you'll see the program's main window, as shown in Figure A.

Figure A - The WHELLO.EXE program is a Windows version of the Hello World program.

Immediately behind the main window, you'll see another window that contains the messages the program is sending to cout, as shown in Figure B. As you move the mouse over the main window, or do other things that send messages to the main window, you'll see the message identification number for each message appear in this window.


Figure B - You can use the EasyWin code to send Windows message information to a separate window.

To close the WHELLO.EXE program, double-click on its System menu icon. When the program's main window closes, close the EasyWin window by double-clicking on its System menu icon. You can quit the IDE by choosing Exit from the File menu.

Conclusion

The EasyWin code in Borland C++ 3.1 provides you with an easy way to convert DOS command-line programs. In addition, you can use the EasyWin code from a true Windows program to display information from the standard output stream cout.

Return to the Borland C++ Developer's Journal index

Subscribe to the Borland C++ Developer's Journal


Copyright (c) 1996 The Cobb Group, a division of Ziff-Davis Publishing Company. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis Publishing Company is prohibited. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Publishing Company.