在《Foundations of GTK+ Development》书中看到对于scrolled window添加内容的办法
After you have set up a scrolled window, you should add a child widget for it to be of any use. There are two possible ways to do this, and the method is chosen based on the type of child widget. If you are using a GtkTextView, GtkTreeView, GtkIconView, GtkViewport, or GtkLayout widget, you should use the default gtk_container_add() function, since all five of these widgets include native scrolling support.
All other GTK+ widgets do not have native scrolling support. For those widgets, gtk_scrolled_window_add_with_viewport() should be used. This function will give the child scrolling support by first packing it into a container widget called a GtkViewport. This widget implements scrolling ability for the child widget that lacks its own support. The viewport is then automatically added to the scrolled window.
使用下面的办法直接在scrolledwindow里面添加一个垂直容器
发现scrolledwindow有内边框很难看,而且无法消除。因为这是gtk_scrolled_window_add_with_viewport自动添加的viewport的边框。看到
It is possible to manually add a widget to a new GtkViewport and then add that viewport to a scrolled window with gtk_container_add().
于是自己创建viewport,取消边框后用gtk_container_add直接加入scrolledwindow就消除了边框
GtkWidget *viewport = gtk_viewport_new(NULL,NULL);
gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport),GTK_SHADOW_NONE);//设置无边框样式
gtk_container_add(GTK_CONTAINER(viewport),box1);
gtk_container_add(GTK_CONTAINER(scrolledWindow),viewport);或者也有一种简单办法,获取子控件GtkViewPort,然后设置阴影类型:
GtkWidget *child = gtk_bin_get_child(GTK_BIN(scrolledWindow232));
if (GTK_IS_VIEWPORT(child)) {
gtk_viewport_set_shadow_type(GTK_VIEWPORT(child), GTK_SHADOW_NONE);
}阴影类型可以设置为这几种
| 枚举值 | 说明 |
|---|---|
GTK_SHADOW_NONE |
无阴影/无边框。完全移除边框效果。 |
GTK_SHADOW_IN |
内阴影。边框看起来向内凹陷(类似凹槽效果)。 |
GTK_SHADOW_OUT |
外阴影。边框看起来向外凸起(类似浮雕效果,最常用)。 |
GTK_SHADOW_ETCHED_IN |
蚀刻内阴影。比 GTK_SHADOW_IN 更深的雕刻效果,常用于菜单分隔线等。 |
GTK_SHADOW_ETCHED_OUT |
蚀刻外阴影。比 GTK_SHADOW_OUT 更深的雕刻效果,线条更明显。 |