WinForms基于任务报表的帮助 > 打印及预览基于任务的帮助 > 以指定角度绘制文本 |
为了以任意角度绘制文本,需要使用到Graphics对象,同时我们要创建一个子方法旋转文本。
Visual Basic
Visual Basic |
拷贝代码
|
---|---|
Me.C1PrintDocument1.StartDoc() Me.C1PrintDocument1.RenderBlockGraphicsBegin() ' 声明graphics对象 Dim g As System.Drawing.Graphics g = Me.C1PrintDocument1.CurrentBlockGraphics Dim fontb = New Font("Arial", 12, FontStyle.Bold) ' 改变文本角度的子方法。 RotateText(g, fontb, "Hello World", -45, Brushes.CadetBlue, 10, 100) Me.C1PrintDocument1.RenderBlockGraphicsEnd() Me.C1PrintDocument1.EndDoc() |
C#
C# |
拷贝代码
|
---|---|
this.c1PrintDocument1.StartDoc(); this.c1PrintDocument1.RenderBlockGraphicsBegin(); // 声明graphics对象 System.Drawing.Graphics g; g = this.c1PrintDocument1.CurrentBlockGraphics; Font fontb = new Font("Arial", 12, FontStyle.Bold); // 改变文本角度的子方法。 RotateText(g, fontb, "Hello World", -45, Brushes.CadetBlue, 10, 100); this.c1PrintDocument1.RenderBlockGraphicsEnd(); this.c1PrintDocument1.EndDoc(); |
Visual Basic
Visual Basic |
拷贝代码
|
---|---|
Public Sub RotateText(ByVal g As Graphics, ByVal f As Font, ByVal s As String, ByVal angle As Single, ByVal b As Brush, ByVal x As Single, ByVal y As Single) If angle > 360 Then While angle > 360 angle = angle - 360 End While ElseIf angle < 0 Then While angle < 0 angle = angle + 360 End While End If ' 创建一个变换矩阵,并旋转指定角度。 Dim myMatrix As New System.Drawing.Drawing2D.Matrix myMatrix.Rotate(angle, Drawing2D.MatrixOrder.Append) ' 应用完变换之后绘制文本至屏幕。 g.Transform = myMatrix g.DrawString(s, f, b, x, y) End Sub |
C#
C# |
拷贝代码
|
---|---|
public void RotateText(Graphics g, Font f, string s, Single angle, Brush b, Single x, Single y) { if (angle > 360) { while (angle > 360) { angle = angle - 360; } } else if (angle < 0) { while (angle < 0) { angle = angle + 360; } } // 创建一个变换矩阵,并旋转指定角度。 System.Drawing.Drawing2D.Matrix myMatrix = new System.Drawing.Drawing2D.Matrix(); myMatrix.Rotate(angle, System.Drawing.Drawing2D.MatrixOrder.Append); // 应用完变换之后绘制文本至屏幕。 g.Transform = myMatrix; g.DrawString(s, f, b, x, y); } |
您所添加的文本显示为45度角: