Menu

CreateFont API

Help
Timo S
2005-01-23
2013-04-02
  • Timo S

    Timo S - 2005-01-23

    I'm writing some text to the window with windows API DrawText, and I'm trying to change font type and size with CreateFont, but I can't get it working. Anyone having an example how to do it??

    Timo

     
    • A.H. Banen

      A.H. Banen - 2005-01-23

      Timo,
      Did you use SelectObject (from gdi32.dll) to associate the newly created font with the device context of what you are drawing on (like in these VB examples: http://www.vbaccelerator.com/home/VB/Tips/Create_an_API_hFont_from_a_VB_StdFont_object/article.asp
      http://www.vb-helper.com/howto_tall_font.html
      )

      If this does not help please post the code snippet of what you already tried, sometimes strange eyes eaasier spot the problem :).

      Andr

       
    • Timo S

      Timo S - 2005-01-24

      Andr,
      This is what i've got so far...

      #-------------------------
      Import "gui"
      Import "system"

      w = gui.window("Draw Text", [0,0,300,300])
      b = gui.button(w, "Draw", [30,30,200,100])

      TRANSPARENT = 1
      DT_SINGLELINE = 32
      DT_CENTER = 1
      DT_VCENTER = 4
      DT_ALLOR = 37

      LOGPIXELSY = 90
      DEFAULT_CHARSET = 1
      SYMBOL_CHARSET = 2
      OUT_DEFAULT_PRECIS = 2
      CLIP_DEFAULT_PRECIS = 0
      DEFAULT_QUALITY = 0
      PROOF_QUALITY = 2
      DEFAULT_PITCH = 0

      user32 = system.dll("user32.dll")
      user32.__loadpfunc__("GDC", "GetDC", 'Int, ['Int])

      gdi32 = system.dll("gdi32.dll")
      user32.__loadpfunc__("DrawText", "DrawTextA",'Int ,['Int, 'Charp, 'Int, 'Structp, 'Int])
      gdi32.__loadpfunc__("SetBackM", "SetBkMode",'int ,['int, 'int])
      gdi32.__loadpfunc__("SetTextC", "SetTextColor",'Int ,['Int, 'Int])
      gdi32.__loadpfunc__("CreateFont", "CreateFontA",'Int, ['Int, 'Int, 'Int, 'Int, 'Int, 'Int, 'Int, 'Int, 'Int, 'Int, 'Int, 'Int, 'Int, 'Charp])
      gdi32.__loadpfunc__("DeleteObject", "DeleteObject", 'Int, ['int])
      gdi32.__loadpfunc__("SelectObject", "SelectObject", 'Int, ['Int, 'int])

      theRect = [:{itype:"i4", dim:4}, nil:]
      theRect[1] = 0
      theRect[2] = 0
      theRect[3] = 300
      theRect[4] = 300

      wdc = user32.GDC(w.hwnd)

      gdi32.SetBackM(wdc,TRANSPARENT)
      gdi32.SetTextC(wdc, 255)

      b.onclick = Func()
          hFont = gdi32.CreateFont(0, 0, 0, 0, 0, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, DEFAULT_PITCH, "Times New Roman")
          hOldFont = gdi32.SelectObject(wdc, hFont)
          string = "Text"
          dummy = user32.DrawText(wdc, string, -1,theRect, DT_ALLOR)
          retval = gdi32.SelectObject(wdc, hOldFont)
          retval = gdi32.DeleteObject(hFont)
      EndFunc

      gui.enter()
      #-------------------------

      Timo

       
    • A.H. Banen

      A.H. Banen - 2005-01-24

      Timo,
      I spend some time trying out what went wrong in your code until it struck me: there can only be a maximum of ten (10) arguments used in a Dialect dll call! This fact is undocumented but can be derived from the source code (Browse the CVS http://cvs.sourceforge.net/viewcvs.py/dialect/dialect/DialectLib/SystemModule.cpp?rev=1.5&view=markup search for CDLLFunc::Exec and then scroll down to:
      if (m_CallType == ctC) {
              switch (argCount) {
      to see what I mean)

      There maybe can be a solution using CreateFont Indirect ( see a.o. http://www.mangovision.com/vbapi/ref/c/createfontindirect.html ) that uses a 50 byte long (I think :) LOGFONT (http://www.mangovision.com/vbapi/ref/l/logfont.html )  structure that could be implemented using a chunk.
      I did not test out CreateFontIndirect yet, as I thought you'd like to know asap what the problem was with CreateFont.

      Andr

       
    • A.H. Banen

      A.H. Banen - 2005-01-24

      Timo,
      Note the LOGFONT structure I use below is 60 bytes long not 50 (a slight miscalculation due to the 'early' hour).
      In the example (I reused your code for the most part) I use CreateFontIndirect and compose a LOGFONT structure by concatenating three partial chunks: lfHWEOW, lfIUSCOCQP and lfFacename into one final chunk. Note that lfHWEOW originally has itype "i4" which has to be converted to itype "ui1" to get the right final itype ("ui1").

      #-----------------------
      Import "gui"
      Import "system"

      Import "math"

      w = gui.window("Draw Text", [0,0,300,300])
      b = gui.button(w, "Draw", [30,30,200,100])

      TRANSPARENT = 1
      DT_SINGLELINE = 32
      DT_CENTER = 1
      DT_VCENTER = 4
      DT_ALLOR = 37

      LOGPIXELSY = 90
      DEFAULT_CHARSET = 1
      SYMBOL_CHARSET = 2
      OUT_DEFAULT_PRECIS = 2
      CLIP_DEFAULT_PRECIS = 0
      DEFAULT_QUALITY = 0
      PROOF_QUALITY = 2
      DEFAULT_PITCH = 0

      FW_DONTCARE = 0
      FW_THIN = 100
      FW_EXTRALIGHT = 200
      FW_ULTRALIGHT = 200
      FW_LIGHT = 300
      FW_NORMAL = 400
      FW_REGULAR = 400
      FW_MEDIUM = 500
      FW_SEMIBOLD = 600
      FW_DEMIBOLD = 600
      FW_BOLD = 700
      FW_EXTRABOLD = 800
      FW_ULTRABOLD = 800
      FW_HEAVY = 900
      FW_BLACK = 900
      ANSI_CHARSET = 0
      ARABIC_CHARSET = 178
      BALTIC_CHARSET = 186
      CHINESEBIG5_CHARSET = 136
      DEFAULT_CHARSET = 1
      EASTEUROPE_CHARSET = 238
      GB2312_CHARSET = 134
      GREEK_CHARSET = 161
      HANGEUL_CHARSET = 129
      HEBREW_CHARSET = 177
      JOHAB_CHARSET = 130
      MAC_CHARSET = 77
      OEM_CHARSET = 255
      RUSSIAN_CHARSET = 204
      SHIFTJIS_CHARSET = 128
      SYMBOL_CHARSET = 2
      THAI_CHARSET = 222
      TURKISH_CHARSET = 162
      OUT_DEFAULT_PRECIS = 0
      OUT_DEVICE_PRECIS = 5
      OUT_OUTLINE_PRECIS = 8
      OUT_RASTER_PRECIS = 6
      OUT_STRING_PRECIS = 1
      OUT_STROKE_PRECIS = 3
      OUT_TT_ONLY_PRECIS = 7
      OUT_TT_PRECIS = 4
      CLIP_DEFAULT_PRECIS = 0
      CLIP_EMBEDDED = 128
      CLIP_LH_ANGLES = 16
      CLIP_STROKE_PRECIS = 2
      ANTIALIASED_QUALITY = 4
      DEFAULT_QUALITY = 0
      DRAFT_QUALITY = 1
      NONANTIALIASED_QUALITY = 3
      PROOF_QUALITY = 2
      DEFAULT_PITCH = 0
      FIXED_PITCH = 1
      VARIABLE_PITCH = 2
      FF_DECORATIVE = 80
      FF_DONTCARE = 0
      FF_ROMAN = 16
      FF_SCRIPT = 64
      FF_SWISS = 32

      user32 = system.dll("user32.dll")
      gdi32 = system.dll("gdi32.dll")

      user32.__loadpfunc__("GDC", "GetDC", 'Int, ['Int])
      user32.__loadpfunc__("DrawText", "DrawTextA",'Int ,['Int, 'Charp, 'Int, 'Structp,'Int])

      gdi32.__loadpfunc__("SetBackM", "SetBkMode",'int ,['int, 'int])
      gdi32.__loadpfunc__("SetTextC", "SetTextColor",'Int ,['Int, 'Int])
      gdi32.__loadpfunc__("CreateFont", "CreateFontA",'int, ['int, 'int, 'int, 'int, 'int, 'int, 'int, 'int, 'int, 'int, 'int, 'int, 'int, 'charp ])
      gdi32.__loadpfunc__("CreateFontIndirect","CreateFontIndirectA", 'int, [ 'structp ])
      gdi32.__loadpfunc__("DeleteObject", "DeleteObject", 'Int, ['int])
      gdi32.__loadpfunc__("SelectObject", "SelectObject", 'Int, ['Int, 'int])

      theRect = [:{itype:"i4", dim:4}, nil:]
      theRect[1] = 0
      theRect[2] = 0
      theRect[3] = 300
      theRect[4] = 300

      wdc = user32.GDC(w.hwnd)

      gdi32.SetBackM(wdc,TRANSPARENT)
      gdi32.SetTextC(wdc, 255)

      b.onclick = Func()
          lfHWEOW = [: {itype:"i4", dim:5}, nil :]
          lfHWEOW[1] = 0 #lfHeight
          lfHWEOW[2] = 0 #lfWidth
          lfHWEOW[3] = 0 #lfEscapement
          lfHWEOW[4] = 0 #fOrientation
          lfHWEOW[5] = FW_BOLD #lfWeight
         
          lfIUSCOCQP = [: {itype:"ui1", dim:8}, nil :]
          lfIUSCOCQP[1] = 0 #lfItalic
          lfIUSCOCQP[2] = 0 #lfUnderline
          lfIUSCOCQP[3] = 0 #lfStrikeOut
          lfIUSCOCQP[4] = DEFAULT_CHARSET # SYMBOL_CHARSET         #lfCharSet
          lfIUSCOCQP[5] = OUT_DEFAULT_PRECIS  #lfOutPrecision
          lfIUSCOCQP[6] = CLIP_DEFAULT_PRECIS #lfClipPrecision
          lfIUSCOCQP[7] = PROOF_QUALITY # DEFAULT_QUALITY     #lfQuality
          lfIUSCOCQP[8] = DEFAULT_PITCH # math.bOR( DEFAULT_PITCH , FF_DECORATIVE)  #lfPitchAndFamily
         
          fontname = "Times New Roman" #"Symbol"
          lfFacename =  [: {itype:"ui1", encoding:"ASCII"}, fontname~join(array(32-len(fontname), chr$(0)), ""):]
         
          LOGFONT = [:{itype:"ui1"}, lfHWEOW :] ~ lfIUSCOCQP ~ lfFacename

          #hFont = gdi32.CreateFont(0, 0, 0, 0, 0, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, DEFAULT_PITCH, fontname ) #"Times New Roman")
          hFont = gdi32.CreateFontIndirect( LOGFONT )
          #hFont = gdi32.CreateFont(0, 0, 0, 0, FW_BOLD, 0, 0, 0, SYMBOL_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH , fontname)
           hOldFont = gdi32.SelectObject(wdc, hFont)
          string = "Text"
          dummy = user32.DrawText(wdc, string, -1,theRect, DT_ALLOR)
          retval = gdi32.SelectObject(wdc, hOldFont)
          retval = gdi32.DeleteObject(hFont)
      EndFunc

      gui.enter()
      #-----------------------

      I hope this helped :)

      Andr

       
    • Timo S

      Timo S - 2005-01-24

      Andr,
      That works fine, thanks for Your help. I'm not very good with API and Dialect chunks, so I must try to learn more about then.

      Timo

       
    • Enrique

      Enrique - 2009-05-21

      I'm trying to use Andr�'s code shown above on my Jornada 720.

      But I'm using coredll.dll instead of user32.dll and gdi32.dll

      Then using DrawTextW/CreateFontIndirectW from coredll.dll

      But the only text i'm getting is "������".

      What can I do to make this work?

       

Log in to post a comment.