GTK#图像按钮在运行时不显示图像 [英] GTK# Image Buttons not showing Images when Running

查看:224
本文介绍了GTK#图像按钮在运行时不显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我尝试在GTK#(Xamarin Studio)中使用图像按钮。我将图像设置为按钮,在UI Builder中图像即将出现。

但是,当我运行程序中没有按钮图像



我在IDE的不同版本以及不同的平台上(Mac和Windows)尝试了这个功能



请帮助

更新:
MainWindow.cs

 使用系统; 
使用Gtk;

public partial class MainWindow:Gtk.Window
{
public MainWindow():base(Gtk.WindowType.Toplevel)
{
Build() ;


protected void OnDeleteEvent(object sender,DeleteEventArgs a)
{
Application.Quit();
a.RetVal = true;


gtk-gui中文件夹我可以找到这些文件



generated.cs

  //此文件由GUI设计器生成。不要修改。 
命名空间Stetic
{
内部类Gui
{
private static bool initialized;

内部静态初始化(Gtk.Widget iconRenderer)
{
if((Stetic.Gui.initialized == false)){
Stetic.Gui.initialized = true;



$ b内部类IconLoader
{
public static Gdk.Pixbuf LoadIcon(Gtk.Widget Widget,string name, Gtk.IconSize大小)
{
Gdk.Pixbuf res = widget.RenderIcon(name,size,null);
if((res!= null)){
return res;
} else {
int sz;
int sy;
global :: Gtk.Icon.SizeLookup(size,out sz,out sy);
尝试{
return Gtk.IconTheme.Default.LoadIcon(name,sz,0);
返回Stetic.IconLoader.LoadIcon(widget,gtk-missing-image)
catch图片大小);
} else {
Gdk.Pixmap pmap = new Gdk.Pixmap(Gdk.Screen.Default.RootWindow,sz,sz);
Gdk.GC gc = new Gdk.GC(pmap);
gc.RgbFgColor = new Gdk.Color(255,255,255);
pmap.DrawRectangle(gc,true,0,0,sz,sz);
gc.RgbFgColor = new Gdk.Color(0,0,0);
pmap.DrawRectangle(gc,false,0,0,(sz - 1),(sz - 1));
gc.SetLineAttributes(3,Gdk.LineStyle.Solid,Gdk.CapStyle.Round,Gdk.JoinStyle.Round);
gc.RgbFgColor = new Gdk.Color(255,0,0); (sz - 4),(sz / 4),((sz - 1) - (sz / 4)),((sz - 1) - (sz / 4))) ; (sz - 1) - (sz / 4)),(sz / 4),(sz / 4),((sz - 1) - (sz / 4)))bb.pmap.DrawLine ;
return Gdk.Pixbuf.FromDrawable(pmap,pmap.Colormap,0,0,0,0,sz,sz);
}
}
}
}
}

内部类ActionGroups
{
public static Gtk.ActionGroup GetActionGroup(System.Type类型)
{
return Stetic.ActionGroups.GetActionGroup(type.FullName);
}

public static Gtk.ActionGroup GetActionGroup(string name)
{
return null;
}
}
}

Mainwindow.cs

