我必须在 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”之间有一点空间。
我怎样才能得到这个偏移量的大小,或者以其他方式克服这个问题?
谢谢你的帮助!