Author Topic: [SDK] 07 - Take control of your window(s) [????? LOOK]  (Read 3160 times)

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1980
    • zapsolution
[SDK] 07 - Take control of your window(s) [????? LOOK]
« on: February 09, 2020, 05:11:30 pm »
WORK IN PROGRESS

In this version we introduce the use of the extended style: WS_EX_LAYERED and WS_EX_COMPOSITED
We need them to perform compositing and alphablending that is the key to create amazing effects.

Compositing means that we paint our controls from bottom to top as multiple overlaping layers just like you could do in painting softwares like PhotoShop the Gimp or zDraw...

Layered window they can only be used with 2000, XP and VISTA, but this tutorial anyway is targeted only to the NT technology.

To create a transparent window, not only it must have the WS_EX_LAYERED extended style but you must also setup the alpha channel like this:
Code: [Select]
DECLARE FUNCTION SetLayeredWindowAttributes (BYVAL LONG, BYVAL LONG, BYVAL BYTE, BYVAL LONG) AS LONG
' Allows to use Windows translucency with 2000 and XP
FUNCTION zSetLayeredAlpha (BYVAL hWnd AS LONG, BYVAL TransLevel AS BYTE) AS LONG
    STATIC hLib???, hProc???
    IF hLib??? = 0 THEN hLib??? = LoadLibrary ("User32.dll")
    IF hLib??? AND zOsVersion > 499 THEN ' If Windows 2000 or XP
       IF hProc??? = 0 THEN hProc??? = GetProcAddress(hLib???, "SetLayeredWindowAttributes")
       IF hProc??? THEN
          OldStyle& = GetWindowLong(hWnd&, %GWL_EXSTYLE)
          CALL SetWindowLong(hWnd&, %GWL_EXSTYLE, OldStyle& OR %WS_EX_LAYERED)
        ' Use this with a transparent color
        ' CALL DWORD hProc??? USING SetLayeredWindowAttributes (hWnd&, RGB(255,0,255), TransLevel?, %LWA_ALPHA OR %LWA_COLORKEY) TO lRet&
          CALL DWORD hProc??? USING SetLayeredWindowAttributes (hWnd&, 0, TransLevel?, %LWA_ALPHA) TO lRet&
          IF lRet& THEN FUNCTION = -1
       END IF
    END IF
END FUNCTION

The TransLevel paremeter ranges from 0 (full transparent mode) to 255 (full opaque mode).
And if you are very attentive, you will see that a special effect is used to show the form while switching from DEFAULT to MAXIMIZE and RESTORE mode.


Starting with this version, you can now paint the background of the form either in tiled or stretched mode, when using the stretched mode there is a new function to compute the zoom factor being used to stretch the background while preserving the aspect of the original image altogether with the necessity to paint the whole form.
Code: [Select]
'// Computes location and size to stretch a bitmap preserving its aspect.
SUB zComputeAspect (BYVAL xPicSize AS LONG, BYVAL yPicSize AS LONG, BYVAL xCell AS LONG, BYVAL yCell AS LONG, BYREF xPos AS LON
, BYREF yPos AS LONG, BYREF xSize AS LONG, BYREF ySize AS LONG)
    LOCAL scale AS SINGLE
    IF xPicSize THEN scale = xCell / xPicSize
    xSize = xPicSize * scale: ySize = yPicSize * scale
    IF ySize < yCell THEN
       IF yPicSize THEN scale = yCell / yPicSize
       xSize = xPicSize * scale: ySize = yPicSize * scale
    END IF
    xPos = (xCell - xSize) \ 2: yPos = (yCell - ySize) \ 2
END SUB

There are also a couple of functions to help computation of true 32-bit ARG colors:
'// Split a true 32-bit ARGB color into its four components.
SUB zSplitColorARGB (BYVAL ARGB&, Alpha AS BYTE, Red AS BYTE, Green AS BYTE, Blue AS BYTE)

'// Add the alpha channel to RGB color to create true 32-bit ARGB color.
FUNCTION zColorARGB (BYVAL Alpha AS BYTE, BYVAL ColrRGB???) AS LONG

For the purpose of compositing I have added a new function to help changing the z-order of controls.
Code: [Select]
'// Set control z-order
SUB zSetZorder(BYVAL hWnd AS LONG, BYVAL UseOrder AS LONG)
    CALL SetWindowPos(hWnd, UseOrder, 0, 0, 0, 0, %SWP_NOMOVE OR %SWP_NOSIZE OR %SWP_NOACTIVATE)
END SUB

I have added a new zFrameButton to show you two things:
1 - Changing the z-order to put the %ID_AEROBUTTON at the z-order's bottom.
2 - Using the %ANCHOR_CENTER_HORZ_BOTTOM to resize the %ID_AEROBUTTON while resizing the main form.

Double buffer has been reworked to take care of a special case when switching from MAXIMIZE to RESTORE mode. To see the double buffer in action, just move any window hover the Template form and see how it is redrawn, then do the same hover any other application and see the difference.
« Last Edit: February 09, 2020, 05:50:49 pm by Patrice Terrier »
Patrice
(Always working with the latest Windows version available...)

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1980
    • zapsolution
Re: [SDK] 07 - Take control of your window(s) [????? LOOK]
« Reply #1 on: February 09, 2020, 06:19:50 pm »
.
Patrice
(Always working with the latest Windows version available...)