【问题标题】:Position Gtk# Dialog in the center of a Gtk window将 Gtk# Dialog 定位在 Gtk 窗口的中心
【发布时间】:2015-08-03 06:56:26
【问题描述】:

我试图将 GTK# 对话框放置在窗口的中心,并且我希望在出现此对话框时使父窗口不可编辑。 我尝试设置parent 属性和对话框并将其位置设置为center on parent。(父窗口位置始终居中,父窗口居中但其中一部分位于任务栏下方)

我做错了什么?

更新:

我尝试设置 transcientfor 属性。

        mywin d = new mywin();
        d.Parent = this;
        d.WindowPosition = WindowPosition.CenterOnParent; 
        d.TransientFor = this; 
        d.WidthRequest = 360;
        d.HeightRequest =260;
        d.Resizable = false; 
        d.Show (); 

当我这样做时,我在应用程序输出中也得到了这个错误

(myapp:5844): Gtk-WARNING **: Can't set a parent on a toplevel widget

(myapp:5844): Gdk-CRITICAL **: inner_clipboard_window_procedure: assertion 'success' failed

更新: 从父级调用对话框

protected void OnButton7Clicked (object sender, EventArgs e)
    {
            var mydiag = new mydiag( this );

            if ( ( (Gtk.ResponseType) mydiag.Run() ) == Gtk.ResponseType.Ok ) {
                // do whatever here...
            }
     }

对话框代码

using System;

namespace myproj
{
    public partial class mydiag : Gtk.Dialog
    {
        public mydiag (Gtk.Window parent)
        {
            this.Build ();
            this.Title = parent.Title + " more info";
            this.Icon = parent.Icon;
            this.Parent = parent;
            this.TransientFor = parent;
            this.SetPosition( Gtk.WindowPosition.CenterOnParent );
            this.ShowAll();
        }
    }
}

【问题讨论】:

  • 您是否将对话框设置为transient_for 父级? (如果您将父级作为 parent 参数传递给标准 GtkDialog 构造函数之一,则可以。)
  • @andlabs 我也试过了。请查看更新。
  • Parent 的属性与GtkWidget 完全不同;它与GtkContainer 有关。摆脱那条线。您使用的是什么操作系统和窗口管理器?
  • @andlabs 我现在正在使用 Windows。但是该应用程序是为 Mac 设计的,这对如何管理窗口有任何影响。我不知道我正在使用什么窗口管理器。我只是使用 Xamarin Studio (单声道+Gtk#)

标签: c# .net mono gtk gtk#


【解决方案1】:

问题在这里报告:

(myapp:5844): Gtk-WARNING **: Can't set a parent on a toplevel widget

您的 mywin 类不是 Gtk.Dialog,而可能是 Gtk.Window。你应该这样创建你的主窗口:

namespace Whatever {
    public class MainWindow: Gtk.Window {
        public MainWindow()
            : base( Gtk.WindowType.Toplevel )
        {
            this.Title = "Gtk# App";
            this.Build();
        }

        private void Build() {
            // create your widgets
        }

        // more things...
}

现在假设您需要为应用中的某些功能打开一个对话框。

namespace Whatever {
    public class MainWindow: Gtk.Window {
        // ...more things
        private void OnWhatever() {
            var dlg = new DlgMoreInfo( this );

            if ( ( (Gtk.ResponseType) dlg.Run() ) == Gtk.ResponseType.Ok ) {
                // do whatever here...
            }

            dlg.Destroy();
        }
    }
}

最后,你需要创建你的对话框DlgMoreInfo居中,等等

namespace Whatever {
    public class DlgMoreInfo : Gtk.Dialog {
        public DlgMoreInfo(Gtk.Window parent) {
            this.Build();

            this.Title = parent.Title + " more info";
            this.Icon = parent.Icon;
            this.TransientFor = parent;
            this.SetPosition( Gtk.WindowPosition.CenterOnParent );
            this.ShowAll();
        }

        private void Build() {
            // Create widgets here...
            // Buttons
            this.AddButton( Gtk.Stock.Cancel, Gtk.ResponseType.Cancel );
            this.AddButton( Gtk.Stock.Ok, Gtk.ResponseType.Ok );
            this.DefaultResponse = Gtk.ResponseType.Ok;
        }
    }
}

您只能创建顶级窗口的子对话框;一个窗口永远不能是另一个窗口的子窗口。

请注意,此代码不使用 Xamarin Studio 的设计器。如果您使用设计器,则在 ShowAll() 之前调用 HideAll(),如果您需要使用小部件,或者将调用移至 Build() 在调用 SetPosition() 之后。

希望这会有所帮助。

【讨论】:

  • 谢谢..它确实是一个窗口。实际上我之前有一个对话框..我替换了它所以很困惑。现在我已经将类更改为对话框>“公共部分类 mywin:Gtk.Dialog”
  • 我猜你正在使用 Xamarin Studio。以正常方式创建一个新对话框,然后复制/粘贴组件。无论如何,我的建议是摆脱设计师。
  • 是的,我正在使用 Xamarin Studio。我如何复制粘贴组件?正常复印能用吗?当我将类更改为对话框时,对话框中没有小部件只是空的(执行时)。此外,设计师将一些按钮作为默认按钮放在新对话框中,我无法摆脱那些..所以我切换到窗口。有吗一种我可以使窗口像对话框一样的方法。我只希望它居中。
  • 正常复制即可。抱歉,我不知道将窗口置于其父窗口中心的方法,因为窗口没有这样的概念。我只能再次推荐你摆脱设计师。手动编码布局,如果您不添加它们,对话框中不会有按钮。看看这个:zetcode.com/gui/gtksharp
  • 我尝试在新创建的对话框中添加您提供的代码。当对话框显示时,主窗口不可编辑。但对话框未以父级为中心。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-28
  • 1970-01-01
相关资源
最近更新 更多