Frame3DGadget: Custom Text Color / Theme Support / 64-Bit

Share your advanced PureBasic knowledge/code with the community.
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Frame3DGadget: Custom Text Color / Theme Support / 64-Bit

Post by Fluid Byte »

Code: Select all

; Title:    Frame3DGadget with custom text color / Theme Support / 64-Bit
;
; Author:   Fluid Byte
; Version:  PureBasic V4.XX
; Platform: Windows XP and higher
; E-Mail:   fluidbyte@web.de

EnableExplicit

Structure FRAME3DEX
	lpPrevFunc.i
	clrText.l
	bThemeXP.b
EndStructure

Procedure Frame3DExProc(hWnd,uMsg,wParam,lParam)
	Protected *frmex.FRAME3DEX, hDC, ps.PAINTSTRUCT, Title.s, fts.SIZE, wrc.RECT, lpBuffer, hThemeButton
	
	*frmex = GetWindowLongPtr_(hwnd,#GWL_USERDATA)
	
	Select uMsg
		Case #WM_NCDESTROY
		FreeMemory(*frmex)
		ProcedureReturn 0
		
		Case #WM_PAINT     
		hdc = BeginPaint_(hwnd,ps)
		
		SelectObject_(hdc,SendMessage_(hwnd,#WM_GETFONT,0,0))
		
		Title = GetGadgetText(GetDlgCtrlID_(hwnd))   
		GetTextExtentPoint32_(hdc,Title,Len(Title),fts)   
		GetClientRect_(hWnd,wrc)
		SetRect_(wrc,wrc\left,wrc\top + fts\cy / 2,wrc\right,wrc\bottom)
		
		If OSVersion() >= #PB_OS_Windows_XP And IsThemeActive_() And IsAppThemed_() And *frmex\bThemeXP
			lpBuffer = AllocateMemory(14) : PokeS(lpBuffer,"Button",-1,#PB_Unicode)     
			
			hThemeButton = OpenThemeData_(WindowID(0),lpBuffer)
			DrawThemeBackground_(hThemeButton,hdc,4,1,wrc,0)
			CloseThemeData_(hThemeButton)
			
			FreeMemory(lpBuffer)
		Else
			DrawEdge_(hdc,wrc,#EDGE_ETCHED,#BF_RECT)
		EndIf
		
		If GetWindowColor(0) > -1
			SetBkColor_(hdc,GetWindowColor(0))
		Else
			SetBkColor_(hdc,GetSysColor_(#COLOR_3DFACE))
		EndIf
		
		SetTextColor_(hdc,*frmex\clrText)
		TextOut_(hdc,9,0,Title,Len(Title))   
		
		EndPaint_(hwnd,ps)
		
		ProcedureReturn 0
	EndSelect
	
	ProcedureReturn CallWindowProc_(*frmex\lpPrevFunc,hWnd,uMsg,wParam,lParam)  
EndProcedure

Procedure Frame3DGadgetEx(Gadget,X,Y,Width,Height,Text.s,Color.l=0)     
	Protected *frmex.FRAME3DEX, HINSTANCE, hResData, Length
	Protected dvi.DLLVERSIONINFO\cbsize = SizeOf(DLLVERSIONINFO)
	
	Frame3DGadget(Gadget,X,Y,Width,Height,Text)
	
	*frmex = AllocateMemory(SizeOf(FRAME3DEX))
	*frmex\lpPrevFunc = SetWindowLongPtr_(GadgetID(Gadget),#GWL_WNDPROC,@Frame3DExProc())
	*frmex\clrText = Color
	
	HINSTANCE = OpenLibrary(#PB_Any,"comctl32.dll")
	
	If HINSTANCE
		CallFunction(HINSTANCE,"DllGetVersion",@dvi)
		If dvi\dwMajorVersion = 6 : *frmex\bThemeXP = #True : EndIf
		CloseLibrary(HINSTANCE)
	EndIf

	SetWindowLongPtr_(GadgetID(Gadget),#GWL_USERDATA,*frmex)
	
	ProcedureReturn GadgetID(Gadget)
EndProcedure

SetGadgetFont(#PB_Default,LoadFont(0,"Arial",9))

OpenWindow(0,0,0,400,300,"Ownerdraw Frame3D Control",#WS_OVERLAPPEDWINDOW | 1)
Frame3DGadgetEx(0,10,5,200,90,"Frame3DGadgetEx #1",#Red)
Frame3DGadgetEx(1,10,100,200,90,"Frame3DGadgetEx #2",RGB(40,180,70))
Frame3DGadgetEx(2,10,195,200,90,"Frame3DGadgetEx #3",#Blue)

While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
Last edited by Fluid Byte on Sun Jan 22, 2017 2:07 pm, edited 6 times in total.
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Frame3DGadget: Custom Text Color / Theme Support / 64-Bi

Post by srod »

Very nice Fluid, works well.

One problem; your code for identifying when themes are active may yield incorrect results. E.g. turn off themes but check the "Request administrator mode for Windows Vista" and your code will think that the manifest added is in fact the one requesting XP themes etc. :)
I may look like a mule, but I'm not a complete ass.
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Re: Frame3DGadget: Custom Text Color / Theme Support / 64-Bi

Post by Fluid Byte »

* investigates *
* locates problem: even with disabled themes manifest is added including specific details for admin mode *
* opens cheatbox *
* looking for fix *
* can't find anything, pressure rises *
* finally realizing that comparing text in manifest files helps identifying activated/disabled skin mode *
* closes cheatbox *
* aplies fix *
* loading . *
* loading .. *
* loading ... *
* loading .... *
* fix aplied *
* exits 3rd person mode *

This works here on Windows 7 x86/x64. Please give it a try and let me now if it works on Vista and XP as well.
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Frame3DGadget: Custom Text Color / Theme Support / 64-Bi

Post by srod »

Yep works fine on Vista 32.

Still wouldn't be a solution I would trust though. Personally, I just load up the comctl32.dll and call the DllGetVersion function. This will tell you which version of the common controls lib your app has linked with.
I may look like a mule, but I'm not a complete ass.
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Re: Frame3DGadget: Custom Text Color / Theme Support / 64-Bi

Post by Fluid Byte »

Guess you are right. Updated. :o

* Sadly puts his cheatbox in the corner with a tear in is eye *
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Frame3DGadget: Custom Text Color / Theme Support / 64-Bi

Post by srod »

Fluid Byte wrote:Guess you are right. Updated. :o

* Sadly puts his cheatbox in the corner with a tear in is eye *
I'm still wiping the tears left over from that thrashing your boys dished out to the England world cup team! You made ours look like a pub team! :)
I may look like a mule, but I'm not a complete ass.
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Re: Frame3DGadget: Custom Text Color / Theme Support / 64-Bi

Post by ABBKlaus »

Shouldn't this be 14 bytes to allocate ?

Code: Select all

lpBuffer = AllocateMemory(14)
* enablepurifier *

BR Klaus
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Re: Frame3DGadget: Custom Text Color / Theme Support / 64-Bi

Post by Fluid Byte »

srod wrote:I'm still wiping the tears left over from that thrashing your boys dished out to the England world cup team! You made ours look like a pub team!
Please don't get me started on this :mrgreen:

+++ Englands team is overaged, will be better next world cup +++ 4:1 = total destruction +++ the 4:2 not seen by the referee -> revenge for Wembley 1966
ABBKlaus wrote:Shouldn't this be 14 bytes to allocate ?
Yes and no. Theoretically you are right because the null-char takes two bytes in Unicode.
Practically it doesn't matter since -1 is passed for length to PokeS(). So it reads to the first null-char in memory.
I updated it anyway though for convenience. :)
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Post Reply