Hello to all,

I'm new to C++, but I have a program in C++ that runs from DOS console. I'd like to execute
the same program adding a simple GUI with a button to select a file and inserting a value in a text
box.

How can I do this?
The C++ code would be the same in Visual C++ and would be needed only to add the GUI or how?

Thanks in advance for any suggestion.

Which version of VC++ do you have? Generally, you start a new project but select Win32 Project instead of Win32 Console Project. You will then get a new project in which you copy your console function(s). You then have to use win32 api functions for the GUI part of the program. Here is a good tutorial.

win32 api is pretty difficult to work with, so new programmers will find it a lot easier to use C# or VB.NET instead.

Hello AncientDragon,

Thanks for your answer and link shared.

I have Visual Studio 2012 Express installed.

So, a C++ program could be used in C# project to add it a GUI? or the C++ code must the translated first to C#?

Thanks again

It depends on the code. Start out by learning C# language then you will know how easy or difficult it will be to convert the code you have.

For some reason the windows form template for c++/cli was included in VS2012 but the references to it were removed. Heres a link to the files redone so that you can start a new windows form project using the .net librairies(for express usr 'vcprojects_WDExpress' instead of 'vcprojects' in the path he gives you), which removes most of the headaches of programming the Windows API's directly. you can still use your c++ code, you may have to add some headers and the std namespace.

On a side note, try to make you subroutines as portable as possible so that you only have to worry about arguments and return types.

Edited 3 Years Ago by tinstaafl

Comments
Thanks for that link :)

Thanks a lot for that link -- worked perfectly :) I had always wondered why Microsoft didn't include it in VC++ 2012 when it was in 2010.

Hello tinstaafl,

Thanks for the answer and link shared. I followed the instructions and copied
the files in correspoding folder, but when I try yo create a new windows form Application and I do click accept button but doesn't work. It seems won't work due to what it says in remarks section :(.

Besides that, do you know which headers to add?

Thanks gain for the help.

It would be worth your time, at this point, to take a step back and decide what you want to do. You have currently chosen to attempt writing GUIs using the Win32 API. Let's go back a step.

On a modern OS, you cannot directly change the value of a pixel on screen. Instead, you ask the OS to do it for you, and if the OS is feeling nice, it will do it. To draw a little box on the screen with things in it, you again ask the OS to do it for you. The Win32 API is how you ask the windows OS to do this sort of thing.

Doing it directly with the Win32 API has advantages and disadvantages. On the advantage side, it's about as low-level as you can reasonably get (although I have seen people write GUIs using assembly), and can give you a great deal of control. On the disadvantage side, it's complicated and (relatively) difficult, and if all you want to do is bang together some standard windows and buttons, it's massively overpowered, and everything you learn is applicable only to windows.

Because of this, people have created "widget toolkits"; these are classes and libraries and functions that sit between you and the OS. You ask the widget toolkit to make you a box, and the widget toolkit handles all the interacting with the OS for you. Generally, it's massively simpler for you. You have less control, but you get a much simpler, easier interface to work with and many of them are cross-platform, so you can take what you learn to other operating systems or even (if you code carefully) have the exact same code build on more than one OS.

For example, here is the simplest way to create a little pop-up box with a message in it using QT, one of the more well-known widget toolkits:

QMessageBox::information(this,"title", "Message");
and here is how I would use QT to create a file browser box for your goal of picking a file (which you'll see is filtered in this example to a few image files), and have the chosen file stored in the variable fileName:

fileName = QFileDialog::getOpenFileName(this,
    tr("Open Image"), "", tr("Image Files (*.png *.jpg *.bmp)"));

So, if your aim is to really get to grips with the Win32 API (which is a very worthy goal), keep going with Win32. If your aim is just to make GUIs, seriously consider looking into the widget toolkits available. They exist for a reason.

Edited 3 Years Ago by Moschops

Hello Moschops,

Thanks for your detailed explanation about the options. I really would like to have that simple GUI to work in more than one OS, that would be great.

I see that QT has its own syntax to create buttons and controls, but the should the C++ code be change? can I call the original C++ code without change anything?

I would like to be able to do click on button to select the file from a folder and when the file is selected, executes the C++ code that I already have.

Many thanks for your suggestions again.

Regards

QT is no more than a set of classes, functions and libraries that you can use. You still code in C++. You just include the right headers and link against the right libraries, just as you already do for your C++ code. The new QT "syntax" is not new syntax; it's still C++ syntax, exactly the same; it's just some new classes and functions.

Thank you Moschops, I'll test it.

2 more questions.

With QT is possible to create a single executable file to install the GUI that contains the C++ code as any setup.exe file?

To begin, initially I'd like to do a simple GUI but "portable", I mean a MyApp.exe file for which only doing double click opens and runs the GUI without needing to install, it´s possible?

I think if I have a more complete and big program have a GUI that needs installation first to run, but if the program is relatively simple, only executes the GUI without installation.

Thanks in advance.

Edited 3 Years Ago by Garidius

Hi Garidius,

Qt will run under virtually any circumstances. That not only includes installed programs or copied-on-the-fly programs, but also other operating systems like linux and mac-os and exotic hardware like microcontrollers, raspberry pie and most mobile phones.

There is a large community around Qt and starting from qt-project.org you will find a host of wikis, discussion forums, support websites and friends to talk about writing great software.

So yes, you can start your program by running it on your machine, than copying a few files to another machine to test it there and when you are up to it, you might make an installer for it. There is even an installation framework for Qt: http://qt-project.org/wiki/Qt-Installer-Framework.

Hello mapsonyllaer,

Thank you for the information. I've reading some of QT from its website but I'm not sure which to install, my computer is 64 bits, with windows7.

MinGW and VS are the compilers? do you know the differences between the openGL and other versions for MingGW and VS?

Qt 5.1.1 for Windows 32-bit (MinGW 4.8, OpenGL, 666 MB) (Info)
Qt 5.1.1 for Windows 32-bit (VS 2010, 505 MB) (Info)
Qt 5.1.1 for Windows 32-bit (VS 2010, OpenGL, 504 MB) (Info)
Qt 5.1.1 for Windows 32-bit (VS 2012, 511 MB) (Info)
Qt 5.1.1 for Windows 64-bit (VS 2012, 525 MB) (Info)
Qt 5.1.1 for Windows 64-bit (VS 2012, OpenGL, 522 MB) (Info)

Edited 3 Years Ago by Garidius

It assume you already have the compiler installed on your computer. Just pick the version of QT that matches the compiler and operating system you have.

This article has been dead for over six months. Start a new discussion instead.