⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch-buttonwidget.html

📁 gtk 开发手册和参考文档。 包括gtk glib gdk等
💻 HTML
字号:
<HTML><HEAD><TITLE>The Button Widget</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.49"><LINKREL="HOME"TITLE="GTK+ 1.2 Tutorial"HREF="gtk-tut.html"><LINKREL="PREVIOUS"TITLE="Widgets Without Windows"HREF="sec-widgetswithoutwindows.html"><LINKREL="NEXT"TITLE="Toggle Buttons"HREF="sec-togglebuttons.html"></HEAD></head><body bgcolor="#FFFFFF" marginheight=0 marginwidth=0 width="100%"><table cellspacing=6 border=0 cellpadding=0 width="100%">  <tr>    <td bgcolor="#FFFFFF" valign=top nowrap>    <centeR><A HREF="/"><img src="/images/gtk-logo-rgb.gif" width=107 height=140 border=0></A>      <font face="helvetica,lucidia" color="#000000"><BR><BR><b>GTK+<BR>The GIMP Toolkit</b></center><BR><table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>General</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="/">Introduction</A><BR><A HREF="/screenshots/">Screenshots</A><br><A HREF="/download/">Download</A><br><A HREF="/mailinglists.html">Mailing Lists</A><BR><A HREF="/bindings.html">Language Bindings</A><BR><A HREF="http://gtk.themes.org/">Themes</A><BR><A HREF="/bugs.html">Bug Tracker</A><BR></td></tr></table></td></tr></table><BR>  <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>Documentation</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="/faq/">FAQ</A><br><A HREF="/tutorial/">Tutorial</A><BR><A HREF="/api/">API Reference</A><br><A HREF="/books.html">Published Books</A><BR></td></tr></table></td></tr></table><BR>  <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>Projects</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="http://www.pango.org/">Pango</A><BR><A HREF="http://sources.redhat.com/inti/">Inti</A><BR><A HREF="http://www.gnome.org/">GNOME</A><BR><A HREF="http://user.sgic.fi/~tml/gimp/win32/">GTK+ for Win32</A><br><A HREF="http://people.redhat.com/sopwith/gtkfb/">GtkFB (Framebuffer)</A><br><A HREF="http://www.directfb.org/gtk.xml">GTK+ on DirectFB</A><BR><A HREF="/beos/">GTK+ for BeOS</A></td></tr></table></td></tr></table><BR>  <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B><B>Applications</B></B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="http://www.gimp.org/">GIMP</A><BR><A HREF="http://www.abiword.org/">Abiword</A><BR><A HREF="http://www.lysator.liu.se/~alla/dia/dia.html">Dia</A><BR><A HREF="http://glade.pn.org/">Glade</A><BR><A HREF="http://www.gnucash.org/">GnuCash</A><BR><A HREF="http://www.gnome.org/projects/gnumeric/">Gnumeric</A><BR><BR><A HREF="http://www.gnome.org/applist/">GNOME Software Map</A><br></td></tr></table></td></tr></table><BR>      </td>  <td bgcolor="#ffffff" valign=top width="99%"><font face="lucida,helvetica"><BODYCLASS="CHAPTER"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">GTK+ 1.2 Tutorial</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="sec-widgetswithoutwindows.html">&#60;&#60;&#60; Previous</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="sec-togglebuttons.html">Next &#62;&#62;&#62;</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="CH-BUTTONWIDGET">Chapter 6. The Button Widget</A></H1><DIVCLASS="TOC"><DL><DT><B>Table of Contents</B></DT><DT>6.1. <AHREF="ch-buttonwidget.html#SEC-NORMALBUTTONS">Normal Buttons</A></DT><DT>6.2. <AHREF="sec-togglebuttons.html">Toggle Buttons</A></DT><DT>6.3. <AHREF="sec-checkbuttons.html">Check Buttons</A></DT><DT>6.4. <AHREF="sec-radiobuttons.html">Radio Buttons</A></DT></DL></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="SEC-NORMALBUTTONS">6.1. Normal Buttons</A></H1><P>We've almost seen all there is to see of the button widget. It'spretty simple. There are however two ways to create a button. You canuse the gtk_button_new_with_label() to create a button with a label,or use gtk_button_new() to create a blank button. It's then up to youto pack a label or pixmap into this new button. To do this, create anew box, and then pack your objects into this box using the usualgtk_box_pack_start, and then use gtk_container_add to pack the boxinto the button.</P><P>Here's an example of using gtk_button_new to create a button with apicture and a label in it. I've broken up the code to create a boxfrom the rest so you can use it in your programs. There are furtherexamples of using pixmaps later in the tutorial.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">/* example-start buttons buttons.c */#include &#60;gtk/gtk.h&#62;/* Create a new hbox with an image and a label packed into it * and return the box. */GtkWidget *xpm_label_box( GtkWidget *parent,                          gchar     *xpm_filename,                          gchar     *label_text ){    GtkWidget *box1;    GtkWidget *label;    GtkWidget *pixmapwid;    GdkPixmap *pixmap;    GdkBitmap *mask;    GtkStyle *style;    /* Create box for xpm and label */    box1 = gtk_hbox_new (FALSE, 0);    gtk_container_set_border_width (GTK_CONTAINER (box1), 2);    /* Get the style of the button to get the     * background color. */    style = gtk_widget_get_style(parent);    /* Now on to the xpm stuff */    pixmap = gdk_pixmap_create_from_xpm (parent-&#62;window, &#38;mask,					 &#38;style-&#62;bg[GTK_STATE_NORMAL],					 xpm_filename);    pixmapwid = gtk_pixmap_new (pixmap, mask);    /* Create a label for the button */    label = gtk_label_new (label_text);    /* Pack the pixmap and label into the box */    gtk_box_pack_start (GTK_BOX (box1),			pixmapwid, FALSE, FALSE, 3);    gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 3);    gtk_widget_show(pixmapwid);    gtk_widget_show(label);    return(box1);}/* Our usual callback function */void callback( GtkWidget *widget,               gpointer   data ){    g_print ("Hello again - %s was pressed\n", (char *) data);}int main( int   argc,          char *argv[] ){    /* GtkWidget is the storage type for widgets */    GtkWidget *window;    GtkWidget *button;    GtkWidget *box1;    gtk_init (&#38;argc, &#38;argv);    /* Create a new window */    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);    gtk_window_set_title (GTK_WINDOW (window), "Pixmap'd Buttons!");    /* It's a good idea to do this for all windows. */    gtk_signal_connect (GTK_OBJECT (window), "destroy",			GTK_SIGNAL_FUNC (gtk_exit), NULL);    gtk_signal_connect (GTK_OBJECT (window), "delete_event",			GTK_SIGNAL_FUNC (gtk_exit), NULL);    /* Sets the border width of the window. */    gtk_container_set_border_width (GTK_CONTAINER (window), 10);    gtk_widget_realize(window);    /* Create a new button */    button = gtk_button_new ();    /* Connect the "clicked" signal of the button to our callback */    gtk_signal_connect (GTK_OBJECT (button), "clicked",			GTK_SIGNAL_FUNC (callback), (gpointer) "cool button");    /* This calls our box creating function */    box1 = xpm_label_box(window, "info.xpm", "cool button");    /* Pack and show all our widgets */    gtk_widget_show(box1);    gtk_container_add (GTK_CONTAINER (button), box1);    gtk_widget_show(button);    gtk_container_add (GTK_CONTAINER (window), button);    gtk_widget_show (window);    /* Rest in gtk_main and wait for the fun to begin! */    gtk_main ();    return(0);}/* example-end */</PRE></TD></TR></TABLE><P>The xpm_label_box function could be used to pack xpm's and labels intoany widget that can be a container.</P><P>Notice in <TTCLASS="LITERAL">xpm_label_box</TT> how there is a call to<TTCLASS="LITERAL">gtk_widget_get_style</TT>. Every widget has a "style", consisting offoreground and background colors for a variety of situations, fontselection, and other graphics data relevant to a widget. These stylevalues are defaulted in each widget, and are required by many GDKfunction calls, such as <TTCLASS="LITERAL">gdk_pixmap_create_from_xpm</TT>, which here isgiven the "normal" background color. The style data of widgets maybe customized, using <AHREF="ch-gtkrcfiles.html">GTK's rc files</A>.</P><P>Also notice the call to <TTCLASS="LITERAL">gtk_widget_realize</TT> after setting thewindow's border width. This function uses GDK to create the Xwindows related to the widget. The function is automatically calledwhen you invoke <TTCLASS="LITERAL">gtk_widget_show</TT> for a widget, and so has not beenshown in earlier examples. But the call to<TTCLASS="LITERAL">gdk_pixmap_create_from_xpm</TT> requires that its <TTCLASS="LITERAL">window</TT> argumentrefer to a real X window, so it is necessary to realize the widgetbefore this GDK call.</P><P>The Button widget has the following signals:</P><P></P><UL><LI><P><TTCLASS="LITERAL">pressed</TT> - emitted when pointer button is pressed withinButton widget</P></LI><LI><P><TTCLASS="LITERAL">released</TT> - emitted when pointer button is released withinButton widget</P></LI><LI><P><TTCLASS="LITERAL">clicked</TT> - emitted when pointer button is pressed and thenreleased within Button widget</P></LI><LI><P><TTCLASS="LITERAL">enter</TT> - emitted when pointer enters Button widget</P></LI><LI><P><TTCLASS="LITERAL">leave</TT> - emitted when pointer leaves Button widget</P></LI></UL></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="sec-widgetswithoutwindows.html">&#60;&#60;&#60; Previous</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="gtk-tut.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="sec-togglebuttons.html">Next &#62;&#62;&#62;</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Widgets Without Windows</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top">&nbsp;</TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Toggle Buttons</TD></TR></TABLE></DIV>        </td>    </tr></table>  </td>  </tr></table></body></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -