SOL9 Sample: GradientPen

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


// SOL2
// 2012/06/30

#include <sol\ApplicationView.h>
#include <sol\PaintDC.h>
#include <sol\Pen.h>
#include <sol/MessageFont.h>
#include "resource.h"

namespace SOL {

class AppView :public ApplicationView {
public:
  enum BACKGROUND_COLOR {
    GREEN_BLUE,
    RED_GREEN,
    RED_BLUE,
    RED,
    GREEN,
    BLUE,
    GRAY,
    LIGHT_GRAY,
  };

public:
  void drawLabel(PaintDC& pdc, int x, int y, int w, int h, const TCHAR* label,
    int fontsize, BACKGROUND_COLOR type)
  {
    float pw = (float)w/(float)0xff;
    
    int heiht = h;

    for(int i = 0; i < 0xff; i++) {
        COLORREF color;
        if (type == GREEN_BLUE) {
          color = RGB(0, i, i);
        }

        if (type == RED_GREEN) {
          color = RGB(i, i, 0);
        }
        if (type == RED_BLUE) {
          color = RGB(i, 0, i);
        }

        if (type == RED) {
          color = RGB(i, 0, 0);
        }
        if (type == GREEN) {
          color = RGB(0, i, 0);
        }

        if (type == BLUE) {
          color = RGB(0, 0, i);
        }
        if (type == GRAY) {
          color = RGB(i, i, i);
        }

        if (type == LIGHT_GRAY) {
          color = RGB(128+i/2, 128+i/2, 128+i/2);
        }

        Pen pen(PS_SOLID, (int)pw+4, color ); 

        HGDIOBJ prev = pdc.select(&pen);
        float fx = (float)x + pw*(float)i;
        pdc.moveTo((int)fx, y,  NULL);
        pdc.lineTo((int)fx, y+h);
        pdc.select(prev);
    }

    MessageFont mfont;
    mfont.create(fontsize);

    HFONT hf = (HFONT)pdc.select(&mfont);  
    pdc.setBkMode(TRANSPARENT);
    int th = pdc.getTextHeight();
    pdc.setTextColor(RGB(0xff, 0xff, 0xff));
    pdc.textOut(x+4, y+(h-th)/2, label, strlen(label));
    //RECT r = {x, y, x+w, y+h};
    //pdc.drawText(label, strlen(label), &r, DT_CENTER|DT_VCENTER);
    pdc.select(hf);
  }

private:
  long paint(Event& event)
  {
    PaintDC pdc(this);
    drawLabel(pdc, 10, 10, 300, 20, _T("Hello world"), 12, LIGHT_GRAY);//GREEN_BLUE);
    drawLabel(pdc, 10, 60, 400, 40, _T("Goodbye world"), 18, RED_GREEN);
    drawLabel(pdc, 10, 130, 500, 60, _T("こんにちは世界"), 24, RED_BLUE);
    drawLabel(pdc, 10, 220, 600, 80, _T("さようなら世界"), 30, BLUE);
    drawLabel(pdc, 10, 320, 700, 100, _T("Gray Gradient Background"), 36, GREEN_BLUE);//GRAY);

    return NULL;
  }

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);    
  }

};

}

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

    Args args;
    args.set(XmNclassStyle, NO_REDRAW);
    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.