Search code examples
pythonwinapipywin32win32guidrawtext

what are the options for win32con.DT_*? python for windows draw text


I just cant find the different options for the draw text function in python for windows. I want the text I am drawing to be on the bottom left corner of the screen rather than the top center. if bottom left is not an option is bottom center possible?

        win32gui.DrawText(hdc, windowText, -1, rect,
        win32con.DT_CENTER | win32con.DT_VCENTER
    )

is there an option such as

win32con.DT_LEFT | win32con.DT_BOTTOM

?

I cannot find any related information online for this snippet of code on Microsoft or python docs.


Solution

  • You can see extensive documentation for corresponding WinAPI functions on Microsoft site:

    DrawText:

    parameter ...

    DT_BOTTOM

    Justifies the text to the bottom of the rectangle. This value is used only with the DT_SINGLELINE value.

    As stated above, DT_BOTTOM works only when DT_SINGLELINE is specified. Therefore you need:

    win32con.DT_LEFT | win32con.DT_BOTTOM | win32con.DT_SINGLELINE
    


    Note, this will not be adequate for drawing multiple lines aligned to bottom of the rectangle. In that case, use DT_CALCRECT flag to calculate the height of the text which is to be drawn. Then offset the rectangle based on the height of that calculated rectangle. Example:

    textformat = win32con.DT_LEFT | win32con.DT_BOTTOM
    calrect = win32gui.DrawText(hdc, text, -1, rect, textformat | win32con.DT_CALCRECT);
    
    rect.top = rect.bottom - calcrect.bottom;
    win32gui.DrawText(hDC, text, -1, rect, textformat)