Win32 Programming Examples using Win32For



What's New

09 Jan 2005:
Okay, so if you've visited this page before, hoping for updates, you've noticed that I haven't updated it for ages (wow!, has it really been six years?). Sorry about that. Some people still seem to find the information here useful, so I will keep it available. I just haven't been able to focus enough effort on it to keep it updated. And don't get too excited about this update, it isn't much of an update.

From the readers perspective, this update mostly includes fixing some broken links. A lot of these are related to Microsoft's web site which seems to constantly get reorganized. In several cases a current link isn't available (or I haven't found one yet). I've left the link in place, but flagged it with  [broken link]. If you find a functional link, let me know what it is. Much of this information was from the Win9x/WinNT era (which explains why many of the links are no longer valid). With Win2k and WinXP this information may no longer be the optimal way to do things. However, I have used most of my example code on my Win2k machine at work without any problems.

I've also made some internal formatting changes to make things fully HTML 3.2 compliant and to change my contact information. Nothing about the information content is really different. However, given that I complain about other sites not being standards compliant, I should make the effort to see that mine is.

Regarding, SwiftForth from Forth, Inc. The program has been updated significantly since my comments below. In particular, there is a nice object oriented interface integrated into it. While it uses a different OO model than Win32Forth, it is easy to get used to and I actually consider it more "Forth-like". A demo version is available as a free download that is basically fully functional (just can't compile stand-alone applications). Unfortunately, it doesn't appear to be actively supported anymore (files are dated 2001).

1 Jun 1999:
I recently purchased a new home computer. Since it is primarily intended to be a game computer for the family, it has Win98 installed on it. Not to fear, I still use Linux (these web pages are maintained on my Linux box). The down side, of course, is that Win98 sucks. On the plus side, I am able to do more Win32Forth programming at home instead of during my lunch hour at work. So I have had a chance to update things a bit again. In particular I have added some information on using the new classes and class extensions that come with the latest version of Internet Explorer.

With the help of several people, I also found a problem using these examples when turnkeying an application under Win95.

As part of the troubleshooting process Tom Zimmer made some suggestions for improving my code. I have updated my class libraries to hopefully address the shortcomings. The updated libraries are discussed here.

I also came across a neat program that allows interactive testing of the common controls and what effect the various flags and messages do.

I recently had a chance to play with an evaluation version of SwiftForth from Forth, Inc. To anybody doing Windows programming professionally (i.e., can afford to lay down cash ;-), I would recommend giving it a look. It has a fairly sane interface to the Win32 API (though not object oriented). My evaluation version has expired so I haven't tried this, but I suspect that Andrew McKewan's ANS class extensions described below would run fine under SwiftForth. Win32Forth is still the best free windows programming environment out there. Still, the price of SwiftForth isn't bad.

I have created a sample of a splitter window (two panes separated by a bar that can be repositioned to change the split between the two panes).

Finally, I have been informed that some of my links had expired. The link to the Generic application has been updated. However, the link to the example common control articles which got me started seems to have been removed from Microsoft's web site. The ControlSpy program described above may make up somewhat for this loss.

2 Mar 1999:
After a longer delay than I had hoped for, I have once again updated things. I have added a number of new classes to my class libraries. I have also broken them out into separate pages so that I can include some explanations, examples, etc. with each. The main page for this new section is here. NOTE: These have only been tested under NT, no guarantees for 95/98 or 3.1/WFW.

19 Oct 1998:
I received a bug report (and fix, thanks to Bruno Gauthier) for the TextEdit example under Win98 and have updated the files to correct this.

I found a problem with the gforth class.fs file that I haven't had time to correct (using a defined class as an IVAR in a new class gives an error). For now I have just gone back to Andrew KcKewan's original files since I am not currently using any of the Mops features in Mike Hore's file. The new files are in this zip.

12 Oct 1998:
Somebody asked if it was possible to create tooltips from a region in a window rather than from a "control." I have a modified version of the toolbar example showing how this is possible.

7 Oct 1998:
I have now included some experimental class libraries along with a few examples using them.


I have created these pages to document my attempts at programming the Microsoft Windows Win32 API (Application Programming Interface). Since I program in Forth these examples are in Win32For by Tom Zimmer and accomplices. IMHO it is the best program out there for getting M$ Windows to do something. Given this last statement, you may wonder why I am executing the samples that follow in such a low-level way instead of using Tom's objects? There are several reasons.

The first is that this all started out with my interest in learning object oriented programming using Forth (not the data structures and "information hiding" that is, IMO, already a part of a well written Forth program, but the methods and inheritance stuff). So it kind of defeats the purpose to use somebody else's classes (like knowing how to drive a car versus knowing how to rebuild an engine).

I, foolishly, thought that M$ Windows would be nicely organized and inherently object oriented and that it would make a good learning tool. Unfortunately, Bill and Company didn't copy the Mac OS/GUI very well (they never seem to copy the other guys stuff very well) and it has been a much steeper learning curve than what I recall about learning the Mac API back in the days when I was using MacFORTH. Oh well, character building exercise.

Another reason is that I use M$ NT 4.0 at work (they make me ;-), but at home I run Linux on a couple Intel PC's, and I also have access to a couple of Mac's. Unfortunately, there is no common Forth programming language out there that has much commonality among the three operating systems (in terms of GUI programming that is). In general I use Win32For for M$ Windows, Mops on the Mac's, and gforth on Linux.

Fortunately, Win32For and Mops share a fair amount of similarity in their object oriented programming model and I have the class code from Andrew McKewan and the Mops-like update, classM, by Mike Hore which I have gotten to work (updated, see What's New, 19 Oct 1998 above) under gforth. This gives me an "almost" consistent OOP language across the platforms.

Finally, while I think Win32For is great, there are some things I don't like about it. Probably the biggest complaint that I have is with how tooltips have been implemented. They are basically handled by Win32For as separate windows. They come up in a color which doesn't match the standard API tooltips and, unlike the API versions, they don't disappear after a short while (leaving the tooltip window obscuring part of the client window until you move the mouse away from the toolbar). Also, the tooltip window as implemented is an active window which means it shows up on the NT taskbar (the One Moment Please! window) and also in the Alt-Tab switching window.

There are some other places were it appears that Win32For is doing things that the API does for you (e.g., accelerator keys). This may be a concession to backwards compatibility with earlier Win95/NT versions or Win32s operation, I'm not sure. However, since I am using NT 4.0 at work and have no intentions of installing windows on my home machines, I can blissfully ignore compatibility issues.

P.S. Just in case you were wondering, I did first look over the Win32For code to see how I could fix these issues, but I didn't see any simple fix. I was able to change the windows style on the tool tips so that they don't show up on the taskbar, but fixing the other issues looked like it would break too much other code.

Compatibility

All the code I have generated was developed on an NT platform using update 5 of Win32For. I don't have access to Win95/98 (and don't want it) and no particular interest in testing this stuff on W3.11 (Win32s) which I at least have access to. There are no guarantees that this code will work on other platforms. The following is the report that Win32For prints out on boot-up showing my environment: 32bit Forth for Windows 95, and NT
Compiled: June 8th, 1998, 9:34am
Version: 3.5 Build: 0096 Release Build
Platform: Windows NT Version: 4.0 Build: 1381

Where I got my information from

The most important piece of information I got my hands on was the Win32 Reference Manual in WinHelp format. This described the API calls and structures. It is written for C, but in Win32For you just reverse the arguments - first one listed is top-of-stack in Win32For and the last one listed is bottom-most on the stack when the Win32For word 'Call' is used. Other links are:
Microsoft Platform SDK - January 1998 Edition [broken link] ( This is a more recent version of the Platform SDK)
Microsoft Developers Network
Another (older) version of the Windows API Help file
Generic: Anatomy of a Simple Win32 Application (or Simple Win32 Application which contains source files)

Finally, there are the Microsoft example files that I used as a basis for my translations. They were also found on the Microsoft  [broken link]web site.
I found a copy of these articles at another site.
Win32 Common Controls, Part 1: The Basics
Win32 Common Controls, Part 2: Status Bars and Toolbars
Win32 Common Controls, Part 3: Trackbars, Progress Bars, and Up-Down Controls
Win32 Common Controls, Part 4: Header Windows and List View Windows
Win32 Common Controls, Part 5: Image Lists and Tree View Windows
Win32 Common Controls, Part 6: Tab Controls and Property Sheets

The examples

The Microsoft examples cover use of the Win32 common controls (status bars, toolbars, trackbars, progress bars, up-down controls, header windows, list view windows, image lists, tree view windows, tab controls, and property sheets). They are written in C, but contain most of the information needed to generate Win32For API calls.

Given my above comments on the tooltips as implemented in Win32For, the first example I implemented was the toolbar example.

The same Microsoft article that discussed toolbars also discussed status bars and they looked simple enough that that was what I tried next. And, lo and behold, it was simple!

A note on programming style

I do not claim that the examples contained here are good Forth programming style. They aren't! Instead, they represent the interactive way I tested the API calls. Many things are done "manually" that could (should) be factored into simple Forth words. I eventually want to generate class definitions that will allow me to interact with the API in a more natural, and object oriented, way. Since this task is a ways off, I have included the samples in their raw state. You'll see what I mean when you start looking at the source code. The advantage is that each of these examples should be self-supporting. So you can pick what you would like to experiment with and not worry about getting something from one of the other examples. Just take the example and recode it to you hearts content.

Please don't write me telling me what a Bozo I am 'cause it could be written much more compactly, better, simpler, (insert your favorite mantra), etc. Again, my goal is to understand the API and much of this has occurred in a quick-and-dirty, interactive fashion. Later on when I try to collect my knowledge into something coherent, then you can tell me what a Bozo I am.

Disclaimer

As in most things, you get what you pay for. Which makes free advice of no value. Therefore, be advised that this information is placed into the bitstream for entertainment purposes only. Any use of this information is at the sole responsibility of the person using the information. I am in no way responsible for anything you might do with this information or consequences resulting from using this information. Come to think of it, I never even saw this information before! Yeah that's it, it must be somebody pretending to be me! Yeah, that's the ticket. (You've been warned)


Author: Jeff Kelm, (how to contact me)
URL: http://www.concentric.net/~jkelm/win32for/index.htm
(Modified: 2005-01-09)