CTreeCtrl 中的字体更改后项目大小未调整

发布于 2024-09-13 13:46:33 字数 663 浏览 3 评论 0原文

我使用以下代码更改 CTreeCtrl 中树项目的字体:

void CTreeCtrlEx::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMTVCUSTOMDRAW pNMCD = reinterpret_cast<LPNMTVCUSTOMDRAW>(pNMHDR);
    *pResult = 0;

    switch(pNMCD->nmcd.dwDrawStage)
    {
        case CDDS_PREPAINT:
            *pResult = CDRF_NOTIFYITEMDRAW;
            return;
        case CDDS_ITEMPREPAINT:
        {
            CFont * pco_font = GetSomeFont();
            ::SelectObject(pNMCD->nmcd.hdc, pco_font->GetSafeHandle());
            *pResult = CDRF_NEWFONT;
        }
        return;
    }
}

但是,文本的末尾被剪裁在项目中,显然它没有
调整为使用新字体的文本长度。
补救办法是什么?

I change the font of tree items in CTreeCtrl with the following code:

void CTreeCtrlEx::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMTVCUSTOMDRAW pNMCD = reinterpret_cast<LPNMTVCUSTOMDRAW>(pNMHDR);
    *pResult = 0;

    switch(pNMCD->nmcd.dwDrawStage)
    {
        case CDDS_PREPAINT:
            *pResult = CDRF_NOTIFYITEMDRAW;
            return;
        case CDDS_ITEMPREPAINT:
        {
            CFont * pco_font = GetSomeFont();
            ::SelectObject(pNMCD->nmcd.hdc, pco_font->GetSafeHandle());
            *pResult = CDRF_NEWFONT;
        }
        return;
    }
}

However, the end of the text is being clipped in the items, apparently it is not being
adjusted to the length of the text with the new font.
what would be the remedy?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘香 2024-09-20 13:46:33

我遇到了同样的问题,可以确认 CCM_SETVERSION 不起作用。事实上,对我来说唯一可靠的方法是在绘制过程完成后重新设置文本。我的方法是捕获 TVN_ITEMEXPANDING 消息并重新设置文本。当节点展开时,这将更正所有子项的文本。当然,这对于粗体根节点不起作用。

这是我的代码的草图:

BEGIN_MESSAGE_MAP(CTreeViewEx, CTreeView)
  ON_NOTIFY_REFLECT(TVN_ITEMEXPANDING, OnItemExpanding)
END_MESSAGE_MAP()

void CTreeViewEx::OnItemExpanding(NMHDR* pNMHDR, LRESULT* pResult) 
{
  NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
  if(pNMTreeView==NULL) {
    return;
  }
  HTREEITEM hTreeItem = pNMTreeView->itemNew.hItem;
  if(hTreeItem!=NULL) {
    ReevaluteItemTextOfChildren(hTreeItem);
    ...
  }
}

void CTdTreeViewEx::ReevaluteItemTextOfChildren(HTREEITEM hRootNode)
{
  if (hRootNode == NULL) {
    return;
  }
  CTreeCtrl& ctlTree = GetTreeCtrl();
  HTREEITEM hTreeItemCursor = ctlTree.GetNextItem(hRootNode, TVGN_CHILD);
  // Loop over all siblings
  while (hTreeItemCursor != NULL) {
    // Change the text of the current item
    CString csItemText(ctlTree.GetItemText(hTreeItemCursor));
    ctlTree.SetItemText(hTreeItemCursor, csItemText);
    // Get the next brother
    HTREEITEM hNextSibling = ctlTree.GetNextItem(hTreeItemCursor, TVGN_NEXT);
    hTreeItemCursor = hNextSibling;
  }
}

I had the same problem and can confirm that CCM_SETVERSION did not work. In fact the only thing that worked reliably for me was to re-set the text after the paint process was finished. The way I did it was by catching the TVN_ITEMEXPANDING message and by re-setting the text. This would correct the text of all sub-items when a node is expanded. Of course this does not work for bold root nodes.

Here is a sketch of my code:

BEGIN_MESSAGE_MAP(CTreeViewEx, CTreeView)
  ON_NOTIFY_REFLECT(TVN_ITEMEXPANDING, OnItemExpanding)
END_MESSAGE_MAP()

void CTreeViewEx::OnItemExpanding(NMHDR* pNMHDR, LRESULT* pResult) 
{
  NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
  if(pNMTreeView==NULL) {
    return;
  }
  HTREEITEM hTreeItem = pNMTreeView->itemNew.hItem;
  if(hTreeItem!=NULL) {
    ReevaluteItemTextOfChildren(hTreeItem);
    ...
  }
}

void CTdTreeViewEx::ReevaluteItemTextOfChildren(HTREEITEM hRootNode)
{
  if (hRootNode == NULL) {
    return;
  }
  CTreeCtrl& ctlTree = GetTreeCtrl();
  HTREEITEM hTreeItemCursor = ctlTree.GetNextItem(hRootNode, TVGN_CHILD);
  // Loop over all siblings
  while (hTreeItemCursor != NULL) {
    // Change the text of the current item
    CString csItemText(ctlTree.GetItemText(hTreeItemCursor));
    ctlTree.SetItemText(hTreeItemCursor, csItemText);
    // Get the next brother
    HTREEITEM hNextSibling = ctlTree.GetNextItem(hTreeItemCursor, TVGN_NEXT);
    hTreeItemCursor = hNextSibling;
  }
}
相对绾红妆 2024-09-20 13:46:33

从我的 MSDN 副本来看,我似乎无法在网上找到:

最常见的控件可以在
本质上是一样的。然而,
列表视图和树视图控件有
一些需要一定程度的功能
自定义绘制的不同方法。

对于 5.0 版本,这两个控件
如果您进行更改,可能会显示剪辑的文本
通过返回 CDRF_NEWFONT 来设置字体。
这种行为是必要的
向后兼容早期版本
通用控件的版本。如果
你想改变a的字体
列表视图或树视图控件,您
如果您发送一个,将会得到更好的结果
带有 wParam 的 CCM_SETVERSION 消息
在添加任何项目之前将值设置为 5
到控件。

另请参阅CCM_SETVERSION 文档

From my copy of MSDN, which I can't seem to find online:

Most common controls can be handled in
essentially the same way. However, the
list-view and tree-view controls have
some features that require a somewhat
different approach to custom draw.

For Version 5.0, these two controls
may display clipped text if you change
the font by returning CDRF_NEWFONT.
This behavior is necessary for
backward compatibility with earlier
versions of the common controls. If
you want to change the font of a
list-view or tree-view control, you
will get better results if you send a
CCM_SETVERSION message with the wParam
value set to 5 before adding any items
to the control.

See also the documentation for CCM_SETVERSION.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文