将任意矩形绘制到 GTK+ DrawingArea填充整个DrawingArea
我有一个 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
I have a GTK+ DrawingArea that should display a rectangle in the top left corner. When I draw the rectangle using Cairo, the whole drawing area is filled with the color of the rectangle. How can I prevent that? Why does Cairo do that? What am I doing wrong?
#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;
}
I compiled the code using
g++ gtktest.cpp `pkg-config --libs --cflags gtkmm-2.4` -o gtktest
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
context->paint()
绘制当前剪辑区域内任何地方的当前源。正确的调用方法是Gtk::Context::fill
。context->paint()
paints the current source everywhere within the current clip region. The proper method to call isGtk::Context::fill
.