Drawtext modes

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Michael Vogel
Addict
Addict
Posts: 2798
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Drawtext modes

Post by Michael Vogel »

Hi,

I'd like to have some additional modes for drawtext (or an additional drawtextplus function) including the DT_XXX constants of windows.

For example things like DT_(NO)PREFIX, DT_CENTER, DT_RIGHT would be nice and could be controlled by something like Drawtextmode or so...

I found this missing, when I saw the cool button example of netmaestro (http://www.purebasic.fr/english/viewtop ... 3&start=10) - the only thing missing is the underlined shortkey character...

Michael
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

For now, you can still use the DrawText_() API which should be straight forward to use.
Pferd
New User
New User
Posts: 1
Joined: Sat Apr 22, 2006 11:22 am

Post by Pferd »

If it only were so straightforward (for me).
Can anybody give me a hint where I can find help for making DrawText_ work? Unfortunately the board's search suppresses the underscore. So my requests for DrawText_ always end up in PB's DrawText() which doesn't help so much.
The problem is that DrawText_ in my example never works for an image DC but only for a windows DC.
Or what am I doing wrong?

Code: Select all

;PB 3.94 (but doesn't work in PB 4 either)
Procedure WindowCallback(WindowID, Message, lParam, wParam)
  If Message = #WM_PAINT
    StartDrawing(WindowOutput())
      DrawImage(UseImage(0), 40, 10)
    StopDrawing()
  EndIf
  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

If OpenWindow(0, 100, 100, 500, 300, #PB_Window_SystemMenu, "PureBasic - Image")
  Gosub CreateImage
  SetWindowCallback(@WindowCallback())
  Repeat
    EventID = WaitWindowEvent()
  Until EventID = #PB_Event_CloseWindow
EndIf
End

CreateImage:
  If CreateImage(0, 255, 255)
    imageDC=StartDrawing(ImageOutput())
    trec.RECT
    trec\left=5: trec\top=5: trec\right=100: trec\bottom=100
    hBrush = CreateSolidBrush_($ff0000)
    FillRect_(imageDC, trec, hBrush)
      DrawText_(imageDC,"TEXT",Len("TEXT"),trec,#DT_CENTER|#DT_VCENTER|#DT_SINGLELINE)
    StopDrawing()
  EndIf
Return
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Post by va!n »

Working version for PureBasic v4 beta 11 :-)

Code: Select all

; PureBasic v4 Code

Procedure WindowCallback(WindowID, Message, lParam, wParam)
  If Message = #WM_PAINT
    StartDrawing(WindowOutput(0))
      DrawImage(ImageID(0), 40, 10)
    StopDrawing()
  EndIf
  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

If OpenWindow(0, 100, 100, 500, 300, "PureBasic - Image", #PB_Window_SystemMenu)
  Gosub CreateImage
  SetWindowCallback(@WindowCallback())
  Repeat
    EventID = WaitWindowEvent()
  Until EventID = #PB_Event_CloseWindow
EndIf
End

CreateImage:
  If CreateImage(0, 255, 255)
    imageDC=StartDrawing(ImageOutput(0))
    trec.RECT
    trec\left=0: trec\top=0: trec\right=100: trec\bottom=100
    hBrush = CreateSolidBrush_($ff0000)
    FillRect_(imageDC, trec, hBrush)
      DrawText_(imageDC,"TEXT",Len("TEXT"),trec,#DT_CENTER|#DT_VCENTER|#DT_SINGLELINE)
    StopDrawing()
  EndIf
Return
and here a version with another API command to draw the text...

Code: Select all

; PureBasic v4 Code

Procedure WindowCallback(WindowID, Message, lParam, wParam)
  If Message = #WM_PAINT
    StartDrawing(WindowOutput(0))
      DrawImage(ImageID(0), 40, 10)
    StopDrawing()
  EndIf
  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

If OpenWindow(0, 100, 100, 500, 300, "PureBasic - Image", #PB_Window_SystemMenu)
  Gosub CreateImage
  SetWindowCallback(@WindowCallback())
  Repeat
    EventID = WaitWindowEvent()
  Until EventID = #PB_Event_CloseWindow
EndIf
End

CreateImage:
  If CreateImage(0, 255, 255)
    imageDC=StartDrawing(ImageOutput(0))
    trec.RECT
    trec\left=5: trec\top=5: trec\right=100: trec\bottom=100
    hBrush = CreateSolidBrush_($ff0000)
    
    FillRect_(imageDC, trec, hBrush)
;     DrawText_(imageDC,"TEXT",Len("TEXT"),trec,#DT_CENTER|#DT_VCENTER|#DT_SINGLELINE)
      TextOut_(imageDC, 5, 5, "TEXT",Len("TEXT"))
    StopDrawing()
  EndIf
Return
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Post by va!n »

here is just one more advanced source...

Setting FrontColor by API and BackgroundColor by API to transparent....

Code: Select all

Procedure WindowCallback(WindowID, Message, lParam, wParam)
  If Message = #WM_PAINT
    StartDrawing(WindowOutput(0))
      DrawImage(ImageID(0), 40, 10)
    StopDrawing()
  EndIf
  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

If OpenWindow(0, 100, 100, 500, 300, "PureBasic - Image", #PB_Window_SystemMenu)
  Gosub CreateImage
  SetWindowCallback(@WindowCallback())
  Repeat
    EventID = WaitWindowEvent()
  Until EventID = #PB_Event_CloseWindow
EndIf
End

CreateImage:
  If CreateImage(0, 255, 255)
    imageDC=StartDrawing(ImageOutput(0))
    trec.RECT
    trec\left=5: trec\top=5: trec\right=100: trec\bottom=100
    hBrush = CreateSolidBrush_($ff0000)
    
    FillRect_(imageDC, trec, hBrush)
    
      SetBkMode_(imageDC, #TRANSPARENT)
      SetTextColor_(imageDC,#White)
;     DrawText_(imageDC,"TEXT",Len("TEXT"),trec,#DT_CENTER|#DT_VCENTER|#DT_SINGLELINE)
      TextOut_(imageDC, 5, 5, "TEXT",Len("TEXT"))
    StopDrawing()
  EndIf
Return
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
User avatar
Michael Vogel
Addict
Addict
Posts: 2798
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Post by Michael Vogel »

And why such a simple code does not work? I get the text cutted on the screen the underscore seems to be displaced also...

Code: Select all

Global DC

Procedure WinDrawText(x,y,w,h,text.s,flags)
 Protected winrect.Rect
 winrect\left=x
 winrect\top=y
 winrect\right=x+w
 winrect\bottom=y+h
 DrawText_(DC,@text,Len(text),@winrect,flags) 
EndProcedure


If OpenWindow(0,100,100,500,300,"DrawText",#PB_Window_SystemMenu) 

  DC=StartDrawing(WindowOutput(0))
     
  Repeat
    WinDrawText(50,10,300,50,"Hello - Yo&u!",#DT_LEFT)
  Until WaitWindowEvent()=#PB_Event_CloseWindow 

  StopDrawing()

EndIf 
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

You have to use MoveToEx_() to change the current text position, the rect is only here for the clipping:

Code: Select all

Global DC

Procedure WinDrawText(x,y,w,h,text.s,flags)
 Protected winrect.Rect
 
 MoveToEx_(DC, x, y, 0)
 
 winrect\left=x
 winrect\top=y
 winrect\right=x+w
 winrect\bottom=y+h
 DrawText_(DC,@text,Len(text),winrect,flags)
EndProcedure


If OpenWindow(0,100,100,500,300,"DrawText")

  Repeat
    DC=StartDrawing(WindowOutput(0))
    If DC
      WinDrawText(50, 100, 200, 50, "Hello - Yo&u!", #DT_LEFT)
      StopDrawing()
    EndIf
  Until WaitWindowEvent() = #PB_Event_CloseWindow

EndIf
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Any idea why tabs doesn't work?

Code: Select all

Global DC 

Procedure WinDrawText(x,y,w,h,text.s,flags) 
 Protected winrect.Rect 
  
 MoveToEx_(DC, x, y, 0) 
  
 winrect\left=x 
 winrect\top=y 
 winrect\right=x+w 
 winrect\bottom=y+h 
 DrawText_(DC,@text,Len(text),winrect,flags) 
EndProcedure 


If OpenWindow(0,100,100,500,300,"DrawText") 

  Repeat 
    DC=StartDrawing(WindowOutput(0)) 
    If DC 
      WinDrawText(50, 100, 200, 50, "Hello" + #TAB$ + "Yo&u!", #DT_LEFT | #DT_EXPANDTABS) 
      StopDrawing() 
    EndIf 
  Until WaitWindowEvent() = #PB_Event_CloseWindow 

EndIf
srod says tab problem seems to be related to the device context returned by StartDrawing(): http://www.purebasic.fr/english/viewtopic.php?p=140785
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

Post by MrMat »

Trond wrote:Any idea why tabs doesn't work?
PSDK entry for DrawText wrote:The text alignment mode for the device context must include the TA_LEFT, TA_TOP, and TA_NOUPDATECP flags.
Since the PB DC has TA_UPDATECP set, use:

Code: Select all

SetTextAlign_(DC, #TA_LEFT | #TA_TOP | #TA_NOUPDATECP)
Mat
User avatar
Michael Vogel
Addict
Addict
Posts: 2798
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re:

Post by Michael Vogel »

Fred wrote:For now, you can still use the DrawText_() API which should be straight forward to use.
I would still like native PB commands to get right aligned, centered and prefix text easily done... any chances for the next PB version?
User avatar
Michael Vogel
Addict
Addict
Posts: 2798
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Drawtext modes

Post by Michael Vogel »

Here's what I use now for simulating the line below the shortcut character -- quite complicate but there's a small bonus, the line color can be set to a prefered colour...

Code: Select all

Global OptIconsActive=#True

#ButtonIconBorder=10
#ButtonIconPadding=6
#ButtonIconDefaultSize=16
#ButtonIconPrefixColor=#Red

Enumeration
	#ButtonIcon_Null		=%0000
	#ButtonIcon_Left		=%0001
	#ButtonIcon_Center	=%0010
	#ButtonIcon_NoPrefix	=%1000
EndEnumeration

#NoTransparency=$ff000000
#FullTransparency=$00000000

Procedure SetGadgetIconText(Gadget,IconHandle,Text.s,IconSize=0,IconFlags=#ButtonIcon_Left)

	Protected Font=GetGadgetFont(#PB_Default)
	Protected BW,BH,IW
	Protected WW,WX,WY
	Protected Prefix,PX,PY,PW

	If IconSize=0
		IconSize=#ButtonIconDefaultSize
	EndIf

	BW=GadgetWidth(Gadget)
	BH=GadgetHeight(Gadget)
	IW=IconSize+#ButtonIconPadding

	CreateImage(Gadget,1,1)
	StartDrawing(ImageOutput(Gadget))
	DrawingFont(Font)

	If IconFlags&#ButtonIcon_NoPrefix=#False
		Prefix=FindString(Text,"&",1)
		If Prefix
			Text=ReplaceString(Text,"&","")
			PX=TextWidth(Left(Text,Prefix-1))
			PW=TextWidth(Mid(Text,Prefix,1))
			PY=TextHeight("W")-1
		EndIf
	EndIf

	WW=IW+TextWidth(Text)
	WY=(BH-TextHeight("Wg"))>>1
	StopDrawing()

	Select IconFlags&(#ButtonIcon_Left|#ButtonIcon_Center)
	Case #ButtonIcon_Left,#ButtonIcon_Null
		WX=#ButtonIconBorder
	Case #ButtonIcon_Center
		WX=(BW-WW)>>1
	EndSelect

	CreateImage(Gadget,BW,BH,32)
	StartDrawing(ImageOutput(Gadget))

	DrawingMode(#PB_2DDrawing_AlphaChannel)
	Box(0,0,BW,BH,#FullTransparency)

	DrawingMode(#PB_2DDrawing_AlphaBlend|#PB_2DDrawing_Transparent)
	DrawingFont(Font)
	If Prefix
		LineXY(WX+IW+PX,WY+PY,WX+IW+PX+PW,WY+PY,#NoTransparency|#ButtonIconPrefixColor)
	EndIf
	DrawText(WX+IW,WY,Text,#NoTransparency|#Black)
	If IconHandle
		DrawImage(IconHandle,WX,(BH-IconSize)>>1)
	EndIf
	StopDrawing()

	SetGadgetAttribute(Gadget,#PB_Button_Image,ImageID(Gadget))

EndProcedure
Procedure GadgetIconText(Gadget,X,Y,W,H,IconHandle,Text.s,Flags=0)

	If OptIconsActive
		ButtonImageGadget(Gadget,X,Y,W,H,0)
		SetGadgetIconText(Gadget,IconHandle,Text,#ButtonIconDefaultSize,Flags)
	Else
		ButtonGadget(Gadget,X,Y,W,H,Text,Flags)
	EndIf

EndProcedure

OpenWindow(0, 0, 0, 512, 384, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)

UsePNGImageDecoder()
IconHandle=CatchImage(0,?IconData)
ImageGadget(99,220,200,200,200,0)

GadgetIconText(1,10,20,400,40,IconHandle,"#&ButtonIcon_Left",#ButtonIcon_Left)
GadgetIconText(2,10,70,400,40,IconHandle,"#ButtonIcon_Left & #ButtonIcon_NoPrefix",#ButtonIcon_Left|#ButtonIcon_NoPrefix)
GadgetIconText(3,10,120,400,40,IconHandle,"#B&uttonIcon_Center",#ButtonIcon_Center)
GadgetIconText(4,10,170,400,40,IconHandle,"#ButtonIcon_Center & #ButtonIcon_NoPrefix",#ButtonIcon_Center|#ButtonIcon_NoPrefix)
GadgetIconText(5,10,220,400,40,IconHandle,"Change Text & Prefix ?!")

Repeat
	Select WaitWindowEvent()

	Case #PB_Event_Gadget
		DisableGadget(1,Random(1));  dis/enables one button
		SetGadgetIconText(5,IconHandle,"Demo &"+Chr(Random(25)+'A'),0,#ButtonIcon_Center); changes button text

	Case #PB_Event_CloseWindow
		Break

	EndSelect
ForEver

DataSection
	IconData:;	Purebasic 'Z' by Michael Vogel
	Data.b 0,0,1,0,1,0,16,16,0,0,1,0,8,0,104,5
	Data.b 0,0,22,0,0,0,40,0,0,0,16,0,0,0,32,0
	Data.b 0,0,1,0,8,0,0,0,0,0,0,1,0,0,0,0
	Data.b 0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0
	Data.b 0,0,33,36,33,0,57,60,57,0,0,32,198,0,115,134
	Data.b 214,0,132,146,222,0,165,174,231,0,222,219,222,0,231,231
	Data.b 231,0,247,243,247,0,0,0,0,0,0,0,0,0,0,0
	Data.q 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	Data.q 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	Data.q 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	Data.q 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	Data.q 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	Data.q 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	Data.q 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	Data.l 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	Data.b 0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