ImageGadget - SetLayeredWindowAttributes_

Just starting out? Need help? Post your questions and find answers here.
eriansa
Enthusiast
Enthusiast
Posts: 277
Joined: Wed Mar 17, 2004 12:31 am
Contact:

ImageGadget - SetLayeredWindowAttributes_

Post by eriansa »

Does something exist to make an ImageGadget semi-transparent?
(cfr. SetLayeredWindowAttributes_)

I can't find anything in the WinSDK...
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

A child window can't be layered.
BERESHEIT
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

I can't give you an answer directly.

But there is something similar, if this helps you: You can also fade in/out an ImageGadget() with AnimateWindow_()!
PB 4.30

Code: Select all

onErrorGoto(?Fred)
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

Not sure what your actual need is, but maybe you can use GDI AlphaBlend(). Here's a sample of what you can do with it...
Image
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Ollivier
Enthusiast
Enthusiast
Posts: 281
Joined: Mon Jul 23, 2007 8:30 pm
Location: FR

Post by Ollivier »

A prehistorical man could help you.

Indeed, I ignore near totally API functions either one or two functions that I grup in such a procedure like that :

Code: Select all

Procedure XPDisp(WindowID.L, ColorKey.L, Alpha.L, nImage.L) 
    ; (From a other code : Thanks to Flype, Le Soldat Inconnu, and other...)
    Protected Bmp.BITMAP
    Protected Pt.POINT
    Protected Blend.BLENDFUNCTION 
    Protected NewFlag.L
    Protected HDC.L
    Protected ULW.L
  
    NewFlag = GetWindowLong_(WindowID, #GWL_EXSTYLE) | #WS_EX_LAYERED
    SetWindowLong_(WindowID, #GWL_EXSTYLE, NewFlag)  
    GetObject_(ImageID(nImage), SizeOf(BITMAP), @Bmp)     
    HDC = StartDrawing(ImageOutput(nImage))     
        With Blend      
            \BlendOp = 0 
            \BlendFlags = 0 
            \AlphaFormat = 0 
            \SourceConstantAlpha = Alpha
        EndWith         
        UpdateLayeredWindow_(WindowID, 0, 0, @Bmp\BmWidth, HDC, @Pt, ColorKey, @Blend, 3)
    StopDrawing()            
  
EndProcedure
Window... No Window... Gadget... Menu : All is the same for me: a set of small layered windows. It's for me the displaying unit. All my GUIs functions used it.

I am sorry. I was not very happy with gadgets, menus, windows & co. So...
Last edited by Ollivier on Mon Sep 10, 2007 6:41 pm, edited 1 time in total.
Ollivier
Enthusiast
Enthusiast
Posts: 281
Joined: Mon Jul 23, 2007 8:30 pm
Location: FR

Post by Ollivier »

...For example, this code:

Code: Select all

      StartDrawing(ImageOutput(Img) )
          For y = 0 To ImageHeight(Img) / 2
              For x = 0 To ImageWidth(Img)
                  p = Point(x, y)
                  r = 255 - Red(p)
                  v = 255 - Red(p)
                  b = 255 - Red(p)
                  d = Sqr(r*r + v*v + b*b)
                  If d < 220
                      If Random(99) = 0
                          g = 255 - Random(128)
                          Plot(x, y, RGB(g, g, g) )
                      Else
                          Plot(x, y, #Black)
                      EndIf
                  EndIf
              Next
          Next
      StopDrawing()
...allows me to change the sky of the above image, like that:
Image
The problem is the fact I am lost with all the possibilities and definition of the API and other DLLs. It's longer for me to search the good function than coding the algo I want. I see really good code with API in the forum (I thank netmaestro and srod who show here it's very very usefull. I hope the others will forgive me...) but I can't learn all these definitions.

EDIT: I thank Flype and "Le soldat inconnu" for XPDisp()

2sparkie : Alphablend : it's an API function ? It uses PNG format.

I try to understand Base64 coding and PNG overview. But I didn't find litteral algorithm in the web.

If anybody knows a page about it?
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Have a play around with this, demo uses this image:

http://www.networkmaestro.com/flower.png

Code: Select all

;==============================================================
; Program:         Alpha Image Gadget Demo
; Author:          Lloyd Gallant (netmaestro)
; Date:            September 9, 2007
; Target OS:       Microsoft Windows 2000/XP +
; Target Compiler: PureBasic 4.xx +
; License:         Free, unrestricted, credit appreciated
;                  but not required
;==============================================================

Enumeration
  #ImgSource
  #ImgOutput
EndEnumeration

Prototype AlphaBlend(hdcDest,nXOriginDest,nYOriginDest,nWDest,nHDest,hdcSrc,nXOriginSrc,nYOriginSrc,nWSrc,nHSrc,Blendfunction)
msimg32 = OpenLibrary(#PB_Any, "msimg32.dll")
If msimg32
  Global AlphaBlend_.AlphaBlend = GetFunction(msimg32, "AlphaBlend")
Else
  MessageRequester("Error!","Can't open msimg32.lib",#MB_ICONERROR)
EndIf

; Source Image
UsePNGImageDecoder()
loadedimage = LoadImage(#PB_Any, "flower.png")

; Prepare source image by drawing it transparently against 
; the window background color (only needs done the once)
; If the image has no transparent parts, doesn't need done at all
CreateImage(#ImgSource, ImageWidth(loadedimage),ImageHeight(loadedimage), 32)
StartDrawing(ImageOutput(#ImgSource))
  Box(0,0,ImageWidth(loadedimage),ImageHeight(loadedimage),GetSysColor_(#COLOR_BTNFACE))
  DrawAlphaImage(ImageID(loadedimage),0,0)
StopDrawing()

; Output Image
CreateImage(#ImgOutput, ImageWidth(loadedimage),ImageHeight(loadedimage), 32)

Procedure SetAlphaImageGadgetState(gadget, imagenumber, outputnumber, alphalevel)

  Protected W = ImageWidth(imagenumber)
  Protected H = ImageHeight(imagenumber)
  Protected Blendmode
  Protected *bf.BLENDFUNCTION = @Blendmode
  With *bf
    \BlendOp             = 0
    \BlendFlags          = 0
    \SourceConstantAlpha = alphalevel
    \AlphaFormat         = 0
  EndWith

  ScreenDC = GetWindowDC_(#Null)
  hdcSrc = CreateCompatibleDC_(ScreenDC)
  ReleaseDC_(#Null, ScreenDC)
  SelectObject_(hdcSrc, ImageID(imagenumber))

  hdcDest = StartDrawing(ImageOutput(outputnumber))
    Box(0,0,W,H,GetSysColor_(#COLOR_BTNFACE))
    AlphaBlend_(hdcDest, 0,0,W,H, hdcSrc,0,0,W,H, BlendMode)
    DeleteDC_(hdcSrc)
  StopDrawing()
  
  SetGadgetState(gadget, ImageID(outputnumber))
  
EndProcedure

OpenWindow(0,0,0,640,480,"Alphablended Image Gadget", $CA0001)
CreateGadgetList(WindowID(0))
ImageGadget(0,160,100,0,0,0)
TrackBarGadget(1,100,400,400,20,0,255)
SetGadgetState(1,128) : SetAlphaImageGadgetState(0, #imgSource, #imgOutput, 128)

Repeat
  ev = WaitWindowEvent()
  If ev = #PB_Event_Gadget
    If EventGadget() = 1
      SetAlphaImageGadgetState(0, #imgSource, #imgOutput, GetGadgetState(1))
    EndIf
  EndIf
Until ev = #WM_CLOSE

CloseLibrary(msimg32)
BERESHEIT
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

@Ollivier: Yes, AlphaBlend is API and Netmaestro's code is just about the same as mine so no need for me to post more code. :wink:
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
eriansa
Enthusiast
Enthusiast
Posts: 277
Joined: Wed Mar 17, 2004 12:31 am
Contact:

Post by eriansa »

thx!
but i was looking for a way to blend an imagegadget with the background of it's parent window (which uses a patternbrush for it's background)

Image

oh well, i'll do it with a layeredwindow instead of an imagegadget.
(if it's not taking too much resources)
Ollivier
Enthusiast
Enthusiast
Posts: 281
Joined: Mon Jul 23, 2007 8:30 pm
Location: FR

Post by Ollivier »

Euh... Yes Eriansa...

But after testing the speed of the execution of netmaestro's code, you can see it's better! As I wrote above, actually I am not very 'seduced' by gadgets, menus, etc... But this idea using imagegadgets is really good !

Indeed, the position of your layered image is independant with UpdateLayeredWindow().

But just test the 2 codes during an other application is displayed in foreground (on the front). Using blended imagegadget is smoother when you are changing windows position (or size) of the other application. This detail could be very important :D

My friends often want me to code applications with big and resizable skins cause their screens are bigger than mine! (resolution)
>> To realise this, Up...LWindow() take more time...

2 sparkie

I didn't realise immediately advantages of the alphablend function instead of ULW(). And netmaestro runs quicker than his shadow!!
POOM >> CODE!
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

I'm really busy so I just hacked this quickly, but I can optimize it later and remove the DrawAlphaImage for speed. But here it is in raw form (and probably leaking) for a pattern:

http://www.networkmaestro.com/rattan.jpg
http://www.networkmaestro.com/flower.png

Code: Select all

;==============================================================
; Program:         Alpha Image Gadget Demo
; Author:          Lloyd Gallant (netmaestro)
; Date:            September 9, 2007
; Target OS:       Microsoft Windows 2000/XP +
; Target Compiler: PureBasic 4.xx +
; License:         Free, unrestricted, credit appreciated
;                  but not required
;==============================================================

Enumeration
  #ImgSource
  #ImgMask
  #ImgOutput
EndEnumeration

Prototype AlphaBlend(hdcDest,nXOriginDest,nYOriginDest,nWDest,nHDest,hdcSrc,nXOriginSrc,nYOriginSrc,nWSrc,nHSrc,Blendfunction)
msimg32 = OpenLibrary(#PB_Any, "msimg32.dll")
If msimg32
  Global AlphaBlend_.AlphaBlend = GetFunction(msimg32, "AlphaBlend")
Else
  MessageRequester("Error!","Can't open msimg32.lib",#MB_ICONERROR)
EndIf

; Source Image
UsePNGImageDecoder():UseJPEGImageDecoder()
Global loadedimage = LoadImage(#PB_Any, "flower.png")

CreateImage(#ImgSource, ImageWidth(loadedimage),ImageHeight(loadedimage), 32)

; Mask Image
maskimage = LoadImage(#PB_Any, "rattan.jpg")
CreateImage(#imgMask, GetSystemMetrics_(#SM_CXSCREEN), GetSystemMetrics_(#SM_CYSCREEN), 32)
ScreenDC = GetWindowDC_(#Null)
hdcMask   = CreateCompatibleDC_(ScreenDC)
ReleaseDC_(#Null, ScreenDC)
SelectObject_(hdcMask, ImageID(#imgMask))
patternBrush = CreatePatternBrush_(ImageID(maskimage))
SelectObject_(hdcMask, patternBrush)
SelectObject_(hdcMask, GetStockObject_(#NULL_PEN))
Rectangle_(hdcMask, 0,0,ImageWidth(#imgMask),ImageHeight(#imgMask))
DeleteDC_(hdcMask)

; Output Image
CreateImage(#ImgOutput, ImageWidth(loadedimage),ImageHeight(loadedimage), 32)

Procedure SetAlphaImageGadgetState(gadget, imagenumber, outputnumber, alphalevel)

  Protected W = ImageWidth(imagenumber)
  Protected H = ImageHeight(imagenumber)
  Protected X = GadgetX(gadget)
  Protected Y = GadgetY(gadget)
  Protected Blendmode
  Protected *bf.BLENDFUNCTION = @Blendmode
  With *bf
    \BlendOp             = 0
    \BlendFlags          = 0
    \SourceConstantAlpha = alphalevel
    \AlphaFormat         = 0
  EndWith

  mask = GrabImage(#imgMask, #PB_Any, X,Y,W,H)
  StartDrawing(ImageOutput(#imgSource))
    DrawImage(ImageID(mask),0,0)
    DrawAlphaImage(ImageID(loadedimage),0,0)
  StopDrawing()

  ScreenDC = GetWindowDC_(#Null)
  hdcSrc   = CreateCompatibleDC_(ScreenDC)
  ReleaseDC_(#Null, ScreenDC)
  SelectObject_(hdcSrc, ImageID(imagenumber))

  hdcDest = StartDrawing(ImageOutput(outputnumber))
    DrawImage(ImageID(mask),0,0)
    AlphaBlend_(hdcDest, 0,0,W,H, hdcSrc,0,0,W,H, BlendMode)
    DeleteDC_(hdcSrc)
  StopDrawing()
  FreeImage(mask)
  SetGadgetState(gadget, ImageID(outputnumber))
  
EndProcedure

OpenWindow(0,0,0,640,480,"Alphablended Image Gadget", $CA0001)
CreateGadgetList(WindowID(0))
ImageGadget(0,160,100,0,0,0)
TrackBarGadget(1,100,400,400,20,0,255)
SetGadgetState(1,128) : SetAlphaImageGadgetState(0, #imgSource, #imgOutput, 128)
SetClassLong_(WindowID(0),#GCL_HBRBACKGROUND, patternBrush)
InvalidateRect_(WindowID(0),0,1)
Repeat
  ev = WaitWindowEvent()
  If ev = #PB_Event_Gadget
    If EventGadget() = 1
      SetAlphaImageGadgetState(0, #imgSource, #imgOutput, GetGadgetState(1))
    EndIf
  EndIf
Until ev = #WM_CLOSE

CloseLibrary(msimg32)
BERESHEIT
Ollivier
Enthusiast
Enthusiast
Posts: 281
Joined: Mon Jul 23, 2007 8:30 pm
Location: FR

Post by Ollivier »

I just write this post cause your code above is really beautiful. I thank you NetMaestro!
Post Reply