Narase
Narase

Reputation: 490

Text over picture

Is it possible to create text displayed on a picture with nana?

I tried this

int main() {
  using namespace nana;

  form fm;
  place plc(fm);
  picture pic(fm);
  label lbl(fm, "LBL", true);

  pic.load(paint::image("xxx.png"));
  pic.caption("PIC");
  pic.align::center, align_v::center);

  lbl.transparent(true);
  lbl.text_align(align::center, align_v::center);

  plc.div("<<here>>");
  plc["here"] << pic;
  plc["here"] << lbl; // (1)

  plc.collocate();
  lbl.move(pic.pos());

  fm.show();
  exec();
}

But its shifting both to the left as nana creates a grid for 2 elements in the layout. Without adding it, so deleting (1), the label will not be shown in the layout at all.

I couldnt find any information on it online. nana::image::caption(std::string) method seems to be ignored

Is there a way to get two elements on top of each other, in a place of one element? My goal is make a picture of a waterdrop and then writing the humidity-% in the middle of it.

Thank you

Upvotes: 1

Views: 294

Answers (1)

ravenspoint
ravenspoint

Reputation: 20492

Display your picture, then use nana::paint::graphics::string to write your text on top of the picture.

This

enter image description here

is produced by

#include <iostream>
#include <nana/gui.hpp>
#include <nana/gui/widgets/picture.hpp>
#include <nana/gui/widgets/label.hpp>

int main()
{
    using namespace nana;

    paint::image I("xxx.bmp");
    if (I.empty())
    {
        msgbox err("Error");
        err << "Cannot read image";
        err.show();
        exit(1);
    }

    form fm({50,50,400,700});

    drawing dw(fm);
    dw.draw([&I](paint::graphics& g)
    {
        I.paste(g, {0,0} );
        g.string({200,400}, "THIS IS A TEST", colors::black);
    });

    fm.show();
    exec();
}

Upvotes: 2

Related Questions