cht電腦資訊Windows
adm Find login register

在 Visual Studio C/C++ 用 gtk+2.0 ||| Developing gtk+2.0 applications on Visual Studio

eliu

joined: 2007-08-09
posted: 11480
promoted: 617
bookmarked: 187
新竹, 台灣
1subject: 在 Visual Studio C/C++ 用 gtk+2.0 ||| Developing gtk+2.0 applications on Visual StudioPromote 2 Bookmark 12009-06-30quote  

看到的文件都是用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)
    C:\gtk\lib
  • 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;
}

edited: 12
eliu

joined: 2007-08-09
posted: 11480
promoted: 617
bookmarked: 187
新竹, 台灣
2subject: Promote 0 Bookmark 02009-03-26quote  

source code 加入

#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")

就可以用 main 的方式,而且沒有/關掉/without console window

int main(int argc, char **argv)
{
    gtk_init (&argc, &argv);

edited: 3
huki
joined: 2008-01-14
posted: 7
promoted: 0
bookmarked: 0
台灣
3subject: Promote 0 Bookmark 02009-09-15quote  

很棒~~~~

現在我也自己試著把我的看圖程式port到windows上~~~

不過問題真多~~~多了glade好像就差蠻多的~~~一直build不過~~~

eliu

joined: 2007-08-09
posted: 11480
promoted: 617
bookmarked: 187
新竹, 台灣
4subject: Promote 0 Bookmark 02009-09-15quote  

很久以前用過 glade,那時候覺得很爛,連 undo都沒有。現在不知道怎麼樣了。

或者改成直接用 container/vbox/hbox,好像也不會太難。

edited: 1
huki
joined: 2008-01-14
posted: 7
promoted: 0
bookmarked: 0
台灣
5subject: Promote 0 Bookmark 02009-09-15quote  

這個我有想過~~~~應該是不難啦!~~~

但現在glade其實我覺得還不錯了~~~

拉ui又方便~~~~~

只是在win32下做~~~還真多問題

CC: Linux
cht電腦資訊Windows
adm Find login register
views:19104