FreeBASIC 和 GTK Glade 的 glade Button 如何工作?

FreeBASIC and GTK Glade how work the glade Button?

几个月前,我开始使用 GTK 库使用 FreeBASIC 进行编程,这样应用程序就可以在 Windows 和 Linux 上运行,而无需重写代码,但作为初学者我只成功创建了面具,但我不知道如何处理按下按钮。

有人能告诉我我需要做什么来处理按钮按下吗?

    #INCLUDE ONCE "gtk/gtk.bi" 
    gtk_init(@__FB_ARGC__, @__FB_ARGV__)





DIM SHARED AS GtkBuilder PTR XML
DIM SHARED AS GObject PTR _
  mainWindow, Main_CloseButton

XML = gtk_builder_new()

SCOPE
DIM AS GError PTR meld
VAR GUISTR = SADD( _
"<?xml version=""1.0"" encoding=""UTF-8""?>" _
"<!-- Generated with glade 3.38.2 -->" _
"<interface>" _
"<requires lib=""gtk+"" version=""3.24""/>" _
"<object class=""GtkWindow"" id=""mainWindow"">" _
"<property name=""can-focus"">False</property>" _
"<child>" _
"<object class=""GtkLayout"">" _
"<property name=""visible"">True</property>" _
"<property name=""can-focus"">False</property>" _
"<child>" _
"<object class=""GtkButton"" id=""Main_CloseButton"">" _
"<property name=""label"" translatable=""yes"">Close</property>" _
"<property name=""width-request"">80</property>" _
"<property name=""height-request"">30</property>" _
"<property name=""visible"">True</property>" _
"<property name=""can-focus"">True</property>" _
"<property name=""receives-default"">True</property>" _
"</object>" _
"<packing>" _
"<property name=""x"">350</property>" _
"<property name=""y"">200</property>" _
"</packing>" _
"</child>" _
"</object>" _
"</child>" _
"</object>" _
"</interface>" _
!"[=10=]")
IF 0 = gtk_builder_add_from_string(XML, GUISTR, -1, @meld) THEN
  WITH *meld
    ?"Error (GTK-Builder):"
    ?*.message
  END WITH
  g_error_free(meld)
  END 2
END IF
END SCOPE

mainWindow = gtk_builder_get_object(XML, "mainWindow")
Main_CloseButton = gtk_builder_get_object(XML, "Main_CloseButton")





    gtk_builder_connect_signals(XML, 0)
    g_object_unref(XML) 
    gtk_widget_show_all(GTK_WIDGET(mainWindow)) 
    gtk_main()

首先,您需要在 XML 代码中添加一个信号,表示按下按钮。 例如

"<property name=""receives-default"">True</property>" _
"<signal name=""clicked"" handler=""on_Main_CloseButton_clicked"" swapped=""no""/>" _
"</object>" _

通过调用创建一个新文件 (on_Main_CloseButton_clicked.bas) 将其包含在“gtk_builder_connect_signals(XML, 0)”之前 有时在编译过程中如果稍后包含它会给出一些错误(可能是编译器错误或代码太乱)

Main_CloseButton = gtk_builder_get_object(XML, "Main_CloseButton")

#INCLUDE "on_Main_CloseButton_clicked.bas"


    gtk_builder_connect_signals(XML, 0)
    g_object_unref(XML) 
    gtk_widget_show_all(GTK_WIDGET(mainWindow)) 
    gtk_main()

并在新文件中写入一个由处理程序激活的子程序

on_Main_CloseButton_clicked.bas

SUB on_Main_CloseButton_clicked CDECL ALIAS "on_Main_CloseButton_clicked" ( _
  BYVAL widget AS GtkWidget PTR, _
  BYVAL user_data AS gpointer) EXPORT ' Standard-Parameterliste

'Place your code here+
end


END SUB