Win32 API samples

This collection of code snippets used to be hosted on www.global-shared.com.
It is an old collection: it all began in 1996 or 1997, when Progress 8 was new and many of us were still using Windows 3.1 !!
Unlike wine or paintings, program code doesn't get better when it ages. You can find fragments that can be improved because Microsoft continuously expands their API, or are outdated because Progress has added features to the ABL so we don't need to use the WIN32 API anymore.


Local names for dates and months

Procedure GetLocaleInfoA can be used to retrieve all the information entered in "Control Panel / Regional Settings", both for the current locale and for all other locales. This can be useful for lots of things like displaying the actual names for days and months.
The demo window, shown in the picture, allows to choose a locale and fills a couple of selection lists showing the month names, abbreviated month names, day names and abbreviated day names. It uses the following definitions:

/* Procedure GetLocaleInfoA is declared in windows.p.

Formatting date and time


In Windows "Control Panel" / "Regional Settings" you can choose a Locale and change several formats for that Locale. However Progress displays dates and times using its own display formats.
You can use GetDateFormatA or GetTimeFormatA to format a date or time according to Windows settings. The resulting character string can be useful for reports or display-only fields. When you call GetDateFormatA or GetTimeFormatA you will probably want it to use the current locale, although it is also possible to use any non-current locale as demonstrated in the above example window.


Internationalisation

.


PlaySound

Function PlaySoundA plays a waveform sound. This function replaces the obsolete function sndPlaySoundA.

&GLOB SND_ASYNC 1
&GLOB SND_NODEFAULT 2
&GLOB SND_LOOP 8
&GLOB SND_PURGE 64
&GLOB SND_APPLICATION 128
&GLOB SND_ALIAS 65536
&GLOB SND_FILENAME 131072
&GLOB SND_RESOURCE 262148
 
PROCEDURE PlaySoundA EXTERNAL "winmm.dll" PERSISTENT :
  DEFINE INPUT PARAMETER  pszSound    AS LONG.
  DEFINE INPUT PARAMETER  hmod        AS LONG.
  DEFINE INPUT PARAMETER  fdwSound    AS LONG.
  DEFINE RETURN PARAMETER ReturnValue AS LONG.
END PROCEDURE.

Notice the 'persistent' option: if the procedure is not declared as persistent it will not be possible to use the SND_ASYNC flag. Also notice that pszSound is declared as LONG. This is not convenient in most cases when you need to pass a character string, but has to be long to support {&SND_PURGE}.


Play an AVI file in a Progress window

It can be attractive to play an animation, for example while the user has to wait while your application communicates with the AppServer.
The source in this example uses some of the MCI series of API commands, making it possible to use this source on a window or on a dialog, and making it possible to play AVI's with or without soundtrack!

The complete example source (aviwin.w) is attached. It is a UIB-generated window (not SmartWindow) so the source is a bit too large to show on this page.

The source relies on the availablity of windows.i and windows.p, timestamp Sep 8, 1997 or later.

The window contains two buttons: button-open and button-stop.


Multimedia

.


Statusbar and Progressbar common controls

Source by Todd Nist, Protech Systems Inc.
Modified by Simon Sweetman

The library COMCTL32.DLL contains common controls like the Status Bar control. This example, provided by Todd Nist, shows how to use the Status Bar control in a Progress window. Simon Sweetman added the progressbar. The example will produce a window that looks like this:


The Status Bar in this example is divided in three parts: the first part contains a progressbar which displays the value of the slider, the second part is shown with SBT_POPOUT style so it appears raised.


Toggle-box with labels on the left

A standard toggle-box widget has its label on the right. This example applies the BS_LEFTTEXT style to bring the label to the left.




As you can see in the picture, the checkboxes are not vertically aligned anymore. Tex Texin wrote a
procedure to fix this: see http:www.xencraft.com/resources/rightside-checkbox.html

   
RUN ToggleLeftText (toggle-1:HWND).
RUN ToggleLeftText (toggle-2:HWND).
    
PROCEDURE ToggleLeftText :
/* -------------------------------------------------------------

Buttons in pushed state

See for an improved version: Buttons in pushed state

This example works best with Progress version 9.
When you create a 'toolbar' with buttons, you may sometimes want to keep a button in a 'pushed' state indicating if the functionality for the button is toggled 'on'.
In that case you could run the following ToggleButton procedure during the ON CHOOSE event of the button widget:

{windows.i}
PROCEDURE ToggleButton :
  DEFINE INPUT PARAMETER hbutton AS HANDLE.
 
  DEFINE VARIABLE ReturnValue AS INTEGER NO-UNDO.
  &SCOP BM_SETSTATE 243
 
  IF hbutton:TYPE NE 'button' THEN RETURN.
 
  IF hbutton:PRIVATE-DATA = 'pushed' THEN DO:
     hbutton:PRIVATE-DATA = ''.
     RUN SendMessageA IN hpApi ( hbutton:HWND, 
                                 {&BM_SETSTATE}, 
                                 0,   /* FALSE, remove BST_PUSHED style */
                                 0, 
                                 OUTPUT ReturnValue).
  END.
  ELSE DO: 
     hbutton:PRIVATE-DATA = 'pushed'.
     RUN SendMessageA IN hpApi ( hbutton:HWND, 
                                 {&BM_SETSTATE}, 
                                 1,  /* TRUE, set BST_PUSHED style */
                                 0, 
                                 OUTPUT ReturnValue).
  END.
 
