使用C1PrintDocument控件 > 渲染对象 > 渲染对象的嵌套、定位和叠放规则 > 渲染对象相对定位的样例 |
下方的样例展示了如何使用渲染对象的相对定位来放置一个图片和文本对象(下方样例中的"myImage"指的是定义在别处的一个System.Drawing.Image类型的对象)。
下方代码通过简单的将对象一个挨着一个放入常规的块状布局流里面的方式,使得文本对象放置在图片下方。
Visual Basic
Visual Basic |
拷贝代码
|
---|---|
Dim doc as New C1PrintDocument Dim rt as New RenderText("test") Dim ri as New RenderImage(myImage) Dim ra As New RenderArea() ra.Children.Add(ri) ra.Children.Add(rt) doc.Body.Children.Add(ra) |
C#
C# |
拷贝代码
|
---|---|
C1PrintDocument doc = new C1PrintDocument(); RenderText rt = new RenderText("test"); RenderImage ri = new RenderImage(myImage); RenderArea ra = new RenderArea(); ra.Children.Add(ri); ra.Children.Add(rt); doc.Body.Children.Add(ra); |
当子对象以相反的顺序被添加到区域中时,这些代码的最终效果(文本位于图像下方)是一样的(因为这两个对象既没有显式指定坐标属性为自动,也没有被添加到块状布局流中。)
Visual Basic
Visual Basic |
拷贝代码
|
---|---|
Dim doc as New C1PrintDocument Dim rt as New RenderText("test") Dim ri as New RenderImage(myImage) Dim ra As New RenderArea() ' 在父对象的顶部放置图片对象: |
C#
C# |
拷贝代码
|
---|---|
C1PrintDocument doc = new C1PrintDocument(); RenderText rt = new RenderText("test"); RenderImage ri = new RenderImage(myImage); RenderArea ra = new RenderArea(); // 在父对象的顶部放置图片对象: ri.Y = 0; // 下方放置文本对象: rt.Y = "next.bottom"; // 文本对象的宽度设为自动: rt.Width = Unit.Auto; ra.Children.Add(rt); ra.Children.Add(ri); doc.Body.Children.Add(ra); |
下方代码将图像对象插入到常规块状布局流中,同时将文本对象放置在图像的右侧,并将其的垂直位置相对于图像对象居中。
Visual Basic
Visual Basic |
拷贝代码
|
---|---|
Dim doc as New C1PrintDocument Dim rt as New RenderText("test") Dim ri as New RenderImage(myImage) Dim ra As New RenderArea() ra.Children.Add(ri) rt.Width = Unit.Auto ' add text after the image: ra.Children.Add(rt) rt.X = "prev.right" rt.Y = "prev.height/2-self.height/2" doc.Body.Children.Add(ra) |
C#
C# |
拷贝代码
|
---|---|
C1PrintDocument doc = new C1PrintDocument(); RenderText rt = new RenderText("test"); RenderImage ri = new RenderImage(myImage); RenderArea ra = new RenderArea(); ra.Children.Add(ri); rt.Width = Unit.Auto; // add text after the image: ra.Children.Add(rt); rt.X = "prev.right"; rt.Y = "prev.height/2-self.height/2"; doc.Body.Children.Add(ra); |
这些代码也是将文本对象放置在图像的右侧,垂直居中---但是在定位表达式中使用了RenderObject.Name替代相对id"prev",此外,文本对象向右偏移了2毫米,用于演示利用表达式来设置绝对长度。
Visual Basic
Visual Basic |
拷贝代码
|
---|---|
Dim doc as New C1PrintDocument Dim rt as New RenderText("test") Dim ri as New RenderImage(myImage) Dim ra As New RenderArea() ri.Name = "myImage" rt.Width = "auto" rt.X = "myImage.right+2mm" rt.Y = "myImage.height/2-self.height/2" ra.Children.Add(ri) ra.Children.Add(rt) doc.Body.Children.Add(ra) |
C#
C# |
拷贝代码
|
---|---|
C1PrintDocument doc = new C1PrintDocument(); RenderText rt = new RenderText("test"); RenderImage ri = new RenderImage(myImage); RenderArea ra = new RenderArea(); ri.Name = "myImage"; rt.Width = "auto"; rt.X = "myImage.right+2mm"; rt.Y = "myImage.height/2-self.height/2"; ra.Children.Add(ri); ra.Children.Add(rt); doc.Body.Children.Add(ra); |
下方代码修改了相同的样例,因此使用内建的Max函数使得文本对象向右偏移了至少6厘米
Visual Basic
Visual Basic |
拷贝代码
|
---|---|
Dim doc as New C1PrintDocument Dim rt as New RenderText("test") Dim ri as New RenderImage(myImage) Dim ra As New RenderArea() ri.Name = "myImage" rt.Width = "auto" rt.X = "Max(myImage.right+2mm,6cm)" rt.Y = "myImage.height/2-self.height/2" ra.Children.Add(ri) ra.Children.Add(rt) doc.Body.Children.Add(ra) |
C#
C# |
拷贝代码
|
---|---|
C1PrintDocument doc = new C1PrintDocument(); RenderText rt = new RenderText("test"); RenderImage ri = new RenderImage(myImage); RenderArea ra = new RenderArea(); ri.Name = "myImage"; rt.Width = "auto"; rt.X = "Max(myImage.right+2mm,6cm)"; rt.Y = "myImage.height/2-self.height/2"; ra.Children.Add(ri); ra.Children.Add(rt); doc.Body.Children.Add(ra); |
下方的代码片段将图像对象放置在了页面的右侧(利用渲染区域的默认宽度---父对象的宽度),尽管文本对象水平居左,垂直方向相对于图像居中
Visual Basic
Visual Basic |
拷贝代码
|
---|---|
Dim doc as New C1PrintDocument Dim rt as New RenderText("test") Dim ri as New RenderImage(myImage) Dim ra As New RenderArea() ri.Name = "myImage" ' 图像居右: ri.X = "parent.right-width" ' 图像居左: rt.X = "0" rt.Y = "myImage.height/2-height/2" ra.Children.Add(ri) ra.Children.Add(rt) doc.Body.Children.Add(ra) |
C#
C# |
拷贝代码
|
---|---|
C1PrintDocument doc = new C1PrintDocument(); RenderText rt = new RenderText("test"); RenderImage ri = new RenderImage(myImage); RenderArea ra = new RenderArea(); ri.Name = "myImage"; // 图像居右: ri.X = "parent.right-width"; // 图像居左: rt.X = "0"; rt.Y = "myImage.height/2-height/2"; ra.Children.Add(ri); ra.Children.Add(rt); doc.Body.Children.Add(ra); |