- 积分
- 106
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2016-4-29
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 xyangtian 于 2017-6-10 16:20 编辑
- <div class="blockcode"><blockquote>using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using Microsoft.Win32;
- using Grapher;
- using System.Reflection;
- namespace WpfApplication1
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
- string[] fileName = new string[] { };
-
- List<string> listFile = new List<string>();
- List<string> listFileName = new List<string>();
- List<string> strListFileName = new List<string>();
-
- private void fileButton_Click(object sender, RoutedEventArgs e)
- {
- OpenFileDialog ofdName = new OpenFileDialog();
- ofdName.Title = "选择网格文件";
- ofdName.Filter = "dat文件|*.dat|所有文件|*.*";
- ofdName.FileName = string.Empty;
- ofdName.RestoreDirectory = true;
- ofdName.DefaultExt = "dat";
- ofdName.Multiselect = true;
- if (ofdName.ShowDialog() == true)
- {
- fileName = ofdName.FileNames;
- for (int i = 0; i < fileName.Length; i++)
- {
- listFile.Add(fileName[i]);
- strListFileName.Add(fileName[i].Substring(fileName[i].LastIndexOf("\") + 1, (fileName[i].LastIndexOf(".") - fileName[i].LastIndexOf("\") - 1)));
-
- listFileName.Add(fileName[i].Remove(fileName[i].LastIndexOf(".")));//移除文件后缀
- }
- }
-
- }
- private void makeButton_Click(object sender, RoutedEventArgs e)
- {
- for (int i = 0; i < listFile.LongCount(); i++)
- {
- Type grapherType = Type.GetTypeFromProgID("Grapher.Application");
- Object grapher = System.Activator.CreateInstance(grapherType);//获取Grapher实例
- grapherType.InvokeMember("Visible", BindingFlags.SetProperty, null, grapher, new Object[] { true });//显示Grapher界面
- Object documents = grapherType.InvokeMember("Documents", BindingFlags.GetProperty, null, grapher, null);
- Object document = documents.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, documents, null);
- Object shapes = document.GetType().InvokeMember("Shapes", BindingFlags.GetProperty, null, document, null);
- Object Graph1 = shapes.GetType().InvokeMember("AddLinePlotGraph", BindingFlags.InvokeMethod, null, shapes, new Object[] { listFile[i], 1, 2 });
- Object graphTitle = Graph1.GetType().InvokeMember("Title", BindingFlags.GetProperty, null, Graph1, null);
- graphTitle.GetType().InvokeMember("Text", BindingFlags.SetProperty, null, graphTitle, new Object[] { strListFileName[i] });//设置图形名
- Object axesItem = Graph1.GetType().InvokeMember("Axes", BindingFlags.GetProperty, null, Graph1, null);
- Object yAxisItem = axesItem.GetType().InvokeMember("Item", BindingFlags.GetProperty, null, axesItem, new Object[] { 2 });//选中Y轴
- Object yAxisTitle = yAxisItem.GetType().InvokeMember("Title", BindingFlags.GetProperty, null, yAxisItem, null);//切换到Y轴的Title属性
- yAxisTitle.GetType().InvokeMember("Text", BindingFlags.SetProperty, null, yAxisTitle, new Object[] { "Emf(mV/A)" });//设置Y中的Text
- Object yAxis = Graph1.GetType().InvokeMember("Axes", BindingFlags.GetProperty, null, Graph1, new Object[] { 2 });
- yAxis.GetType().InvokeMember("Scale", BindingFlags.SetProperty, null, yAxis, new Object[] { 2 });//设置为对数(log)
- Object xAxisItem = axesItem.GetType().InvokeMember("Item", BindingFlags.GetProperty, null, axesItem, new Object[] { 1 });//选中X轴
- Object xAxisTitle = xAxisItem.GetType().InvokeMember("Title", BindingFlags.GetProperty, null, xAxisItem, null);//切换到X轴的Title属性
- xAxisTitle.GetType().InvokeMember("Text", BindingFlags.SetProperty, null, xAxisTitle, new Object[] { "T(ms)" });//设置X中的Text
- Object xAxis = Graph1.GetType().InvokeMember("Axes", BindingFlags.GetProperty, null, Graph1, new Object[] { 1 });
- xAxis.GetType().InvokeMember("Scale", BindingFlags.SetProperty, null, xAxis, new Object[] { 2 });
- document.GetType().InvokeMember("SaveAs", BindingFlags.InvokeMethod, null, document, new Object[] {listFileName[i]+".grf"});
- grapher.GetType().InvokeMember("Quit", BindingFlags.InvokeMethod, null, grapher, null);
- System.GC.Collect(System.GC.GetGeneration(grapher));
- MessageBox.Show("ok");
- }
- }
- }
-
- }
复制代码 不知为何grapher不能像类一样new()新对象,一直报错。只能通过反射调用。哪位高手能够解答一下。 |
|