SOL9 Sample: SolImageBlender

SOL9 2.0 Samples

1 Screenshot


2 Source code

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



// SOL9 2.0 
// 2012/05/28 Sample program to use SOL::ColorListView class

#define COMMONCONTROLS_V6

#include <sol\ApplicationView.h>
#include <sol\Static.h>

#include <sol\ClientDC.h>
#include <sol\DropFiles.h>
#include <sol\FileDialog.h>
#include <sol\ListViewColorChooser.h>
#include <sol\ColorBox.h>
#include <sol\ImageView.h>
#include <sol\ValueSlider.h>

#include "resource.h"

namespace SOL {

class SolImageBlender :public ApplicationView {

private:
  FileDialog    fileDialog;

  _bstr_t imageFileName;

  ImageView    imageView;
  
  ListViewColorChooser  listView;
  
  ValueSlider       alphaScaler;
  
  int      alphaScale;

  COLORREF  color;

public:
  SolImageBlender(Application& applet, const TCHAR* caption, Args& args)
        :ApplicationView(applet, caption, args.set(XmNstyle, WS_CLIPCHILDREN)
                        .set(XmNexStyle, WS_EX_CONTROLPARENT) )
  {
    Args ar;
    ar.reset();
    ar.set(XmNscaling, ImageBox::StretchWindow);
    imageView.create(this, _T(""), ar);

    ar.reset();
    ar.set(XmNcolorChooserTitle, _T("BackgroundColor"));
    ar.set(XmNstyle, WS_VISIBLE);
    ar.set(XmNitemHeight, 30);
    //ar.set(XmNrgbStep, 0x33);
    ar.set(XmNrgbStep, 0x20);
    
    listView.create(this, NULL, ar);

    ar.reset();
    ar.set(XmNstyle, WS_VISIBLE);

    ar.set(XmNsliderTitle, _T("Transparency(%)"));
    ar.set(XmNsliderPosition, 100);
    alphaScaler.create(this, NULL, ar);
    alphaScale = 100;

    //Add an eventHandler to catch an event from ListViewColorChooser.
    addEventHandler(WM_COLORLISTVIEW_SELCHANGED, this,
      (Handler)&SolImageBlender::colorChanged, NULL);
    //Add an eventHandler to catch an event from ValueSlider.
    addEventHandler(WM_VALUESLIDER_CHANGED, this, 
      (Handler)&SolImageBlender::valueSliderChanged, NULL);

    //Add menuCallbacks
    addCallback(XmNmenuCallback, IDM_NEW, this,
      (Callback)&SolImageBlender::clear, NULL);
    addCallback(XmNmenuCallback, IDM_OPEN, this,
      (Callback)&SolImageBlender::open, NULL);
    addCallback(XmNmenuCallback, IDM_SAVEAS, this,
      (Callback)&SolImageBlender::saveAs, NULL);

    addCallback(XmNmenuCallback, IDM_EXIT, this,
      (Callback)&SolImageBlender::exit, NULL);
  
    addCallback(XmNmenuCallback, IDM_VERSION, this,
      (Callback)&SolImageBlender::version, NULL);

    //Create a fileDialog
    ar.reset();
    ar.set(XmNfilter, 
      _T("Image File(*.bmp,*.jpg,*.png,*.tif,*.ico,*.gif)\0*.bmp;*.jpg;*.png;*.tif;*.ico;*.gif\0JPEG File*.jpg)\0*.jpg\0PNG File(*.png)\0*.png\0Tiff File(*.tif)\0*.tif\0Icon File(*.ico)\0*.ico;\0GIF File(*.gif)\0*.gif;\0"));

    fileDialog.create(this, _T(""), ar);
    
    color = RGB(0xff, 0xff, 0xff);

    restorePlacement();
  }

private:
  void  clear(Action& action)
  {
    if (imageView.getImage()) {
      int rc = MessageBox(getWindow(), _T("Are you sure to clear the current image?"), _T("SolImageBlender"),
        MB_OKCANCEL | MB_ICONQUESTION);
      
      if (rc == IDOK) {
        imageFileName = _T("");

        imageView.setImageFileName(_T(""));

        imageView.clear();
        imageView.invalidateAll();

        imageView.update();
        setText(_T("SolImageBlender"));
      }
    }
  }

private:
  void  saveAs(Action& action)
  {
    Args ar;
    
    TCHAR dir[MAX_PATH];
    memset(dir, (TCHAR)0, CountOf(dir));
    //Call getFileFolder method in SOL::ApplicationView.
    //This gets a previously select folder from a registry(profile of this application) for fileDialog
    if (restoreFileFolder(dir, CountOf(dir))) {
      ar.set(XmNdirectory, dir);
      fileDialog.setValues(ar);
    }
  
    if(fileDialog.save()){
      TCHAR* filename = fileDialog.getFileName();

      TCHAR* ftitle = fileDialog.getFileTitle();
    
      File file;
      if (file.isExistent(filename) == false) {
        //Call saveFileFolder method in SOL::ApplicationView.
        saveFileFolder(filename);
        imageView.saveAs(filename);
    
        TCHAR title[MAX_PATH];
        imageFileName = filename;
        _stprintf_s(title, CountOf(title), _T("%s - ImageViewer"), (const TCHAR*)imageFileName);
        setText(title);
      }
    }  
  }

private:
  long colorChanged(Event& event)
  {
    color = (COLORREF)event.getLParam();

    drawImage(imageFileName);
    return 0;
  }

private:
  String getAlphaScale()
  {
    TCHAR string[128];
    _stprintf_s(string, CountOf(string), _T("alpha:%02f"), (float)alphaScale/100.0);
    
    string[10] = NULL;
    return String(string);
  }

private:
  long valueSliderChanged(Event& event)
  {
    this->alphaScale  = (int)event.getLParam();
    HWND tracker = (HWND)event.getWParam();

    if (tracker == this->alphaScaler) {

      String scale = getAlphaScale();

      imageView.blendImage((const TCHAR*)this->imageFileName, color, (const TCHAR*)scale);
    }
    return 0L;
  }

private:
  long size(Event& event)
  {
    int width, height;
    event.getSize(width, height);

    ClientDC dc(this);
    int h = dc.getTextHeight();

    //listView.reshape(2, 4, 230, height-6);
    listView.reshape(2, 4+4, 230, height-10);
    alphaScaler.reshape(250, 0, width - 250-10, h*2);
    imageView.reshape(250, h*2, width - 250-10, height-h*2-2);

    return 0L;
  }

private:
  void drawImage(_bstr_t& imageFieName)
  {
    TCHAR title[MAX_PATH];
    //clear();
    try {
      if (imageFileName.length()>0) {
        String scale = getAlphaScale();

        imageView.blendImage(imageFileName, color, (const TCHAR*)scale);

        _stprintf_s(title, CountOf(title), _T("%s - ImageViewer"), (const TCHAR*)imageFileName);
      
        setText(title);

        setScrollPos(HORIZONTAL, 0);

        setScrollPos(VERTICAL, 0);

      //UINT width = image->GetWidth();
      //UINT height = image->GetHeight();

      //setScrollExtent(width, height);

        invalidate((const RECT*)NULL);

        update();
      }
    } catch (...) {

    }
  }

private:
  //2012/04/16 EventHandler for WM_DROPFILES
  long dropFiles(Event& event) 
  {
    TCHAR fileName[1024];
    DropFiles drop((HDROP)event.getWParam());
    fileName[0] = ZERO;
    int num = drop.queryFile(0, fileName, CountOf(fileName));

    if(num > 0) {
      imageFileName = fileName;
      drawImage(imageFileName);
    }
    return 0;
  }

private:
  void  open(Action& action)
  {
    Args ar;
    
    TCHAR dir[MAX_PATH];
    memset(dir, (TCHAR)0, CountOf(dir));
    //Call getFileFolder method in SOL::ApplicationView.
    //This gets a previously select folder from a registry(profile of this application) for fileDialog
    if (restoreFileFolder(dir, CountOf(dir))) {
      ar.set(XmNdirectory, dir);
      fileDialog.setValues(ar);
    }
  
    if(fileDialog.open()){
      //TCHAR title[MAX_PATH];

      imageFileName = fileDialog.getFileName();
      TCHAR* filename = fileDialog.getFileName();

      TCHAR* ftitle = fileDialog.getFileTitle();
    
      File file;
      if (file.isExistent(filename)) {
        //Call saveFileFolder method in SOL::ApplicationView.
        saveFileFolder(filename);

        drawImage(imageFileName);
      }
    }  
  }

private:
  void version(Action& action)
  {    
    showMessageDialog(_T("SolImageBlender 1.0"), 
      _T("ImageBlender sample program to blend an image file with transparency on a background color."));

  }
};

}


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(XmNbackground, (COLOR_BTNFACE +1));
    SolImageBlender 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.