From 59cc0feb66d6a606acc521d32bc3277834b767b6 Mon Sep 17 00:00:00 2001 From: Weijie Zhao Date: Wed, 8 May 2019 16:01:11 +0800 Subject: [PATCH] fix bug : width of multiline text label --- windows/text.cpp | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/windows/text.cpp b/windows/text.cpp index eafbe714d..969318f8d 100644 --- a/windows/text.cpp +++ b/windows/text.cpp @@ -48,8 +48,11 @@ int uiWindowsWindowTextWidth(HWND hwnd) size.cx = 0; size.cy = 0; + //save the max width of multiline text + auto maxWidth = size.cx; text = windowTextAndLen(hwnd, &len); + WCHAR *start = text, *end = text; if (len == 0) // no text; nothing to do goto noTextOrError; @@ -66,12 +69,31 @@ int uiWindowsWindowTextWidth(HWND hwnd) ReleaseDC(hwnd, dc); goto noTextOrError; } - if (GetTextExtentPoint32W(dc, text, len, &size) == 0) { - logLastError(L"error getting text extent point"); - // continue anyway, assuming size is 0 - size.cx = 0; - size.cy = 0; + + // calculate width of each line + while(start != text + len) { + while(*start == '\n' && start != text + len) { + start++; + } + if(start == text + len) { + break; + } + end = start + 1; + while(*end != '\n' && end != text + len){ + end++; + } + if (GetTextExtentPoint32W(dc, start, end - start, &size) == 0) { + logLastError(L"error getting text extent point"); + // continue anyway, assuming size is 0 + size.cx = 0; + size.cy = 0; + } + if(size.cx > maxWidth) { + maxWidth = size.cx; + } + start = end; } + // continue on errors; we got what we want if (SelectObject(dc, prevfont) != hMessageFont) logLastError(L"error restoring previous font into device context"); @@ -79,7 +101,7 @@ int uiWindowsWindowTextWidth(HWND hwnd) logLastError(L"error releasing DC"); uiprivFree(text); - return size.cx; + return maxWidth; noTextOrError: uiprivFree(text);