SOL9 Sample: DIBSection

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


// SOL9
// 2012/06/28

#include <sol\ApplicationView.h>
#include <sol\PaintDC.h>
#include <sol\ClientDC.h>
#include <sol\DIBSection.h>
#include <sol\AffineTransform.h>
#include <sol\MessageFont.h>
#include <sol\CourierFont.h>
#include <sol\FileDialog.h>
#include <sol\Icon.h>

#include "resource.h"

// This is a sample program for SOL::DIBSection class.
// 1 Create an instance of DIBSection class,
// 2 Draw pixels, texts and an icon on it. 
// 3 Create a rotated image from it,
// 4 Save the image as an image file of format 'bmp', 'jpg', 'png', 'tif' or 'gif'.

namespace SOL {

class AppView :public ApplicationView {
private:
  static const int IMAGE_WIDTH  = 400;
  static const int IMAGE_HEIGHT = 400;

private:
  DIBSection   baseImage;
  DIBSection*  rotatedImage;

  MessageFont  mfont;
  CourierFont  cfont;
  
  FileDialog fileDialog;

  public:
  /**
   * Construtor
   */
  AppView(Application& applet, const TCHAR* label, Args& args)
    :ApplicationView(applet, label, args)
  {
    mfont.create(18);
    cfont.create(12);

    ClientDC cdc(this);

    // Create a baseImage by using DIBSection class.
    createBaseImage(cdc);

    AffineTransform transform;
    // Create a rotatedImage from baseImage by using AffineTransform class.
    rotatedImage = transform.rotate(cdc, baseImage, 30.0);

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

    fileDialog.create(this, _T(""), ar);

    addEventHandler(WM_PAINT, this, (Handler)&AppView::paint, NULL);

    addCallback(XmNmenuCallback, IDM_SAVEAS, this, (Callback)&AppView::saveAs, NULL);
    addCallback(XmNmenuCallback, IDM_EXIT, this, (Callback)&AppView::exit, NULL);

  }

public:
  /**
   * Destructor
   */
  ~AppView() {
    delete rotatedImage;
  }

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);
      TCHAR jpgFileName[1024];
      _stprintf_s(jpgFileName, CountOf(jpgFileName), _T("%s.jpg"), _T("DIBSection"));
      ar.set(XmNfileName, jpgFileName);
      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);
          
        TCHAR msg[1024];
        _stprintf_s(msg, CountOf(msg), _T("Failed to save an image file '%s'"), filename);

        try {
          bool rc = rotatedImage->saveAs(filename);
          if (rc == false) {
            showMessageDialog(msg, MB_OK|MB_ICONERROR);
          } else {
            setCaption(filename);
            _stprintf_s(msg, CountOf(msg), _T("OK. Saved an image file '%s'"), filename);
            showMessageDialog(msg, MB_OK|MB_ICONINFORMATION);
          }
        } catch (...) {
          showMessageDialog(msg, MB_OK|MB_ICONERROR);
        }
      }
    }  
  }

private:
  void createBaseImage(DC cdc) {
    int num = 0xff;

    baseImage.create(cdc, IMAGE_WIDTH, IMAGE_HEIGHT);
    for (int r = 0; r<num; r++) {
      for(int g = 0; g<num; g++) {
        for(int b = 0; b<num; b++) {
          baseImage.setPixel(b + r,g + r, RGB(r, g, b));
        }
      }
    }
    

    // Draw two text strings into a MemoryDC of the baseImage .
    DC dc = baseImage.getDC();
    const TCHAR* string = _T("SOL9 C++ Class Library");
    const TCHAR* antillia = _T("http://www.antillia.com/");

    HFONT hf = (HFONT)dc.select(&mfont);
    dc.setBkMode(TRANSPARENT);
    dc.setTextColor(RGB(255,255,255));
    dc.textOut(4, 40, string, strlen(string));
    dc.select(hf);

    dc.setTextColor(RGB(0, 0, 0));
    hf =(HFONT)dc.select(&cfont);
    dc.textOut(8, 200, antillia, strlen(antillia));
    dc.select(hf);

    //DrawIcon
    Icon icon(IDI_PC);
    dc.drawIconEx(80, 80, (HICON)icon, 100, 100);
  }

private:
  void setCaption(const TCHAR* fileName)
  {
    TCHAR title[1024];
    _stprintf_s(title, CountOf(title), _T("%s - DIBSection"), fileName);
    setText(title);
  }

private:
  /**
   * WM_PAINT event handler.
   */
  long paint(Event& event)
  {
    PaintDC pdc(this);

    if (rotatedImage) {
      rotatedImage->draw(pdc, 10, 10);  
    }
  
    return 0;
  }
};

}

//////////////////////////////////////////
// Program Main
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(XmNwidth, 660);
    args.set(XmNheight, 580);

    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.