1

我尝试在 Windows 上使用 cairo 和 gtk+-3.6.1 制作一个简单的 rounded_rectangle,但出现问题。认为这个版本的 gtk+-3.6.1 在取自http://www.tarnyko.net/en/的 Windows 上出现问题。以下是我的代码中的一些错误::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(文件):

#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);

来自代码块输出的错误:

-------------- 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)
4

1 回答 1

1

请在下面找到一些可能有帮助的观察结果。

  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`。通过这些设置,您应该能够编译和链接您的代码。

  2. 代码
    由于更改,您正在使用的代码将无法使用 Gtk+3.0 编译。
    有关更改的完整列表,请参阅此链接。与您的代码相关的更改如下:

    • 在 Gtk+ 3.0 中,expose-event不可用。drawmain.c. _
    • GdkDrawable在 Gtk+ 3.0 中已弃用。此外,您不能GtkWidget直接访问成员,您将不得不使用访问器功能。因此在drawing函数中roundedrectangle.ccairo_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. 也可以将主窗口设置为应用程序可绘制并在其上绘制圆角矩形。

希望这可以帮助!

于 2013-01-05T18:34:31.163 回答