原創(chuàng)|使用教程|編輯:龔雪|2014-05-14 09:39:19.000|閱讀 874 次
概述:本文主要介紹在使用DotImage的過程中如何動態(tài)調整Text Annotation的字體大小。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
在WinForms項目中,當調整TextAnnotation時,.Net圖像處理控件DotImage會自動換行TextAnnotation中的文本。如果你想動態(tài)改變字體大小本身,你可以在 AnnotateViewer的AnnotationResized事件中進行如下操作:
C# Sample:
-----
private void annotateViewer1_AnnotationResized(object sender, AnnotationEventArgs e)
{
ResizeFont(e.Annotation);
}
private void ResizeFont(AnnotationUI baseAnno)
{
TextAnnotation tAnno = baseAnno as TextAnnotation;
if (tAnno != null)
{
Size annoSize = tAnno.Size.ToSize();
System.Drawing.Font annoFont = new Font(tAnno.Font.Name, tAnno.Font.Size);
SizeF size;
System.Drawing.Font newFont = AppropriateFont(annotateViewer1.CreateGraphics(),
12f,
72f,
annoSize,
tAnno.Text,
annoFont,
out size);
tAnno.Font = new AnnotationFont(newFont.Name, newFont.Size);
}
}
public static Font AppropriateFont(Graphics g, float minFontSize,
float maxFontSize, Size layoutSize, string s, Font f, out SizeF extent)
{
if (maxFontSize == minFontSize)
f = new Font(f.FontFamily, minFontSize, f.Style);
extent = g.MeasureString(s, f);
if (maxFontSize <= minFontSize)
return f;
float hRatio = layoutSize.Height / extent.Height;
float wRatio = layoutSize.Width / extent.Width;
float ratio = (hRatio < wRatio) ? hRatio : wRatio;
float newSize = f.Size * ratio;
if (newSize < minFontSize)
newSize = minFontSize;
else if (newSize > maxFontSize)
newSize = maxFontSize;
f = new Font(f.FontFamily, newSize, f.Style);
extent = g.MeasureString(s, f);
return f;
}
VB.Net Sample:
----- Private Sub annotateViewer1_AnnotationResized(ByVal sender As Object, ByVal e AsAnnotationEventArgs) ResizeFont(e.Annotation) End Sub Private Sub ResizeFont(ByVal baseAnno As AnnotationUI) Dim tAnno As TextAnnotation = TryCast(baseAnno, TextAnnotation) If tAnno IsNot Nothing Then Dim annoSize As Size = tAnno.Size.ToSize() Dim annoFont As System.Drawing.Font = New Font(tAnno.Font.Name, tAnno.Font.Size) Dim size As SizeF Dim newFont As System.Drawing.Font = AppropriateFont(annotateViewer1.CreateGraphics(), 12.0F, 72.0F, annoSize, tAnno.Text, annoFont, _ size) tAnno.Font = New AnnotationFont(newFont.Name, newFont.Size) End If End Sub Public Shared Function AppropriateFont(ByVal g As Graphics, ByVal minFontSize As Single, ByValmaxFontSize As Single, ByVal layoutSize As Size, ByVal s As String, ByVal f As Font, _ ByRef extent As SizeF) As Font If maxFontSize = minFontSize Then f = New Font(f.FontFamily, minFontSize, f.Style) End If extent = g.MeasureString(s, f) If maxFontSize <= minFontSize Then Return f End If Dim hRatio As Single = layoutSize.Height / extent.Height Dim wRatio As Single = layoutSize.Width / extent.Width Dim ratio As Single = If((hRatio < wRatio), hRatio, wRatio) Dim newSize As Single = f.Size * ratio If newSize < minFontSize Then newSize = minFontSize ElseIf newSize > maxFontSize Then newSize = maxFontSize End If f = New Font(f.FontFamily, newSize, f.Style) extent = g.MeasureString(s, f) Return f End Function
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@ke049m.cn