SOL9 Sample: GradientBrush

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * GradientBrush.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL++2000
// 2000.02.16

#include <sol\ApplicationView.h>
#include <sol\PaintDC.h>
#include <sol\Brush.h>
#include <sol\StockObject.h>
#include <sol\Pen.h>
#include <sol\Font.h>
#include "resource.h"

namespace SOL {

class AppView :public ApplicationView {

public:
  AppView(Application& applet, const TCHAR* label, Args& args)
  :ApplicationView(applet, label, args)
  {
    addEventHandler(WM_PAINT, this, (Handler)&AppView::paint, NULL);
    addCallback(XmNmenuCallback, IDM_EXIT, this,
      (Callback)&AppView::exit, NULL);
  }

private:
  long paint(Event& event)
  {
    PaintDC pdc(this);

    StockObject pen(NULL_PEN);
    pdc.select(&pen);

    int width = 200;
    float p =(float)width/255.0; //+1;
    SIZE size;
    const TCHAR* string = _T("Good morning!");
    int margin = 8;
    pdc.getTextExtent(string, strlen(string), &size);
    int height = size.cy + margin*2;

    int x = 10;
    int y = 10;

    //red-gradient background
    for(int i = 0; i < 255; i++) {
      Brush brush(RGB(255, 128+i/2, 128+i/2));
      HGDIOBJ prev = pdc.select(&brush);
      pdc.rectangle(x+(int)((float)i*p), y, x+20+(int)((float)i*p), y+height);
      pdc.select(prev);
    }
    pdc.setBkMode(TRANSPARENT);

    int yd = (height - size.cy)/2;
    int xd = margin;
    pdc.textOut(x+xd, y+yd, string, strlen(string));

    //blue-gradient background
    width = 300;
    p =(float)width/255.0; //+1;
    y = 10+60;
    for(int i = 0; i < 255; i++) {
      Brush brush(RGB(128+i/2, 128+i/2, 255) );
      HGDIOBJ prev = pdc.select(&brush);
      pdc.rectangle(x+(int)((float)i*p), y, x+20+(int)((float)i*p), y+height);
      pdc.select(prev);
    }
    string = _T("Good afternoon!");
    pdc.textOut(x+xd, y+yd, string, strlen(string));

    //green-gradient background
    y = 10+60*2;
    width = 400;
    string = _T("Good evening!                                                              Good night!\r\nGood bye.");
    RECT r;
    pdc.drawText(string, strlen(string), &r, DT_CALCRECT);
    int w  = r.right - r.left;
    if (w >width) {
      width = w;
    }
    p = (float)width/255.0; //+1;
    r.left = x+xd;
    r.right = r.left + width;
    int h = r.bottom - r.top;
    r.top  = y+yd;
    r.bottom = r.top  + h +10*2;
  
    for(int i = 0; i < 255; i++) {
      Brush brush(RGB(128+i/2, 255, 128+i/2) );
      HGDIOBJ prev = pdc.select(&brush);
      pdc.rectangle(x+(int)((float)i*p), y, x+20+(int)((float)i*p), y+r.bottom-r.top);
      pdc.select(prev);
    }  
    pdc.drawText(string, strlen(string), &r, 0);
    return 0L;
  }
};

}

void  Main(int argc, TCHAR** argv)
{
  ModuleFileName module(argv[0]);
  const TCHAR* name = module.getFileName();
  try {
    Application applet(name, argc, argv);

    Args args;
    //2009/10/12
    args.set(XmNclassStyle, 0);

    AppView appview(applet, name, args);
    appview.realize();
    applet.run();

  } catch (Exception& ex) {
    caught(ex);
  } catch (...) {
    caught(UnknownException());
  }
}

Last modified: 1 Feb 2017

Copyright (c) 2017 Antillia.com ALL RIGHTS RESERVED.