我有一个GTK + DrawingArea,它应该在左上角显示一个矩形。当我使用Cairo绘制矩形时,整个绘图区域将填充矩形的颜色。我怎么能防止这种情况?开罗为什么这样做?我做错了什么?
#include <gtkmm.h>
class Window : public Gtk::Window
{
private:
Gtk::DrawingArea area;
bool on_area_expose(GdkEventExpose* event)
{
Gtk::Allocation allocation = area.get_allocation();
Cairo::RefPtr<Cairo::Context> context =
area.get_window()->create_cairo_context();
int width = allocation.get_width();
int height = allocation.get_height();
context->set_source_rgba(0, 0, 0, 1);
context->rectangle(0, 0, double(width)/10, double(height)/10);
context->paint();
return true;
}
public:
Window() : Gtk::Window()
{
area.signal_expose_event().connect(
sigc::mem_fun(*this, &Window::on_area_expose));
add(area);
}
};
int main(int argc, char* argv[])
{
Gtk::Main app(argc, argv);
Window window;
window.show_all();
Gtk::Main::run(window);
return 0;
}
我使用
编译了代码g++ gtktest.cpp `pkg-config --libs --cflags gtkmm-2.4` -o gtktest