0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Gtk3アプリ ダイアログを利用する

Last updated at Posted at 2021-06-14

####メッセージダイアログを利用する

Screenshot from 2021-06-14 14-20-30.png ####ダイアログのサンプルサイトが見つかる

monoですが利用できます。
C#のダイアログのサンプルプログラムがそのまま載っています。他のダイアログサンプルも載っています。

galdeに配置したメッセージダイアログを配置したやり方
Screenshot from 2021-06-14 14-22-21.png

galdeに配置した場合、プロパティをGUIから変更できます。
Screenshot from 2021-06-14 14-31-12.png

プログラムでやる場合はMessageDialogを宣言します

      MessageDialog md = new MessageDialog (this, 
      DialogFlags,
      MessageType.Question, 
      ButtonsType.YesNo, "Are you sure you want to quit?");

      ResponseType result = (ResponseType)md.Run ();
		if (result == ResponseType.Yes)
			Application.Quit();
		else
			md.Destroy();
using System;
using Gtk;
using UI = Gtk.Builder.ObjectAttribute;

namespace SubWinGtkApplication
{
    partial class MainWindow
    {
		//Gladeに配置したやり方
		private void on_MainWindow_mesWinOpen_Clicked(object sender , EventArgs e){
			
			ResponseType result = (ResponseType)mesDialog.Run ();

			if (result == ResponseType.Yes)
				Application.Quit();
			else
				mesDialog.Destroy();
		}

		//プログラムですべてやるやり方
		private void on_MainWindow_mesWinOpen2_Clicked(object sender , EventArgs e){
			
			MessageDialog md = new MessageDialog (this, 
				DialogFlags,
				MessageType.Question, 
				ButtonsType.YesNo, "Are you sure you want to quit?");
	
			ResponseType result = (ResponseType)md.Run ();

			if (result == ResponseType.Yes)
				Application.Quit();
			else
				md.Destroy();
		}
    }
}

builder.Autoconnect(this)でgldadeのweditとソースのwedit変数をフェッチしています。

builder.Autoconnect(this);
using System;
using Gtk;
using UI = Gtk.Builder.ObjectAttribute;
using GtkSharp.Webkitgtk3Sharp;

namespace SubWinGtkApplication
{
    partial class MainWindow
    {
		[UI] private readonly Gtk.MessageDialog mesDialog = null;
		[UI] private readonly Gtk.Button mesWinOpen = null;
		[UI] private readonly Gtk.Button mesWinOpen2 = null;
	}
}

namespace SubWinGtkApplication
{
    partial class MainWindow : Window
    {

        public MainWindow() : this(new Builder("MainWindow.glade"))
        {

        }

        private MainWindow(Builder builder) : base(builder.GetRawOwnedObject("MainWindow"))
        {
            builder.Autoconnect(this);
        }
    }
}

####Blocks風に書く
Objective-cのBlocks風な書き方にする

その場でMessageDialogを呼び出して結果を返します。

   public void _mkMesDialog(Window win ,string title,Action<MessageDialog,ResponseType> func)
   {
       MessageDialog md = new MessageDialog (win, 
          DialogFlags.DestroyWithParent,
          MessageType.Question, 
          ButtonsType.YesNo, title);
	  ResponseType result = (ResponseType)md.Run ();
       func(md , result);
   }

   _mkMesDialog(this, "タイトル", delegate(MessageDialog md, ResponseType result)
   {
      if (result == ResponseType.Yes)
          Application.Quit();
      else
          md.Destroy();
   });

using System;
using Gtk;
using UI = Gtk.Builder.ObjectAttribute;

namespace SubWinGtkApplication2
{
    class MainWindow : Window
    {
        public void _mkMesDialog(Window win ,Action<MessageDialog,ResponseType> func)
        {
            MessageDialog md = new MessageDialog (win, 
                DialogFlags.DestroyWithParent,
                MessageType.Question, 
                ButtonsType.YesNo, "Are you sure you want to quit?");
	
            ResponseType result = (ResponseType)md.Run ();
            func(md , result);
        }
    
        public MainWindow() : this(new Builder("MainWindow.glade"))
        {
            _mkMesDialog(this, delegate(MessageDialog md, ResponseType result)
            {
                if (result == ResponseType.Yes)
                    Application.Quit();
                else
                    md.Destroy();
            });
        }

        private MainWindow(Builder builder) : base(builder.GetRawOwnedObject("MainWindow"))
        {
            builder.Autoconnect(this);
        }
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.18" />
  <object class="GtkWindow" id="MainWindow">
    <property name="can_focus">False</property>
    <property name="title" translatable="yes">Example Window</property>
    <property name="default_width">480</property>
    <property name="default_height">240</property>
    <child>
      <placeholder />
    </child>
    <child>
      <object class="GtkLayout">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkButton" id="mesWinOpen">
            <property name="label" translatable="yes">メッセージボタン</property>
            <property name="width_request">134</property>
            <property name="height_request">36</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <signal name="clicked" handler="on_MainWindow_mesWinOpen_Clicked" swapped="false" />
          </object>
          <packing>
            <property name="x">20</property>
            <property name="y">16</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton" id="mesWinOpen2">
            <property name="label" translatable="yes">メッセージボタン2</property>
            <property name="width_request">159</property>
            <property name="height_request">34</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <signal name="clicked" handler="on_MainWindow_mesWinOpen2_Clicked" swapped="false" />
          </object>
          <packing>
            <property name="x">23</property>
            <property name="y">76</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
  <object class="GtkMessageDialog" id="mesDialog">
    <property name="can_focus">False</property>
    <property name="type_hint">dialog</property>
    <property name="buttons">ok-cancel</property>
    <child internal-child="vbox">
      <object class="GtkBox">
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <property name="spacing">2</property>
        <child internal-child="action_area">
          <object class="GtkButtonBox">
            <property name="can_focus">False</property>
            <property name="homogeneous">True</property>
            <property name="layout_style">end</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">False</property>
            <property name="position">0</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

####サブウィンドウを開く
同じグレードファイルの中にサブウィンドウを置いて、同じMainWindowクラス内定義し開くやり方

Screenshot from 2021-06-15 15-48-52.png
namespace SubWinGtkApplication2
{
    partial class MainWindow
    {

	    [UI] private readonly Gtk.Window subWin = null;

	    private void on_MainWindow_mainBtn_Clicked(object sender , EventArgs e){			
			subWin.Show();			
	    }
    }
}

Gtk3アプリ ポップアップウィンドウを作るに続く

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?