c# print right left up down
2016-11-04 16:32
155 查看
``` private void button1_Click(object sender, EventArgs e) { System.Drawing.Printing.PrintDocument pd = new System.Drawing.Printing.PrintDocument(); pd.DocumentName = pd.PrinterSettings.MaximumCopies.ToString(); pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.pd_PrintPage); pd.PrintController = new System.Drawing.Printing.StandardPrintController(); pd.Print(); } private void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { int w = 30; int h = 100; string ss = "实现真正意义的校园一卡通,就需要做到各类关联系统的学生、教师身份统一认证,最好能做到比如饭卡系统"; Rectangle fontRectanle = new Rectangle(50, 20, w, h); Font f = new Font("宋体", 14); e.Graphics.PageUnit = System.Drawing.GraphicsUnit.Millimeter; e.Graphics.DrawRectangle(new System.Drawing.Pen(Brushes.Black), fontRectanle);//画边框 DrawStringWrap(e.Graphics, f, ss, fontRectanle); } /// <summary> /// 绘制文本自动换行(超出截断) /// </summary> /// <param name=\"graphic\">绘图图面</param> /// <param name=\"font\">字体</param> /// <param name=\"text\">文本</param> /// <param name=\"recangle\">绘制范围</param> private void DrawStringWrap(Graphics graphic, Font font, string text, Rectangle recangle) { List<string> textRows = GetStringRows(graphic, font, text, recangle); int rowHeight = (int)(Math.Ceiling(graphic.MeasureString("测", font).Height)); int wordWidth = (int)(Math.Ceiling(graphic.MeasureString("测", font).Width));//计算一个字的宽度 int top = recangle.Top;// (recangle.Height - rowHeight * drawRowCount) / 2; int left = recangle.Left + recangle.Width - wordWidth; StringFormat drawFormat = new StringFormat(StringFormatFlags.DirectionVertical); for (int i = 0; i < textRows.Count; i++) { Rectangle fontRectanle = new Rectangle(left - wordWidth * i, top, wordWidth, recangle.Height); graphic.DrawString(textRows[i], font, new SolidBrush(Color.Black), fontRectanle, drawFormat); } } /// <summary> /// 将文本分行 /// </summary> /// <param name=\"graphic\">绘图图面</param> /// <param name=\"font\">字体</param> /// <param name=\"text\">文本</param> /// <param name=\"width\">行宽</param> /// <returns></returns> private List<string> GetStringRows(Graphics graphic, Font font, string text, Rectangle recangle) { int wordWidth = (int)(Math.Ceiling(graphic.MeasureString("测", font).Height));//计算一个字的高度 int rows = recangle.Height / wordWidth; //计算能显示多少行 rows += (int)(rows * 0.5);//0.5是各系数,应能定义 int cols = text.Length / rows;//计算需要分多少列 if (text.Length % rows >= 0) { cols++; } List<string> textRows = new List<string>(); string tem = text; for (int index = 0; index < cols; index++) { try { textRows.Add(tem.Substring(index * rows, rows)); } catch { textRows.Add(tem.Substring(index * rows)); } } return textRows; }
“`
相关文章推荐
- C#中 MouseLeftButtonDown MouseLeftButtonUp MouseMove 使用
- 编写程序,打印输入中单词长度的直方图(up/down/left/right)
- 让Up,Down,Left,Right失去作用 禁用Up,Down,Left,Right
- Keys.Up,Down,Left,Right用法及键盘值
- j2me学习笔记【12】——游戏操作Canvas类的up、down、left、right、fire属性使用实例
- How to handle the MouseLeftButtonDown and MouseLeftButtonUp events of the Button control?
- C# 中用 PadLeft、PadRight 补足位数
- C# 中用 PadLeft、PadRight 补足位数
- C#把子控件的MouseUP MouseDown MouseMove三个事件提交到父控件
- C# 中 PadLeft和PadRight 的用法
- RadTreeViewItem - event MouseLeftButtonDown or Up never fires
- C# 中Left() , Right(), Mid()
- travel the binary tree by level 3 ( from down to top and from right to left every level )
- C# join DataTable (support Left/Right/Full join)
- IMarkupPointer :: Right and IMarkupPointer :: Left 'notes document describes
- C# join DataTable (support Left/Right/Full join)转贴
- c#中自定义字符截取函数:Left、Right 、Mid
- C#唯一随机数 和 PadLeft、PadRight 补足位数
- 用 JScript 模拟写 C# PadLeft PadRight