我需要知道用开罗在gtk中绘制形状的最简单方法

时间:2019-05-05 02:36:33

标签: c gtk3 cairo

有人可以告诉我在gtk的窗口中绘制矩形的最简单方法吗?请?简单。这是一个挑战。编写文件以用几行代码来绘制任何cairo形状。我在网上找到了工作的绘图代码,但是它非常复杂,并且并非全部都是必需的。有人可以给我写代码来用几行代码绘制任何cairo形状,以便我可以将其用作游戏参考吗?

这是一些有效的代码,但是要复杂得多。我不需要单击鼠标就可以绘制矩形,只需从四个数字中绘制一个矩形即可。有人,请将其减少为绘制一个矩形所必需的。最好是我想画一个矩形,除了主函数外没有其他任何函数(一旦主函数返回零,则一切结束)

代码:

#include <gtk/gtk.h>

/* Surface to store current scribbles */
static cairo_surface_t *surface = NULL;

static void
clear_surface (void)
{
  cairo_t *cr;

  cr = cairo_create (surface);

  cairo_set_source_rgb (cr, 1, 1, 1);
  cairo_paint (cr);

  cairo_destroy (cr);
}

/* Create a new surface of the appropriate size to store our scribbles */
static gboolean
configure_event_cb (GtkWidget         *widget,
                    GdkEventConfigure *event,
                    gpointer           data)
{
  if (surface)
    cairo_surface_destroy (surface);

  surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
                                               CAIRO_CONTENT_COLOR,
                                               gtk_widget_get_allocated_width (widget),
                                               gtk_widget_get_allocated_height (widget));

  /* Initialize the surface to white */
  clear_surface ();

  /* We've handled the configure event, no need for further processing. */
  return TRUE;
}

/* Redraw the screen from the surface. Note that the ::draw
 * signal receives a ready-to-be-used cairo_t that is already
 * clipped to only draw the exposed areas of the widget
 */
static gboolean
draw_cb (GtkWidget *widget,
         cairo_t   *cr,
         gpointer   data)
{
  cairo_set_source_surface (cr, surface, 0, 0);
  cairo_paint (cr);

  return FALSE;
}

/* Draw a rectangle on the surface at the given position */
static void
draw_brush (GtkWidget *widget,
            gdouble    x,
            gdouble    y)
{
  cairo_t *cr;

  /* Paint to the surface, where we store our state */
  cr = cairo_create (surface);

  cairo_rectangle (cr, x - 3, y - 3, 6, 6);
  cairo_fill (cr);

  cairo_destroy (cr);

  /* Now invalidate the affected region of the drawing area. */
  gtk_widget_queue_draw_area (widget, x - 3, y - 3, 6, 6);
}

/* Handle button press events by either drawing a rectangle
 * or clearing the surface, depending on which button was pressed.
 * The ::button-press signal handler receives a GdkEventButton
 * struct which contains this information.
 */
static gboolean
button_press_event_cb (GtkWidget      *widget,
                       GdkEventButton *event,
                       gpointer        data)
{
  /* paranoia check, in case we haven't gotten a configure event */
  if (surface == NULL)
    return FALSE;

  if (event->button == GDK_BUTTON_PRIMARY)
    {
      draw_brush (widget, event->x, event->y);
    }
  else if (event->button == GDK_BUTTON_SECONDARY)
    {
      clear_surface ();
      gtk_widget_queue_draw (widget);
    }

  /* We've handled the event, stop processing */
  return TRUE;
}

/* Handle motion events by continuing to draw if button 1 is
 * still held down. The ::motion-notify signal handler receives
 * a GdkEventMotion struct which contains this information.
 */
static gboolean
motion_notify_event_cb (GtkWidget      *widget,
                        GdkEventMotion *event,
                        gpointer        data)
{
  /* paranoia check, in case we haven't gotten a configure event */
  if (surface == NULL)
    return FALSE;

  if (event->state & GDK_BUTTON1_MASK)
    draw_brush (widget, event->x, event->y);

  /* We've handled it, stop processing */
  return TRUE;
}

static void
close_window (void)
{
  if (surface)
    cairo_surface_destroy (surface);
}

