如何直接在桌面上画图?

发布于 2024-11-30 19:10:56 字数 135 浏览 1 评论 0原文

我想知道如何在 X11 环境中使用 Cairo (用 C 语言)直接在根窗口上绘制,以制作小部件。我复制了tint2的部分代码,但它相当庞大,而且我唯一的结果并不令人满意。 我很高兴有一个完整的工作示例代码,或者至少有一些技巧或小程序可供学习。 谢谢你们!

I'm wondering how to draw directly on the root window in an X11 environment with Cairo (in C), in order to make widgets. I've copied some parts of the code of tint2, but it's quite enormous, and the only result I have is not satisfying.
I would be pleased to have a complete working sample code, or at least some tips or little programs to study.
Thank you guys !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痞味浪人 2024-12-07 19:10:56

“底部”窗口是根窗口。问题是,在某些桌面环境中,我们的窗口位于根窗口之上,因此,如果您更改根窗口,您将看不到所做的更改:您需要更改顶部的窗口。

该程序执行您所要求的操作:在根窗口上绘图。要测试它,我建议您:

  • ctrl+alt+f1
  • 以 root 身份登录
  • 停止您的桌面环境(“/etc/init.d/gdm stop”、“/etc/init.d/kdm stop”或任何需要的内容)你的发行版)
  • X -noreset -retro &
  • DISPLAY=:0.0 xterm &
  • DISPLAY=:0.0 元城市 &

然后,返回 X(ctrl+alt+f7 或 f8)并运行该程序。

如果你想在 Nautilus 的顶部窗口上绘图,你需要找出它的窗口 ID,然后将其用作“w”变量。 “xwininfo”命令可能会帮助您测试......

#include <assert.h>
#include <stdio.h>
#include <X11/Xlib.h>
#include <cairo.h>
#include <cairo-xlib.h>

int width, height;

void draw(cairo_t *cr) {
    int quarter_w = width / 4;
    int quarter_h = height / 4;
    cairo_set_source_rgb(cr, 1.0, 0.0, 0.0);
    cairo_rectangle(cr, quarter_w, quarter_h, quarter_w * 2, quarter_h * 2);
    cairo_fill(cr);
}

int main() {
    Display *d = XOpenDisplay(NULL);
    assert(d);

    int s = DefaultScreen(d);
    Window w = RootWindow(d, s);
    width = DisplayWidth(d, s);
    height = DisplayHeight(d, s);

    cairo_surface_t *surf = cairo_xlib_surface_create(d, w,
                                  DefaultVisual(d, s),
                                  width, height);
    cairo_t *cr = cairo_create(surf);

    XSelectInput(d, w, ExposureMask);

    draw(cr);

    XEvent ev;
    while (1) {
    XNextEvent(d, &ev);
        printf("Event!\n");
        if (ev.type == Expose) {
            draw(cr);
        }
    }

    cairo_destroy(cr);
    cairo_surface_destroy(surf);
    XCloseDisplay(d);
    return 0;
}

The "bottom" window is the root window. The problem is that in some desktop environments we have windows on top of the root window, so if you change the root window, you won't see your changes: you need to change the window that's on the top.

This program does what you ask for: draw on the root window. To test it, I suggest you to:

  • ctrl+alt+f1
  • login as root
  • stop your desktop environment ("/etc/init.d/gdm stop", "/etc/init.d/kdm stop" or whatever is needed in your distro)
  • X -noreset -retro &
  • DISPLAY=:0.0 xterm &
  • DISPLAY=:0.0 metacity &

Then, go back to X (ctrl+alt+f7 or maybe f8) and run the program.

If you want to draw on Nautilus' top window, you will need to find out its window ID and then use it as the "w" variable. The "xwininfo" command might help you testing...

#include <assert.h>
#include <stdio.h>
#include <X11/Xlib.h>
#include <cairo.h>
#include <cairo-xlib.h>

int width, height;

void draw(cairo_t *cr) {
    int quarter_w = width / 4;
    int quarter_h = height / 4;
    cairo_set_source_rgb(cr, 1.0, 0.0, 0.0);
    cairo_rectangle(cr, quarter_w, quarter_h, quarter_w * 2, quarter_h * 2);
    cairo_fill(cr);
}

int main() {
    Display *d = XOpenDisplay(NULL);
    assert(d);

    int s = DefaultScreen(d);
    Window w = RootWindow(d, s);
    width = DisplayWidth(d, s);
    height = DisplayHeight(d, s);

    cairo_surface_t *surf = cairo_xlib_surface_create(d, w,
                                  DefaultVisual(d, s),
                                  width, height);
    cairo_t *cr = cairo_create(surf);

    XSelectInput(d, w, ExposureMask);

    draw(cr);

    XEvent ev;
    while (1) {
    XNextEvent(d, &ev);
        printf("Event!\n");
        if (ev.type == Expose) {
            draw(cr);
        }
    }

    cairo_destroy(cr);
    cairo_surface_destroy(surf);
    XCloseDisplay(d);
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文