在gtk中绘制点/线。 C++

发布于 2025-01-05 20:07:49 字数 101 浏览 1 评论 0原文

嘿,我正在使用 C++ 编写一个具有绘图区域的程序。我需要能够检测来自绘图区域的鼠标信号并在这些位置绘制点。我该怎么做?没有多少论坛可以帮助解决这些问题,而且文档也不清楚。有人可以帮我吗?

Hey I am using c++ to write a program which has a drawing area. I need to make it possible to detect mouse signals from the drawing area and draw points at these positions. How can I do this? There are not many forums which help with this stuff and the documentation is not clear. Can somebody please help me?

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

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

发布评论

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

评论(2

末が日狂欢 2025-01-12 20:07:49

gtk3 应用程序中的绘图是响应“绘图”信号而完成的。您将回调函数连接到“draw”信号,当 gtk+ 需要重绘窗口时,您的回调函数就会被调用。

g_signal_connect (drawingarea, "draw",
                  G_CALLBACK (draw_cb), NULL);

回调看起来像这样:

static gboolean
draw_cb (GtkWidget *widget,
         cairo_t   *cr,
         gpointer   data)
{
  // do your drawing on the provided cairo_t
  // ...

  return FALSE;
}

现在,要处理鼠标单击,您需要将另一个回调函数连接到“button-press-event”:

g_signal_connect (drawingarea, "button-press-event",
                  G_CALLBACK (button_press_event_cb), NULL);

请参阅 绘图区域参考文档 的一个小示例和 绘图区域演示以获得完整的应用程序。

Drawing in a gtk3 application is done in response to the "draw" signal. You connect a callback function to the "draw" signal, and when gtk+ needs to redraw the window, your callback is invoked.

g_signal_connect (drawingarea, "draw",
                  G_CALLBACK (draw_cb), NULL);

The callback looks something like this:

static gboolean
draw_cb (GtkWidget *widget,
         cairo_t   *cr,
         gpointer   data)
{
  // do your drawing on the provided cairo_t
  // ...

  return FALSE;
}

Now, to handle mouse clicks, you'll need to connect another callback function to "button-press-event":

g_signal_connect (drawingarea, "button-press-event",
                  G_CALLBACK (button_press_event_cb), NULL);

See the drawing area reference docs for a small example and the drawing area demo for a complete application.

花伊自在美 2025-01-12 20:07:49

gtkmm 与 C++ 一起使用,这是最好的选择。大多数事件可以通过重载特定的虚拟方法来捕获。对于鼠标按下,您可以重载 on_button_press_event 方法。 gtkmm.org 上的教程还不错(对于开源教程来说),您可以通过阅读基础知识并根据需要查看其他感兴趣的章节来获得良好的开端。

Use gtkmm with C++, it's the best option. Most events can be captured by overloading a particular virtual method. For a mouse press, you overload the on_button_press_event method. The tutorial at gtkmm.org is not too bad (for open source tutorials that is), you can get a good start by reading about the basics and look at other chapters of interest as needed.

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