Dela via


Creating Irregularly Shaped Windows Sample

File: ...\Samples\Solution\Toledo\Irregular.scx

The main Visual FoxPro window and any forms you create have rectangular-shaped client areas by default. To change the shape, you can use the Windows API SetLayeredWindowAttributes function, which sets the opacity and transparency color key of a layered window. SetLayeredWindowAttributes is supported in Microsoft Windows 2000 and Windows XP.

Windows API Function Declarations

In this sample, the form's Init event contains the following declarations for the necessary API functions:

DECLARE INTEGER SetLayeredWindowAttributes IN win32api;
      INTEGER HWND, INTEGER crKey, INTEGER bAlpha, INTEGER dwFlags
DECLARE INTEGER SetWindowLong IN user32.DLL ;
   INTEGER hWnd, INTEGER nIndex, INTEGER dwNewLong
DECLARE INTEGER GetWindowLong IN user32.DLL ;
   INTEGER hWnd, INTEGER nIndex

For more information about the Init event, see Init Event. For detailed information about these declarations and functions, see MSDN online at https://msdn.microsoft.com.

Displaying Irregularly-Shaped Windows

You can display a nonrectangular form by using the SetLayeredWindowAttributes function and setting a transparent color. Shapes that are drawn using this color appear transparent. Also, any mouse clicks in these shapes pass through to the visible form.

This functionality is supported only in Microsoft Windows 2000 and Windows XP but is more efficient than previous techniques for setting a bounding region on the form. Although this technique makes a form transparent, the form must be set up properly to work with this functionality.

To set up the form

  1. On the form, set the FormShowWindow property to 2 (As Top-Level Form) to make it possible to draw a layered window.

  2. Turn off the window frame because it is not drawn when transparent by setting the following properties for the form:

    BorderStyle   = 0
    Caption   = ""
    Closable   = .F.
    ControlBox   = .F.
    TitleBar   = 0
    
  3. In the MakeIrregular method of the sample, set the magenta areas, as specified by the nColor variable, to transparent using the following code:

    * Gets existing flags from the window.
    lnFlags = GetWindowLong(nHWND, GWL_EXSTYLE)
    ThisForm.nFlags = lnFlags 
    * Appends Layered flag to existing flags.
    lnFlags = BITOR(lnFlags, WS_EX_LAYERED)
    * Sets new flags to the window.
    SetWindowLong(nHWND, GWL_EXSTYLE, lnFlags)      SetLayeredWindowAttributes(nHWND, nColor, 0, LWA_COLORKEY)
    

For more information, see Form Designer, Creating Forms, and ShowWindow Property.

See Also

Solutions Samples