在没有窗口管理器的情况下运行Gtk2应用程序时,光标不可见

本文关键字:光标 应用程序 Gtk2 窗口管理器 运行 情况下 | 更新日期: 2023-09-27 18:06:46

我正在启动一个gtk#应用程序,旨在在我的ArchLinux live CD上创建GUI配置器工具。

我已经将应用程序的所有先决条件融入到live CD中,但是我遇到了一个问题,如果我在没有运行窗口管理器的情况下启动应用程序,它将有一个看不见的光标图标。我可以移动鼠标,看到悬停在按钮上的效果,点击它们-只是有一个看不见的光标。

应用程序非常简单,因为我刚刚开始:

using System;
using Gtk;
namespace StoneInstallerWizard
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            // Initialize GTK.
            Application.Init();
            // Create a window.
            Window window = new Window("StoneInstallerWizard");
            // Attach closing part to the delete event.
            window.DeleteEvent += delegate
            {
                Application.Quit();
            };
            // Window settings.
            window.WindowPosition = WindowPosition.Center;
            window.Resizable = false;
            window.TypeHint = Gdk.WindowTypeHint.Dialog;
            // HorizontalBox.
            var hbox = new HBox();
            window.Add(hbox);
            // Close button.
            var closeBtn = new Button(Stock.Close);
            closeBtn.Clicked += delegate
            {
                Application.Quit();
            };
            hbox.Add(closeBtn);
            // Next button.
            var nextBtn = new Button(Stock.Apply);
            nextBtn.Clicked += delegate
            {
                var message = new MessageDialog(window, DialogFlags.Modal | DialogFlags.DestroyWithParent, MessageType.Info, ButtonsType.Ok, "And so we continue...");
                ResponseType messageResponse = (ResponseType) message.Run();
                // Clicked or closed, doesn't matter.
                if (messageResponse == ResponseType.Ok || messageResponse == ResponseType.DeleteEvent)
                {
                    message.Destroy();
                    Application.Quit();
                }
            };
            hbox.Add(nextBtn);
            // Show the window and start the app.
            window.ShowAll();
            Application.Run();
        }
    }
}

如果我要创建一个默认派生的~/.xinitrc,并将exec部分替换为我的应用程序,然后继续使用startx -当它启动时,但光标是隐藏的。

现在,如果我使用默认的xinitrc配置(在Arch上它带有twm,三个xterms和xclock)并通过那里运行应用程序,我将有一个可见的光标占用默认样式。

如果我要运行cinnamon-session,我看到我的光标,并且(我没有运行应用程序)我假设光标将持续到应用程序。

Arch附带一个默认的Gtk2配置文件,它使用Adwaita主题,我也安装了这个主题。

我假设我必须为我的Gtk应用程序设置一个游标,所以我确实尝试添加window.GdkWindow.Cursor = new Gdk.Cursor(Gdk.CursorType.Arrow);,但显然,最终在(WM和独立)与NullExceptionError作为GdkWindow变量未设置。

我的光标在剥离的X设置中消失可能是什么问题?

注:所有未提及的配置文件都保持默认值。

更新

如果我添加TextView到我的应用程序,运行它并悬停在TextView上,它将显示文本光标图标I,如果我悬停,它将恢复到默认的X交叉图标,但持续

在没有窗口管理器的情况下运行Gtk2应用程序时,光标不可见

在发现在TextView中光标变得可见后,我确信问题发生在我的代码中的某个地方。

window.GdkWindow被取消的问题使我认为,也许它没有设置,直到应用程序已经Run()

在我记忆的某个地方,突然出现了一个问题,我在SO上发现了一个关于事件的问题,一旦Widget被曝光就会被调用-开始寻找它们。

一开始,我选择了window.Shown event:

window.Shown += delegate
{
    window.GdkWindow.Cursor = new Gdk.Cursor(Gdk.CursorType.Arrow);
};

瞧!

然后我继续切换到更合理的window.Realized事件,这也证明是值得的。

谜底解开了!