Compdigitec Labs

« | Home | »

Console-free gtkmm applications in Visual Studio 2008

By admin | December 6, 2009

When compiling Gtkmm programs on Microsoft Windows using Visual C++, a black console box pops up when you run the program. However, setting the application subsystem to Windows will cause the program to complain about WinMain and other problems. Here is a versatile solution from the Gtkmm mailing list to get Gtkmm to work on Windows transparently without breaking interoperability with other platforms, such as Linux, though some modifications were required to make it work.

Insert the following code before your main() and after your header #includes:

#ifdef WIN32
	#include <windows.h>
#endif

using namespace std;
using namespace Gtk;

int main(int argc, char* argv[]);

#ifdef WIN32

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
	// check windows version
         DWORD dwVersion = GetVersion();

         if (!(dwVersion < 0x80000000))
         {
                 MessageBox(0, _T("This application requires Windows 2000/XP or above!"), _T("Fatal Error"), MB_OK);
                 return -1;
         }

         return main(__argc, __argv);
}

#endif

Here is a sample Gtkmm application without console boxes on Windows:

#include <gtkmm.h>
#include <iostream>
#ifdef WIN32
	#include <windows.h>
#endif

using namespace std;
using namespace Gtk;

int main(int argc, char* argv[]);

#ifdef WIN32

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
	// check windows version
         DWORD dwVersion = GetVersion();

         if (!(dwVersion < 0x80000000))
         {
                 MessageBox(0, _T("This application requires Windows 2000/XP or above!"), _T("Fatal Error"), MB_OK);
                 return -1;
         }

         return main(__argc, __argv);
}

#endif

int main(int argc, char* argv[])
{
	Gtk::Main kit(argc, argv);
	Gtk::Window window;
	Gtk::Main::run(window);

	return 0;
}

If you found this article helpful or interesting, please help Compdigitec spread the word. Don’t forget to subscribe to Compdigitec Labs for more useful and interesting articles!

Topics: Windows | 10 Comments »

10 Responses to “Console-free gtkmm applications in Visual Studio 2008”

  1. Rory Says:
    March 20th, 2010 at 11:39

    I am still getting a console window displayed even in the example.

    Visual studio is forcing me to use #include “stdafx.h”. Could this be the differance?

  2. admin Says:
    March 20th, 2010 at 18:18

    @Rory:

    It doesn’t hurt to try commenting out the #include statement. Also, be sure you have selected the Windows subsystem, and not the Console subsystem.

  3. Rory Says:
    March 21st, 2010 at 08:28

    Ok thanks. With the windows subsytem all works well. Cheers

  4. Rory Says:
    March 21st, 2010 at 08:46

    Do you have any experiance using a glade comstructed GUI with visual studio?

  5. PK Says:
    July 9th, 2012 at 01:03

    Kudos!

    This is exactly what I was looking for and works perfectly for me, thank you very much.

  6. marlin model 60 Says:
    October 1st, 2021 at 12:35

    … [Trackback]

    […] Find More to that Topic: compdigitec.com/labs/2009/12/06/console-free-gtkmm-applications-in-visual-studio-2008/ […]

  7. DevOps as a service companies Says:
    October 6th, 2021 at 15:20

    … [Trackback]

    […] There you can find 36897 additional Info on that Topic: compdigitec.com/labs/2009/12/06/console-free-gtkmm-applications-in-visual-studio-2008/ […]

  8. Buy Glo Carts Says:
    October 19th, 2021 at 13:12

    … [Trackback]

    […] Find More on on that Topic: compdigitec.com/labs/2009/12/06/console-free-gtkmm-applications-in-visual-studio-2008/ […]

  9. kardinal stick Says:
    December 3rd, 2021 at 20:08

    … [Trackback]

    […] Find More to that Topic: compdigitec.com/labs/2009/12/06/console-free-gtkmm-applications-in-visual-studio-2008/ […]

  10. สล็อตเว็บตรง Says:
    December 28th, 2021 at 18:25

    … [Trackback]

    […] Read More to that Topic: compdigitec.com/labs/2009/12/06/console-free-gtkmm-applications-in-visual-studio-2008/ […]

Comments