UserGuide

Calling Native Windows APIs

From Xojo Documentation

You can call into Win32 APIs (aka WinAPI) to use methods and properties that are not built into the framework by using the Declare command. To create a Declare statement you first need to track down the API you want to use using Microsoft's documentation: Microsoft Developer Documentation.

The Win32 API is largely based on the C/C++ programming language and makes heavy use of structures.

Flash the Window

As a simple example, you can call a function to flash the window to get the user's attention. Refer to the FlashWindow doc page in the Microsoft Win32 docs, where you can see the declaration for this method. It looks like this:

BOOL WINAPI FlashWindow(
  _In_ HWND hWnd,
  _In_ BOOL bInvert
);


This tells you that this method is a function (the BOOL at the beginning indicates it returns a Boolean) and that it takes two parameters. The HWND parameter is the handle to the window, which you get from Xojo as Window.Handle (it is an Integer). The BOOL parameter is a Boolean. At the bottom of the doc page there is a section that tells you the library that contains this function, which is "User32.lib". With this this information you can create the Declare statement to this function, which looks like this:

Declare Function FlashWindow Lib "User32" (handle As Integer, invert As Boolean) As Boolean

You can call this function by passing in a window handle, so if you had the above Declare on a button's Action event handler you could flash the window with this code:

Call FlashWindow(MyWindow.Handle, True)

Since the return value is not needed, the Call statement is used to avoid having to declare a variable to store the result.

If you wanted to make this more easily accessible you could use the Extends method feature to make this available for any window. To do this, create a global method on a module with this code:

Public Sub Flash(Extends w As Window)
#If TargetWindows Then
Declare Function FlashWindow Lib "User32" (handle As Integer, invert As Boolean) As Boolean
Call FlashWindow(w.Handle, True)
#EndIf
End Sub

You could then call this method from code on a Window like this:

Self.Flash

Related Information