Gtk+:如何从开罗上下文设置窗口的光标?
我编写了以下代码来从 Cairo::Context
设置 Gtk::Window
的光标。当我运行程序并将光标移动到窗口中时,光标变为顶部的水平黑线,然后是底部的一些无法定义的白色形状。我原以为光标会变成黑色的 16×16 正方形。为什么光标没有呈现出我想要的形状?
#include <gtkmm.h>
const int size = 16, hotspot = 0;
class Window : public Gtk::Window
{
public:
void change_cursor()
{
Glib::RefPtr<Gdk::Drawable> pixmap = Gdk::Pixmap::create(
Glib::RefPtr<Gdk::Drawable>(), size, size, get_window()->get_depth());
pixmap->set_colormap(get_window()->get_colormap());
Cairo::RefPtr<Cairo::Context> context = pixmap->create_cairo_context();
context->set_source_rgba(0, 0, 0, 0);
context->rectangle(0, 0, size, size);
context->fill();
Glib::RefPtr<Gdk::Pixbuf> pixbuf
= Gdk::Pixbuf::create(pixmap, 0, 0, size, size);
Gdk::Cursor cursor(get_window()->get_display(), pixbuf, hotspot, hotspot);
window->set_cursor(cursor);
}
};
int main(int argc, char* argv[])
{
Gtk::Main app(argc, argv);
Window window;
window.show_all();
window.change_cursor();
Gtk::Main::run(window);
return 0;
}
当我将 Gdk::Pixmap 绘制到屏幕上时,它看起来很好。当我将 Gdk::Pixbuf 绘制到屏幕上时,我得到了垃圾。
I have written the following code to set the cursor of a Gtk::Window
from a Cairo::Context
. When I run the program and move the cursor into the window, the cursor changes to a horizontal black line at the top, followed by some undefinable white shape at the bottom. I was expecting the cursor to change into a black 16×16 square. Why doesn't the cursor assume the shape I intended?
#include <gtkmm.h>
const int size = 16, hotspot = 0;
class Window : public Gtk::Window
{
public:
void change_cursor()
{
Glib::RefPtr<Gdk::Drawable> pixmap = Gdk::Pixmap::create(
Glib::RefPtr<Gdk::Drawable>(), size, size, get_window()->get_depth());
pixmap->set_colormap(get_window()->get_colormap());
Cairo::RefPtr<Cairo::Context> context = pixmap->create_cairo_context();
context->set_source_rgba(0, 0, 0, 0);
context->rectangle(0, 0, size, size);
context->fill();
Glib::RefPtr<Gdk::Pixbuf> pixbuf
= Gdk::Pixbuf::create(pixmap, 0, 0, size, size);
Gdk::Cursor cursor(get_window()->get_display(), pixbuf, hotspot, hotspot);
window->set_cursor(cursor);
}
};
int main(int argc, char* argv[])
{
Gtk::Main app(argc, argv);
Window window;
window.show_all();
window.change_cursor();
Gtk::Main::run(window);
return 0;
}
When I draw the Gdk::Pixmap
to the screen, it looks fine. When I draw the Gdk::Pixbuf
to the screen, I get garbage.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有找出问题的原因,但有一种解决方法:
Gdk::Pixbuf
的像素作为数据缓冲区创建Cairo::ImageSurface
。Cairo::ImageSurface
创建一个Cairo::Context
。Cairo::Context
(这很重要,因为Gdk::Pixbuf
的像素数据似乎还没有初始化)。代码如下所示:
当我现在绘制到该上下文并从 Gdk::Pixbuf 设置光标时,我几乎得到了我想要的:形状很好,但红色和蓝色交换了。可以按照问题 4291994(如何将 Cairo 图像表面的内容写入 Gdk Pixbuf?) 中所述进行修复:
I did not figure out the cause of the problem, but a way to work around it:
Gdk::Pixbuf
.Cairo::ImageSurface
using theGdk::Pixbuf
's pixels as data buffer.Cairo::Context
from theCairo::ImageSurface
.Cairo::Context
(this is important, becauseGdk::Pixbuf
's pixel data seem not to be initialized yet).The code looks like this:
When I now paint to that context and set the cursor from the
Gdk::Pixbuf
, I get almost what I want: the shape is fine, but Red and Blue are swapped. This can be fixed as outlined in Question 4291994 (How to write contents of a Cairo Image surface into a Gdk Pixbuf?):