我尝试在Windows上使用cairo和gtk + -3.6.1创建一个简单的rounded_rectangle,但是出了点问题。认为在http://www.tarnyko.net/en/取得的窗口上,这个版本的gtk + -3.6.1出现错误。以下是我的代码:: blocks IDE
中的一些错误main.c(文件);
#include <stdlib.h>
#include <gtk/gtk.h>
#include "roundedrectangle.h"
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
GtkWidget *win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
//cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 100, 100);
//cairo_t *cr = cairo_create(surface);
GtkWidget *image = gtk_image_new_from_file("C:/Users/Doudieu/Desktop/image.png");
gtk_widget_set_app_paintable(image, TRUE);
gtk_container_add(GTK_CONTAINER(win), image);
struct user_data data = {50,50,100,100,20};
user_t *dat;
dat = &data;
//g_signal_connect(G_OBJECT(win), "expose-event", G_CALLBACK(drawing), (gpointer)dat);
g_signal_connect(G_OBJECT(image), "expose-event", G_CALLBACK(drawing), (gpointer)dat);
gtk_widget_show_all(win);
gtk_main();
return 0;
}
roundedrectangle.c(file):
#include "roundedrectangle.h"
int rounded_rectangle(cairo_t *cr, double x, double y, double width, double height, double radius[4])
{
if(((width<2*radius[0])&&(width<2*radius[1])&&(width<2*radius[2])&&(width<2*radius[3]))||((width<2*radius[0])&&(width<2*radius[1])&&(width<2*radius[2])&&(width<2*radius[3])))
{
printf("values of radius not good\n");
return -1;
}
double from_degre = PI/180;
//Corner C
cairo_arc(cr, x+width-radius[2], y+height-radius[2], radius[2], 0*from_degre, 90*from_degre);
//Corner D
cairo_arc(cr, x+radius[3], y+height-radius[3], radius[3], 90*from_degre, 180*from_degre);
//Corner A
cairo_arc(cr, x+radius[0], y+radius[0], radius[0], 180*from_degre, 270*from_degre);
//Corner B
cairo_arc(cr, x+width-radius[1], y+radius[1], radius[1], 270*from_degre, 360*from_degre);
cairo_close_path(cr);
return 0;
}
int rounded_rectangle_uni(cairo_t *cr, double x, double y, double width, double height, double radius)
{
double radius_uni[4] = {radius, radius, radius, radius};
if( rounded_rectangle( cr, x, y, width, height, radius_uni ) == -1)
{
printf("values of radius not good\n");
return -1;
}
return 0;
}
gboolean drawing(GtkWidget *win, cairo_t *cr, gpointer data)
{
GtkAllocation allo;
if(gtk_widget_get_app_paintable(win) == FALSE)
{
printf("background will be erase after\n");
return FALSE;
}
cairo_t *c = gdk_cairo_create(GDK_DRAWABLE(win->window));
gtk_widget_get_allocation(win, &allo);
struct user_data dat = *((user_t *)data);
cairo_set_source_rgba(c, 0.2, 0.1, 0, 1);
cairo_set_line_width(c, 4);
rounded_rectangle_uni(c, allo.x, allo.y, dat.width, dat.height, dat.border_radius);
cairo_stroke(c);
cairo_set_source_rgba(c, 0.0, 0.1, 0.2, 0.5);
rounded_rectangle_uni(c, allo.x, allo.y, dat.width, dat.height, dat.border_radius);
cairo_fill(c);
cairo_destroy(c);
return TRUE;
}
gboolean drawing1(GtkWidget *win, cairo_t *cr, gpointer data)
{
return TRUE;
}
roundedrectangle.h(文件):
#ifndef ROUNDED_RECTANGLE_H_INCLUDED
#define ROUNDED_RECTANGLE_H_INCLUDED
#endif // ROUNDED_RECTANGLE_H_INCLUDED
#include <gtk/gtk.h>
#include <math.h>
#define PI 3.141592653589793238462643383
typedef struct user_data user_t;
int rounded_rectangle(cairo_t *cr, double x, double y, double width, double height, double radius[4]);
int rounded_rectangle_uni(cairo_t *cr, double x, double y, double width, double height, double radius);
struct user_data
{
double x;
double y;
double width;
double height;
int border_radius;
};
gboolean drawing(GtkWidget *win, cairo_t *cr, gpointer data);
gboolean drawing1(GtkWidget *win, cairo_t *cr, gpointer data);
来自codeblocks输出的错误:
-------------- Build: Debug in cairo (compiler: GNU GCC Compiler)---------------
mingw32-gcc.exe -mms-bitfields -Wall -g -mms-bitfields -Ic:/gtk+/include/atk-1.0 -Ic:/gtk+/include/cairo -Ic:/gtk+/include/gdk-pixbuf-2.0 -Ic:/gtk+/include/glib-2.0 -Ic:/gtk+/lib/glib-2.0/include -Ic:/gtk+/include/pango-1.0 -Ic:/gtk+/include -Ic:/gtk+/include/freetype2 -Ic:/gtk+/include/libpng14 -IC:/GTK+-3.6.1/include/gtk-3.0 -IC:\gtk+\include -IC:\gtk+\include\gtk-2.0 -IC:\gtk+\include\cairo -IC:\gtk+\include\gdk -IC:\gtk+\include\glib-2.0 -IC:\gtk+\lib\glib-2.0\include -IC:\gtk+\include\pango-1.0 -IC:\gtk+\lib\gtk-2.0\include -IC:\gtk+\include\atk-1.0 -IC:\gtk+\include\gdk-pixbuf-2.0 -c C:\Users\Doudieu\Desktop\cairo\main.c -o obj\Debug\main.o
In file included from C:/GTK+-3.6.1/include/gtk-3.0/gdk/gdk.h:31:0,
from C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtk.h:30,
from C:\Users\Doudieu\Desktop\cairo\main.c:2:
C:/GTK+-3.6.1/include/gtk-3.0/gdk/gdkversionmacros.h:147:6: error: missing binary operator before token "("
C:/GTK+-3.6.1/include/gtk-3.0/gdk/gdkversionmacros.h:155:5: error: missing binary operator before token "("
C:/GTK+-3.6.1/include/gtk-3.0/gdk/gdkversionmacros.h:158:5: error: missing binary operator before token "("
C:/GTK+-3.6.1/include/gtk-3.0/gdk/gdkversionmacros.h:164:5: error: missing binary operator before token "("
C:/GTK+-3.6.1/include/gtk-3.0/gdk/gdkversionmacros.h:172:5: error: missing binary operator before token "("
C:/GTK+-3.6.1/include/gtk-3.0/gdk/gdkversionmacros.h:178:5: error: missing binary operator before token "("
C:/GTK+-3.6.1/include/gtk-3.0/gdk/gdkversionmacros.h:186:5: error: missing binary operator before token "("
C:/GTK+-3.6.1/include/gtk-3.0/gdk/gdkversionmacros.h:192:5: error: missing binary operator before token "("
C:/GTK+-3.6.1/include/gtk-3.0/gdk/gdkversionmacros.h:200:5: error: missing binary operator before token "("
C:/GTK+-3.6.1/include/gtk-3.0/gdk/gdkversionmacros.h:206:5: error: missing binary operator before token "("
C:/GTK+-3.6.1/include/gtk-3.0/gdk/gdkversionmacros.h:214:5: error: missing binary operator before token "("
In file included from C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtkwindow.h:33:0,
from C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtkdialog.h:33,
from C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtkaboutdialog.h:30,
from C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtk.h:31,
from C:\Users\Doudieu\Desktop\cairo\main.c:2:
C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtkapplication.h:77:1: error: unknown type name 'GMenuModel'
C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtkapplication.h:80:49: error: unknown type name 'GMenuModel'
C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtkapplication.h:83:1: error: unknown type name 'GMenuModel'
C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtkapplication.h:86:49: error: unknown type name 'GMenuModel'
In file included from C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtkmenu.h:34:0,
from C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtklabel.h:35,
from C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtkaccellabel.h:36,
from C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtk.h:33,
from C:\Users\Doudieu\Desktop\cairo\main.c:2:
C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtkmenushell.h:117:41: error: unknown type name 'GMenuModel'
In file included from C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtklabel.h:35:0,
from C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtkaccellabel.h:36,
from C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtk.h:33,
from C:\Users\Doudieu\Desktop\cairo\main.c:2:
C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtkmenu.h:119:44: error: unknown type name 'GMenuModel'
In file included from C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtk.h:58:0,
from C:\Users\Doudieu\Desktop\cairo\main.c:2:
C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtkbutton.h:84:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'void'
C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtkbutton.h:86:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'void'
C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtkbutton.h:88:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'void'
C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtkbutton.h:90:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'void'
In file included from C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtk.h:131:0,
from C:\Users\Doudieu\Desktop\cairo\main.c:2:
C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtkmenubar.h:73:42: error: unknown type name 'GMenuModel'
In file included from C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtk.h:132:0,
from C:\Users\Doudieu\Desktop\cairo\main.c:2:
C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtkmenubutton.h:82:46: error: unknown type name 'GMenuModel'
C:/GTK+-3.6.1/include/gtk-3.0/gtk/gtkmenubutton.h:84:1: error: unknown type name 'GMenuModel'
Process terminated with status 1 (0 minutes, 2 seconds)
24 errors, 0 warnings (0 minutes, 2 seconds)
答案 0 :(得分:1)
请在下面找到一些可能有用的观察结果。
构建环境:
看来,您发布的内容在构建环境中存在轻微的错误配置。编译器命令包含目录c:/gtk+/include/...
,而错误都指向C:/GTK+-3.6.1/include/...
。这可能是项目构建设置中的一些配置错误
更好的构建方法是使用pkg-config
。如果您在包的安装路径下选中bin
文件夹,则会看到pkg-config.exe
使用此方法的一种方法是在IDE中添加设置,在项目的“构建选项”下,将“编译器设置”的“其他选项”设置为`[GTK_3_6_INSTALLED_DIR] \ bin \ pkg-config.exe --cflags gtk + -3.0` (请注意命令开头和结尾的回滴答。)
同样在“链接器设置”的“其他选项”下添加`[GTK_3_6_INSTALLED_DIR] \ bin \ pkg-config.exe --libs gtk + -3.0` 。通过这些设置,您应该能够编译和链接您的代码。
代码:
由于更改,您使用的代码将无法使用Gtk + 3.0进行编译
有关更改的完整列表,请参阅this link。与您的代码相关的更改如下:
expose-event
不可用。在main.c
中使用draw
代替。 GdkDrawable
在Gtk + 3.0中已弃用。此外,您无法直接访问GtkWidget
的成员,您必须使用访问者功能。因此,在drawing
中的roundedrectangle.c
函数中,cairo_t *c = gdk_cairo_create(GDK_DRAWABLE(win->window));
应更改为cairo_t *c = gdk_cairo_create(gtk_widget_get_window(win));
#endif // ROUNDED_RECTANGLE_H_INCLUDED
应位于头文件的末尾。GtkDrawingArea
而不是GtkImage
。也可以将主窗口设置为app paintable并绘制圆角矩形。 希望这有帮助!