[Windows] Skin Window with PNG

Share your advanced PureBasic knowledge/code with the community.
Nituvious
Addict
Addict
Posts: 1029
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

[Windows] Skin Window with PNG

Post by Nituvious »

Hi, I wanted see if it was possible to skin a window with a transparent PNG image.
This is an extremely simple window skinning example.

If a PB Pro can follow up and tell me if the code is redundant or bad, please let me know so I can see where to improve it.
Image

Code: Select all

; Skinny example
; PB 4.51 (x86)
; by Nituvious @ 2/21/2011

;IncludeFile "WinAPI.pbi"

Procedure OpenTransparentWindow(transColor, x, y, width, height) ;-- Create an invisible, borderless, and transparent window
	; HideWindow(winResult, 0) after gadgets have been placed. Otherwise you may see flicker.
	; I do not know if this works on all versions of windows or just Vista / 7
	; I have only tested on both x86 and x64 Windows 7

	winResult = OpenWindow(#PB_Any, 1, 1, 1, 1, "",#PB_Window_BorderLess| #PB_Window_Invisible)
	SetWindowColor(winResult,transColor)

	SetWindowLongPtr_(WindowID(winResult),#GWL_EXSTYLE,  #WS_EX_LAYERED)  ; -- According to the MSDN, this function will work with both x86 and x64 ( http://msdn.microsoft.com/en-us/library/ms644898(v=VS.85).aspx )
	SetWindowPos_(WindowID(winResult),#Null, x, y, width, height, #Null) ; -- Must redraw window, according to the MSDN

	SetLayeredWindowAttributes_(WindowID(winResult),transColor,#Null,#LWA_COLORKEY)

	ProcedureReturn winResult

EndProcedure

Procedure TransparentTextGadget(hwnd.l, x, y, string.s, Font.l, Color.l) ;-- Create Text without a background
	; TextGadget() seem's to leave holes behind everything
	; It is probably better practice to remove the StartDrawing(WindowOutput(hwnd)) from this procedure
	; and instead place it within the main loop. I do not know if keeping it within a Procedure would hinder performance.
	StartDrawing(WindowOutput(hwnd))
		If Font.l <> #Null
			DrawingFont(FontID(Font.l))
		EndIf
		DrawingMode(#PB_2DDrawing_Transparent)
		DrawText(x, y, string.s,Color.l)
	StopDrawing()
EndProcedure


Enumeration 1
	#Window_Font
	#Window_SkinPNG
	#Window_Skin
	#Window_Close
EndEnumeration

UsePNGImageDecoder()
LoadFont(#Window_Font, "Gabriola", 24)
LoadImage(#Window_SkinPNG,"C:\skin.png")

MyWindow = OpenTransparentWindow(RGB(255,0,0), 450,200, 400,200)
ImageGadget(#Window_Skin,0,0,200,200,ImageID(#Window_SkinPNG)) : DisableGadget(#Window_Skin,1) ; <-- borderless image gadget is our "skin", for some reason things act strangely if the gadget does not get disabled


Frame3DGadget(#PB_Any,5, 26, 135,40,"")
ButtonGadget(#Window_Close, 330,25, 15,15,"X")

HideWindow(MyWindow, 0) ; <-- no flashy here

Repeat : Delay(1)

	eventID = WaitWindowEvent(1)

	If eventID = #PB_Event_Gadget
		Select EventGadget()
			Case #Window_Close
				eventID = #PB_Event_CloseWindow
		EndSelect
	EndIf

	TransparentTextGadget(MyWindow, 10, 20, "Hello World!", #Window_Font, RGB(250,0,0))

Until eventID = #PB_Event_CloseWindow
[Edit] fixed code so it should work now
Last edited by Nituvious on Tue Feb 22, 2011 3:38 am, edited 1 time in total.
▓▓▓▓▓▒▒▒▒▒░░░░░
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: [Windows] Skin Window with PNG

Post by rsts »

winapi.pbi?

cheers
Nituvious
Addict
Addict
Posts: 1029
Joined: Sat Jul 11, 2009 4:57 am
Location: United States

Re: [Windows] Skin Window with PNG

Post by Nituvious »

rsts wrote:winapi.pbi?

cheers
Oops, sorry about that! It was just an include with contained function prototypes so I could use the functions in the demo version. Sorry about that.
I have edited the code so it should work now.
▓▓▓▓▓▒▒▒▒▒░░░░░
User avatar
Alireza
Enthusiast
Enthusiast
Posts: 143
Joined: Sat Aug 16, 2008 2:02 pm
Location: Iran

Re: [Windows] Skin Window with PNG

Post by Alireza »

Nice example, Thanks
PB v 5.6 :D
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: [Windows] Skin Window with PNG

Post by ts-soft »

Nice example.

You should change the eventloop to:

Code: Select all

Repeat
  
  eventID = WaitWindowEvent()
  
  If eventID = #PB_Event_Gadget
    Select EventGadget()
      Case #Window_Close
        eventID = #PB_Event_CloseWindow
    EndSelect
  ElseIf eventID = #PB_Event_Repaint
    TransparentTextGadget(MyWindow, 10, 20, "Hello World!", #Window_Font, RGB(250,0,0))
  EndIf
  
  
Until eventID = #PB_Event_CloseWindow 
A delay in event loop is a bad idea. Code outsided the events the same.

Greetings - Thomas
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: [Windows] Skin Window with PNG

Post by IdeasVacuum »

XP x86 PB4.51, the Window cannot be moved?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: [Windows] Skin Window with PNG

Post by ts-soft »

IdeasVacuum wrote:XP x86 PB4.51, the Window cannot be moved?
The code isn't designed to move :wink:

Code: Select all

Repeat
  
  eventID = WaitWindowEvent()
  
  If eventID = #PB_Event_Gadget
    Select EventGadget()
      Case #Window_Close
        eventID = #PB_Event_CloseWindow
        
    EndSelect
  ElseIf eventID = #PB_Event_Repaint
    TransparentTextGadget(MyWindow, 10, 20, "Hello World!", #Window_Font, RGB(250,0,0))
    
  ElseIf  #WM_KEYDOWN
    If GetActiveGadget() <> #Window_Close
      ReleaseCapture_()
      SendMessage_(WindowID(MyWindow), #WM_SYSCOMMAND, #SC_MOVE | #HTCAPTION, 0)
    EndIf
  EndIf   
  
Until eventID = #PB_Event_CloseWindow 
Last edited by ts-soft on Tue Feb 22, 2011 1:27 pm, edited 1 time in total.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: [Windows] Skin Window with PNG

Post by c4s »

IdeasVacuum wrote:XP x86 PB4.51, the Window cannot be moved?
Use this code at e.g. #WM_LBUTTONDOWN and it should work:

Code: Select all

SendMessage_(WindowID(WindowNr), #WM_NCLBUTTONDOWN, #HTCAPTION, 0)
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
User avatar
ultralazor
Enthusiast
Enthusiast
Posts: 186
Joined: Sun Jun 27, 2010 9:00 am

Re: [Windows] Skin Window with PNG

Post by ultralazor »

There are a lot of old skinning threads here, I remember stuff about gadgets breaking. You can't do it with GTK/Linux and Mac/MUI without further hacking UI libs.
so many ideas so little time..
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Re: [Windows] Skin Window with PNG

Post by ricardo »

But, if the image (PNG) has some part of it with alpha transparency, it does no show as transparent (showing the desktop) but get colored by red. How to avoid this problem?
ARGENTINA WORLD CHAMPION
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: [Windows] Skin Window with PNG

Post by c4s »

@ricardo
Today is your lucky day. ;)
Yesterday I needed the same, so I just created a code (which is based on another one I forgot). It's a little more complex than this but works great. See here:
http://www.purebasic.fr/english/viewtop ... 12&t=45811
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Re: [Windows] Skin Window with PNG

Post by ricardo »

c4s wrote:@ricardo
Today is your lucky day. ;)
Yesterday I needed the same, so I just created a code (which is based on another one I forgot). It's a little more complex than this but works great. See here:
http://www.purebasic.fr/english/viewtop ... 12&t=45811

Thanks!! :)

I found a different solution, BUT found the same problem... cant put gadgets on it!!
Now, with your code i guess this other question was already answered too :D

Thank you again!
ARGENTINA WORLD CHAMPION
Post Reply