Office中国论坛/Access中国论坛
标题:
【作业】05课-CA135B_Amas
[打印本页]
作者:
Amas
时间:
2014-4-12 22:40
标题:
【作业】05课-CA135B_Amas
using Microsoft.Office.Tools.Ribbon;
using Word = Microsoft.Office.Interop.Word;
using System.Windows.Forms;
namespace Lesson05
{
public partial class Ribbon1
{
private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
{
}
//删除所有嵌入式图片
private void btnDelAllInlineShapes_Click(object sender, RibbonControlEventArgs e)
{
Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
if (doc.InlineShapes.Count > 0)
{
foreach (Word.InlineShape shape in doc.InlineShapes)
{
shape.Delete();
}
}
else
{
MessageBox.Show("文档没有嵌入式图片");
}
}
//插入签名
private void btnInsertSignature_Click(object sender, RibbonControlEventArgs e)
{
Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
Word.Range range;
//判断当前文档段落数是否满足要求
if (doc.Paragraphs.Count < 3)
{
//段落数小于3,提示在文档尾添加
DialogResult result = MessageBox.Show("文档段落数小于3,\n是否在文档尾添加签名", "提示", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
range = doc.Range(doc.Content.End - 1, doc.Content.End - 1);
range.InsertAfter("\n西西");
}
else
{
return;
}
}
//在第3段尾添加签名
else
{
int iEnd = (doc.Paragraphs[3].Range.End)-1;
doc.Range(0, iEnd).InsertAfter("西西");
}
}
}
}
复制代码
作者:
Amas
时间:
2014-4-13 01:11
选作题:统一设置嵌入式图片大小
[attach]53806[/attach]
using Microsoft.Office.Tools.Ribbon;
using Word = Microsoft.Office.Interop.Word;
using System.Windows.Forms;
namespace Lesson05
{
public partial class Ribbon1
{
//统一嵌入式图片大小
private void btnSetInlineShapesSize_Click(object sender, RibbonControlEventArgs e)
{
Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
if (doc.InlineShapes.Count > 0)
{
frmSetShapesSize setDialog = new frmSetShapesSize();
setDialog.ShowDialog();
}
else
{
MessageBox.Show("文档中没有嵌入式图片");
}
}
}
}
复制代码
using System;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using System.Text.RegularExpressions;
namespace Lesson05
{
public partial class frmSetShapesSize : Form
{
Document doc = Globals.ThisAddIn.Application.ActiveDocument;
float height, width;
public frmSetShapesSize()
{
InitializeComponent();
radioButton1.Checked = true;
textBox1.Enabled = true;
radioButton2.Checked = false;
textBox2.Enabled = false;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text != "" && IsNumber(textBox1.Text))
{
height = Convert.ToSingle(textBox1.Text);
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
if (textBox2.Text != "" && IsNumber(textBox2.Text))
{
width = Convert.ToSingle(textBox2.Text);
}
}
//设置图片大小
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" && textBox2.Text == "")
{
MessageBox.Show("请输入高度和宽度");
return;
}
foreach (InlineShape shape in doc.InlineShapes)
{
shape.LockAspectRatio = Microsoft.Office.Core.MsoTriState.msoTrue;
if (radioButton1.Checked)
{
shape.Height = height;
shape.ScaleWidth = shape.ScaleHeight;
}
if (radioButton2.Checked)
{
shape.Width = width;
shape.ScaleHeight = shape.ScaleWidth;
}
}
this.Dispose();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
textBox1.Enabled = radioButton1.Checked;
textBox2.Text = "";
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
textBox2.Enabled = radioButton2.Checked;
textBox1.Text = "";
}
//判断内容是否为数字函数
public bool IsNumber(String strNumber)
{
Regex objNotNumberPattern = new Regex("[^0-9.-]");
Regex objTwoDotPattern = new Regex("[0-9]*[.][0-9]*[.][0-9]*");
Regex objTwoMinusPattern = new Regex("[0-9]*[-][0-9]*[-][0-9]*");
String strValidRealPattern = "^([-]|[.]|[-.]|[0-9])[0-9]*[.]*[0-9]+$";
String strValidIntegerPattern = "^([-]|[0-9])[0-9]*$";
Regex objNumberPattern = new Regex("(" + strValidRealPattern + ")|(" + strValidIntegerPattern + ")");
return !objNotNumberPattern.IsMatch(strNumber) &&
!objTwoDotPattern.IsMatch(strNumber) &&
!objTwoMinusPattern.IsMatch(strNumber) &&
objNumberPattern.IsMatch(strNumber);
}
}
}
复制代码
测试环境:WIN7 + VS2010 + OFFICE2010
[attach]53807[/attach]
欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/)
Powered by Discuz! X3.3