static void
activate (GtkApplication *app,
          gpointer        user_data)
{
  GtkWidget *window;
  GtkWidget *frame;
  GtkWidget *drawing_area;

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "Drawing Area");

  g_signal_connect (window, "destroy", G_CALLBACK (close_window), NULL);

  gtk_container_set_border_width (GTK_CONTAINER (window), 8);

  frame = gtk_frame_new (NULL);
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
  gtk_container_add (GTK_CONTAINER (window), frame);

  drawing_area = gtk_drawing_area_new ();
  /* set a minimum size */
  gtk_widget_set_size_request (drawing_area, 100, 100);

  gtk_container_add (GTK_CONTAINER (frame), drawing_area);

  /* Signals used to handle the backing surface */
  g_signal_connect (drawing_area, "draw",
                    G_CALLBACK (draw_cb), NULL);
  g_signal_connect (drawing_area,"configure-event",
                    G_CALLBACK (configure_event_cb), NULL);

  /* Event signals */
  g_signal_connect (drawing_area, "motion-notify-event",
                    G_CALLBACK (motion_notify_event_cb), NULL);
  g_signal_connect (drawing_area, "button-press-event",
                    G_CALLBACK (button_press_event_cb), NULL);

  /* Ask to receive events the drawing area doesn't normally
   * subscribe to. In particular, we need to ask for the
   * button press and motion notify events that want to handle.
   */
  gtk_widget_set_events (drawing_area, gtk_widget_get_events (drawing_area)
                                     | GDK_BUTTON_PRESS_MASK
                                     | GDK_POINTER_MOTION_MASK);

  gtk_widget_show_all (window);
}

int
main (int    argc,
      char **argv)
{
  GtkApplication *app;
  int status;

  app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}

1 个答案:

答案 0 :(得分:2)

  

有人可以告诉我最简单的方法来绘制矩形   gtk中的窗口?

是的,但是您将获得一个默认的Gtk窗口,其中带有cairo_rectangle,该默认的Gtk窗口将默认200x200窗口填充为矩形周围10像素的边框。绘制矩形并不需要太多操作(您甚至不必用单独的颜色填充矩形-尽管这使其与普通窗口背景无法区分)。

只需创建gtk窗口并使用gtk窗口作为容器添加开罗绘图区域(除非使用诸如vbox之类的布局容器,否则您将无法向该窗口添加其他任何内容。)

大约最小的是:

#include <cairo.h>
#include <gtk/gtk.h>

static void draw_rect (cairo_t *cr)
{
    cairo_rectangle (cr, 10, 10, 180, 180);     /* set rectangle */
    cairo_set_source_rgb (cr, 0.3, 0.4, 0.6);   /* set fill color */
    cairo_fill (cr);                            /* fill rectangle */
}

static gboolean on_draw_event (GtkWidget *widget, cairo_t *cr,
                                gpointer user_data)
{
    draw_rect (cr);     /* draw rectangle in window */

    return FALSE;
    (void)user_data, (void)widget;  /* suppress -Wunused warning */
}

int main (int argc, char *argv[])
{
    GtkWidget *window;          /* gtk windows */
    GtkWidget *darea;           /* cairo drawing area */

    gtk_init (&argc, &argv);    /* required with every gtk app */

    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);  /* create window */
    darea = gtk_drawing_area_new();                 /* create cairo area */
    gtk_container_add (GTK_CONTAINER(window), darea);   /* add to window */

    /* connect callbacks to draw rectangle and close window/quit app */
    g_signal_connect (G_OBJECT(darea), "draw",
                        G_CALLBACK(on_draw_event), NULL);
    g_signal_connect (G_OBJECT(window), "destroy",
                        G_CALLBACK(gtk_main_quit), NULL);

    gtk_widget_show_all (window);   /* show all windows */

    gtk_main();     /* pass control to gtk event-loop */
}

注意:,您可以删除(void)user_data, (void)widget;行,该行仅禁止显示未使用的参数警告)

编译

gcc -Wall -Wextra -pedantic -std=c11 -O3 `pkg-config --cflags --libs gtk+-3.0`

使用/输出示例

$ ./bin/cairo_rectangle_min

enter image description here