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 1 year has passed since last update.

Gtk3アプリ GtkButton処理関連

Last updated at Posted at 2022-01-27

####ボタンのWidthHeightの取得

Gtk.Button btn = new Button();
int width;
int height;
btn.GetSizeRequest(out width,out height);

####ボタンのWidthHeightの設定

Gtk.Button btn = new Button();
btn.SetSizeRequest(100, 35);

####ボタンを動的に追加する

GtkLayoutをGladeに追加します。IDをつけます。ここではlayOut1

Screenshot from 2022-01-27 22-45-12.png

追加した後にShowAll()を実行しないと表示されないです。

public MainWindow() : this(new Builder("MainWindow.glade"))
{
    Gtk.Button btn = new Button();
    btn.Label = "テスト";
    layOut1.Put(btn,100,120);
    btn.Clicked += delegate(object? sender, EventArgs args)
    {
        Console.WriteLine("テスト");
    };
    this.ShowAll();
}

####ボタンを動的に連続して追加する

for(int i=0;i<5;i++)
{
	Gtk.Button btn = new Button();
	btn.Label = "test" + i.ToString();
	btn.Name = i.ToString();
	btn.Clicked += delegate(object? sender, EventArgs args)
	{
		Console.WriteLine(((Gtk.Button)sender).Name);
	};
	int width;
	int height;
	btn.GetSizeRequest(out width,out height);
	int kankaku = height + i * 30;
	this.layOut.Put(btn,200,kankaku);
}
this.ShowAll();

####独自の変数を追加しイベントの中で取得する

class ButtonEx : Gtk.Button
{
   public string TestStr { get; set; }
}

public MainWindow() : this(new Builder("MainWindow.glade"))
{
    ButtonEx btn = new ButtonEx();
    btn.Label = "テスト";
    btn.TestStr = "testtest";
    layOut1.Put(btn,100,120);
    btn.Clicked += delegate(object? sender, EventArgs args)
    {
        Console.WriteLine(((ButtonEx)sender).TestStr);
    };
    this.ShowAll();
}

####WidthHeight取得の拡張クラス

static public Dictionary<int,int> _getWidthHeight(this Gtk.Button btn)
{
	int width;
	int height;
	btn.GetSizeRequest(out width,out height);
	return  new Dictionary<int, int>() {{ width, height} };
}
static public void _setWidthHeight(this Gtk.Button btn,int width,int height)
{
  btn.SetSizeRequest(width, height);
}
static public int _getWidth(this Gtk.Button btn)
{
    int width;
    int height;
    btn.GetSizeRequest(out width,out height);
    return width;
}
static public int _getHeight(this Gtk.Button btn)
{
    int width;
    int height;
    btn.GetSizeRequest(out width,out height);
    return height;
}

Gtk3アプリ Gtk.Scaleを利用するへ続く

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?