Handle PowerScript function
Description
Obtains the Windows handle of a PowerBuilder object. You can
get the handle of the application, a window, or a control, but not
a drawing object.
Syntax
|
1 |
<span>Handle</span> ( <span>objectname</span> {, <span>previous</span> } ) |
|
Argument |
Description |
|---|---|
|
objectname |
The name of the PowerBuilder object for |
|
previous (optional) |
(Obsolete argument) A boolean indicating In current versions of Windows, Handle always |
Return Values
Long. Returns the handle of objectname.
If objectname is an application and previous is true, Handle always
returns 0.
If objectname cannot be referenced at
runtime, Handle returns 0 (for example, if objectname is
a window and is not open).
Usage
Use Handle when you need an object handle
as an argument to Windows Software Development Kit (SDK) functions
or the PowerBuilder Send function.
Use IsValid instead of the Handle function
to determine whether a window is open.
When you ask for the handle of the application, Handle returns
0 when you are using the PowerBuilder Run command.
As far as Windows is concerned, your application does not have a
handle when it is run from PowerBuilder. When you build and run
an executable version of your application, the Handle function
returns a valid handle for the application.
If you ask for the handle of a previous instance of an application
by setting the previous flag to true, Handle always
returns 0 in current versions of Windows. Use the Windows FindWindow function
to determine whether an instance of the application’s main
window is already open.
Examples
This statement returns the handle to the window w_child:
|
1 |
<span>Handle</span>(w_child) |
These statements use an external function called FlashWindow to
change the title bar of a window to inactive and then return it
to active. The external function declaration is:
|
1 |
function boolean flashwindow(uint hnd, boolean inst) &    library "user.exe" |
The code that flashes the window’s title
bar is:
|
1 |
integer nLoop  // Loop counter<br>long hWnd  // Handle to control<br> <br>// Get the handle to a PowerBuilder window.<br>hWnd = <span>Handle</span>(Parent)<br>// Make the title bar flash 300 times.<br>FOR nLoop = 1 to 300<br>   FlashWindow (hWnd, true)<br>NEXT<br>// Return the window to its original state.<br>FlashWindow (hWnd, FALSE) |
For applications, the Handle function
does not return a useful value when the previous flag
is true. You can use the FindWindow Windows
function to determine whether a Windows application is already running. FindWindow returns
the handle of a window with a given title.
Declare FindWindow and SetForegroundWindow as
global external functions:
|
1 |
PUBLIC FUNCTION unsignedlong FindWindow (long  &<br>   classname, string windowname) LIBRARY "user32.dll" &<br>   ALIAS FOR FindWindowW |
|
1 |
PUBLIC FUNCTION int SetForegroundWindow (unsignedlong &<br>   hwnd) LIBRARY "user32.dll" ALIAS FOR  &<br>   SetForegroundWindowW |
Then add code like the following to your application’s
Open event:
|
1 |
unsignedlong hwnd<br> <br>hwnd = FindWindow( 0, "Main Window")<br>if hwnd = 0 then<br>   // no previous instance, so open the main window<br>   open( w_main )<br>else<br>   // open the previous instance window and halt<br>   SetForegroundWindow( hwnd )<br>   HALT CLOSE<br>end if |