2011-01-25 86 views
0

我有一个GTK + DrawingArea应该在左上角显示一个矩形。当我使用开罗绘制矩形时,整个绘图区域都填充了矩形的颜色。我怎样才能防止呢?开罗为什么这样做?我究竟做错了什么?绘制任何矩形到一个GTK + DrawingArea填充整个DrawingArea

#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 

回答