如何在 Gtkmm DrawingArea 中绘制 cairo_surface_t

How to draw a cairo_surface_t in a Gtkmm DrawingArea

我正在尝试在 Gtkmm DrawingArea 中绘制 cairo_surface_t。

cairo_surface_t * 可以用来初始化可以传递给 Cairo::Context::set_source 的 Glib::RefPtr 吗?

#include <gtkmm.h>
class CDrawingArea : Gtk::DrawingArea
{
public:
   CDrawingArea();
   virtual ~CDrawingArea();
   void DrawSurface (cairo_surface_t * surface); 
protected:
   bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr);
   cairo_surface_t * m_surface;
};

#include "CDrawingArea.h"

CDrawingArea::CDrawingArea()
{
}

CDrawingArea::~CDrawingArea()
{
}

void CDrawingArea::DrawSurface (cairo_surface_t * surface)
{
   m_surface = surface;
   Glib::RefPtr<Gdk::Window> win = get_window();
   if (win)
   {
      Gdk::Rectangle r(0, 0, get_allocation().get_width(), get_allocation().get_height());
      win->invalidate_rect(r, false);
   }
}

bool CDrawingArea::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
{
   // Cairo::RefPtr<Cairo::Surface> refSurface  ??;
   // cr->set_source(refSurface, 0, 0);
   cr->paint();
}

您是否在寻找 Surface 的构造函数?

Cairo::Surface::Surface (   cairo_surface_t *   cobject,
bool    has_reference = false    
)           [explicit]
Create a C++ wrapper for the C instance.

This C++ instance should then be given to a RefPtr.

(复制粘贴破坏了格式,抱歉) https://cairographics.org/documentation/cairomm/reference/classCairo_1_1Surface.html#a47b90669a5a85d187cc0d28dc99915c8