- fade in - fade out

Just starting out? Need help? Post your questions and find answers here.
StarWarsFan
Enthusiast
Enthusiast
Posts: 169
Joined: Sat Mar 14, 2015 11:53 am

- fade in - fade out

Post by StarWarsFan »

From what I found on the net, I made myself a routine that can
- fade in
- fade out
an entire screen with a graphic on.

I would now like to achieve that for a simple gadget,
how do I do that?
- fade a gadget in and out?

Any help appreciated. Greetings!!
Image - There is usually a lot of "try this, maybe do that" but ONLY an example that one can test for themself and get an immediate result actually brings people forward.
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Hello friends!

Post by IdeasVacuum »

If you are on Windows, you can overlay a Gadget with an ImageGadget(), setting it to be topmost (Z level). The image would start -off opaque (Window colour) and then you can use a 2D drawing loop to change the colour until transparent.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
StarWarsFan
Enthusiast
Enthusiast
Posts: 169
Joined: Sat Mar 14, 2015 11:53 am

Re: Hello friends!

Post by StarWarsFan »

IdeasVacuum wrote:...setting it to be topmost (Z level). The image would start -off opaque (Window colour) and then you can use a 2D drawing loop to change the colour until transparent.
Yes, I am on Windows 7

How do I do that: set to topmost Z level?
I have not found that in https://www.purebasic.com/documentation ... adget.html

Have you got a little code example there?

Many thanks this far!
Image - There is usually a lot of "try this, maybe do that" but ONLY an example that one can test for themself and get an immediate result actually brings people forward.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: - fade in - fade out

Post by netmaestro »

Give this code a try for fun. It's an animation so it'll probably run smoothest with the debugger turned off

Code: Select all

; Yet another useless program from netmaestro
; No warranty of course but if it develops a fever
; or starts to squeak let me know

Declare textProc(hwnd, msg, wparam, lparam)
Declare fadeIn(gadget)
Declare fadeOut(gadget)

