在Mono/GTK中创建可关闭选项卡

在Mono/GTK中创建可关闭选项卡,mono,gtk#,Mono,Gtk#,我正在尝试创建新的GTK笔记本选项卡,其中包含名称(作为标签)和关闭按钮(作为带有图像的按钮),代码如下: Label headerLabel = new Label(); headerLabel.Text = "Header"; HBox headerBox = new HBox(); Button closeBtn = new Button(); Image closeImg = new Image(Stock.Close, IconSize.Menu); closeBtn.Image =

我正在尝试创建新的GTK笔记本选项卡,其中包含名称(作为标签)和关闭按钮(作为带有图像的按钮),代码如下:

Label headerLabel = new Label();
headerLabel.Text = "Header";
HBox headerBox = new HBox();
Button closeBtn = new Button();
Image closeImg = new Image(Stock.Close, IconSize.Menu);

closeBtn.Image = closeImg;
closeBtn.Relief = ReliefStyle.None;

headerBox.Add(headerLabel);
headerBox.Add(closeBtn);
headerBox.ShowAll();

MyNotebook.AppendPage(childWidget, headerBox);
这似乎工作得很好;但是,按钮的大小大约是所需大小的1.5-2倍,因此按钮内的图像周围有大量额外空间。看了之后,我现在发现罪魁祸首是GTK按钮的“内边框”样式属性,但是(对于GTK来说是新的)我似乎不知道如何覆盖它的值

有没有什么方法是我不知道的?我对不使用按钮/图像组合没有任何保留意见,因此欢迎任何更明显的建议


注意:我在链接的问题中看到了使用EventBox的建议,但我无法将Relief和mouseover效果添加到该小部件。

使用Gtk.box.PackStart/PackEnd方法而不是通用的Gtk.Container.add方法向box添加项目。PackStart/PackEnd将允许您控制如何分配子部件空间:

headerBox.PackStart (headerLabel, true, true, 0);
headerBox.PackEnd (closeBtn, false, false, 0);
添加以下内容:

RcStyle rcStyle = new RcStyle ();
rcStyle.Xthickness = 0;
rcStyle.Ythickness = 0;
closeBtn.ModifyStyle (rcStyle);

你很幸运。我昨天刚刚做了完全相同的事情,幸运的是,我可以给你一些代码。诀窍是创建一个自定义选项卡小部件

public class MultiTab : Gtk.Box
{
    public Gtk.Label Caption;
    Gtk.Image img = new Gtk.Image(Platform.IMG + "tab_close.ico");
    public Gtk.ToolButton Close;
    public Gtk.Notebook _parent;
    
    public MultiTab ( string name )
    {
        CreateUI(name);
    }
    
    public MultiTab(string name, Gtk.Notebook parent)
    {
        _parent = parent;
        CreateUI(name);
        CreateHandlers();
    }
    
    void CreateUI(string name)
    {
        Caption = new Gtk.Label(name);
        Close = new Gtk.ToolButton(img,"");
        PackStart( Caption );
        PackStart( Close );
        ShowAll();
        Close.Hide();
    }
    
    void CreateHandlers()
    {
        Close.Clicked +=  delegate {
            _parent.RemovePage(_parent.CurrentPage);
        };
    }
    
    public bool Active;
    
}    
接下来,您只需在Gtk中使用此小部件(或您创建的类似小部件)。笔记本如下:

   MyNoteBook.AppendPage(new <YourPage>(), new MultiTab("<your caption>",this));
MyNoteBook.AppendPage(new(),new MultiTab(“,this));
你完成了

以下是一个屏幕截图:


谢谢您的帮助,但是使用PackStart/PackEnd似乎没有任何改变;该按钮占用的空间与我使用Add时相同。从我所读到的内容来看,我认为问题与GTK按钮的默认内边框大小有关。你为什么不看看一些编写良好的GTK应用程序的源代码,这些应用程序具有此类按钮?Gedit就是一个很好的例子。