Ver. 2 :StringGadget() Text Vertically Aligned (Windows)

Share your advanced PureBasic knowledge/code with the community.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Ver. 2 :StringGadget() Text Vertically Aligned (Windows)

Post by RASHAD »

It is so simple but very effective
I wonder if somebody posted alike before this post (It should be)

Code: Select all

Global fID,hh

Procedure setColor(cont,gad,bkcolor,fcolor)
  If bkcolor <= 0
    SetGadgetColor(cont,#PB_Gadget_BackColor,$FFFFFF)
  Else
    SetGadgetColor(cont,#PB_Gadget_BackColor,bkcolor)
    SetGadgetColor(gad,#PB_Gadget_BackColor,bkcolor)
  EndIf
  SetGadgetColor(gad,#PB_Gadget_FrontColor,fcolor)
EndProcedure

Procedure fHeight(fName$,fSize)
  fID = LoadFont(#PB_Any,fName$,fSize)
  hdc = StartDrawing(WindowOutput(0))
    DrawingFont(FontID(fID))
    hh = TextHeight("Q") + 6
  StopDrawing()
EndProcedure

Procedure SizeCB()
  ResizeGadget(10,#PB_Ignore,#PB_Ignore,WindowWidth(0)-20,#PB_Ignore)
  ResizeGadget(0,#PB_Ignore,#PB_Ignore,WindowWidth(0)-20,#PB_Ignore)
  ResizeGadget(11,#PB_Ignore,#PB_Ignore,WindowWidth(0)-20,#PB_Ignore)
  ResizeGadget(1,#PB_Ignore,#PB_Ignore,WindowWidth(0)-20,#PB_Ignore)
  ResizeGadget(12,#PB_Ignore,#PB_Ignore,WindowWidth(0)-20,#PB_Ignore)
  ResizeGadget(2,#PB_Ignore,#PB_Ignore,WindowWidth(0)-20,#PB_Ignore)
EndProcedure

Text$ = "Test for vertical centered text - horizontal (above) or right aligned (below)..."

OpenWindow(0,0,0,300,200,"Edit Control VCenter",#PB_Window_SystemMenu| #PB_Window_MinimizeGadget| #PB_Window_MaximizeGadget| #PB_Window_SizeGadget| #PB_Window_ScreenCentered)
fHeight("Georgia",12)
ContainerGadget(10,10,10,280,40,#PB_Container_Flat)
  StringGadget(0,0,GadgetHeight(10)/2-hh/2,280,hh,Text$,#ES_LEFT|#PB_String_BorderLess)
  SetGadgetFont(0,FontID(fID))  
  setcolor(10,0,0,$0000FF)
CloseGadgetList()

fHeight("Tahoma",8)
ContainerGadget(11,10,60,280,40,#PB_Container_Flat)
  StringGadget(1,0,GadgetHeight(11)/2-hh/2,280,hh,Text$,#ES_CENTER|#PB_String_BorderLess)
  SetGadgetFont(1,FontID(fID))
  setcolor(11,1,$B2EEFE,$00FF00)
CloseGadgetList()

fHeight("Consolas",14)
ContainerGadget(12,10,110,280,40,#PB_Container_Flat)
  StringGadget(2,0,GadgetHeight(12)/2-hh/2,280,hh,Text$,#ES_RIGHT|#PB_String_BorderLess)
  SetGadgetFont(2,FontID(fID))
  setcolor(12,2,$D3FEFC,$FF0000)
CloseGadgetList()

BindEvent(#PB_Event_SizeWindow,@SizeCB())

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
        End
   
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 0,1,2
          Select EventType()

          EndSelect
      EndSelect
    
    EndSelect

ForEver
Last edited by RASHAD on Sun Jan 31, 2016 4:59 am, edited 1 time in total.
Egypt my love
User avatar
Michael Vogel
Addict
Addict
Posts: 2807
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: StringGadget() Text Vertically Aligned (Windows)

Post by Michael Vogel »

Great, but not perfect...

Just add the folowing code line below to see the difference to the standard StringGadgets...
StringGadget(3,10,160,280,40,Text$)


As soon I try to simulate the border color of default StringGadgets it gets complicated again...

Code: Select all

Global fID,hh

Procedure setColor(cont,gad,bkcolor,fcolor)
	If bkcolor <= 0
		SetGadgetColor(cont,#PB_Gadget_BackColor,$FFFFFF)
	Else
		SetGadgetColor(cont,#PB_Gadget_BackColor,bkcolor)
		SetGadgetColor(gad,#PB_Gadget_BackColor,bkcolor)
	EndIf
	SetGadgetColor(gad,#PB_Gadget_FrontColor,fcolor)
EndProcedure
Procedure fHeight(fName$,fSize)
	fID = LoadFont(#PB_Any,fName$,fSize)
	hdc = StartDrawing(WindowOutput(0))
	DrawingFont(FontID(fID))
	hh = TextHeight("Q") + 6
	StopDrawing()
EndProcedure
Procedure SizeCB()
	ResizeGadget(10,#PB_Ignore,#PB_Ignore,WindowWidth(0)-20,#PB_Ignore)
	ResizeGadget(0,#PB_Ignore,#PB_Ignore,WindowWidth(0)-20,#PB_Ignore)
	ResizeGadget(11,#PB_Ignore,#PB_Ignore,WindowWidth(0)-20,#PB_Ignore)
	ResizeGadget(1,#PB_Ignore,#PB_Ignore,WindowWidth(0)-20,#PB_Ignore)
	ResizeGadget(12,#PB_Ignore,#PB_Ignore,WindowWidth(0)-20,#PB_Ignore)
	ResizeGadget(2,#PB_Ignore,#PB_Ignore,WindowWidth(0)-20,#PB_Ignore)
EndProcedure

Text$ = "Test for vertical centered text - horizontal (above) or right aligned (below)..."

OpenWindow(0,0,0,300,250,"Edit Control VCenter",#PB_Window_SystemMenu| #PB_Window_MinimizeGadget| #PB_Window_MaximizeGadget| #PB_Window_SizeGadget| #PB_Window_ScreenCentered)
fHeight("Georgia",12)
ContainerGadget(10,10,10,280,40,#PB_Container_Flat)
StringGadget(0,0,GadgetHeight(10)/2-hh/2,280,hh,Text$,#ES_LEFT|#PB_String_BorderLess)
SetGadgetFont(0,FontID(fID))
setcolor(10,0,0,$0000FF)
CloseGadgetList()

fHeight("Tahoma",8)
TextGadget(21,10,60,280,40,"")
TextGadget(11,11,61,278,38,"")
DisableGadget(21,1)
DisableGadget(11,1)
SetGadgetColor(11,#PB_Gadget_BackColor,#White)
StringGadget(1,11,61+GadgetHeight(11)/2-hh/2,278,hh,Text$,#ES_CENTER|#PB_String_BorderLess)
SetGadgetFont(1,FontID(fID))
SetGadgetColor(21,#PB_Gadget_BackColor,GetSysColor_(#COLOR_ACTIVEBORDER))
;setcolor(11,1,$B2EEFE,$00FF00)

fHeight("Consolas",14)
ContainerGadget(12,10,110,280,40,#PB_Container_Flat)
StringGadget(2,0,GadgetHeight(12)/2-hh/2,280,hh,Text$,#ES_RIGHT|#PB_String_BorderLess)
SetGadgetFont(2,FontID(fID))
setcolor(12,2,$D3FEFC,$FF0000)
CloseGadgetList()

StringGadget(3,10,160,280,40,Text$)

BindEvent(#PB_Event_SizeWindow,@SizeCB())

Repeat
	Select WaitWindowEvent()
	Case #PB_Event_CloseWindow
		End

	Case #PB_Event_Gadget
		Select EventGadget()
		Case 1
			Select EventType()
				Case #PB_EventType_Focus
				SetGadgetColor(21,#PB_Gadget_BackColor,GetSysColor_(#COLOR_HIGHLIGHT))
				SetGadgetColor(11,#PB_Gadget_BackColor,#White)
				Case #PB_EventType_LostFocus
				SetGadgetColor(21,#PB_Gadget_BackColor,GetSysColor_(#COLOR_ACTIVEBORDER))
				SetGadgetColor(11,#PB_Gadget_BackColor,#White)
			EndSelect
		EndSelect

	EndSelect

ForEver
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Ver. 2 :StringGadget() Text Vertically Aligned (Windows)

Post by RASHAD »

@Michael
I am not suppose to simulate what MS or Fred & freak did
The forum member can adapt what he got for his needs
But for your sake next is version 2
Code for 6 myStringGadget()

Code: Select all

Global fID,hh
Global Dim gad(5,1)

Procedure myStringFont(fName$,fSize)
  f = LoadFont(#PB_Any,fName$,fSize)
  fID = FontID(f)
  hdc = StartDrawing(WindowOutput(0))
    obj = SelectObject_(hdc, fID )
    GetTextMetrics_(hdc, @fnt.TEXTMETRIC)
    hh = fnt\tmheight
    DeleteObject_(obj)
  StopDrawing()
EndProcedure

Procedure myStringGadget(Gadget,x,y,Width,Height,Content$,bColor,fColor,Flags)
  If hh = 0
    fID = GetGadgetFont(#PB_Default)
    hdc = StartDrawing(WindowOutput(0))
      obj = SelectObject_(hdc, fID )
      GetTextMetrics_(hdc, @fnt.TEXTMETRIC)
      hh = fnt\tmheight
      DeleteObject_(obj)
    StopDrawing()
  EndIf
  gad(gadget,0) = ContainerGadget(#PB_Any,x,y,Width,Height,#PB_Container_BorderLess)
    SetGadgetColor(gad(gadget,0),#PB_Gadget_BackColor,0)
      gad(gadget,1) = TextGadget(#PB_Any,1,1,Width-2,Height-2,"")
      StringGadget(Gadget,2,GadgetHeight(gad(gadget,1))/2-hh/2,Width-4,hh,Content$,#PB_String_BorderLess|Flags)
      SetGadgetFont(Gadget,fID)
  CloseGadgetList()
  If bColor <= 0
    SetGadgetColor(gad(Gadget,1),#PB_Gadget_BackColor,$FFFFFF)
  Else
    SetGadgetColor(gad(Gadget,1),#PB_Gadget_BackColor,bColor)
    SetGadgetColor(Gadget,#PB_Gadget_BackColor,bColor)
  EndIf
  SetGadgetColor(Gadget,#PB_Gadget_FrontColor,fColor)
EndProcedure

Procedure myStringSize()
  For g = 0 To 2
    ResizeGadget(gad(g,0),#PB_Ignore,#PB_Ignore,WindowWidth(0)-20,#PB_Ignore)
    ResizeGadget(gad(g,1),#PB_Ignore,#PB_Ignore,WindowWidth(0)-22,#PB_Ignore)
    ResizeGadget(g,#PB_Ignore,#PB_Ignore,WindowWidth(0)-24,#PB_Ignore)
  Next
EndProcedure

Procedure myStringResize(Gadget,x,y,Width,Height)
    ResizeGadget(gad(Gadget,0),x,y,Width,#PB_Ignore)
    ResizeGadget(gad(Gadget,1),1,1,Width-2,#PB_Ignore)
    ResizeGadget(Gadget,2,#PB_Ignore,Width-4,#PB_Ignore)
EndProcedure

Text$ = "Test for vertical centered text - horizontal (above) or right aligned (below)..."

OpenWindow(0,0,0,300,200,"Edit Control VCenter",#PB_Window_SystemMenu| #PB_Window_MinimizeGadget| #PB_Window_MaximizeGadget| #PB_Window_SizeGadget| #PB_Window_ScreenCentered)

myStringFont("Georgia",12) ;Should be before creating the string gadget                         
myStringGadget(0,10,10,280,40,Text$,0,$0000FF,#ES_LEFT)

myStringFont("Tahoma",8)
myStringGadget(1,10,60,280,40,Text$,$B2EEFE,$00FF00,#ES_CENTER)

myStringFont("Consolas",14)
myStringGadget(2,10,110,280,40,Text$,$D3FEFC,$FF0000,#ES_RIGHT)

ButtonGadget(10,10,170,80,20,"Resize")

BindEvent(#PB_Event_SizeWindow,@myStringSize())

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
        End
   
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 0,1,2
          Select EventType()
            Case #PB_EventType_Focus
                SetGadgetColor(gad(EventGadget(),0),#PB_Gadget_BackColor, GetSysColor_(#COLOR_HIGHLIGHT))
            Case #PB_EventType_LostFocus
                SetGadgetColor(gad(EventGadget(),0),#PB_Gadget_BackColor,0)

          EndSelect
          
        Case 10
          myStringResize(1,20,60,350,40)
          
      EndSelect
    
    EndSelect

ForEver
Egypt my love
User avatar
Michael Vogel
Addict
Addict
Posts: 2807
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Ver. 2 :StringGadget() Text Vertically Aligned (Windows)

Post by Michael Vogel »

@Rashad, don't worry, as I told you, I gave up already some days ago :wink:

I do a simplier approach now (See here, F6 goes through the setting pages), which uses your code to put the text on the right (!) position :wink:

Anyhow, as you said, the original windows behavour can't be simulated easily, so your examples are the best in showing alternatives (even the inactive borders are not black here on Windows 8.1).
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Ver. 2 :StringGadget() Text Vertically Aligned (Windows)

Post by Kwai chang caine »

Thanks at you two for your sharing 8)
ImageThe happiness is a road...
Not a destination
Post Reply