📄 form2.cs
字号:
int y = 0;
int ss = 0;
while (ss<textBox1.SelectionStart)
{
if (ss+textBox1.Lines[y].Length+2<=textBox1.SelectionStart)
{
ss += textBox1.Lines[y].Length+2;
y++;
}
else
{
x = textBox1.SelectionStart -ss;
break;
}
}
return new Point(x+1, y+1);
}
private void miColor_Click(object sender, System.EventArgs e)
{
if(colorDialog1.ShowDialog()==DialogResult.OK)
{
textBox1.ForeColor =colorDialog1.Color ;
}
}
public void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
switch(e.Button .Tag .ToString ())
{
case "tbtnCopy":textBox1.Copy ();
break;
case "tbtnCut": textBox1.Cut();
break;
case "tbtnPaste":textBox1.Paste ();
break;
case "tbPreview":
ppDialog.ShowDialog();
break;
case "tbPrint":
pDoc.Print();
break;
}
}
/*private void miWindow_Popup(object sender, System.EventArgs e)
{
for(int i=0;i<miWindow.MenuItems .Count ;i++)
miWindow.MenuItems [i].Enabled =this.MdiChildren .Length >0;
}*/
private void miCopy_Click(object sender, System.EventArgs e)
{
textBox1.Copy();
}
private void miCut_Click(object sender, System.EventArgs e)
{
textBox1.Cut();
}
private void miPaste_Click(object sender, System.EventArgs e)
{
textBox1.Paste();
}
private void miSelectAll_Click(object sender, System.EventArgs e)
{
textBox1.SelectAll();
}
private void conCopy_Click(object sender, System.EventArgs e)
{
textBox1.Copy();
}
private void conPaste_Click(object sender, System.EventArgs e)
{
textBox1.Paste();
}
private void conCut_Click(object sender, System.EventArgs e)
{
textBox1.Cut();
}
private void conSelectAll_Click(object sender, System.EventArgs e)
{
textBox1.SelectAll();
}
private void pDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
if (manuscript)
PrintDocOnManuscript(e);
else
PrintDoc(e);
}
int paraNo = 0; // 当前段落序号
int pageNo = 1; // 当前页号
int totalPages = 1; // 总页数
int leftLines = 0; // 段落跨页时,在下一页的行数
bool manuscript = true;
int cy = 0; // 文本段落计数
int cx = 0; // 段落内字符计数
// 非稿纸方式
private void PrintDoc(System.Drawing.Printing.PrintPageEventArgs e)
{
Font fontPara = new Font("宋体",12); // 正文字体
Font fontHF = new Font("宋体",9); // 页眉/脚字体
Brush brushPara = Brushes.Black; // 正文画刷
Pen penHF = Pens.Silver; // 页眉/脚画笔(用于画线等)
Brush brushHF = Brushes.Silver; // 页眉/脚画刷
// 打印页眉
e.Graphics.DrawString(fileName, fontHF, brushHF, new PointF(e.MarginBounds.Left, e.MarginBounds.Top - fontHF.GetHeight(e.Graphics)-5));
e.Graphics.DrawLine(penHF,e.MarginBounds.Left,e.MarginBounds.Top-4,e.MarginBounds.Right,e.MarginBounds.Top-4);
RectangleF f;
SizeF s;
float lineHeight = fontPara.GetHeight(e.Graphics); // 行高
int linesPerPage = (int)(e.MarginBounds.Height / lineHeight); // 每页行数
// 计算总页数
int totalLines = 0;
if (pageNo == 1)
{
for(int i=0; i<textBox1.Lines.Length; i++)
{
s = e.Graphics.MeasureString(textBox1.Lines[i], fontPara, e.MarginBounds.Width);
totalLines += (int)(s.Height/lineHeight);
}
totalPages = totalLines/linesPerPage + (totalLines%linesPerPage==0?0:1);
}
// 打印页脚
string footer = "第" + pageNo.ToString() + "页 共" + totalPages.ToString() + "页";
SizeF sf = e.Graphics.MeasureString(footer,fontHF);
e.Graphics.DrawString(footer, fontHF, brushHF, new PointF((e.PageBounds.Width - sf.Width) / 2, e.MarginBounds.Bottom + 2));
int lineNo = 0; // 行计数
// 打印上页剩余的部分
f = e.MarginBounds;
f.Inflate(1,1);
e.Graphics.SetClip(f);
if (leftLines>0)
{
s = e.Graphics.MeasureString(textBox1.Lines[paraNo], fontPara, e.MarginBounds.Width);
f = new RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, s.Width, s.Height);
f.Offset(0,-(s.Height-leftLines*lineHeight));
e.Graphics.DrawString(textBox1.Lines[paraNo], fontPara, brushPara, f);
lineNo = leftLines;
paraNo++;
}
while (paraNo<textBox1.Lines.Length)
{
// 求当前段落打印行数
s = e.Graphics.MeasureString(textBox1.Lines[paraNo], fontPara, e.MarginBounds.Width);
int strLines = (int)(s.Height/lineHeight);
// 求实际要打印当前段落的行数
int pLines;
if (lineNo + strLines <= linesPerPage)
{
pLines = strLines;
f = new RectangleF(e.MarginBounds.Left, e.MarginBounds.Top + lineNo * lineHeight, s.Width, s.Height);
}
else
{
pLines = (linesPerPage - lineNo);
f = new RectangleF(e.MarginBounds.Left, e.MarginBounds.Top + lineNo * lineHeight, s.Width, pLines*lineHeight);
}
// 打印当前段落的前 pLines 行
e.Graphics.DrawString(textBox1.Lines[paraNo], fontPara, brushPara, f);
lineNo += pLines;
// 判断是否已打印完一页
if (lineNo == linesPerPage && paraNo+1 < textBox1.Lines.Length)
{
e.HasMorePages = true;
leftLines = strLines - pLines;
if (leftLines==0) paraNo++;
break;
}
paraNo++;
}
pageNo++;
}
private void PrintDocOnManuscript(System.Drawing.Printing.PrintPageEventArgs e)
{
Font fontPara = new Font("宋体",12); // 正文字体
Font fontHF = new Font("宋体",9); // 页眉/脚字体
Brush brushPara = Brushes.Black; // 正文画刷
Pen penHF = Pens.Silver; // 页眉/脚画笔(用于画线等)
Brush brushHF = Brushes.Silver; // 页眉/脚画刷
// 打印页眉
e.Graphics.DrawString(fileName, fontHF, brushHF, new PointF(e.MarginBounds.Left, e.MarginBounds.Top - fontHF.GetHeight(e.Graphics)-5));
e.Graphics.DrawLine(penHF,e.MarginBounds.Left,e.MarginBounds.Top-4,e.MarginBounds.Right,e.MarginBounds.Top-4);
// 测量一个测字的尺寸,用于决定格子的大小
SizeF s = e.Graphics.MeasureString("好", fontPara);
int gridSize = (int)Math.Max(s.Height, s.Width) + 2; // 格子尺寸
int dy = gridSize - (int)s.Height + 2; // 文字与格子顶线的距离
int lineHeight = 3*gridSize/2; // 行高
int linesPerPage = e.MarginBounds.Height / lineHeight; // 每页行数
int charsPerPage = e.MarginBounds.Width / gridSize; // 每行字符数
// 求总页数
if (pageNo==1)
{
int lines = 0;
for(int i=0; i<textBox1.Lines.Length; i++)
{
if(textBox1.Lines[i].Length == 0) // 空行
lines++;
else
lines += textBox1.Lines[i].Length/charsPerPage + (textBox1.Lines[i].Length%charsPerPage==0?0:1);
}
totalPages = lines/linesPerPage + (lines%linesPerPage==0?0:1);
}
// 左边距
int marginLeft = e.MarginBounds.Left + (e.MarginBounds.Width - (charsPerPage*gridSize))/2;
// 画格子
for(int i=0; i<linesPerPage; i++)
for(int j=0; j<charsPerPage; j++)
e.Graphics.DrawRectangle(penHF, marginLeft + j*gridSize, e.MarginBounds.Top+i*lineHeight, gridSize, gridSize);
// 画外框
e.Graphics.DrawRectangle(penHF, marginLeft, e.MarginBounds.Top, gridSize*charsPerPage, lineHeight*linesPerPage);
// 打印页脚
string footer = "第" + pageNo.ToString() + "页 共" + totalPages.ToString() + "页";
SizeF sf = e.Graphics.MeasureString(footer,fontHF);
e.Graphics.DrawString(footer, fontHF, brushHF, new PointF((e.PageBounds.Width - sf.Width) / 2, e.MarginBounds.Bottom + 2));
footer = linesPerPage.ToString() + "行 × " + charsPerPage.ToString() + "列 = "+(linesPerPage*charsPerPage).ToString()+"字";
sf = e.Graphics.MeasureString(footer,fontHF);
e.Graphics.DrawString(footer, fontHF, brushHF, new PointF(e.MarginBounds.Right - sf.Width, e.MarginBounds.Bottom + 2));
// 打印文本
int r = 0; // 稿纸上的当前行计数
int c; // 稿纸上的当前行字符计数
while (cy<textBox1.Lines.Length)
{
c = 0;
while (cx<textBox1.Lines[cy].Length && c<charsPerPage)
{
s = e.Graphics.MeasureString(textBox1.Lines[cy].Substring(cx,1), fontPara);
e.Graphics.DrawString(textBox1.Lines[cy].Substring(cx,1), fontPara, brushPara, marginLeft + c*gridSize + 1, r*lineHeight+e.MarginBounds.Top + dy);
c++;
cx++;
}
if (cx>=textBox1.Lines[cy].Length)
{
cx = 0;
cy++;
}
r++;
// 判断是否已打印完一页
if (r == linesPerPage)
{
if (cy < textBox1.Lines.Length-1 || cy < textBox1.Lines.Length && cx < textBox1.Lines[cy].Length)
e.HasMorePages = true;
break;
}
}
pageNo++;
}
private void Form2_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (textBox1.Modified)
{
DialogResult dr = MessageBox.Show(this,"文件:"+fileName+"\r\n已被修改,是否要保存?","提示",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Warning);
switch(dr)
{
case DialogResult.Yes:
break;
case DialogResult.Cancel:
e.Cancel = true;
break;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -