gpt4 book ai didi

c++ - 在 C++ gtk::drawarea 中:如何刷新图像

转载 作者:行者123 更新时间:2023-11-30 04:21:04 25 4
gpt4 key购买 nike

我在 C++ 中使用 gtkmm 作为 GUI。我有一个 Gtk::DrawingArea,上面有一张图片(文件名):

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;
}

我想要一个改变图像的函数(使用 filename2)。但我找不到如何:-/

有人可以帮我解决这个问题吗?谢谢你

最佳答案

作为类(class)成员:

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

关于c++ - 在 C++ gtk::drawarea 中:如何刷新图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14812580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com