在pane_page.cs里面
using System;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;//erphone
namespace Niu_Niu_edit
{
public partial class pane_page : UserControl
{
public Word.Application m_app;
public pane_page()
{
InitializeComponent();
}
//m_app 代表当前应用程序
private void button1_Click(object sender, EventArgs e)
{
m_app = Globals.ThisAddIn.Application;
Word.Document m_doc = m_app.ActiveDocument;//定义当前文档叫做m_doc
if (float.Parse(textBox1.Text) < (float.Parse(textBox3.Text) + float.Parse(textBox4.Text)))
{
MessageBox.Show("页面宽度小于左右边距之和!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
textBox1.Text = "19.5";
return;
}//确保纸张宽度>页边距之和
if (float.Parse(textBox2.Text) < (float.Parse(textBox4.Text) + float.Parse(textBox5.Text)))
{
MessageBox.Show("页面高度小于上下边距之和!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
textBox2.Text = "27";
return;
}//确保纸张宽度>页边距之和
if (Math.Abs(float.Parse(textBox1.Text)) > 55.87)
{
MessageBox.Show("页面宽度应当在±55.87cm之内", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
textBox1.Text = "19.5";
return;
}//确保纸张宽度在word限定的纸张宽度范围
if (Math.Abs(float.Parse(textBox2.Text)) > 55.87)
{
MessageBox.Show("页面高度应当在±55.87cm之内", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
textBox2.Text = "27";
return;
}//确保纸张高度在word限定的纸张宽度范围
m_doc.PageSetup.PageWidth = m_app.CentimetersToPoints(float.Parse(textBox1.Text));// 纸张宽度
m_doc.PageSetup.PageHeight = m_app.CentimetersToPoints(float.Parse(textBox2.Text));// 纸张高度
//m_doc.PageSetup.TopMargin = m_app.CentimetersToPoints(float.Parse(textBox1.Text));// 上边距
//m_doc.PageSetup.BottomMargin = m_app.CentimetersToPoints(float.Parse("&2.54"));// 下边距
//m_doc.PageSetup.LeftMargin = m_app.CentimetersToPoints(float.Parse("&4.17"));// 左边距
//m_doc.PageSetup.RightMargin = m_app.CentimetersToPoints(float.Parse("&3.17"));// 右边距
}
private void button2_Click(object sender, EventArgs e)
{
m_app = Globals.ThisAddIn.Application;
Word.Document m_doc = m_app.ActiveDocument;//定义当前文档叫做m_doc
if (float.Parse(textBox1.Text) < (float.Parse(textBox3.Text) + float.Parse(textBox4.Text)))
{
MessageBox.Show("页面宽度小于左右边距之和!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
textBox3.Text = "2";
textBox4.Text = "2";
return;
}//确保纸张宽度>左右页边距之和
if (float.Parse(textBox2.Text) < (float.Parse(textBox5.Text) + float.Parse(textBox6.Text)))
{
MessageBox.Show("页面高度小于上下边距之和!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
textBox5.Text = "2";
textBox6.Text = "2";
return;
}//确保纸张高度>上下页边距之和
if (Math.Abs(double.Parse(textBox3.Text)) > 55.87)
{
MessageBox.Show("页边距应当在±55.87cm之内", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
textBox3.Text = "2";
return;
}//确保纸张高度在word限定的纸张宽度范围
if (Math.Abs(double.Parse(textBox4.Text)) > 55.87)
{
MessageBox.Show("页边距应当在±55.87cm之内", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
textBox4.Text = "2";
return;
}//确保纸张高度在word限定的纸张宽度范围
if (Math.Abs(double.Parse(textBox5.Text)) > 55.87)
{
MessageBox.Show("页边距应当在±55.87cm之内", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
textBox5.Text = "2";
return;
}//确保纸张高度在word限定的纸张宽度范围
if (Math.Abs(double.Parse(textBox6.Text)) > 55.87)
{
MessageBox.Show("页边距应当在±55.87cm之内", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
textBox6.Text = "2";
return;
}//确保纸张高度在word限定的纸张宽度范围
m_doc.PageSetup.LeftMargin = m_app.CentimetersToPoints(float.Parse(textBox3.Text));// 左边距
m_doc.PageSetup.RightMargin = m_app.CentimetersToPoints(float.Parse(textBox4.Text));// 右边距
m_doc.PageSetup.TopMargin = m_app.CentimetersToPoints(float.Parse(textBox5.Text));// 上边距
m_doc.PageSetup.BottomMargin = m_app.CentimetersToPoints(float.Parse(textBox6.Text));// 下边距
}
private void button3_Click(object sender, EventArgs e)
{
m_app = Globals.ThisAddIn.Application;
Word.Document m_doc = m_app.ActiveDocument;//定义当前文档叫做m_doc
if (float.Parse(textBox7.Text) > (float.Parse(textBox3.Text) + float.Parse(textBox4.Text)))
{
MessageBox.Show("纸张高度小于页眉页脚之和!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
textBox7.Text = "1.5";
return;
}//确保纸张高度>页眉页脚之和
if (float.Parse(textBox8.Text) > (float.Parse(textBox3.Text) + float.Parse(textBox4.Text)))
{
MessageBox.Show("纸张高度小于页眉页脚之和!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
textBox8.Text = "1.5";
return;
}//确保纸张高>页眉页脚之和
if (Math.Abs(double.Parse(textBox7.Text)) > 55.87)
{
MessageBox.Show("页眉高度应当在±55.87cm之内", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
textBox7.Text = "2";
return;
}//确保纸张高度在word限定的纸张宽度范围
if (Math.Abs(double.Parse(textBox8.Text)) > 55.87)
{
MessageBox.Show("页脚高度应当在±55.87cm之内", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
textBox8.Text = "2";
return;
}//确保纸张高度在word限定的纸张宽度范围
if (float.Parse(textBox7.Text) > 55.88)
{
MessageBox.Show("页眉值过大!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
textBox7.Text = "1.5";
return;
}//页眉页脚之和<word限定范围55.88cm
if (float.Parse(textBox7.Text) > 55.88)
{
MessageBox.Show("页脚值过大!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
textBox8.Text = "1.5";
return;
}//页眉页脚之和<word限定范围55.88cm
m_doc.PageSetup.HeaderDistance = m_app.CentimetersToPoints(float.Parse(textBox7.Text));//页眉顶端距离
m_doc.PageSetup.FooterDistance = m_app.CentimetersToPoints(float.Parse(textBox8.Text));//页脚底端距离
}
private void button4_Click(object sender, EventArgs e)
{
button1_Click(sender, e);
button2_Click(sender, e);
button3_Click(sender, e);
}
private void button5_Click(object sender, EventArgs e)
{
textBox1.Text = "19.5";
textBox2.Text = "27";
textBox3.Text = "2";
textBox4.Text = "2";
textBox5.Text = "2";
textBox6.Text = "2";
textBox7.Text = "1.5";
textBox8.Text = "1.5";
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
e.Handled = true;
//小数点的处理。
if ((int)e.KeyChar == 46) //小数点
{
if (textBox1.Text.Length <= 0)
e.Handled = true; //小数点不能在第一位
else
{
float f;
float oldf;
bool b1 = false, b2 = false;
b1 = float.TryParse(textBox1.Text, out oldf);
b2 = float.TryParse(textBox1.Text + e.KeyChar.ToString(), out f);
if (b2 == false)
{
if (b1 == true)
e.Handled = true;
else
e.Handled = false;
}
}
}
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
e.Handled = true;
//小数点的处理。
if ((int)e.KeyChar == 46) //小数点
{
if (textBox2.Text.Length <= 0)
e.Handled = true; //小数点不能在第一位
else
{
float f;
float oldf;
bool b1 = false, b2 = false;
b1 = float.TryParse(textBox2.Text, out oldf);
b2 = float.TryParse(textBox2.Text + e.KeyChar.ToString(), out f);
if (b2 == false)
{
if (b1 == true)
e.Handled = true;
else
e.Handled = false;
}
}
}
}
private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
{
if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
e.Handled = true;
//小数点的处理。
if ((int)e.KeyChar == 46) //小数点
{
if (textBox3.Text.Length <= 0)
e.Handled = true; //小数点不能在第一位
else
{
float f;
float oldf;
bool b1 = false, b2 = false;
b1 = float.TryParse(textBox3.Text, out oldf);
b2 = float.TryParse(textBox3.Text + e.KeyChar.ToString(), out f);
if (b2 == false)
{
if (b1 == true)
e.Handled = true;
else
e.Handled = false;
}
}
}
}
private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
{
if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
e.Handled = true;
//小数点的处理。
if ((int)e.KeyChar == 46) //小数点
{
if (textBox4.Text.Length <= 0)
e.Handled = true; //小数点不能在第一位
else
{
float f;
float oldf;
bool b1 = false, b2 = false;
b1 = float.TryParse(textBox4.Text, out oldf);
b2 = float.TryParse(textBox4.Text + e.KeyChar.ToString(), out f);
if (b2 == false)
{
if (b1 == true)
e.Handled = true;
else
e.Handled = false;
}
}
}
}
private void textBox5_KeyPress(object sender, KeyPressEventArgs e)
{
if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
e.Handled = true;
//小数点的处理。
if ((int)e.KeyChar == 46) //小数点
{
if (textBox5.Text.Length <= 0)
e.Handled = true; //小数点不能在第一位
else
{
float f;
float oldf;
bool b1 = false, b2 = false;
b1 = float.TryParse(textBox5.Text, out oldf);
b2 = float.TryParse(textBox5.Text + e.KeyChar.ToString(), out f);
if (b2 == false)
{
if (b1 == true)
e.Handled = true;
else
e.Handled = false;
}
}
}
}
private void textBox6_KeyPress(object sender, KeyPressEventArgs e)
{
if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
e.Handled = true;
//小数点的处理。
if ((int)e.KeyChar == 46) //小数点
{
if (textBox6.Text.Length <= 0)
e.Handled = true; //小数点不能在第一位
else
{
float f;
float oldf;
bool b1 = false, b2 = false;
b1 = float.TryParse(textBox6.Text, out oldf);
b2 = float.TryParse(textBox6.Text + e.KeyChar.ToString(), out f);
if (b2 == false)
{
if (b1 == true)
e.Handled = true;
else
e.Handled = false;
}
}
}
}
private void textBox7_KeyPress(object sender, KeyPressEventArgs e)
{
if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
e.Handled = true;
//小数点的处理。
if ((int)e.KeyChar == 46) //小数点
{
if (textBox7.Text.Length <= 0)
e.Handled = true; //小数点不能在第一位
else
{
float f;
float oldf;
bool b1 = false, b2 = false;
b1 = float.TryParse(textBox7.Text, out oldf);
b2 = float.TryParse(textBox7.Text + e.KeyChar.ToString(), out f);
if (b2 == false)
{
if (b1 == true)
e.Handled = true;
else
e.Handled = false;
}
}
}
}
private void textBox8_KeyPress(object sender, KeyPressEventArgs e)
{
if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
e.Handled = true;
//小数点的处理。
if ((int)e.KeyChar == 46) //小数点
{
if (textBox8.Text.Length <= 0)
e.Handled = true; //小数点不能在第一位
else
{
float f;
float oldf;
bool b1 = false, b2 = false;
b1 = float.TryParse(textBox8.Text, out oldf);
b2 = float.TryParse(textBox8.Text + e.KeyChar.ToString(), out f);
if (b2 == false)
{
if (b1 == true)
e.Handled = true;
else
e.Handled = false;
}
}
}
}
}
}