获取MFC ListCtrl子项中子字符串的矩形

获取MFC ListCtrl子项中子字符串的矩形,mfc,drawtext,listctrl,Mfc,Drawtext,Listctrl,我必须围绕MFC ListCtrl子项中显示的字符串的子字符串绘制一个矩形,为此,我在CustomDraw处理程序中使用以下代码: CDC* deviceContext = CDC::FromHandle (customDraw->nmcd.hdc); CRect rectangleOfCurrentlyDrawnCell; ASSERT(0 != list.GetSubItemRect (rowOfCurrentlyDrawnCell, columnOfCurrentlyDrawnCel

我必须围绕MFC ListCtrl子项中显示的字符串的子字符串绘制一个矩形,为此,我在CustomDraw处理程序中使用以下代码:

CDC* deviceContext = CDC::FromHandle (customDraw->nmcd.hdc);
CRect rectangleOfCurrentlyDrawnCell;
ASSERT(0 != list.GetSubItemRect (rowOfCurrentlyDrawnCell, columnOfCurrentlyDrawnCell, LVIR_LABEL, rectangleOfCurrentlyDrawnCell));
rectangleOfCurrentlyDrawnCell.DeflateRect (1, 1);

CString cellText = list.GetItemText (rowOfCurrentlyDrawnCell, columnOfCurrentlyDrawnCell);
CString prefix = cellText.Mid (0, subStringOffset); // The leading part of the cell's text, that shouldn't be emphasized.
CString emphasizedText = cellText.Mid (subStringOffset, subStringLength); // The part of the cell's text that should be emphasized.
CSize sizeOfPrefix = deviceContext->GetTextExtent (prefix);
CSize sizeOfEmphasizedText = deviceContext->GetTextExtent (emphasizedText);
int left = (rectangleOfCurrentlyDrawnCell.left + sizeOfPrefix.cx);
int right = (left + sizeOfEmphasizedText.cx);
CRect emphasizedRectangle (left, rectangleOfCurrentlyDrawnCell.top, right, rectangleOfCurrentlyDrawnCell.bottom);

const COLORREF cyan = RGB(0, 255, 255);
deviceContext->Draw3dRect (emphasizedRectangle, cyan, cyan);
现在,这几乎完美地工作了,只有一个问题:矩形是在子字符串开头的左侧绘制的,因为每个子项的文本都显示在子项左边框的某个小偏移处

例如,下一幅图像显示了上述代码创建的矩形,以防需要加框的子字符串为“530684”。请注意,尾随的“4”位于帧外,帧开始时有点偏左,因此帧左侧和前导的“5”之间有一点空间

如何获得此偏移量的大小,或以其他方式克服此问题


谢谢你的帮助

另请注意:使用
ASSERT(0!=list.GetSubItemRect(…)
只能在调试模式下工作。我怀疑您想使用
验证
。至于你的问题:只需找到costant值(我认为它将是2个像素)并将rect的偏移量添加到右边value@cha:感谢您提供断言/验证信息。关于常量偏移量,我已经使用了textMetrics.tmAveCharWidth,它是6,但我不确定这个解决方案有多普遍。毕竟,文本显示使用的是系统设置,而不是常量。因为您已经在单元格内容上自定义了绘图,所以接管单元格的整个渲染(包括数字)可能没什么大不了的。然后您就可以完全控制前导缩进的大小,以及如何处理它。