END PROCEDURE.

Notes

There are several button types in Progress: flat, no-focus, buttons with images and 'normal' buttons with text. In version 9 this procedure works for all button types, but in version 8 it works only for buttons with no images.


Buttons in pushed state

This example has some advantages over the older example. This example shows an easy way to create buttons that can have a "pushed" state. These buttons are actually toggle-box widgets with an alternative layout. That is convenient because you don't need any special code to read and set the logical value: the usual statement toggle-1=TRUE will be sufficient to show the button in pushed state.

Width and height are set high enough to fit the image. You can simply use the UIB/AB to do this.


Selection-list with item-data

Every widget can have private-data, but it is less commonly known that each list-item in a selection list can have its own private data.
The itemdata for a list-item is of type integer, or actually a DWORD.
Perhaps it is best to think of a selection list as a container of list-items, where each list-item is uniquely identified by a SelIndex. Each list-item contains two data members: the (visible) character string and the itemdata. The itemdata has no meaning to Windows so you can safely use it for storing application-specific information.
Normally I would prefer to use the Progress 9 feature of LIST-ITEM-PAIRS instead of this API solution. But LIST-ITEM-PAIRS are designed to work only if the LIST-ITEMs (e.g. screen-values) are unique!


Combo-box with item-data

Page Selection-list with item-data shows how and when to use itemdata with a selection list. The same works for a combo-box, except the message constants are different.
Let's skip the background info and go right to the source:

{windows.i}
PROCEDURE AddComboItem :
/*------------------------------------------------------------------------------
  Purpose:     add an item to the combo-box. Each item can be associated with 
               itemdata (is low-level implementation of PRIVATE-DATA).
------------------------------------------------------------------------------*/

System colors

The functions GetSysColor and SetSysColors can be used to access the system colors.
This is for example useful for displaying text in disabled native fill-in widgets.
Text in a disabled native fill-in is rendered in the system-color COLOR_GRAYTEXT, which is gray. The background color of a disabled fill-in is also gray (but not COLOR_GRAYTEXT) so it is difficult to read the text.
To make the text more readable you may want to change the RGB-value of the COLOR_GRAYTEXT system color.
There are several ways to change COLOR_GRAYTEXT:
You can set or modify the registry key

    "HKEY_CURRENT_USER\Control Panel\Colors\GrayText"

cut, copy and paste

A typical Windows application has an Edit menu with options for Undo, Cut, Copy and Paste. There are 4GL methods for making such a menu. This site usually does not cover 4GL methods, but I would like to make an exception this time because these 4GL methods are not documented. This was posted by email to PEG by Matt Gilarde:

Starting in 8.2A, Progress supports several new attributes and
methods which make implementing the Edit menu very simple.
Unfortunately, these features were added in the late stage of
8.2 development and were not adequately documented.  Briefly,
the new attributes and methods are:

Changing the shape of a window

example by Simon de Kraa

This example program replaces the rectangular region of a window by a polygon. The technique is basically the same as described on page round widgets. You can download the example source from the attachments table.
A polygon is an array of points. The coordinates for the points in this example are calculated using the trigonometry functions sin and cos (found in the Visual C runtime module which is available on almost every PC). The number of points can be selected using the slider control; selecting a large number of points is somewhat slow but gives an interesting result as shown in the picture.


#
Syndicate content