An Intro to C/C++ Programming

Copyright©2004 by Daniel B. Sedory


PREV
UP
NEXT

A GUI "Hello World!" Program

SWinHi.zip - A simple Windows™ "Hello World!" program.

Creating a fully functional Microsoft® Windows™ GUI (Graphical User Interface) Application either with C or C++ for the first time, involves a learning curve that may feel as difficult as mastering a whole new programming language! The reason is due to the complexity of all the Win32 API (Application Programming Interface) functions that must be used correctly in order to interact with the Windows™ OS itself just to place a window on the display screen! You can find online Tutorials which cover many of the API GUI functions; this is only one small example. There are so many complex 32-bit Windows™ API GUI (window) and console (command prompt) functions, I doubt that anyone could ever use all of them.

When you use MAKEswinhi.cmd to produce swinhi.exe, you should see a couple warning messages like this:

Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
swinhi.cpp:
Warning W8057 swinhi.cpp 110: Parameter 'hPrevInstance' is never used in function __stdcall WinMain(HINSTANCE__ *,HINSTANCE__ *,char *,int)
Warning W8057 swinhi.cpp 110: Parameter 'lpCmdLine' is never used in function __stdcall WinMain(HINSTANCE__ *,HINSTANCE__ *,char *,int)
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

Link Successful; you still need to test your program!

The program will function just fine. Run it a few times, noting where the window pops up on your screen. You should see it in a slightly different location, overlapping previous instances of the window, each time you run it. This is exactly how a Windows™ OS operates unless we specify (in the program) where the window should show up on the screen.

Exercise: Locate my comments in the source code about the digits that control the size of the window when it's created. Notice the comment "X, Y, Width, Height." Now change both of the "CW_USEDEFAULT" variables on that line to: "200,150," (but first make a copy of the line and place "//" in front, so you don't forget its original contents), recompile and run the program. You should now see the window in a much different location on your screen, and each instance will always appear in the same location! Replace those digits with "0,0," and go through the process again; note where the window appears. Finally, insert "CW_USEDEFAULT" in place of the two window size digits and recompile/run it again! How does the window appear now? When you run the program at a different screen resolution, does the size of the window change?

Caution: If you ever decide to create a Windows™ GUI program for many users, then don't ever set its coordinates near the bottom-right hand corner! Why? Because your 1024 x 768 display corner would be way off the screen for anyone who must still use a 640 x 480 resolution; some may even decide that your program was broken under those circumstances!

   By now you should see why the defaults may be best for a commercial program whose users may have a wide variety of displays.

http://www.winprog.org/tutorial/bcpp.html. (Part of theForger's tutorial; see below) contains some very useful information about the Borland C++ 5.5 compiler/linker; it's must reading for anyone using this free C++ package!

 

Using TD32 on GUI (Window) Source Code

The first thing you'll realize is that there won't be any extra 'window' to look at for a program's output until you run the program's own code which creates a window. If that code has a problem, you'll never see the window. The function which actually places the window on your screen is on line 99 of the swinhi.cpp file: "ShowWindow(hwnd, nCmdShow)".

Enter: td32 swinhi.exe at its folder's command-line prompt. You should see something similar to this:

Begin stepping through (F7 key) the program, but before you execute the line with "ShowWindow" make sure you're Desktop is clear and move the Debugger window down and out of the way so you can see the SWinHi window appear.

At that point, even though it may look like a normal window, you won't be able to move it! And once the execution begins looping inside what we've labeled as the Message Loop; a while() function that starts on line 105, it becomes very difficult for our window to respond to normal events. Pressing the F9 key will either terminate the program immediately (due to a prior mouse click) or allow you to close the window. In order to debug programs like this, you'd set up a breakpoint somewhere outside of the while() loop and simply let it run through the loop if necessary.

The main idea of this section was just to show you the major differences between running a Console and a Window program under Borland's Turbo Debugger.

 

 


PREV
UP
NEXT

Last Update: 27 March 2004.

  Back to The Starman's Realm Index page!

 

 

Hosted by uCoz