0

I'm new to GDI+ and am trying to modify a program so that it draws a black rectangle over a specified object and then display text describing what the covered object is. I've been able to get the black rectangle to display but have gotten very stuck on getting GdipDrawString to work. I've reached a point where I'm no longer getting any compile or runtime errors, but the text still isn't displaying visibly.

Here's a simplified version of the function with the call to GdipDrawString:

def doPaint(hwnd):
    rect = RECT()
    windll.user32.GetClientRect(c_int(hwnd), byref(rect))
    ps = PAINTSTRUCT()
    hdc = windll.user32.BeginPaint(c_int(hwnd), byref(ps))
    windll.user32.FillRect(hdc, byref(rect), blackBrush)
    gdiplus.GdipDrawString("TEST", font, whiteBrush, byref(rect)) 
    windll.user32.EndPaint(c_int(hwnd), byref(ps))

Here are the definitions of some of the most relevant pieces:

blackBrush = windll.gdi32.CreateSolidBrush(blackColor)
whiteBrush = windll.gdi32.CreateSolidBrush(whiteColor)

class RECT(Structure):
    _fields_ = [('left', c_long),
                ('top', c_long),
                ('right', c_long),
                ('bottom', c_long)]

font = windll.gdi32.CreateFontW(48, 0, 0, 0, weight, False, False, False,
       charSet, outPrecision, clipPrecision, quality, family | pitch, "")

Any guidance would be much appreciated! I've been using a string literal for simplicity's sake while testing and have played around with the specified font values because I'm not sure why else FillRect would work but not DrawString. Swapping the brushes used for FillRect and DrawString produces a white rectangle and no text.

  • You check for errors non of the functions. Alsy you don't define *argtypes* and *restype* for the functions. You aren't using correct types either. Check https://stackoverflow.com/questions/50325050/ctypes-argumenterror-when-calling-dll-functions-with-python/50329487#50329487 (and there are many others) for more details. – CristiFati Oct 28 '18 at 23:12
  • I'm not sure I understand what you mean. I was getting type errors earlier but am not getting them now, so I assumed the CreateFontW function call created a valid font. And FillRect is working just fine, so I don't think it's an issue with all of the types and I didn't need to define argtypes or restype for that. Do you know what makes this case different? – Flannery Currin Oct 29 '18 at 04:55
  • Based on that answer you linked to though, I should specify that I'm using Python 2 since that's what the tool I'm trying to modify is built in. – Flannery Currin Oct 29 '18 at 05:42
  • Did you check the `font` return value? Defining `.argtypes` and `.restype` helps check for errors, otherwise ctypes does some default marshaling of Python types to C types the stack may not be correct for the call. for example, CreateFontW's last parameter is an LPCWSTR, but your last parameter (if Python 2) is a byte string. It should be a Unicode string to marshal correctly. If you defined, `.argtypes` correctly, it would have caught that. – Mark Tolonen Oct 29 '18 at 05:52
  • The `font` return value is an int, which I thought might just be how things translated over. I've tried using `.argtypes` and `.restype` to debug some, but I'm honestly not sure how those are supposed to be defined for `GdipDrawString` since there are so many versions out there. Right now it's telling me that I'm missing 12 bytes, but I'm trying to follow [this format](https://msdn.microsoft.com/en-us/library/aa327572(v=vs.71).aspx). – Flannery Currin Oct 29 '18 at 14:03

0 Answers0