示例代码:
CString strCompactedPath = L"C:\\Users\\ajtru\\AppData\\Roaming\\Meeting Schedule Assistant\\Workbook-S-140-PublicTalk-WatchtowerStudy-ServiceTalk-Videoconference.xsl";
PathCompactPath(nullptr, strCompactedPath.GetBuffer(_MAX_PATH), 300);
strCompactedPath.ReleaseBuffer();
结果:
...\\Workbook-S-140-PublicTalk-Watchtow...
这不是我想要的!我想要删除一些路径并保留开始/结束。
我正在用C++20编译,所以还有其他方法吗?
我想是因为文件名本身太长了:
Workbook-S-140-PublicTalk-WatchtowerStudy-ServiceTalk-Videoconference.xsl
所以 300 甚至 50000 像素是不够的。
我的目标?
如果完整路径不适合标题栏,则用省略号压缩。我看300或500都不够。
这是我想出的解决方案,似乎有效:
TITLEBARINFO tbi{};
tbi.cbSize = sizeof(TITLEBARINFO);
if (GetTitleBarInfo(&tbi))
{
const int buttonWidth = GetSystemMetrics(SM_CXSIZE);
const int availableWidth = tbi.rcTitleBar.right - tbi.rcTitleBar.left - (3 * buttonWidth);
// Limit the length of the file name by using ellipses ...
CString strCompactedPath = m_openFilePath;
PathCompactPath(nullptr, strCompactedPath.GetBuffer(_MAX_PATH), availableWidth);
strCompactedPath.ReleaseBuffer();
SetWindowText(strCompactedPath);
}
else
{
SetWindowText(m_openFilePath);
}