用WPF写界面,来调用C++内核,一直觉得很高深,到底是怎么实现的呢。。。
首先WPF界面上应该有相应的按钮(Button),而Button的对应事件处理函数中可以调用C++内核——其实就是C++程序编译链接后生成的可执行程序(.exe文件)。
而事件处理函数中该如何调用.exe文件呢?朋友说她只用了一个命令就是ShellExecute,于是我开始各种搜ShellExecute的用法,无意间也还看到了Process类的调用方法,于是写了那篇《C#如何运行外部程序(打开可执行程序):ShellExcute和Process》。
下面给出一个WPF调用C++内核的小例子:
MainWindow.xaml
<Window x:Class="WPFcallC__.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel Name="sp1">
<Grid>
<TextBox Height="25" Text = "{Binding Path=TextBoxValue}" HorizontalAlignment="Left" Margin="15,29,0,0" Name="textBox1" VerticalAlignment="Top" Width="347" />
<Button Click="InFileSelect" Height="24" HorizontalAlignment="Left" Margin="377,30,0,0" Name="inFileDialog" VerticalAlignment="Top" Width="64">输入文件</Button>
</Grid>
<Grid>
<TextBox Height="25" Text = "{Binding Path=TextBoxValue}" HorizontalAlignment="Left" Margin="15,29,0,0" Name="textBox2" VerticalAlignment="Top" Width="347" />
<Button Click="OutFolderSelect" Height="24" HorizontalAlignment="Left" Margin="377,30,0,0" Name="outFileDialog" VerticalAlignment="Top" Width="64">输出文件</Button>
</Grid>
<Button Click="Run" Height="24" HorizontalAlignment="Center" Margin="15" Name="run" VerticalAlignment="Top" Width="64">运行</Button>
</StackPanel>
</Window>
MainWindow.xaml.cs
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace WPFcallC__
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void InFileSelect(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "选择文件";
openFileDialog.Filter = "文本文件|*.txt|WORD文件|*.doc|所有文件|*.*";
openFileDialog.FileName = string.Empty;
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;
//openFileDialog.DefaultExt = "zip";
DialogResult result = openFileDialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.Cancel)
{
return;
}
string fileName = openFileDialog.FileName;
this.textBox1.Text = fileName;
StreamWriter sw=new StreamWriter("C:\\Users\\Raysun\\Documents\\Visual Studio 2010\\Projects\\testIO\\config.txt");
sw.Write(fileName);
sw.Write(" || ");
sw.Close();
}
private void OutFolderSelect(object sender,RoutedEventArgs e)
{
FolderBrowserDialog m_Dialog = new FolderBrowserDialog();
DialogResult result = m_Dialog.ShowDialog();
if (result == System.Windows.Forms.DialogResult.Cancel)
{
return;
}
string m_Dir = m_Dialog.SelectedPath;
this.textBox2.Text = m_Dir;
StreamReader sr = new StreamReader("C:\\Users\\Raysun\\Documents\\Visual Studio 2010\\Projects\\testIO\\config.txt");
string filename = sr.ReadToEnd();
sr.Close();
StreamWriter sw = new StreamWriter("C:\\Users\\Raysun\\Documents\\Visual Studio 2010\\Projects\\testIO\\config.txt");
sw.Write(filename);
sw.Write(m_Dir);
sw.Write(" || ");
sw.Close();
}
public enum ShowWindowCommands : int
{
SW_HIDE = 0,
SW_SHOWNORMAL = 1, //用最近的大小和位置显示,激活
SW_NORMAL = 1,
SW_SHOWMINIMIZED = 2,
SW_SHOWMAXIMIZED = 3,
SW_MAXIMIZE = 3,
SW_SHOWNOACTIVATE = 4,
SW_SHOW = 5,
SW_MINIMIZE = 6,
SW_SHOWMINNOACTIVE = 7,
SW_SHOWNA = 8,
SW_RESTORE = 9,
SW_SHOWDEFAULT = 10,
SW_MAX = 10
}
[DllImport("shell32.dll")]
public static extern IntPtr ShellExecute(
IntPtr hwnd,
string lpszOp,
string lpszFile,
string lpszParams,
string lpszDir,
ShowWindowCommands FsShowCmd
);
private void Run(object sender,RoutedEventArgs e)
{
/* 用ExcuteShell命令打开外部程序 */
//IntPtr ip=ShellExecute(IntPtr.Zero, "open", "C:\\Users\\Raysun\\Documents\\Visual Studio 2010\\Projects\\testIO\\Debug\\testIO.exe", null, null, ShowWindowCommands.SW_SHOWNORMAL);
//System.Windows.MessageBox.Show(ip.ToString());
/* 用Process打开外部程序 */
Process process = new Process();
process.StartInfo.FileName = "C:\\Users\\Raysun\\Documents\\Visual Studio 2010\\Projects\\testIO\\Debug\\testIO.exe";
process.Start();
this.Close();
}
}
}