自定义多页打印.

xufengo0oID 2007-08-06 11:58:24
请问怎样实现当我按下按钮后,一起将几个tabpage里的内容分别打印在不同的页面上?
...全文
158 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
liusong_china 2007-08-09
  • 打赏
  • 举报
回复
学习
jlzan1314 2007-08-06
  • 打赏
  • 举报
回复
hoho,强啊.至今我都是半懂状态.
lnwuyaowei 2007-08-06
  • 打赏
  • 举报
回复
自已写打印程序???

以下代码供参考:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Drawing.Printing;

namespace CapsEditor
{
public partial class Form1 : Form
{
private const uint margin = 20;
#region 成员变量
private ArrayList documentLines = new ArrayList();
private uint lineHeight;
private Size documnetSize;
private uint nLines;
private Font mainFont;
private Font emptyDocumnetFont;
private Brush mainBrush = Brushes.Blue;
private Brush emptyDocumentBrush = Brushes.Red;
private Point mouseDoubleClickPosiTion;
private OpenFileDialog fileOpenDialog = new OpenFileDialog();
private bool documentHasData = false;

private int pagesPrinted = 0;
#endregion
public Form1()
{
CreateFonts();
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}
private void CreateFonts()
{
mainFont = new Font("宋体", 14);
lineHeight = (uint)mainFont.Height;
emptyDocumnetFont = new Font("Verdana", 13, FontStyle.Bold);
}
private void LoadFile(string FileName)
{
StreamReader sr = new StreamReader(FileName);
string nextLine;
documentLines.Clear();
nLines = 0;
TextLineInfoMation nextLineInfo;
while((nextLine = sr.ReadLine())!=null)
{
nextLineInfo = new TextLineInfoMation();
nextLineInfo.Text = nextLine;
documentLines.Add(nextLineInfo);
++nLines;
}
sr.Close();
documentHasData = (nLines > 0) ? true : false;

CalculateLineWidths();
CalculateDocumentSize();

this.Invalidate();
}

private void CalculateDocumentSize()
{
if (!documentHasData)
{
documnetSize = new Size(100, 200);
}
else
{
documnetSize.Height = (int)(nLines * lineHeight) + 2 * (int)margin;
uint maxLineLength = 0;
foreach (TextLineInfoMation nextWord in documentLines)
{
uint tempLineLength = nextWord.Width + 2 * margin;
if (tempLineLength > maxLineLength)
maxLineLength = tempLineLength;
}
documnetSize.Width = (int)maxLineLength;
}
this.AutoScrollMinSize = documnetSize;
}

private void CalculateLineWidths()
{
Graphics dc = this.CreateGraphics();
foreach (TextLineInfoMation nextLine in documentLines)
{
nextLine.Width = (uint)dc.MeasureString(nextLine.Text, mainFont).Width;
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
fileOpenDialog.ShowDialog();
this.LoadFile(fileOpenDialog.FileName);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics dc = e.Graphics;
int scrollPositionX = this.AutoScrollPosition.X;
int scrollPositionY = this.AutoScrollPosition.Y;
dc.TranslateTransform(scrollPositionX, scrollPositionY);
if (!documentHasData)
{
dc.DrawString("<无数据文件>",emptyDocumnetFont,emptyDocumentBrush,new Point(20,20));
return;
}
int minLineInClipRegion = WorldYCoordinateToLineIndex(e.ClipRectangle.Top-scrollPositionY);
if (minLineInClipRegion == -1)
minLineInClipRegion = 0;
int maxLineClipRegion = WorldYCoordinateToLineIndex(e.ClipRectangle.Bottom - scrollPositionY);
if (maxLineClipRegion >= this.documentLines.Count || maxLineClipRegion == -1)
maxLineClipRegion = this.documentLines.Count - 1;

TextLineInfoMation nextLine;
for (int i = minLineInClipRegion; i < maxLineClipRegion; i++)
{
nextLine = (TextLineInfoMation)documentLines[i];
dc.DrawString(nextLine.Text, mainFont, mainBrush, this.LineIndexToWorldCoordinates(i));

}


}

private Point LineIndexToWorldCoordinates(int lineIndex)
{
Point TopLeftCorner = new Point((int)margin, (int)(lineHeight * lineIndex + margin));
return TopLeftCorner;
}
//依据座标计算出所在行
private int WorldYCoordinateToLineIndex(int y)
{
if (y < margin) return -1;
return (int)((y - margin) / lineHeight);

}

private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
{
this.pagesPrinted = 0;
PrintPreviewDialog ppd = new PrintPreviewDialog();
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
ppd.Document = pd;
ppd.ShowDialog();
}

void pd_PrintPage(object sender, PrintPageEventArgs e)
{
float yPos = 0;
float leftMargin = e.MarginBounds.Left;
float topMargin = e.MarginBounds.Top;
string line = null;

int linesPerPage = (int)(e.MarginBounds.Height/mainFont.GetHeight(e.Graphics));
int lineNo = this.pagesPrinted*linesPerPage;
//print each line of the file.

int coutn = 0;
while (coutn < linesPerPage && lineNo < this.nLines)
{
line = ((TextLineInfoMation)this.documentLines[lineNo]).Text;
yPos = topMargin + (coutn * mainFont.GetHeight(e.Graphics));
e.Graphics.DrawString(line, mainFont, Brushes.Blue, leftMargin, yPos, new StringFormat());
lineNo++;
coutn++;
}

if (this.nLines > lineNo)
e.HasMorePages = true;
else
e.HasMorePages = false;

pagesPrinted++;
}

private void ttttToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
}
}
class TextLineInfoMation
{
public string Text;
public uint Width;
}
}
xufengo0oID 2007-08-06
  • 打赏
  • 举报
回复
有那位高手帮帮
xufengo0oID 2007-08-06
  • 打赏
  • 举报
回复
cs
hm7921936 2007-08-06
  • 打赏
  • 举报
回复
BS 还是CS?
lvxianda 2007-08-06
  • 打赏
  • 举报
回复
PrintPage里定义不同的方法
判断打印时每页的号码
来分别处理
xufengo0oID 2007-08-06
  • 打赏
  • 举报
回复
顶一下

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