-1

我是 Dot Net 的新手,如果我用比例尺测量,我想打印一个宽度为 20 毫米、高度为 8 毫米的矩形。我也想在矩形中间打印文本。谁能建议我如何实现这一点?


真的很抱歉之前没有说清楚。我试过使用“PageUnits”它工作正常。但是,我对边距有疑问。

如果我使用打印机“HP LaserJet P2035n”,我可以打印正确的边距(左侧 8.8 毫米和顶部 22 毫米)。如果使用“Canon iR2020 PCL5e”打印,我会得到不正确的边距(左边 8.1 毫米和顶部 8.0 毫米),我应该得到左边 8.8 毫米和顶部 22 毫米的边距。有人可以解释一下我在哪里做错了。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
namespace ConsoleApplication6
{
    class DrawShape
    {
        public static void DrawRec()
        {
            PrintDocument doc = new PrintDocument();
            doc.PrintPage += doc_PrintPage;
            doc.Print();
        }

        static void doc_PrintPage(object sender, PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;
            PageSettings PageSet = new PageSettings();
            float MarginX = PageSet.PrintableArea.X;
            float MarginY = PageSet.PrintableArea.Y;
            float x = (float)(8.8-((MarginX/100)*25.4));
            float y = (float)(22-((MarginY/100)*25.4));
            g.PageUnit = GraphicsUnit.Millimeter;
            g.DrawRectangle(Pens.Black, x, y, 20, 8);

        }
    }
}
4

1 回答 1

0

你可能想从这个开始:

    private void button1_Click(object sender, EventArgs e)
    {
        using (Graphics formGraphics = this.CreateGraphics())
        {
            formGraphics.PageUnit = GraphicsUnit.Millimeter;
            formGraphics.DrawRectangle(Pens.Blue, 0, 0, 20, 80);
        }
    }

然后,您可以在 Graphics 对象上使用 DrawString 在矩形内绘制文本。

于 2013-05-13T19:10:10.143 回答