Member Avatar for Johnbonono

I want to know how you would go about adding a GUI to a command line application.

For example, I think uTorrent can be used in the command line? but it also has a regular Windows version.

Recommended Answers

All 9 Replies

well, You cant simply do that. The developer of the program must provide GUI version of program. If you are developer, you have to write a code for GUI design...

I want to know how you would go about adding a GUI to a command line application.

For example, I think uTorrent can be used in the command line? but it also has a regular Windows version.

You can create a 'batch-file', and then double-click on it (it will be executed by the command line). The extension used for this is .bat.
Not that hard to do. You can google a little bit around, and will learn quickly how to do this.
For example, visit this site:
http://rancidtaste.hubpages.com/hub/How-to-create-a-batch-file

For example, visit this site:
http://rancidtaste.hubpages.com/hub/How-to-create-a-batch-file

Or not.

The page you linked is all too brief, misleading in some places, and flat out wrong in others. For example, the writer doesn't even know the purpose of @Echo Off in a batch file.

Type @echo off at the beginning of the file. This line prevents the spaces in the batch file to read during execution time.

Say what?

Here are a few far better resources:
http://www.computerhope.com/batch.htm
http://www.ericphelps.com/batch/
http://users.cybercity.dk/~bse26236/home.html

Perhaps the writer of the page you linked could learn something there...

Cheers :)
PP

Just my cent.
If you only will provide a gui for the application, you can add a form that will be the gui for it.

The form must contain the necessary info an controls to be able to call the right methods or functions you already defined.

Usually, on the main procedure, you will check if the environment is user interactive to show the form, unless a parameter in the call to the application asks for not to use the GUI.

If you must show the form, then show it as dialog, and end the application when the form is closed.

Hope this helps.

Member Avatar for Johnbonono

Just my cent.
If you only will provide a gui for the application, you can add a form that will be the gui for it.

The form must contain the necessary info an controls to be able to call the right methods or functions you already defined.

Usually, on the main procedure, you will check if the environment is user interactive to show the form, unless a parameter in the call to the application asks for not to use the GUI.

If you must show the form, then show it as dialog, and end the application when the form is closed.

Hope this helps.

Okay well I kind of know how to make Windows forms applications if that's what you mean. But most tutorials just show how to add buttons and stuff to the form and call functions with them. I guess you can make class files and include them in the main file, but where would you define functions?

Some of this code is mine and some is not. It is an example of how to build a GUI for a console application. In this case, the application is md5sums.exe, which takes a file or folder name as an argument then calculates a checksum for the file, or all files in the folder. The controls on the form are

txtOutput - displays program output (stdout and stderr)
tstFile - for the user to enter a file or folder name
btnExecute - executes the command

Public Class Form1

    Private WithEvents MyProcess As Process
    Private Delegate Sub UpdateOutputDelegate(ByVal text As String)

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        MyProcess = New Process

    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        MyProcess.Close()

    End Sub

    Private Sub btnExecute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExecute.Click

        If My.Computer.FileSystem.FileExists(txtFile.Text) Or My.Computer.FileSystem.DirectoryExists(txtFile.Text) Then

            With MyProcess.StartInfo
                .FileName = "cmd.exe"
                .Arguments = "/c d:\utils\md5sums.exe " & txtFile.Text
                .UseShellExecute = False
                .CreateNoWindow = True
                .RedirectStandardInput = True
                .RedirectStandardOutput = True
                .RedirectStandardError = True
            End With

            MyProcess.Start()

            MyProcess.BeginErrorReadLine()      'start async read on stderr
            MyProcess.BeginOutputReadLine()     'start async read on stdout

        Else

            MsgBox("file or folder not found", vbOKOnly, txtFile.Text)

        End If

    End Sub

    Private Sub MyProcess_ErrorDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles MyProcess.ErrorDataReceived

        'add stderr text to output display

        UpdateOutput(vbCrLf & "Error: " & e.Data)

    End Sub

    Private Sub MyProcess_OutputDataReceived(ByVal sender As Object, ByVal e As System.Diagnostics.DataReceivedEventArgs) Handles MyProcess.OutputDataReceived

        'add stdout text to output display

        UpdateOutput(vbCrLf & e.Data)

    End Sub

    Private Sub UpdateOutput(ByVal text As String)

        'add text to output display

        If txtOutput.InvokeRequired Then
            Dim myDelegate As New UpdateOutputDelegate(AddressOf UpdateOutput)
            Me.Invoke(myDelegate, text)
        Else
            txtOutput.AppendText(text)
        End If

    End Sub

End Class

Code could be added to parse the text in txtOutput and extract the checksum. Comments on how to improve this code are always appreciated.

Member Avatar for Johnbonono

What about in C++?

What about in C++?

Then that's a question for the C++ forum.

Member Avatar for Johnbonono

Then that's a question for the C++ forum.

But I'm talking about Windows applications specifically.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.