Mask a window with regions

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Mask a window with regions

Post by BackupUser »

Restored from previous forum. Originally posted by altesocke.

Code updated for 5.20+

hi forum

below you see a little example, how to mask a window with regions.
for more information about using regions see the MSDN online: http://msdn.microsoft.com/library/en-us ... s_7ab7.asp

cu socke

Code: Select all

#Window1 = 1
#W1Btn1 = 1
#W1Btn2 = 2

WinW=500 : WinH=410
hwnd = OpenWindow( #Window1, (GetSystemMetrics_(#SM_CXSCREEN)-WinW)/2, (GetSystemMetrics_(#SM_CYSCREEN)-WinH)/2, WinW, WinH,"Region Mask", 0)   ; centred window
If hwnd > 0
  
  ButtonGadget(#W1Btn1,103,330 ,89,25,"Mask Window")
  ButtonGadget(#W1Btn2,264,330 ,89,25,"Close")
  
  RectRgn = CreateRoundRectRgn_(0, 0, WinW, WinH, 50, 50)  ; rectangle region to give the window rounded edges
  EllipRgn = CreateEllipticRgn_(50, 50, 250, 250) ; elliptic region
  CombinedRgn = RectRgn ; to combine regions, the target region must exist before calling CombineRgn
  CombineRgn_(CombinedRgn, CombinedRgn, EllipRgn, #RGN_XOR) ;the parameter #RGN_XOR creates the union of two combined regions except for any overlapping areas.
  
  Repeat
    Delay(1)
    EventID.l = WaitWindowEvent()
    
    Select EventID
        
      Case #PB_Event_Gadget
        
        Select EventGadget()
          Case #W1Btn1 ;----------Code
            SetWindowRgn_(hwnd, RectRgn, #True) ;sets the window region of a window, #TRUE repaints the window
            
          Case #W1Btn2 ;----------Code
            EventID = #PB_Event_CloseWindow
            
        EndSelect
        
    EndSelect
    
  Until EventID = #PB_Event_CloseWindow
  
EndIf
End

Aeroschmelz
User
User
Posts: 25
Joined: Mon Apr 12, 2004 9:14 am

Post by Aeroschmelz »

Is there a possibility to make the window transparent, but keep the gadgets on it visible ?
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post by LarsG »

I believe this was asked by another guy just recently.. try searching the forums for some code..

AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
Aeroschmelz
User
User
Posts: 25
Joined: Mon Apr 12, 2004 9:14 am

Post by Aeroschmelz »

Didnt find any ! Sorry, only transparent windows, but not with the gadgets visible...
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post by fweil »

...,

Maybe if you start workking with that you could find a way to catch gadgets regions and then make only gadgets visible.

Code: Select all

Structure eyes
  x.l
  y.l
EndStructure

  WindowXSize.l = 480
  WindowYSize.l = 360
  Where_I_Look.POINT
  Quit.l = #FALSE
  OldWhere_I_Look_x.l
  OldWhere_I_Look_y.l
  
  GetCursorPos_(Start.POINT)
  hWnd = OpenWindow(0, Start\x - WindowXSize / 2, Start\y - WindowYSize / 2, WindowXSize, WindowYSize, #PB_Window_SystemMenu, "Eyes")
  If hWnd
      AddKeyboardShortcut(0, #PB_Shortcut_Escape, 99)
      EllipRgn1 = CreateEllipticRgn_(WindowXSize / 4, WindowYSize / 2, WindowXSize / 4 + 100, WindowYSize / 2 + 50) ; elliptic region
      EllipRgn2 = CreateEllipticRgn_(WindowXSize / 2, WindowYSize / 2, WindowXSize / 2 + 100, WindowYSize / 2 + 50) ; elliptic region
      CombinedRgn = EllipRgn1 ; to combine regions, the target region must exist before calling CombineRgn
      CombineRgn_(CombinedRgn, CombinedRgn, EllipRgn2, #RGN_XOR) ;the parameter #RGN_XOR creates the union of two combined regions except for any overlapping areas.
      SetWindowRgn_(hWnd, CombinedRgn, #TRUE) ;sets the window region of a window, #TRUE repaints the window
      Repeat
        GetCursorPos_(Where_I_Look)
        If Where_I_Look\x <> OldWhere_I_Look_y Or Where_I_Look\y <> Where_I_Look_y
            ImageID = CreateImage(0, WindowXSize, WindowYSize)
            StartDrawing(ImageOutput())
              Box(0, 0, WindowXSize, WindowYSize, 0)
              FrontColor(255, 255, 255)
              DrawingMode(1)
              Ellipse(WindowXSize / 4, WindowYSize / 2, 100, 50, $300000)
              Ellipse(WindowXSize / 2, WindowYSize / 2, 100, 50, $300000)
              Circle(WindowXSize / 2 + (Where_I_Look\x - WindowXSize / 2) / 5, WindowYSize / 2- (WindowYSize / 2- Where_I_Look\y) / 8, 25, $C0FFFF)
              Circle(WindowXSize / 4 + (Where_I_Look\x - WindowXSize / 2) / 5, WindowYSize / 2- (WindowYSize / 2- Where_I_Look\y) / 8, 25, $C0FFFF)
            StopDrawing()
            StartDrawing(WindowOutput())
              DrawImage(ImageID, 0, 0)
            StopDrawing()
            OldWhere_I_Look_x= Where_I_Look\x
            OldWhere_I_Look_y = Where_I_Look\y
        EndIf
        Delay(1)
      Until WindowEvent() = #PB_Event_CloseWindow Or EventMenuID() = 99
  EndIf
  TerminateProcess_(GetCurrentProcess_(), 0)
End
I just let you think ATM but I start a workaround now.

Rgrds
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
Aeroschmelz
User
User
Posts: 25
Joined: Mon Apr 12, 2004 9:14 am

Post by Aeroschmelz »

Why will this one work:

RgnB = CreateRectRgn_(25,0, 970, 30)

and this won't

RgnB = CreateRectRgn_(25,0, WindowWidth.f, 30)
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

Perhaps, WindowsWidth should be an integer instead of a float?
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
Aeroschmelz
User
User
Posts: 25
Joined: Mon Apr 12, 2004 9:14 am

Post by Aeroschmelz »

Ok, it works now, does somebody now how can i reset the window to show fully up again. This command somehow:

DeleteObject_(GRgn.l)
Post Reply