Search code examples
pythonmfcpywin32

How to set lfFaceName by using pywin32


I have an tiny MFC program which is going to be port to pywin32.
But I can not manage to handle lfFaceName of LOGFONT object.
lfWidth & lfHeight could be easily handled but lfFaceName not.
I've tried several different lfFaceName but seems result is same?
This is an example python code fragment which I've tried.

lf            = win32gui.LOGFONT()
lf.lfFaceName = "Arial"    #Is this the correct way???

if iWidth < 16:
    lf.lfWidth = iWidth
else:
    lf.lfWidth = iWidth/2
if lf.lfHeight > iHeight:
    lf.lfHeight = iHeight
if lf.lfHeight < iHeight-1:
    lf.lfHeight = iHeight-1

s_Font = win32gui.CreateFontIndirect(lf)
win32gui.SelectObject (s_DC, s_Font)
win32gui.DrawText(s_DC, sText, len(sText),(0, 0, iWidth, iHeight), DT_VCENTER|DT_SINGLELINE|DT_CENTER)

Solution

  • If wrong lfFaceName had been assigned.There will not be any error indicated but the default FaceName will be used.So an FaceName Enum is needed for this application. We could get the full lfFaceName list by FaceName[].

    import win32gui
    
    
    def callback(font, tm, fonttype, fonts):
        fonts.append(font)
        FaceName.append(font.lfFaceName)
        return True
    
    
    FaceName = []
    fonts    = []
    hdc = win32gui.CreateDC('DISPLAY','Display',None)
    win32gui.EnumFontFamilies(hdc, None, callback, fonts)