开发者

How to truncate HWND title

开发者 https://www.devze.com 2023-01-12 04:24 出处:网络
I am creating a window which opens to a dyna开发者_如何转开发mic 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.

I am creating a window which opens to a dyna开发者_如何转开发mic 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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消