VisualAge RPG Tips - Calling Windows APIs - Examples
DISCLAIMER
These sample programs are provided by IBM as simple examples. These examples have not been thoroughly
tested under all conditions. IBM, therefore, can not guarantee or imply reliability, serviceability, or function of these samples.
All programs contained herein are provided to you "AS IS". The implied warranties of merchantability and
fitness for a particular purpose are expressly disclaimed.
In a previous tip, we described how you can call Window's APIs from
a VisualAge RPG program. In this tip we give a few more examples on
calling those APIs.
Always on top
Ever see those pesky windows in some applications that insist on
staying on top of other windows even though they don't have focus? Here's
how you can make your own using the SetWindowPos API. You could put
the code in a window's create event as shown here, or make it a menu
option. It can be useful in a 'please wait' window in your application if
you're loading other windows since the newly created windows will get
focus and overlay your 'please wait' window.
Here is the prototype for the API
*
D SetWindowPos PR 10U 0 ExtProc('SetWindowPos')
D DLL('USER32')
D Linkage(*StdCall)
D 10U 0 Value Window handle
D 10U 0 Value Insert after
D 10U 0 Value X
D 10U 0 Value Y
D 10U 0 Value cx
D 10U 0 Value cy
D 10U 0 Value wFlags
*
D hWnd S 10U 0 Window handle
D uRC S 10U 0 Return code
*
|
Put this code in the window's create event to make it stay on top. A
return code greater than zero indicates success:
*
C ONTOP BEGACT CREATE ONTOP
*
C 'OnTop' Getatr 'Handle' hWnd Get window handle
C Eval uRC=SetWindowPos(hWnd: -1: Call the API
C 0: 0: 0: 0: 3)
*
C ENDACT
*
|
Create Internet Hyperlinks
Given a file name, the ShellExecuteA API will execute the
application to process that file based on its content or file extension.
For example for a file named MyDoc.txt ShellExecuteA will invoke
NOTEPAD.EXE. If the file name contains some internet protocol such as 'http://'
the default internet browser will be invoked. You could use this to
connect users to your web page or send you e-mail. You could even write
the application help in HTML and invoke it with this API.
In this example, pressing a push button will connect to the VisualAge
RPG home page. Here is the prototype for the ShellExecuteA API as well as
declarations for other variables used in this sample:
*
D ShellExecuteA PR 10U 0 ExtProc('ShellExecuteA')
D DLL('SHELL32')
D Linkage(*StdCall)
D 10U 0 Value Window handle
D * Value ->Operation
D * Value ->File name
D 10U 0 Value ->Parameters
D 10U 0 Value ->Directory
D 10U 0 Value Show commmand
*
D ulRC S 10U 0 Return code
D hWnd S 10U 0 Window handle
D VARPG S 255A Inz('http://www.software. File name
D ibm.com/ad/varpg')
D Operation S 5A Inz('open') Operation
D lpOperation S * Inz(%Addr(Operation)) -> operation
D lpVARPG S * Inz(%Addr(VARPG)) -> file name
|
Here is the code behind the PRESS event of the push button. A return
code greater than 32 (ulRC) indicates success:
*
C PB_LINK BEGACT PRESS MAIN
*
C 'Main' Getatr 'Handle' hWnd
C Eval Operation=%Trim(Operation)+x'00'
C Eval VARPG=%Trim(VARPG)+x'00'
C Eval ulRC=ShellExecuteA(hWnd:lpOperation:
C lpVARPG:0: 0: 1)
*
C ENDACT
*
|
To send e-mail, use the mailto syntax. For example: mailto://varpg@vnet.ibm.com
If you have Service Pack #3 installed, you can emulate a hyperlink on
your window by using a static text part. Change it's font style to
underline, and set its foreground color to blue, the usual hyperlink color.
Use the MOUSESHAPE and MOUSEICON attributes to change the pointer to a
hand as the mouse moves over it in the static text's MOUSEMOVE event.
Return the mouse shape to its default in the MOUSEMOVE event of the canvas
part. Use the static text's CLICK event to call the API.
Undo Editing
This tip uses the SendMessageA API. This API is used extensively
by Windows to communicate with parts. The first parameter is the handle of
the part to communicate with. The second parameter is the ID of the
message to send. The value of the last two parameters depends on the
message ID. In this case they are not required. In this example we use the
WM_UNDO message that will undo any editing performed on a part that has a
Text attribute; in this case a Multi-line entry field, MLE1. The message
ID for the UNDO message is x'C7', decimal 199.
Here is the prototype for the SendMessageA API and other variables used
in this sample:
*
D SndMessage PR 10U 0 ExtProc('SendMessageA')
D DLL('USER32')
D Linkage(*StdCall)
D 10U 0 Value Control handle
D 10U 0 Value Message ID
D 10U 0 Value Parm 1
D 10U 0 Value Parm 2
*
D hWnd S 10U 0 Part handle
D ulRC S 10U 0 Return code
*
|
And here is the code that is executed when the push button is pressed:
*
C PB_UNDO BEGACT PRESS MAIN
*
C 'MLE1' Getatr 'Handle' hWnd
C Eval ulRC=SndMessage(hWnd:199:0:0)
*
C ENDACT
*
|
There is a WM_CANUNDO message, x'C6', decimal 198, that could be called
first. It indicates if the undo buffer has data that can be used to
reverse the last edit operation. A non-zero return code indicates there is
data in the undo buffer.
|