Jump to content

ImageButton with text positioning


Recommended Posts

I needed to get a buttons with image and texts on it (see http://community.ugfx.org/files/file/11-%C2%B5gfx-studio/, first screenshot), but I wanted text to be placed not at default center, so I made custom draw routine for this:

// structure with custom text positioning
typedef struct {
    gdispImage* image;
    coord_t     x;
    coord_t     y;
    coord_t     width;
    coord_t     height;
    justify_t   justify;
} custom_button_t;

// custom draw function
void gwinButtonDraw_ImageText(GWidgetObject *gw, void *param) {
    coord_t sy;

    custom_button_t* params = (custom_button_t*)param;
    if (!params) {
    const GColorSet* colors;
    if (!gwinGetEnabled((GHandle)gw)) {
        colors = &gw->pstyle->disabled;
        sy = 2 * gw->g.height;
    } else if ((gw->g.flags & GBUTTON_FLG_PRESSED)) {
        colors = &gw->pstyle->pressed;
        sy = gw->g.height;
    } else {
        colors = &gw->pstyle->enabled;
        sy = 0;
    gdispGImageDraw(gw->g.display, params->image, gw->g.x, gw->g.y, gw->g.width, gw->g.height, 0, sy);
    gdispGDrawStringBox(gw->g.display, gw->g.x+1+params->x, gw->g.y+1+params->y, params->width-2, params->height-2, gw->text, gw->g.font, colors->text, params->justify);

// define text positions and justify
custom_button_t param_rec_gray = { &button_rec_gray, 0, 45, 86, 40, justifyCenter };

// usage
gwinSetCustomDraw(ghButton_rec, gwinButtonDraw_ImageText, &param_rec_gray);

This worked for me.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...