GtkSharp CSS 样式使用

GTKSharp CSS 样式初探

dotnet 下的 GtkWindow 使用css文件定义界面样式

环境:.net6.0、Gtk3

  1. 创建一个 gtk# 项目
dotnet new gtkapp
  1. 运行刚新建的项目
dotnet run

没有使用任何样式的原始界面如下:
没有加载样式的原始界面接下来用样式修改界面

  1. 新建一个 css 文件 style.css
/* You can edit the text in this window to change the
 * appearance of this Window.
 * Be careful, if you screw it up, nothing might be visible
 * anymore. :)
 */

/* The content of reset.css has been applied to reset all properties to
 * their defaults values and overrides all user settings and the theme in use */

/* Set a very futuristic style by default */
* {
  color: green;
  font-family: Monospace;
  border: 1px solid;
}

/* Make sure selections are visible */
:selected {
  background-color: darkGreen;
  color: black;
}

  1. 修改 Window 代码引用样式文件

        private MainWindow(Builder builder) : base(builder.GetRawOwnedObject("MainWindow"))
        {

#region ----------------样式加载部分-------------------
Gtk.CssProvider provider = new CssProvider();
        provider.LoadFromPath("style.css");

#endregion
            builder.Autoconnect(this);

#region ----------引用样式-----------
ApplyCss(this,provider,800);
#endregion
            DeleteEvent += Window_DeleteEvent;
            _button1.Clicked += Button1_Clicked;
        }


#region --------------------样式的启用功能----------------
//这个方法将样式使能到每个界面组件
private void ApplyCss (Widget widget, CssProvider provider, uint priority)
		{
			widget.StyleContext.AddProvider (provider, priority);
			var container = widget as Container;
			if (container != null) {
				foreach (var child in container.Children) {
					ApplyCss (child, provider, priority);
				}
			}
		}
    }
#endregion
    

现在运行一下程序看到界面如下图:
使用样式后的程序主界面
5. 再加入几个样式

* {
  color: green;
  font-family: Monospace;
  border: 1px solid;
/*改变字号和外边距*/
  font-size: 12pt;
  margin: 2px;

}

/*对应组件样式改变*/
label{
    color: red;
}

button{
    color:blue;
background-color:yellow;
}

window{
  padding:0px;
  border: 5px solid;
  border-color: red;
}

现在再运行一下程序,得到的界面如下:
最后的程序界面可以看到定义的样式已经起作用了。

本次样式功能使用参考自:https://github.com/mono/gtk-sharp/blob/main/sample/GtkDemo/DemoCssBasics.cs

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值