简体   繁体   中英

PYTHON - win32gui.DrawText rect even pattern

My applicaton is really simple. It just displays 12 (text) data in my Window using win32gui.DrawText . However I am having trouble positioning the text or to be more precise the rect s. My rect s always turn out uneven or they are shorter or longer than intended. However I want all of them to be even, I want them to share the space the Window is providing.

What I am trying to archive is this Schema:

WINDOW 文本应用架构

Also note how in the first rect the text is aligned left and in the second its aligned right. Repeating this pattern.

I'm a newbie and Ive not been able to figure out how to get the rect tuple coordinates to behave this way.

EDIT : code (I removed unrelated code)

   thistuple2 = (145, 160, 0, 0)

   thistuple3 = (305, 60, 0, 0)
   thistuple4 = (305, 160, 20, 0)

   thistuple5 = (315, 60, 180, 0)
   thistuple6 = (315, 160, 180, 0)

   hDC, paintStruct = win32gui.BeginPaint(fenster)
   
   dpiScale = win32ui.GetDeviceCaps(hDC, win32con.LOGPIXELSX) / 60.0
   fontSize = 10
   lf = win32gui.LOGFONT()
   lf.lfFaceName = "Segoe UI"
   lf.lfHeight = int(round(dpiScale * fontSize))

   hf = win32gui.CreateFontIndirect(lf)
   win32gui.SelectObject(hDC, hf)
            
   win32gui.SetBkMode(hDC, win32con.TRANSPARENT)
   win32gui.SetTextColor(hDC,win32api.RGB(255,255,255))

   rect = win32gui.GetClientRect(fenster)
   win32gui.DrawText(
            hDC,
            "VLO ",
            -1,
            thistuple1,
            win32con.DT_SINGLELINE | win32con.DT_CENTER | win32con.DT_VCENTER | win32con.DT_RIGHT)


   rect = win32gui.GetClientRect(fenster)
   win32gui.DrawText(
            hDC,
            "DFH ",
            -1,
            thistuple2,
            win32con.DT_SINGLELINE | win32con.DT_CENTER | win32con.DT_VCENTER)

   rect = win32gui.GetClientRect(fenster)
   win32gui.DrawText(
            hDC,
            "130242",
            -1,
            thistuple3,
            win32con.DT_SINGLELINE | win32con.DT_CENTER | win32con.DT_VCENTER | win32con.DT_RIGHT)

   rect = win32gui.GetClientRect(fenster)
   win32gui.DrawText(
            hDC,
            "120",
            -1,
            thistuple4,
            win32con.DT_SINGLELINE | win32con.DT_CENTER | win32con.DT_VCENTER | win32con.DT_RIGHT)


   rect = win32gui.GetClientRect(fenster)
   win32gui.DrawText(
            hDC,
            "VHN ",
            -1,
            thistuple5,
            win32con.DT_SINGLELINE | win32con.DT_CENTER | win32con.DT_VCENTER)

   rect = win32gui.GetClientRect(fenster)
   win32gui.DrawText(
            hDC,
            "LOU ",
            -1,
            thistuple6,
            win32con.DT_SINGLELINE | win32con.DT_CENTER | win32con.DT_VCENTER)

   win32gui.EndPaint(fenster, paintStruct)

I know these are only 6 Items. I stopped drawing the remaining ones, because I was getting a headache with the rect coordinates. Also in the final product the text is not static, just here for display purposes.

EDIT: I managed to archive it myself yesterday by just giving all the rects flashy bg color and manually guessing the rect coordinates over and over again until I managed to place them where I wanted them and I fixed the alignment as well. Since I am new to py I didn't know there are things like Qt and their library, I'll redo the application with it since its easier to dynamically change a label instead of redrawing everytime with DrawText when the data changes.

In order to be even, you need Mapping Modes and Translations . Typically, The calling function sequence is like this Drawing a Custom Window Background :

    hdc = (HDC) wParam; 
    GetClientRect(hwnd, &rc); 
    SetMapMode(hdc, MM_ANISOTROPIC); 
    SetWindowExtEx(hdc, 100, 100, NULL); 
    SetViewportExtEx(hdc, rc.right, rc.bottom, NULL);
    //possible SetWindowOrgEx and SetViewportOrgEx following

坐标空间和变换

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM