SOL9 Sample: WICImageViewer
|
1 Screenshot
2 Source code
/*
* WICImageViewer.cpp
* Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
// 2015/10/01 This is a simple ImageViewer sample program based on WIC and DirectX APIs.
// 2015/10/10 Modified to use WICBitmapFileReader.
#define COMMONCONTROLS_V6
#include <sol/COMInitializer.h>
#include <sol/StringConverter.h>
#include <sol/FileDialog.h>
#include <sol/wic/WICImagingFactory.h>
#include <sol/wic/WICFormatConverter.h>
#include <sol/wic/WICBitmapDecoder.h>
#include <sol/wic/WICBitmapFrameDecode.h>
#include <sol/wic/WICBitmapFileReader.h>
#include <sol/directx/DirectXView.h>
#include <sol/directx/DirectXMainView.h>
#include <sol/directx/Direct2D1Factory.h>
#include <sol/directx/Direct2D1HwndRenderTarget.h>
#include <sol/directx/Direct2D1Bitmap.h>
#include "resource.h"
namespace SOL {
class MainView :public DirectXMainView {
private:
//Inner class starts.
class SimpleView : public DirectXView {
private:
SmartPtr<Direct2D1HwndRenderTarget> renderTarget;
SmartPtr<Direct2D1Bitmap> bitmap;
StringT<wchar_t> url; //Url for an image file
private:
void resize(int w, int h)
{
try {
auto size = D2D1::SizeU(w, h);
renderTarget ->resize(size);
} catch (Exception& ex) {
ex.display();
}
}
void display()
{
if (bitmap == NULL) {
return;
}
try {
if (!(renderTarget -> checkWindowState() & D2D1_WINDOW_STATE_OCCLUDED)){
renderTarget -> beginDraw();
renderTarget -> setTransform(D2D1::Matrix3x2F::Identity());
renderTarget -> clear(D2D1::ColorF(D2D1::ColorF::White));
auto size = renderTarget->getSize();
auto rectangle = D2D1::RectF(0.0f, 0.0f, size.width, size.height);
renderTarget -> drawBitmap(*bitmap, rectangle);
renderTarget->endDraw();
}
} catch (Exception& ex) {
ex.display();
}
}
public:
SimpleView(MainView* parent, const TCHAR* name, Args& args)
:DirectXView(parent, name, args)
{
try {
Direct2D1Factory* d2d1Factory = parent -> getD2D1Factory();
renderTarget = new Direct2D1HwndRenderTarget(*d2d1Factory, getWindow());
} catch (Exception& ex) {
ex.display();
}
}
~SimpleView()
{
}
void loadBitmap(const TCHAR* filename)
{
try {
StringConverter converter;
wchar_t* url = converter.toWideChar(filename);
WICImagingFactory* imagingFactory = getImagingFactory();
WICBitmapFileReader reader(*imagingFactory, url);
delete[] url;
//2015/10/10
Direct2D1Bitmap* tbitmap = reader.read(*renderTarget);
if (tbitmap) {
bitmap = tbitmap;
}
invalidateAll();
} catch (Exception& ex) {
ex.display();
}
}
};
// Inner class ends.
private:
SmartPtr<SimpleView> view;
FileDialog fileDialog;
public:
// Constructor
MainView(Application& applet, const TCHAR* name, Args& args)
:DirectXMainView(applet, name,
args.set(XmNstyle, (ulong)WS_CLIPCHILDREN) )
{
try {
Args ar;
view = new SimpleView(this, _T(""), ar);
addEventHandler(WM_SIZE, this,
(Handler)&MainView::size, NULL);
} catch (Exception& ex) {
ex.display();
}
Args ar;
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);
restorePlacement();
}
public:
~MainView()
{
}
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* filename = fileDialog.getFileName();
TCHAR* ftitle = fileDialog.getFileTitle();
File file;
if (file.isExistent(filename)) {
//Call saveFileFolder method in SOL::ApplicationView.
saveFileFolder(filename);
view -> loadBitmap(filename);
TCHAR caption[MAX_PATH];
_stprintf_s(caption, CountOf(caption), _T("%s - %s"), filename, getAppName());
setText(caption);
}
}
}
private:
long size(Event& event)
{
int w, h;
event.getSize(w, h);
if (view.notNull()) {
view -> reshape(10, 10, w-20, h-20);
}
return 0;
}
};
}
//////////////////////////////////////////////
//
void Main(int argc, TCHAR** argv)
{
const TCHAR* appClass = appName(argv[0]);
try {
HeapSetInformation(nullptr, HeapEnableTerminationOnCorruption, nullptr, 0);
COMInitializer initializer( COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
Application applet(appClass, argc, argv);
Args args;
MainView view(applet, appClass, args);
view.realize();
applet.run();
} catch (Exception& ex) {
ex.display();
}
}
Last modified: 8 Dec 2016
Copyright (c) 2016 Antillia.com ALL RIGHTS RESERVED.