Gtkmm 3.24:用 cairo 绘图

问题描述 投票:0回答:0

使用 Gtkmm 3.24 和开罗。我在 Gtk::DrawingArea 中画一条对角线到全尺寸 Gtk::Window.Then 我使用 cr->clip 限制渲染区域。同时,改变窗口大小时,对角线显示不正确。有什么方法可以解决这个问题。这是一个示例代码。

main.cpp

#include <clocale>
#include <gtkmm.h>

class main_window : public Gtk::Window {
private:
    Gtk::DrawingArea drawing_area;
public:
    main_window();
    ~main_window(); 
private:
    bool draw(const Cairo::RefPtr<Cairo::Context>& cr);
    
};

main_window::main_window()
{
    set_default_size(300, 300);
    add(drawing_area);
    signal_draw().connect(
            sigc::mem_fun(*this, &main_window::draw));
    show_all();

}

bool main_window::draw(const Cairo::RefPtr<Cairo::Context>& cr)
{
    int width = get_width();
    int height = get_height();
    int radius = std::min(width, height) / 2;
    
    cr->set_source_rgb(1,1,1);
    cr->arc(width / 2, height / 2, radius, 0, 2 * M_PI);
    cr->clip();
    
    // Background
    cr->set_source_rgb(0, 0, 0);
    cr->rectangle(0, 0, width, height);
    cr->fill(); 
    
    cr->set_source_rgb(1,1,1);
    
    // Diagonal
    cr->move_to(0, 0);
    cr->line_to(width, height);
    cr->stroke();
    
    return true;
}

main_window::~main_window() noexcept = default;

int main(int argc, char *argv[])
{
    auto app = Gtk::Application::create(argc, argv, "");
    setlocale(LC_ALL, "C");

    main_window win;
    return app->run(win);
}type here

我希望在更改窗口大小时,对角线显示是正确的

c++ cairo gtkmm3
© www.soinside.com 2019 - 2024. All rights reserved.