Prototype AlphaBlend_(hdcDest.l,xoriginDest.l,yoriginDest.l,wDest.l,hDest.l,hdcSrc.l,xoriginSrc.l,yoriginSrc.l,wSrc.l,hSrc.l,ftn.l)
Global msimg32 = OpenLibrary(#PB_Any, "msimg32.dll")
Global AlphaBlend_.AlphaBlend_ = GetFunction(msimg32, "AlphaBlend")

OpenWindow(0, 0, 0, 320, 240, "", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)

Global original ;unaltered gadget image
Global bf.BLENDFUNCTION
With bf
  \BlendOp = #AC_SRC_OVER
  \BlendFlags = 0
  \AlphaFormat = #AC_SRC_ALPHA
  \SourceConstantAlpha = 0
EndWith

TextGadget(0, 10, 10, 300, 20, "Hello World! Wanna try PureBasic?")
SetProp_(GadgetID(0),"oldproc",SetWindowLongPtr_(GadgetID(0),#GWL_WNDPROC, @textProc()))
CreateImage(0,300,20,32,GetSysColor_(#COLOR_BTNFACE)|255<<24)

ButtonGadget(1, 10, 50, 120, 20, "Fade Out")
ButtonGadget(2, 140, 50, 120, 20, "Fade In")

Repeat
  EventID = WaitWindowEvent()
  Select EventID
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          fadeOut(0)
        Case 2
          fadeIn(0)
      EndSelect
  EndSelect
Until EventID = #PB_Event_CloseWindow

Procedure textProc(hwnd, msg, wparam, lparam)
  Static firstCall = 1
  oldproc = GetProp_(hwnd, "oldproc")
  Select msg
    Case #WM_NCDESTROY
      RemoveProp_(hwnd, "oldproc")
      CloseLibrary(msimg32)
      
    Case #WM_PAINT
      hdcDest = BeginPaint_(hwnd, @ps.PAINTSTRUCT)
      ; We need an image with the original unaltered pixels
      ; but we only do this once on the first call
      If firstcall
        original = CreateImage(#PB_Any,300,20)
        hdcOrig = StartDrawing(ImageOutput(original))
          BitBlt_(hdcOrig,0,0,300,20,hdcDest,0,0,#SRCCOPY)
        StopDrawing()
        firstcall=0
      EndIf
      ; Reset the DC to its original state
      ; otherwise blending on top of blending
      ; spins out of control quickly
      hdcOrig = StartDrawing(ImageOutput(original))
        BitBlt_(hdcDest,0,0,300,20,hdcOrig,0,0,#SRCCOPY)
      StopDrawing()
      alphaDC = StartDrawing(ImageOutput(0))
        AlphaBlend_(hdcDest,0,0,300,20,alphaDC,0,0,300,20,PeekL(@bf))
      StopDrawing()
      EndPaint_(hwnd, ps)
      ProcedureReturn 0
  EndSelect
  ProcedureReturn CallWindowProc_(oldproc, hwnd, msg, wparam, lparam)
EndProcedure

Procedure fadeInHelper(uTimerID.l, uMsg.l, dwUser.l, dw1.l, dw2.l)
  bf\SourceConstantAlpha - 1
  If bf\SourceConstantAlpha > 0
    InvalidateRect_(GadgetID(gadget),0,0)
  Else
    timeKillEvent_(uTimerID)
    DisableGadget(1,0)
    DisableGadget(2,1)
  EndIf
EndProcedure

Procedure fadeIn(gadget)
  bf\SourceConstantAlpha = 255
  timeSetEvent_(5,5,@fadeInHelper(), gadget, #TIME_PERIODIC)
EndProcedure

Procedure fadeOutHelper(uTimerID.l, uMsg.l, dwUser.l, dw1.l, dw2.l)
  bf\SourceConstantAlpha + 1
  If bf\SourceConstantAlpha < 255
    InvalidateRect_(GadgetID(gadget),0,0)
  Else
    timeKillEvent_(uTimerID)
    DisableGadget(1,1)
    DisableGadget(2,0)
  EndIf
EndProcedure

Procedure fadeOut(gadget)
  bf\SourceConstantAlpha = 0
  timeSetEvent_(5,5,@fadeOutHelper(), gadget, #TIME_PERIODIC)
EndProcedure
BERESHEIT
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: - fade in - fade out

Post by netmaestro »

Here's another somewhat simpler version that can be used for more complex gadgets:

Code: Select all

; Yet another useless program from netmaestro
; No warranty of course but if it develops a fever
; or starts to squeak let me know

CompilerIf #PB_Compiler_Thread=0
  MessageRequester("","Please compile threadsafe")
  End
CompilerEndIf

Declare fadeIn(gadget)
Declare fadeOut(gadget)

OpenWindow(0, 0, 0, 320, 240, "", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)

Enumeration #PB_Event_FirstCustomValue
  #UpdateImage
EndEnumeration

Global original ;unaltered gadget image
Global alphalevel=0

ListIconGadget(0, 10, 10, 300, 120,"column 0",200)
AddGadgetItem(0, -1, "Line 1")
AddGadgetItem(0, -1, "Line 2")
AddGadgetItem(0, -1, "Line 3")
AddGadgetItem(0, -1, "Line 4")
AddGadgetItem(0, -1, "Line 5")
AddGadgetItem(0, -1, "Line 6")
AddGadgetItem(0, -1, "Line 7")

UpdateWindow_(GadgetID(0))

CreateImage(0,300,120,24,GetSysColor_(#COLOR_BTNFACE))

ButtonGadget(1, 10, 210, 120, 20, "Fade Out")
ButtonGadget(2, 140, 210, 120, 20, "Fade In")

Repeat
  EventID = WaitWindowEvent()
  Select EventID
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          fadeOut(0)
        Case 2
          fadeIn(0)
      EndSelect
      
    Case #UpdateImage
      If IsGadget(2)
        SetGadgetState(10, ImageID(2))
      EndIf
  EndSelect
Until EventID = #PB_Event_CloseWindow

Procedure fadeInHelper(void)
  alphalevel = 255
  Repeat
    alphalevel - 1
    CopyImage(original, 2)
    StartDrawing(ImageOutput(2))
      DrawAlphaImage(ImageID(0),0,0,alphalevel)
    StopDrawing()
    PostEvent(#UpdateImage)   
    Delay(5)
  Until alphalevel = 0
  DisableGadget(1,0)
  DisableGadget(2,1)
  HideGadget(10, 1)
  HideGadget(0, 0)
EndProcedure

Procedure fadeIn(gadget)
  CreateThread(@fadeInHelper(),0)
EndProcedure

Procedure fadeOutHelper(void)
  alphalevel = 0
  Repeat
    alphalevel + 1
    CopyImage(original, 2)
    StartDrawing(ImageOutput(2))
      DrawAlphaImage(ImageID(0),0,0,alphalevel)
    StopDrawing()
    PostEvent(#UpdateImage) 
    Delay(5)
  Until alphalevel = 255
  DisableGadget(1,1)
  DisableGadget(2,0)
EndProcedure

Procedure fadeOut(gadget)
  GetWindowRect_(GadgetID(0),@rc.RECT)
  If original > 0 And IsImage(original):FreeImage(original):EndIf
  original = CreateImage(#PB_Any, rc\right-rc\left,rc\bottom-rc\top,24,GetSysColor_(#COLOR_BTNFACE))
  hdcSrc = GetDC_(WindowID(0))
  hdcDest = StartDrawing(ImageOutput(original))
    BitBlt_(hdcDest,0,0,rc\right-rc\left,rc\bottom-rc\top,hdcSrc,GadgetX(0),GadgetY(0),#SRCCOPY)
  StopDrawing()
  ReleaseDC_(WindowID(0), hdcSrc)
  HideGadget(0,1)
  ImageGadget(10,10,10,300,120,ImageID(original))
  CreateThread(@fadeOutHelper(),0)
EndProcedure
BERESHEIT
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: - fade in - fade out

Post by RASHAD »

Code: Select all

Global win

Procedure WndProc(hwnd, uMsg, wParam, lParam)
 result = #PB_ProcessPureBasicEvents
 Select uMsg
         
   Case #WM_MOVE
      If IsWindow(0)
        MoveWindow_(WindowID(0),GadgetX(gadget, #PB_Gadget_ScreenCoordinate),GadgetY(gadget, #PB_Gadget_ScreenCoordinate),GadgetWidth(gadget), GadgetHeight(gadget),1)
      EndIf   
             
   EndSelect      
  ProcedureReturn result 
EndProcedure

Procedure Fade(Gadget,fade)
  If IsWindow(gadget) = 0
    OpenWindow(gadget,GadgetX(gadget, #PB_Gadget_ScreenCoordinate),GadgetY(gadget,#PB_Gadget_ScreenCoordinate), GadgetWidth(gadget),GadgetHeight(gadget),"", #PB_Window_BorderLess,WindowID(win))
    SetWindowColor(gadget,GetSysColor_(#COLOR_BTNFACE))
    AnimateWindow_(WindowID(gadget),10,#AW_BLEND|#AW_HIDE)
  EndIf
  If fade = 1
    fade = 0
    AnimateWindow_(WindowID(gadget),3000, #AW_BLEND|#AW_HIDE)
  Else
    fade = 1
    AnimateWindow_(WindowID(gadget),3000, #AW_BLEND|#AW_ACTIVATE)
  EndIf
EndProcedure

win = OpenWindow(#PB_Any, 0, 0, 600, 400, "", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)

ListIconGadget(0, 10, 10, 300, 120,"column 0",200)
AddGadgetItem(0, -1, "Line 1")
AddGadgetItem(0, -1, "Line 2")
AddGadgetItem(0, -1, "Line 3")
AddGadgetItem(0, -1, "Line 4")
AddGadgetItem(0, -1, "Line 5")
AddGadgetItem(0, -1, "Line 6")
AddGadgetItem(0, -1, "Line 7")

;ButtonGadget(1,10,140,80,20,"TEST")


ButtonGadget(2, 10, 370, 120, 20, "Fade Out")
ButtonGadget(3, 140, 370, 120, 20, "Fade In")

SetWindowCallback(@WndProc())

Repeat
  EventID = WaitWindowEvent()
  Select EventID
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 2
          Fade(0,0)
                    
        Case 3
          Fade(0,1)         
      EndSelect     

  EndSelect
Until EventID = #PB_Event_CloseWindow
Egypt my love
StarWarsFan
Enthusiast
Enthusiast
Posts: 169
Joined: Sat Mar 14, 2015 11:53 am

Re: - fade in - fade out

Post by StarWarsFan »

Thank you, netmaestro!
-> Your code#1 flickers a bit, your code #2 works fine!

Thanks to RASHAD, too!

I am now trying to use that with a .PNG logo graphic.
Image - There is usually a lot of "try this, maybe do that" but ONLY an example that one can test for themself and get an immediate result actually brings people forward.
Post Reply