Page 1 of 3

StringGadget Vertical?

Posted: Fri Dec 16, 2005 4:33 am
by oryaaaaa
Can you vertically specify with StringGadget?

Thanks

Posted: Fri Dec 16, 2005 7:42 pm
by srod
Not that I'm aware of. You can certainly draw angled text as demonstrated with:

Code: Select all

If OpenWindow(0, 0, 0, 640, 400, #PB_Window_SystemMenu, "Angled text!")
  hdc = GetDC_(WindowID())
  SetBkMode_(hdc, #TRANSPARENT)
  hfont = CreateFont_(30,16, 2700,0,0,0,0,0,0,0,0,0,0,"ARIAL")
  SelectObject_(hdc, hfont)
  TextOut_(hdc, 310, 100,"Pure Basic", 10)
  DeleteObject_(hfont)
  ReleaseDC_(WindowID(), hdc)
  Repeat
    EventID.l = WaitWindowEvent()
  Until EventID = #PB_EventCloseWindow 
EndIf

End
It is entirely conceivable that you could use code similar to the above in conjunction with, say a subclassed text gadget, in which you take over the paint process and thus get the text gadget to display angled text etc. The problem (at least one of them :lol:) with the string gadget will be the caret.

Your best bet might be to create your own custom control.

Posted: Tue Dec 20, 2005 5:51 pm
by oryaaaaa
Sorry

Code: Select all

;                Up
;
;              Center
;
;               Down

Posted: Tue Dec 20, 2005 6:00 pm
by srod
Isn't this simply a multi-line string gadget?

Posted: Tue Dec 20, 2005 8:49 pm
by josku_x
Yes?

Make a StringGadget that's small in width, huge in height and add these constants in the flags:

Code: Select all

#PB_String_MultiLine|#ES_AUTOVSCROLL
I tried to make one it isn't something special. just the #ES_AUTOVSCROLL is needed...

:roll: (Yes! Even I can help :wink: )

Posted: Tue Dec 20, 2005 9:43 pm
by oryaaaaa
no, sorry.

Question from person who started PB recently
It seems to want to decide the position of length.
is not possible to do in my knowledge.

Posted: Tue Dec 20, 2005 10:45 pm
by srod
I think I understand; you wish to position the text vertically within a string gadget? I.e. sometimes place the text at the top, sometimes in the middle etc?

Unfortunately I do not think that there is a simple style-flag to achieve this. You can certainly set the left and right margins, but I haven't as yet come across a flag to set a vertical margin. I'll have a scan through MSDN in a minute.

I actually needed this myself a while ago, but everytime I repositioned the text within the gadget (through some custom drawing), the cursor would not follow! I ended up cheating.

Basically, create some kind of container gadget and paint it to look like the string gadget's background. Then simply position a borderless string gadget at the appropriate position within the container to make it look like a single edit control and that's it - job done!

If I've understood you correctly, and if I've time, I'll put together a small example for you later on.

Posted: Wed Dec 21, 2005 2:04 am
by Sparkie
How about something like this...

Code: Select all

; ************************************************
; Code:   Verticaly and Horizontaly centered text
;         in a multiline StringGadget
; Author: Sparkie
; Date:   December 20, 2005
; OS:     Windows only
; ************************************************

; ************************************************
; Procedure: Center text in Multiline StringGadget
; ************************************************
Procedure StringGadgetVCenter(gadNum)
  ;--> Get line count of StringGadget
  lineCount = SendMessage_(GadgetID(gadNum), #EM_GETLINECOUNT, 0, 0)
  myText$ = GetGadgetText(gadNum)
  ;--> Get width and height of text on one line
  hdc = GetDC_(GadgetID(gadNum))
  GetTextExtentPoint32_(hdc, myText$, Len(myText$), @textXY.SIZE)
  ReleaseDC_(GadgetID(gadNum), hdc)
  ;--> Set rect coordinates for StringGadget
  eRect.RECT
  eRect\left = 0
  eRect\top = (GadgetHeight(gadNum) - textXY\cy*lineCount) / 2
  eRect\right = GadgetWidth(gadNum) - (eRect\left * 2)
  eRect\bottom = eRect\top + textXY\cy*lineCount
  SendMessage_(GadgetID(gadNum), #EM_SETRECT, 0, eRect)
EndProcedure
; ************************************************
; Main Window 
; ************************************************
If OpenWindow(0, 0, 0, 300, 200, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, "StringGadget Centered Text") And CreateGadgetList(WindowID(0))
  ;--> #ES_CENTER will not work with a singleline StringGadget on Win95/NT4
  StringGadget(0, 50, 10, 200, 80, "Vertical and Horizontal" + #CRLF$ + "Centered Text in" + #CRLF$ + "Multiline StringGadget", #PB_String_MultiLine | #ES_CENTER)
  StringGadgetVCenter(0)
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
End

Posted: Wed Dec 21, 2005 2:07 am
by rsts
wow Sparkie, some nice code.

cheers

Posted: Wed Dec 21, 2005 2:38 am
by srod
Bloody hell, I'd never have thought of doing that! So simple. :)

I guess you'd have to call it every time new lines were added or deleted etc.

Posted: Wed Dec 21, 2005 2:43 am
by Sparkie
Thanks guys. :)

@srod: Yes, as it stands it only accepts 3 lines of text. If I get time this weekend, I'll see if I can improve on it. Or better yet, maybe I'll just let you handle the rest of it. :grin:

Posted: Wed Dec 21, 2005 3:14 am
by srod
Now that sound like a challenge to me!

Here ya go...

***EDIT: It now scrolls correctly and accounts for any scrollbars! -Twit that I am!

Code: Select all

; ************************************************ 
; Code:   Verticaly and Horizontaly centered text 
;         in a multiline StringGadget 
; Author: Sparkie + srod! (But mostly Sparkie!)
; Date:   December 20, 2005 
; OS:     Windows only 
; ************************************************ 

; ************************************************ 
; Procedure: Center text in Multiline StringGadget 
; ************************************************ 
Procedure StringGadgetVCenter(gadNum) 
  ;--> Get line count of StringGadget 
  lineCount = SendMessage_(GadgetID(gadNum), #EM_GETLINECOUNT, 0, 0) 
  myText$ = GetGadgetText(gadNum) 
  ;--> Get width and height of text on one line 
  hdc = GetDC_(GadgetID(gadNum)) 
  GetTextExtentPoint32_(hdc, myText$, Len(myText$), @textXY.SIZE) 
  ReleaseDC_(GadgetID(gadNum), hdc) 
  eRect.RECT 
  ;--> Set rect coordinates for StringGadget 
  ;The following line is used to set the left and right boundaries and takes any scrollbars
  ;into account.
  getclientrect_(GadgetID(gadNum), eRect) 
  eRect\top = (GadgetHeight(gadNum) - textXY\cy*lineCount) / 2
  eRect\bottom = eRect\top + textXY\cy*lineCount 
;Check if the rectangle is too high.  
  if eRect\bottom<gadgetheight(gadNum) 
    SendMessage_(GadgetID(gadNum), #EM_SETRECT, 0, eRect) 
  endif
EndProcedure 


; ************************************************ 
; Main Window 
; ************************************************ 
If OpenWindow(0, 0, 0, 300, 200, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, "StringGadget Centered Text") And CreateGadgetList(WindowID(0)) 
  ;--> #ES_CENTER will not work with a singleline StringGadget on Win95/NT4 
  StringGadget(0, 50, 10, 200, 80, "Vertical and Horizontal" + #CRLF$ + "Centered Text in" + #CRLF$ + "Multiline StringGadget", #PB_String_MultiLine | #ES_CENTER|#WS_VSCROLL|#ES_AUTOVSCROLL) 
  StringGadgetVCenter(0) 
  numlines = sendmessage_(gadgetid(0), #EM_GETLINECOUNT,0,0)
  
  Repeat
    eventid=waitwindowevent()
    if eventtype() = #PB_EventType_Change and eventgadgetid() = 0 and numlines<>sendmessage_(gadgetid(0), #EM_GETLINECOUNT,0,0)
      numlines = sendmessage_(gadgetid(0), #EM_GETLINECOUNT,0,0)
      StringGadgetVCenter(0) 

    endif  
  Until eventid = #PB_Event_CloseWindow 
EndIf 
End 

Posted: Tue Dec 27, 2005 8:22 pm
by oryaaaaa
Sparkie Thanks!
srod Thanks!

Very Nice code! PureBasic lovers increases! :D

Posted: Wed Dec 28, 2005 9:58 am
by akira takama
The all of you that thank you very much.
I am a japanese PB user.
This next, please help it.

sorry for my bad english.

Posted: Mon Jul 02, 2007 6:45 pm
by Fluid Byte
* BUMP *

Just needed this and took me a while to find it as it seems like a rather rare request. While I tested this I noticed some flaws. First off, the code doesn't take the currently selected font into account wich makes the lines get splitted into 2 or more blocks. Second, if you made this fix you also need to add the 2 x 2 pixel border when calculating 'eRect\bottom' or it causes the same problem.

Here's my fixed version for future searches:

Code: Select all

Procedure StringGadgetVCenter(GadgetID)
	hwndEdit = GadgetID(GadgetID)
	
	LineCount = SendMessage_(hwndEdit,#EM_GETLINECOUNT, 0, 0)

	hdc = GetDC_(hwndEdit)
	SelectObject_(hdc,GetGadgetFont(0))
	GetTextExtentPoint32_(hdc,"ABC",3,fsz.SIZE)
	ReleaseDC_(hwndEdit,hdc)	
	
	GetClientRect_(hwndEdit,eRect.RECT)
	eRect\top = (GadgetHeight(GadgetID) - fsz\cy * LineCount) / 2
	eRect\bottom = eRect\top + (fsz\cy * LineCount) + 4
	
	If eRect\bottom < GadgetHeight(GadgetID)
		SendMessage_(hwndEdit,#EM_SETRECT,0,eRect)
	EndIf
EndProcedure

Text$ = "Vertical and Horizontal" + #CRLF$ + "Centered Text in" + #CRLF$ + "Multiline StringGadget"

OpenWindow(0,0,0,300,200,"Edit Control VCenter",#WS_SYSMENU | #WS_CAPTION | 1) 
CreateGadgetList(WindowID(0))
StringGadget(0,10,10,280,180,Text$,#ES_MULTILINE | #ES_CENTER )
SetGadgetFont(0,LoadFont(0,"Arial",12))
StringGadgetVCenter(0)

LinesNum = SendMessage_(GadgetID(0), #EM_GETLINECOUNT,0,0)

Repeat
	EventID = WaitWindowEvent()
	
	Select EventGadget() 
		Case 0
		If EventType() = #PB_EventType_Change And LinesNum <> SendMessage_(GadgetID(0),#EM_GETLINECOUNT,0,0)
		  LinesNum = SendMessage_(GadgetID(0),#EM_GETLINECOUNT,0,0)
		  
		  StringGadgetVCenter(0)
		EndIf
	EndSelect
Until EventID = #PB_Event_CloseWindow