The C++ program reads a file, processes the content and safes the result in another file.The filenames have to be typed in the console. The program works fine, but typing the filenames can be pretty unpleasant if the file and the program are not in the same directory.
Therefore I made the C#/WPF windows desktop program.You can open a file browsing your computer. The program puts the filename in a textbox so you can copy it. The same with the output file.
I start both programs, and when the C++ program asks the filename I use the other program to find the filename,copy it and paste it in the console. The same with the output file. This works fine, but how can I merge the two programs? Here is the second program:
Layout in xaml:
<Window x:Class="Cx_desktop_for_tracebitmap.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Cx_desktop_for_tracebitmap"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel DockPanel.Dock="Top">
<Label Content="Click input file or output file" ></Label>
<Button Click="input_Click" Content="input file" Name="Button_Input" Width="60" Margin="10" HorizontalAlignment="Left"/>
<Button Click="output_Click" Content="output file" Name="Button_Output" Width="60" Margin="10" HorizontalAlignment="Left"/>
<Label Content="Input Filename" DockPanel.Dock="Top" HorizontalAlignment="Center" ></Label>
<TextBox Name="inputname" Text="" Margin="10" Width="500" DockPanel.Dock="Top" HorizontalAlignment="Center"></TextBox>
<Label Content="Output Filename" DockPanel.Dock="Top" HorizontalAlignment="Center" ></Label>
<TextBox Name="outputname" Text="" Margin="10" Width="500" DockPanel.Dock="Top" HorizontalAlignment="Center"></TextBox>
Button Click="Go_Click" Content="GO" Name="Button_Go "Width="60" Height="60" Margin="10" HorizontalAlignment="Left" Visibility="Hidden"/>
</StackPanel>
</Window>
C# in Mainwindow:
using System.Diagnostics;
using System.Windows;
using Microsoft.Win32;
namespace Cx_desktop_for_tracebitmap
{
/// <summary>
/// find filenames
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private string file_in = "";
private string file_out = "";
private bool BoolIn = false;
private bool BoolOut = false;
private void input_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == true)
{
file_in = fix_filename(openFileDialog.FileName);
inputname.Text = file_in;
BoolIn = true;
if (BoolOut) Button_Go.Visibility = Visibility.Visible;
}
}
private void output_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
if (saveFileDialog.ShowDialog() == true)
{
file_out = fix_filename(saveFileDialog.FileName);
outputname.Text = file_out;
BoolOut = true;
if (BoolIn) Button_Go.Visibility = Visibility.Visible;
}
}
private string fix_filename(string fixme)
{
char quote = (char)34;
string fixed_filename = quote+fixme+quote;
return fixed_filename;
}
private void Go_Click(object sender, RoutedEventArgs e)
{
string arguments = file_in + " " + file_out;
Process pBPM_reader; pBPM_reader = new Process();
pBPM_reader.StartInfo.FileName = @"BMP READER versie 4.exe";
pBPM_reader.StartInfo.Arguments = arguments;
pBPM_reader.Start();
}
}
}
I edited the program to show how I did it:
- I added a new button (GO) to start the C++ program. It is hidden at startup and apears when both files are chozen.
- The filenames can contain spaces. They must be wrapped between quotationmarks.That happens in the fix_filename method
- The click on the GO button starts a new Process that runs the C++ program. The Arguments take only one string, and the arguments are separated bij spaces.
Here is the heather and the first line od the C++ file. It shows how to extract the arguments
int main(int argc, char *argv[])
{
std::vector<std::string> argList(argv, argv + argc);
.
.
.
}