看到的文件都是用MinGW比較多。這邊試試用 Microsoft Visual Studio,Visual studio 的 IDE 還不錯。
- gtk win32 download gtk-win32 bundle,解開到某個目錄,例如c:\gtk
- 修改 C:\autoexec.bat,用控制台 control panel->系統(system)->進階(advanced)->環境變數(environ variable)也可以
set path=%path%;c:\gtk\bin |
- 修改工具(Tool)->選項(Options)->專案&方案(Project&Solution)->VC++目錄->include檔案->
C:\gtk\include\gtk-2.0
C:\gtk\lib\glib-2.0\include
... |
- 與上面類似的地方 程式庫(library)
- project 屬性 ->linker->輸入(input)->其它相依性(dependency) gtk-win32-2.0.lib glib-2.0.lib gobject-2.0.lib
- 如果 linker->subsystem 直接選 Windows (/SUBSYSTEM:WINDOWS),會導致沒有 argc, argv,這邊可以自己處理。如果沒有argument 的需求,可以直接用 gtk_init (0, NULL),GUI 的程式很少有 argument 的需求。直接用 msvcrt.dll 好像也可以,沒試過,這邊就不寫了。
#include "stdafx.h"
#include "tgtk.h"
#include <gtk/gtk.h>
#include <windows.h>
static void hello( GtkWidget *widget,
gpointer data )
{
g_print ("Hello World\n");
}
static gboolean delete_event( GtkWidget *widget,
GdkEvent *event,
gpointer data )
{
g_print ("delete event occurred\n");
return TRUE;
}
static void destroy( GtkWidget *widget,
gpointer data )
{
gtk_main_quit ();
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
#if 0
//Why there is CommandLineToArgvW but not CommandLineToArgvA in MSDN
// parse lpCmdLine into argc,argv, buggy
int argc;
char *argv_[32];
char *token;
argc = 1;
argv_[0]="fake exec name";
token = strtok(lpCmdLine, " ");
while (token) {
argv_[ argc ++ ] = token;
token = strtok(NULL, " ");
}
char **argv=argv_;
gtk_init (&argc, &argv);
#else
gtk_init (0, NULL);
#endif
GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_realize(window);
g_signal_connect (G_OBJECT (window), "delete_event",
G_CALLBACK (delete_event), NULL);
g_signal_connect (G_OBJECT (window), "destroy",
G_CALLBACK (destroy), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
#if 1
GtkWidget *button = gtk_button_new_with_label ("Hello World");
#else
GtkWidget *button = gtk_button_new_from_stock(GTK_STOCK_OK);
#endif
g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (hello), NULL);
g_signal_connect_swapped (G_OBJECT (button), "clicked",
G_CALLBACK (gtk_widget_destroy),
G_OBJECT (window));
gtk_container_add (GTK_CONTAINER (window), button);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}