tags:

views:

37

answers:

2

I am creating a window which opens to a dynamic title. I would like to have the window's title truncate if the window is resized and there isn't room to show the full title.

For example, I have

HWND handle = GetHWND(); // gets me the correct handle
std::wstring title = L"some fairly long window title";
SetWindowTextW(handle, title.c_str());

Is there either a way to truncate the HWND title automatically or alternately a way to get the pixel width of the title text so I can set the title manually if the window shrinks?

+1  A: 

You can get the width of a string drawn to a given device context (HDC) by means of the GetTextExtentPoint32 function.

Andreas Rejbrand
+1  A: 

Instead of setting the text via SetWindowText, respond to WM_GETTEXT. The two are related: DefWindowProc responds to WM_GETTEXT by returning the value you set earlier with SetWindowText.

Now when you receive the WM_GETTEXT message you can return a text that's appropriate at that moment, taking the current width into account.

MSalters