首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FLTK:如何将图形对象置于前台?

FLTK:如何将图形对象置于前台?
EN

Stack Overflow用户
提问于 2020-12-24 22:54:45
回答 1查看 57关注 0票数 0

我在一个窗口中有两个可拖动的对象:包含黑色方块的对象在背景中,而包含红色方块的对象在前景中。

这种布局是由它们的绘制顺序决定的。我希望将当前正在拖动的框放在前景中:例如,如果我拖动带有黑色正方形的框,我希望它放在前景中。一旦我丢弃了它,它应该会留在前台。如果我拖动红色的,我希望它在前景中,而黑色的在背景中。

我试着查阅了FLTK1.3.5关于clipping的文档,但我没有找到任何有用的东西(至少,就我所能理解的而言)。有什么方法可以实现我想要的吗?

下面列出了代码(由一个here表示)。

代码语言:javascript
复制
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Scroll.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Pixmap.H>
#include <iostream>

static char *box1_xpm[] = {                       // XPM
"20 20 2 1",
"  c #000000",
"# c None",
"####################",
"####################",
"#                  #",
"#                  #",
"#                  #",
"#                  #",
"#                  #",
"#                  #",
"#                  #",
"#                  #",
"#                  #",
"#                  #",
"#                  #",
"#                  #",
"#                  #",
"#                  #",
"#                  #",
"#                  #",
"####################",
"####################"
};

static char *box2_xpm[] = {                       // XPM
"20 20 2 1",
"  c #FF0000",
"# c None",
"####################",
"####################",
"#                  #",
"#                  #",
"#                  #",
"#                  #",
"#                  #",
"#                  #",
"#                  #",
"#                  #",
"#                  #",
"#                  #",
"#                  #",
"#                  #",
"#                  #",
"#                  #",
"#                  #",
"#                  #",
"####################",
"####################"
};

Fl_Double_Window *G_win    = NULL;
Fl_Scroll        *G_scroll = NULL;
static Fl_Pixmap  G_box1(box1_xpm);
static Fl_Pixmap  G_box2(box2_xpm);

#define BOXWIDTH  50
#define BOXHEIGHT 50

// A 'MOVABLE' BOX
class Box : public Fl_Box {
protected:
    int handle(int e) {
        static int offset[2] = { 0, 0 };
        int ret = Fl_Box::handle(e);
        switch ( e ) {
            case FL_PUSH:
                offset[0] = x() - Fl::event_x();    // save where user clicked for dragging
                offset[1] = y() - Fl::event_y();
                return(1);
            case FL_RELEASE:
                return(1);
            case FL_DRAG:
                position(offset[0]+Fl::event_x(), offset[1]+Fl::event_y());     // handle dragging
                G_win->redraw();
                return(1);
        }
        return(ret);
    }
public:
    Box(int X, int Y, int idx) : Fl_Box(X,Y,BOXWIDTH,BOXHEIGHT,0) {
        idx>0? image(G_box1):image(G_box2);
        box(FL_UP_BOX);
        color(FL_GRAY);
    }
};

/// MAIN
int main() {
    G_win = new Fl_Double_Window(200,200);
    new Box(20,BOXHEIGHT,2);
    new Box(20+BOXWIDTH,BOXHEIGHT,0);
    G_win->resizable(G_win);
    G_win->show();
    return(Fl::run());
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-25 03:07:32

按构件在父级的子级()列表中出现的顺序绘制构件。诀窍是确保被拖动的小部件是最后一个绘制的小部件。按如下方式更改推送案例

代码语言:javascript
复制
            case FL_PUSH:
                {
                    offset[0] = x() - Fl::event_x();    // save where user clicked for dragging
                    offset[1] = y() - Fl::event_y();

                    // Do we need to rearrange?
                    int last_ix = G_win->children() - 1;
                    Box* last = (Box*)G_win->child(last_ix);
                    if (last != this)
                    {
                        // Widgets get drawn in the order in which they were inserted.
                        // Remove this widget from the parent
                        G_win->remove(this);
                        // Re-add it at the bottom of the list
                        G_win->add(this);
                    }
                }
                return(1);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65439922

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档