GTK+學習筆記1

大漠銀狐發表於2004-07-08

1.將使用控制檯建立的GTK+工程專案中的控制檯視窗去掉

      a.)將工程設定中[C/C++]/[Preprocessor definitions]中的“_CONSOLE”改為

                  “_WINDOWS”

      b).將工程設定中[Link]/[Project Option]中的 "/subsystem:console" 改為

               “/subsystem:windows”

      c).將main函式改為

#if defined(G_OS_WIN32) && defined(_WINDOWS)
//Windows平臺沒有windows控制檯視窗
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                    LPSTR     lpCmdLine,
                     int       nCmdShow)
#else 
//windows平臺控制檯或Linux 平臺
int main(int argc, char* argv[])
#endif
{

#if defined(G_OS_WIN32) && defined(_WINDOWS)

//去掉控制檯輸出
       gtk_init(&__argc, &__argv);
 
       /* We don't want a console window.. */
       /*
       *  Any calls to the glib logging functions, result in a call to AllocConsole().
       *  ME and 98 will in such cases produce a console window(2000 not), despite
       *  being built as a windows app rather than a console app.  So we should either
       *  ignore messages by setting dummy log handlers, or redirect messages.
       *  This requires setting handlers for all domains(any lib which uses g_logging).
       */

 
       g_log_set_handler(NULL,(GLogLevelFlags) (G_LOG_LEVEL_MASK  | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION), DrCOM_dummy_log_handler, NULL); 
    g_log_set_handler("Gdk",(GLogLevelFlags)(G_LOG_LEVEL_MASK| G_LOG_FLAG_FATAL  | G_LOG_FLAG_RECURSION), DrCOM_dummy_log_handler, NULL);
 g_log_set_handler("Gtk",(GLogLevelFlags)(G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION),
  DrCOM_dummy_log_handler, NULL);
 g_log_set_handler("GLib",(GLogLevelFlags)(G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION),
  DrCOM_dummy_log_handler, NULL);
 g_log_set_handler("GModule",(GLogLevelFlags)(G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION),
  DrCOM_dummy_log_handler, NULL);
 g_log_set_handler("GLib-GObject",(GLogLevelFlags)(G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION),
  DrCOM_dummy_log_handler, NULL);
 g_log_set_handler("GThread",(GLogLevelFlags)(G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION),
  DrCOM_dummy_log_handler, NULL);
 
  /* g_print also makes a call to AllocConsole(), therefore a handler needs to be
    set here aswell */

    g_set_print_handler( DrCOM_dummy_print );
 
#else
 gtk_init(&argc, &argv);
#endif

       gtk_main();
       return 0;

}

#ifdef G_OS_WIN32
static void DrCOM_dummy_print( const gchar* string )
{
 return;
}

static void DrCOM_dummy_log_handler (const gchar    *domain,
            GLogLevelFlags  flags,
            const gchar    *msg,
            gpointer        user_data)
{
 return;
}
#endif

相關文章