Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a C++ Windows application

Status
Not open for further replies.

thegame8311

Technical User
Jan 6, 2012
133
0
0
US
I am wanting to create application that has alot of forms but works mostly with c++ on the backend. I am using VS 2010/ VS C++ 2010, I can make a form, I can code in c++, but how to i make it a regular application instead of a console application. I can provide more details on my idea if needed
 
Step by step:

1. Start VC++ 2010
2. New Project...
3. Left panel (Installed Templates): CLR
4. Right panel: Windows Forms Application, set project name etc

That's all, now make Windows Forms GUI application.
Welcome to the "MS Forever Pseudo-C++ World;)...

Are you sure that it's a "regular" application?
 
ok, well what I want to know is can I use the regular c++ code that I was taught, for example

Code:
#include <iostream>

using namespace std;

int main()
{
  cout << "hi" << endl;
  return 0;
}

and stuff like that instead of things like this:

Code:
#include "stdafx.h"
#include "Form1.h"

using namespace Test1;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
	// Enabling Windows XP visual effects before any controls are created
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(false); 

	// Create the main window and run it
	Application::Run(gcnew Form1());
	return 0;
}

which is what I current have the the main.cpp file when I created the project

like if I wanted to create a button, but make a function that does exactly what I want it to do, can I just drop the button in the form and call my function from the the button function?
 
You created a Form Project.

You need a win32 console program. Create an empty project. Add a new file to the project and type your code in.
 
No any "forms" if you want code
Code:
#include <iostream>

using namespace std;

int main()
{
  cout << "hi" << endl;
  return 0;
}
On the one hand it's true "regular" console application code, on the other hand, no std::cout object in C++ GUI application with forms...

In other words, you want stratosphere submarine...
 
xwb - So the console will control what the form does or I am missing the pint?

ArkM - so there is no way to combine the 2 samples of code to work together?

The idea for me here is to create a program that stores, updates, teams in a league. i know a database will be required, but I want the interface to be Windows GUI, but the data processesing and etc to be C++. If doing that through the forms in the only I understand but If I can combined the 2(forms, and c++ code) then I can make it more stable and powerful
 
Well, I think that Windows Forms programming in MS C++/CLI is a kind of outrage upon the C++ language ;)

You may program in clear C++ with Ultimate++ (or Qt - but it's not "clear" C++;) environments. For example, download and install Ultimate++ TheIDE (there are lots of example projects in the installation).

No any standard GUI model (therefore no any "forms") in C++ language. However there are many class libraries with GUI classes (see examples above). Remember: std::cout is an implementation of abstract character stream. If you want, you may implement your own C++ class to "print" data in cout << ... style and send this output to your own console-like window (one of all others).
 
So in order to do this I would have to conform to the way that it is done in visual c++ or pick something else?
 
Ultimate++ and Qt environments are compiler-independent. They work with Visual C++ and some other C++ compilers for Windows, Linux and MacOS. So you may write portable C++ codes with (or without) GUI. Use proper GUI C++ libraries - that's all.

All GUI applications in typical OSes are event-driven. Must be external GUI events generator (OS). The program must catch and process (in a good time) GUI events (repaint window, for example). No any external (asynchronous) events in console applications - there is a single control flow which runs through main() function. That's why you need some library to incapsulate these drastic differences between console and GUI undeground program logics.
 
I'm not sure why you think you can't use standard C++ in a Windows application. If you choose a Win32 project all the pieces you speak of are there. They just have a slightly different look in Windows than say, Linux. But the template takes care of setting up the main function for you.

You'll get a _tWinMain function which is your application entry point, and sets up some other miscellaneous variables. Once it does that, it then the begins the message pump

Code:
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
    if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

This basically pulls the messages off the message queue, and "pumps" the message into your application. And all your messages are handled by the callback function WndProc.

You simply handle the BN_CLICK, WM_COMMAND, WM_PAINT, etc. messages in WndProc.

Also, this sample code was taken from Visual studio 2010, and is unmanaged c++ code. The sample you are showing appears to be managed code.

The real issue with a Win32 application, which you'll soon find out, is that the WndProc procedure turns in to a HUGE switch statement as you handle more and more messages/notifications...
 
ok Vachaun22 but say when the button click happens i want it to run a cpp file that has this:

Code:
cout << "hi" << endl;

everytime I try to compile that it fails and gives that line as an error.
 
ok one last question for this thread, When i make a form, the form design is in the header file(.h), is there a way that I can get the form to do the action from a cpp file instead?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top