SOL9 Sample: Direct2DGradientBrush
|
1 Screenshot
2 Source code
/*
* Direct2DGradientBrush.cpp
* Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
// 2015/10/01
#define COMMONCONTROLS_V6
#include <sol/ApplicationView.h>
#include <sol/COMInitializer.h>
#include <sol/PaintDC.h>
#include <sol/StringT.h>
#include <sol/directx/DirectXMainView.h>
#include <sol/directx/DirectXView.h>
#include <sol/directx/Direct2D1EllipseGeometry.h>
#include <sol/directx/Direct2D1RectangleGeometry.h>
#include <sol/directx/Direct2D1RoundedRectangleGeometry.h>
#include <sol/directx/Direct2D1LinearGradientBrush.h>
#include <sol/directx/Direct2D1GradientStopCollection.h>
#include <sol/directx/Direct2D1RadialGradientBrush.h>
#include "resource.h"
namespace SOL {
class MainView :public DirectXMainView {
private:
//Inner class starts.
class SimpleView : public DirectXView {
private:
SmartPtr<Direct2D1HwndRenderTarget> renderTarget;
SmartPtr<Direct2D1RectangleGeometry> rectangle;
SmartPtr<Direct2D1EllipseGeometry> radialEllipse;
SmartPtr<Direct2D1RoundedRectangleGeometry> roundedRectangle;
SmartPtr<Direct2D1LinearGradientBrush> blueLinearGradientBrush;
SmartPtr<Direct2D1LinearGradientBrush> greenLinearGradientBrush;
SmartPtr<Direct2D1RadialGradientBrush> redRadialGradientBrush;
private:
void resize(int w, int h)
{
try {
auto size = D2D1::SizeU(w, h);
renderTarget ->resize(size);
} catch (Exception& ex) {
ex.display();
}
}
void display()
{
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();
renderTarget -> setTransform(
D2D1::Matrix3x2F::Translation(0, size.height- 300)
);
renderTarget -> fillGeometry(*rectangle,
*greenLinearGradientBrush);
renderTarget -> fillGeometry(*roundedRectangle,
*blueLinearGradientBrush);
renderTarget -> fillGeometry(*radialEllipse,
*redRadialGradientBrush);
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());
rectangle = new Direct2D1RectangleGeometry(*d2d1Factory,
D2D1::RectF(400.0f, 200.0f, 200.0f, 100.0f) );
radialEllipse = new Direct2D1EllipseGeometry(*d2d1Factory,
D2D1::Ellipse( D2D1::Point2F(200, 0), 80.0f, 60.0f) );
roundedRectangle = new Direct2D1RoundedRectangleGeometry(*d2d1Factory,
D2D1::RoundedRect(D2D1::RectF(500.0f, 0.0f, 300.0f, 80.0f), 30.0f, 30.0f) );
static const D2D1_GRADIENT_STOP redStops[] = {
{0.f, D2D1::ColorF(1.0f, 1.0f, 0.f,0.5f)},
{1.f, D2D1::ColorF(1.f, 0.5f, 0.5f, 1.0f)}
};
static const D2D1_GRADIENT_STOP greenStops[] = {
{ 0.f, D2D1::ColorF(0.f, 1.f, 1.f, 0.5f)},
{ 1.f, D2D1::ColorF( 0.f, 1.f, 0.f, 1.f )},
};
static const D2D1_GRADIENT_STOP blueStops[] = {
{ 0.f, D2D1::ColorF(0.f, 1.f, 1.f, 0.5f)},
{ 1.f, D2D1::ColorF( 0.f, 0.f, 1.f, 1.f )},
};
Direct2D1GradientStopCollection *redGradientStops = new Direct2D1GradientStopCollection(
*renderTarget,
redStops,
CountOf(redStops));
Direct2D1GradientStopCollection *greenGradientStops = new Direct2D1GradientStopCollection(
*renderTarget,
greenStops,
CountOf(greenStops));
Direct2D1GradientStopCollection *blueGradientStops = new Direct2D1GradientStopCollection(
*renderTarget,
blueStops,
CountOf(blueStops));
blueLinearGradientBrush = new Direct2D1LinearGradientBrush(*renderTarget,
D2D1::LinearGradientBrushProperties(
D2D1::Point2F(100, 0),
D2D1::Point2F(400, 400)),
D2D1::BrushProperties(),
*blueGradientStops);
greenLinearGradientBrush = new Direct2D1LinearGradientBrush(*renderTarget,
D2D1::LinearGradientBrushProperties(
D2D1::Point2F(100, 0),
D2D1::Point2F(400, 400)),
D2D1::BrushProperties(),
*greenGradientStops);
redRadialGradientBrush = new Direct2D1RadialGradientBrush(*renderTarget,
D2D1::RadialGradientBrushProperties(
D2D1::Point2F(100, 0),
D2D1::Point2F(100, 100),
100.0f, 100.0f),
D2D1::BrushProperties(),
*redGradientStops);
delete blueGradientStops;
delete redGradientStops;
delete greenGradientStops;
} catch (Exception& ex) {
ex.display();
}
}
~SimpleView()
{
}
};
// Inner class ends.
private:
SmartPtr<SimpleView> view;
public:
// Constructor
MainView(Application& applet, const TCHAR* name, Args& args)
:DirectXMainView(applet, name,
args.set(XmNstyle, (ulong)WS_CLIPCHILDREN) )
{
try {
Args ar;
ar.set(XmNstyle, WS_BORDER|WS_CHILD|WS_VISIBLE);
view = new SimpleView(this, _T(""), ar);
addEventHandler(WM_SIZE, this,
(Handler)&MainView::size, NULL);
} catch (Exception& ex) {
ex.display();
}
restorePlacement();
}
public:
~MainView()
{
}
private:
long size(Event& event)
{
int w, h;
event.getSize(w, h);
if (view.notNull()) {
view -> reshape(10, 10, w-20, h-20);
view -> postResizeRequest(w - 20, h - 20);
}
return 0;
}
};
}
//////////////////////////////////////////////
//
void Main(int argc, TCHAR** argv)
{
const TCHAR* appClass = appName(argv[0]);
TCHAR directory[MAX_PATH];
appDirectory(argv[0], directory, CountOf(directory));
try {
HeapSetInformation(nullptr, HeapEnableTerminationOnCorruption, nullptr, 0);
COMInitializer initializer( COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
Application applet(appClass, argc, argv);
Args args;
args.set(XmNapplicationDirectory, directory);
MainView imageViewer(applet, appClass, args);
imageViewer.realize();
applet.run();
} catch (Exception& ex) {
ex.display();
}
}
Last modified: 8 Dec 2016
Copyright (c) 2016 Antillia.com ALL RIGHTS RESERVED.