2017-03-04 95 views
1

我在tvirtuailstringtree绘制文本和图像,如onbeforecellpaint事件之后TVirtuailStringTree文字和图像对齐

begin 
Textrectplace := NewRect; 
Textrectplace.Left := Textrectplace.Left + 2; 
Textrectplace.Width := 24; 
Textrectplace.Height := Data.image.height; 
Textrectplace.Top := Textrectplace.Top; 
Textrectplace.Bottom := Textrectplace.Bottom; 
xOfftext := Textrectplace.Left + Textrectplace.Width + 4; 

yOfftext := Textrectplace.Top - 3 + ((Data.image.height - TargetCanvas.TextHeight('H')) div 2); 
TargetCanvas.font.color := clgray; 
TargetCanvas.font.Size := 10; 
TargetCanvas.TextOut(xOfftext, yOfftext, Data.text); 
end; 

end; 


begin 
imgrect:= Textrectplace; 
imgrect.Left := imgrect.Left + 150; 
imgrect.Width := 24; 
imgrect.Height := 36; 
imgrect.Top := imgrect.Top - 6 + ((Data.image.height - TargetCanvas.TextHeight('H')) div 2); 
imgrect.Bottom := imgrect.Bottom; 

TargetCanvas.Draw(imgrect.Left, imgrect.Top, Data.image); 
end; 

我对文字和图像对齐一个问题,我想对齐到左的文本和部分处理。图像有对齐问题我想使它与右边的文字对齐,当前没有textoverflow如果节点有短文本,它的一切都很好,图像正确地显示文本。但是如果文字太长,它的图像就会变成overflow

我这里是在图像示例图像

enter image description here

它显示了如何从长期发短信节点样子,应该如何,如果文本太长而列表宽度小的比对文字图像的它应该显示我长点头...直到列表变得更大然后显示全文是我长结文本我怎么能做到这一点

更新的代码

procedure TForm1.virtuailtreeBeforeCellPaint(Sender: TBaseVirtualTree; 
     TargetCanvas: TCanvas; Node: PVirtualNode; Column: TColumnIndex; 
     CellPaintMode: TVTCellPaintMode; CellRect: TRect; var ContentRect: TRect); 
var 
Data: ^PnodeData; 
NewRect: TRect; 
Textrectplace: TRect; 
imgrect : TRect; 

begin 



if not Assigned(Node) then 
begin 
exit; 
end; 

Data := virtuailtree.GetNodeData(Node); 

NewRect := CellRect; 




//text 
begin 
Textrectplace := NewRect; 
Textrectplace.Left := Textrectplace.Left + 2; 
Textrectplace.Width := 70; 
Textrectplace.Height := 30; 
Textrectplace.Top := Textrectplace.Top; 
Textrectplace.Bottom := Textrectplace.Bottom; 
TargetCanvas.font.color := clgray; 
TargetCanvas.font.Size := 10; 
DrawText(TargetCanvas.Handle, pChar(Data.text), Length(Data.text) 
, Textrectplace, DT_End_Ellipsis); 
end; 

end; 

//right image that should be stay at the right position 


begin 
imgrect := Textrectplace; 
imgrect.left := imgrect.left + 150; 
imgrect.Width := 24; 
imgrect.Height := 36; 
imgrect.Top := imgrect.Top - 6 + ((30 - TargetCanvas.TextHeight('H')) div 2); 
imgrect.Bottom := imgrect.Bottom; 


TargetCanvas.Draw(imgrect.left, imgrect.Top, Data.image); 
end; 




end; 
+0

什么是'NewRect',在那里,它是如何得到它的价值? “XOffText”的含义是什么(为什么它给出的值超出了'TextRectPlace'。你考虑过使用'DrawText()'而不是'TextOut()'吗? –

+0

使用textwidth函数来查看文本是否过长,然后缩小它的字符,直到它适合 – Dsm

+0

NewRect是一个'TRect' var。xofftext使绘图文本居中与图像,drawtext将有所作为? – Raelpaul

回答

1

为了缩短文本,以适应TRect可以使用WINAPI DrawText()函数中,与DT_END_ELLIPSIS格式说明。

要调整文本的空间,当TVirtualStringTree被调整(例如,用TSplitter)只需使用:

TextRectPlace.Right := CellRect - imgRect.width; 
imgRect.Left := TextRectPlace.Right;