How to get unicode text from controls like button and label ?

New to FreeBASIC? Post your questions here.
kcvinu
Posts: 134
Joined: Oct 07, 2015 16:44
Location: Keralam, India

How to get unicode text from controls like button and label ?

Postby kcvinu » Feb 25, 2018 20:28

Hi there,
I am practicing with writing my own gui library in FreeBasic. But i have few doubts on how to retrieve unicode text from controls.
Here is the property signature code for my button class.

Code: Select all

' Declaration section in Type NewButton
Declare Property Text() As LPTSTR
Declare Property Text(Byval sText As LPTSTR)


And here is my property code. In this code, only setter portion is working very well. Getter code is not working.

Code: Select all

'getter
Property NewButton.Text() As LPTSTR
   Dim BufferSize As Long
   Dim Buffer As LPTSTR
   BufferSize = GetWindowTextLength(This.Handle)
   Buffer = Space(BufferSize)
   GetWindowText(This.Handle, Buffer, BufferSize + 1) 
   Property =  Rtrim(Buffer, Chr(0))
End Property

'Setter
Property NewButton.Text(Byval sText As LPTSTR)
   SetWindowText(This.Handle, sText)
End Property


I know that using space function with LPTSTR is wrong. How can i deal this problem ? If i use String instead of LPTSTR, this Getter property will work. But i want my library to support unicode string. Any help ?
kcvinu
Posts: 134
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: How to get unicode text from controls like button and label ?

Postby kcvinu » Feb 25, 2018 20:40

Here is an example of my working gui class.

Code: Select all

#include "GuiClass.bi"
Dim msg as MSG
dim hGui as NewGui = NewGui("Hey this is my Window",400, 400)
dim winHwnd As HWND = hGui.Handle
Dim btn1 As NewButton = NewButton(winHwnd, "Uni String",,,50,50)
Dim btn2 As NewButton = NewButton(winHwnd, "Uni too",,,180,50)

Dim stat As BOOL = False

 Do
   MainEventLoop(msg )
   select Case msg.HWND
   
      Case winHwnd
         Select Case msg.message
            Case WM_LBUTTONUP
               Print "Window Clicked"
            ' wm_create is not working for some unknown reason
            Case WM_CREATE
               Print "This wont happen"
            Case WM_PAINT
               
               If stat = False Then
                  'Here i can do anything because i cant do it in WM_CREATE
                  stat = TRUE
               End If
         End Select
         
      Case btn1.Handle
         Select Case msg.message 
            
            Case WM_LBUTTONUP
               
               sMsgBox("Hey you clicked on Button1")
         End Select
         
      Case btn2.Handle
         Select Case msg.message
            Case WM_LBUTTONUP
               btn1.Text = "He changed"
               
         End Select         
         
      
   End Select
loop until WindowClose(msg) = true
End
kcvinu
Posts: 134
Joined: Oct 07, 2015 16:44
Location: Keralam, India

Re: How to get unicode text from controls like button and label ?

Postby kcvinu » Feb 26, 2018 15:42

Hi,
I got the solution to my problem. Thanks to @Jose Rocca for his wonderful CWSTR lib and CBSTR lib. Now i can easily use dynamic unicode string in my programs. I wonder, why dont devs of FreeBasic include this lib in FreeBasic core. It is worth to add. A must have item for any language.

Return to “Beginners”

Who is online

Users browsing this forum: w92139 and 1 guest