  //这个文件已经由GUI设计器生成。不要修改。 

public partial class MainWindow
{
private global :: Gtk.Fixed fixed1;

全球私人:: Gtk.Button button1;

protected void Build()
{
global :: Stetic.Gui.Initialize(this);
// Widget MainWindow
this.Name =MainWindow;
this.Title = global :: Mono.Unix.Catalog.GetString(MainWindow);
this.WindowPosition =((global :: Gtk.WindowPosition)(4));
//容器子元素MainWindow.Gtk.Container + ContainerChild
this.fixed1 = new global :: Gtk.Fixed();
this.fixed1.Name =fixed1;
this.fixed1.HasWindow = false;
//容器小孩fixed1.Gtk.Fixed + FixedChild
this.button1 = new global :: Gtk.Button();
this.button1.CanFocus = true;
this.button1.Name =button1;
this.button1.UseUnderline = true;
this.button1.Label = global :: Mono.Unix.Catalog.GetString(GtkButton);
global :: Gtk.Image w1 = new global :: Gtk.Image();
w1.Pixbuf = global :: Stetic.IconLoader.LoadIcon(this,gtk-delete,global :: Gtk.IconSize.Menu);
this.button1.Image = w1;
this.fixed1.Add(this.button1);
global :: Gtk.Fixed.FixedChild w2 =((global :: Gtk.Fixed.FixedChild)(this.fixed1 [this.button1]));
w2.X = 153;
w2.Y = 137;
this.Add(this.fixed1); ((this.Child!= null)){
this.Child.ShowAll();
}
this.DefaultWidth = 400;
this.DefaultHeight = 300;
this.Show();
this.DeleteEvent + = new global :: Gtk.DeleteEventHandler(this.OnDeleteEvent);



解决方案

据我所知,代码没有问题。这与Gtk主题有关。

根据主题的设置,您可能会看到图标。顺便说一句,Xamarin使用的主题不同于默认使用的主题,所以从Xamarin Studio(F5)或命令行运行应用程序可能会得到不同的结果。



如果我没有记错,在Windows下,有一个默认的Gtk主题和一个Windows主题。有一个工具来切换主题,但不幸的是,我可以得到它。



编辑:



以下是如何让主题运行:



以下是一个可让您选择主题的小软件: http://sourceforge.net/projects/tumagcc/files/gtk2_prefs.exe/download



您还必须在网络上的某个地方下载Gtk主题(我试过的第一个主题在窗口上显得很丑并且无法使用,所以您可能需要在此尝试一下)。将主题(通常是tar.gz文件)解压到共享/主题下的Gtk安装。确保目录结构,一旦提取的主题如下所示:

  {Gtk} /share/themes/MyFantasticTheme/Gtk-2.0 

然后以管理员身份启动小软件,在列表中选择您的主题,然后单击确定。现在从Xamarin Studio外部运行你的应用程序。



编辑#2:



经过一番捣鼓之后,我发现了一个在按钮中显示图标和标签的方式:

  Application.Init(); 
Gtk.Settings.Default.ThemeName =Theme / gtk-2.0 / gtkrc;
Gtk.Rc.Parse(./Theme/gtk-2.0/gtkrc);

我的主题由 Theme / gtk-2.0 / gtkrc (有关gtkrc文件的文档很容易找到,因为它不是Gtk#特定的):

  gtk-button-images = 1 

我认为会发生的事情是,应用程序启动,并且此默认主题不能被重写。为什么这样?也许是因为默认的主题运行在一个特定的Gtk引擎上,它不允许按钮中的图标。通过覆盖默认主题,我相信你可以将Gtk切换到默认引擎。

Im trying to use Image Buttons in GTK# (Xamarin Studio).I set the Image to the button and in the UI Builder the Image is coming up.

But when i run the program there is no image in the button

I tried this in Different versions of the IDE and on different platforms(Mac and Windows)

Please help

Update: MainWindow.cs

using System;
using Gtk;

public partial class MainWindow: Gtk.Window
{
    public MainWindow () : base (Gtk.WindowType.Toplevel)
    {
        Build ();
    }

    protected void OnDeleteEvent (object sender, DeleteEventArgs a)
    {
        Application.Quit ();
        a.RetVal = true;
    }
}

Within gtk-gui folder i can find these files

generated.cs

// This file has been generated by the GUI designer. Do not modify.
namespace Stetic
{
    internal class Gui
    {
        private static bool initialized;

        internal static void Initialize (Gtk.Widget iconRenderer)
        {
            if ((Stetic.Gui.initialized == false)) {
                Stetic.Gui.initialized = true;
            }
        }
    }

    internal class IconLoader
    {
        public static Gdk.Pixbuf LoadIcon (Gtk.Widget widget, string name, Gtk.IconSize size)
        {
            Gdk.Pixbuf res = widget.RenderIcon (name, size, null);
            if ((res != null)) {
                return res;
            } else {
                int sz;
                int sy;
                global::Gtk.Icon.SizeLookup (size, out  sz, out  sy);
                try {
                    return Gtk.IconTheme.Default.LoadIcon (name, sz, 0);
                } catch (System.Exception) {
                    if ((name != "gtk-missing-image")) {
                        return Stetic.IconLoader.LoadIcon (widget, "gtk-missing-image", size);
                    } else {
                        Gdk.Pixmap pmap = new Gdk.Pixmap (Gdk.Screen.Default.RootWindow, sz, sz);
                        Gdk.GC gc = new Gdk.GC (pmap);
                        gc.RgbFgColor = new Gdk.Color (255, 255, 255);
                        pmap.DrawRectangle (gc, true, 0, 0, sz, sz);
                        gc.RgbFgColor = new Gdk.Color (0, 0, 0);
                        pmap.DrawRectangle (gc, false, 0, 0, (sz - 1), (sz - 1));
                        gc.SetLineAttributes (3, Gdk.LineStyle.Solid, Gdk.CapStyle.Round, Gdk.JoinStyle.Round);
                        gc.RgbFgColor = new Gdk.Color (255, 0, 0);
                        pmap.DrawLine (gc, (sz / 4), (sz / 4), ((sz - 1) - (sz / 4)), ((sz - 1) - (sz / 4)));
                        pmap.DrawLine (gc, ((sz - 1) - (sz / 4)), (sz / 4), (sz / 4), ((sz - 1) - (sz / 4)));
                        return Gdk.Pixbuf.FromDrawable (pmap, pmap.Colormap, 0, 0, 0, 0, sz, sz);
                    }
                }
            }
        }
    }

    internal class ActionGroups
    {
        public static Gtk.ActionGroup GetActionGroup (System.Type type)
        {
            return Stetic.ActionGroups.GetActionGroup (type.FullName);
        }

        public static Gtk.ActionGroup GetActionGroup (string name)
        {
            return null;
        }
    }
}

Mainwindow.cs

// This file has been generated by the GUI designer. Do not modify.

public partial class MainWindow
{
    private global::Gtk.Fixed fixed1;

    private global::Gtk.Button button1;

    protected virtual void Build ()
    {
        global::Stetic.Gui.Initialize (this);
        // Widget MainWindow
        this.Name = "MainWindow";
        this.Title = global::Mono.Unix.Catalog.GetString ("MainWindow");
        this.WindowPosition = ((global::Gtk.WindowPosition)(4));
        // Container child MainWindow.Gtk.Container+ContainerChild
        this.fixed1 = new global::Gtk.Fixed ();
        this.fixed1.Name = "fixed1";
        this.fixed1.HasWindow = false;
        // Container child fixed1.Gtk.Fixed+FixedChild
        this.button1 = new global::Gtk.Button ();
        this.button1.CanFocus = true;
        this.button1.Name = "button1";
        this.button1.UseUnderline = true;
        this.button1.Label = global::Mono.Unix.Catalog.GetString ("GtkButton");
        global::Gtk.Image w1 = new global::Gtk.Image ();
        w1.Pixbuf = global::Stetic.IconLoader.LoadIcon (this, "gtk-delete", global::Gtk.IconSize.Menu);
        this.button1.Image = w1;
        this.fixed1.Add (this.button1);
        global::Gtk.Fixed.FixedChild w2 = ((global::Gtk.Fixed.FixedChild)(this.fixed1 [this.button1]));
        w2.X = 153;
        w2.Y = 137;
        this.Add (this.fixed1);
        if ((this.Child != null)) {
            this.Child.ShowAll ();
        }
        this.DefaultWidth = 400;
        this.DefaultHeight = 300;
        this.Show ();
        this.DeleteEvent += new global::Gtk.DeleteEventHandler (this.OnDeleteEvent);
    }
}

解决方案

As far as I know, there's nothing wrong with the code. This is related to how Gtk theming.

Depending on how your themes are set, you might see the icon or not. By the way, the theme used by Xamarin is different from the one used by default, so running your application from within Xamarin Studio (F5) or from the command line might give different results.

If I remember well, under windows, there's a default Gtk theme and a Windows Theme. There's a tool to switch themes, but unfortunately, I can get my hands on it.

EDIT :

Here's how to get the themes running :

Here is a little software that will let you select a theme: http://sourceforge.net/projects/tumagcc/files/gtk2_prefs.exe/download

You'll also have to download a Gtk theme somewhere on the web (the first one I tried turned out to be ugly and unusable on windows, so you'll probably have to experiment a little bit here). Extract the theme (usually a tar.gz file) to your Gtk installation under share/themes. Make sure the directory structure, once the theme extracted is like :

{Gtk}/share/themes/MyFantasticTheme/Gtk-2.0

Then launch the little software as Administrator, select your theme in the list and click OK. Now run your app from outside Xamarin Studio.

EDIT #2:

After some fiddling I found out a way that displays icons and labels within buttons:

Application.Init ();
Gtk.Settings.Default.ThemeName = "Theme/gtk-2.0/gtkrc";
Gtk.Rc.Parse ("./Theme/gtk-2.0/gtkrc");

My theme consists of a single line inside Theme/gtk-2.0/gtkrc (documentation about the gtkrc file can be easily found, since it's not Gtk# specific):

gtk-button-images = 1

What I think happens is that a default theme is somehow set when your application starts, and this default theme can't be overriden. Why is it so? Maybe because the default theme runs with a specific Gtk engine for windows that doesn't allow icons in buttons. By overriding the default theme, I believe that you switch Gtk to the default engine.

这篇关于GTK#图像按钮在运行时不显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