2013-02-11 51 views
0

我在C++中使用gtkmm作为GUI。 我有一个Gtk::DrawingArea其上我的图像(文件名):in C++ gtk :: drawarea:如何刷新图像

class MyArea : public Gtk::DrawingArea 
{ 
public: 
    MyArea(string filename) 
    { 
     m_image = Gdk::Pixbuf::create_from_file(filename.c_str()); 
    } 

    virtual bool on_draw(const Cairo::RefPtr<Cairo::Context>& cr) 
    { 
     if (!m_image) 
      return false; 

     Gtk::Allocation allocation = get_allocation(); 
     const int width = allocation.get_width(); 
     const int height = allocation.get_height(); 

     // Draw the image in the middle of the drawing area, or (if the image is 
     // larger than the drawing area) draw the middle part of the image. 
     Gdk::Cairo::set_source_pixbuf(cr, m_image, (width - m_image->get_width())/2, (height - m_image->get_height())/2); 
     cr->paint(); 

     return true; 
    } 
    Glib::RefPtr<Gdk::Pixbuf> m_image; 
} 

我想有改变图像(具有文件名2)的功能。但我找不到如何: -/

有人可以帮我这个请。谢谢

+0

我试图调用'创建我的文件名2另一m_image后得出()'或'signal_draw',但它不工作 – Jav 2013-02-11 13:07:35

回答

1

当你的类的成员变量:

void change_image(string filename2) 
{ 
    m_image = Gdk::Pixbuf::create_from_file(filename2.c_str()); 
    queue_draw(); 
} 
+0

谢谢你为你的答案。我已经想尝试这个,但“on_draw”有一个我没有的参数: -/ – Jav 2013-02-11 13:11:54

+0

@Jav看我的编辑。 – 2013-02-11 13:22:33

+0

TY。有用。但我不明白为什么不在构造函数中调用“on_draw”: -/ – Jav 2013-02-11 13:37:27