Displaying Unicode text in a graphicbox

by Richard Russell, March 2015

LB Booster provides built-in support to output Unicode text to the mainwin, and to the printer using LPRINT, but not to GUI controls. In the case of controls other than a TEXTEDITOR it is quite easy to support Unicode output using a little API code.

Below is listed a program to illustrate displaying Cyrillic text in a GRAPHICBOX (ensure that you select Unicode Support in the Options menu):

   global CP.UTF8
   CP.UTF8 = hexdec("FDE9")
   nomainwin
 
   graphicbox #w.gb, 10, 10, 280, 100
   open "Unicode test" for window as #w
   #w "trapclose  [quit]"
   #w.gb "font Times_New_Roman 20"
   hwgb = hwnd(#w.gb)
   call GraphicsTextUnicode hwgb, "Это демонстрация"
   call GraphicsTextUnicode hwgb, "российского текста"
   wait
 
  [quit]
   close #w
   end
 
  sub GraphicsTextUnicode hgb, utf8$
   calldll #kernel32, "MultiByteToWideChar", _
     CP.UTF8 as long, 0 as long, _
     utf8$ as ptr, -1 as long, _
     0 as long, 0 as long, ret as long
     bytes = 2 * ret
   calldll #kernel32, "GlobalAlloc", 0 as long, _
     bytes as long, wide as long
   calldll #kernel32, "MultiByteToWideChar", _
     CP.UTF8 as long, 0 as long, _
     utf8$ as ptr, -1 as long, _
     wide as long, ret as long, ret as long
   calldll #gdi32, "GetDC", hgb as ulong, hdc as ulong
   struct rc, left as long, top as long, _
              right as long, bottom as long
   calldll #gdi32, "GetCurrentPositionEx", _
     hdc as ulong, rc as struct, ret as long
   calldll #user32, "DrawTextW", hdc as ulong, wide as long, _
     -1 as long, rc as struct, 2304 as long, ret as long
   rc.bottom.struct += ret
   calldll #gdi32, "MoveToEx", hdc as ulong, _
      rc.left.struct as long, rc.bottom.struct as long, _
      0 as long, ret as long
   calldll #kernel32, "GlobalFree", wide as long, _
     ret as long
  end sub