diff --git a/src/D2DLibExport/D2DDevice.cs b/src/D2DLibExport/D2DDevice.cs index d5409d958..4b1210b45 100644 --- a/src/D2DLibExport/D2DDevice.cs +++ b/src/D2DLibExport/D2DDevice.cs @@ -100,7 +100,7 @@ public D2DRectangleGeometry CreateRectangleGeometry(FLOAT width, FLOAT height) public D2DRectangleGeometry CreateRectangleGeometry(D2DRect rect) { - HANDLE rectGeometryHandle = D2D.CreateRectangleGeometry(this.Handle, ref rect); + HANDLE rectGeometryHandle = D2D.CreateRectangleGeometry(this.Handle, rect); return new D2DRectangleGeometry(this, rectGeometryHandle); } diff --git a/src/D2DLibExport/D2DLib.cs b/src/D2DLibExport/D2DLib.cs index d4bda2847..a6acc87b8 100644 --- a/src/D2DLibExport/D2DLib.cs +++ b/src/D2DLibExport/D2DLib.cs @@ -219,7 +219,7 @@ public static extern HANDLE CreateTextPathGeometry(HANDLE ctx, [In] string text, #region Geometry [DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)] - public static extern HANDLE CreateRectangleGeometry([In] HANDLE ctx, [In] ref D2DRect rect); + public static extern HANDLE CreateRectangleGeometry([In] HANDLE ctx, [In] D2DRect rect); [DllImport(DLL_NAME, CallingConvention = CallingConvention.Cdecl)] public static extern void DestroyGeometry(HANDLE geometryContext); diff --git a/src/Examples/Examples.csproj b/src/Examples/Examples.csproj index adb2c7701..b739430d5 100644 --- a/src/Examples/Examples.csproj +++ b/src/Examples/Examples.csproj @@ -19,4 +19,10 @@ + + + Form + + + diff --git a/src/Examples/SampleCode/Test.cs b/src/Examples/SampleCode/Test.cs new file mode 100644 index 000000000..c2d63ae14 --- /dev/null +++ b/src/Examples/SampleCode/Test.cs @@ -0,0 +1,78 @@ +/* +* MIT License +* +* Copyright (c) 2009-2021 Jingwood, unvell.com. All right reserved. +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in all +* copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +* SOFTWARE. +*/ + +namespace unvell.D2DLib.Examples.SampleCode +{ + public partial class TestForm : ExampleForm + { + public TestForm() + { + Text = "HelloWorld - d2dlib Examples"; + } + + protected override void OnRender(D2DGraphics g) + + { + + g.Clear(D2DColor.FromGDIColor(this.BackColor)); + + + + using (var rect = this.Device.CreateRectangleGeometry(new D2DRect(10, 10, 20, 20))) + + { + + g.DrawPath(rect, D2DColor.BlueViolet, 1, D2DDashStyle.Dash); + + } + + + + using (var circle = this.Device.CreateEllipseGeometry(new D2DPoint(100, 100), new D2DSize(50, 50))) + + { + + using (var pen = this.Device.CreatePen(D2DColor.BlueViolet, D2DDashStyle.Dash)) + + { + + if (pen != null) + + { + + g.DrawPath(circle, pen, 2); + + g.FillPath(circle, D2DColor.DarkBlue); + + } + + } + + } + + } + + } + +} diff --git a/src/d2dlib/Context.h b/src/d2dlib/Context.h index a056f7c0d..1e22de050 100644 --- a/src/d2dlib/Context.h +++ b/src/d2dlib/Context.h @@ -71,6 +71,7 @@ enum GeometryType { typedef struct D2DGeometryContext { D2DContext* d2context; + GeometryType geometryType; ID2D1Geometry* geometry; } D2DGeometryContext; diff --git a/src/d2dlib/Geometry.cpp b/src/d2dlib/Geometry.cpp index b9ab40277..8e1627fd0 100644 --- a/src/d2dlib/Geometry.cpp +++ b/src/d2dlib/Geometry.cpp @@ -33,12 +33,15 @@ void DestroyGeometry(HANDLE geometryHandle) { delete context; } -HANDLE CreateRectangleGeometry(HANDLE ctx, D2D1_RECT_F& rect) +HANDLE CreateRectangleGeometry(HANDLE ctx, D2D1_RECT_F rect) { RetrieveContext(ctx); ID2D1RectangleGeometry* rectGeo; - context->factory->CreateRectangleGeometry(rect, &rectGeo); + HRESULT hr = context->factory->CreateRectangleGeometry(&rect, &rectGeo); + if (!SUCCEEDED(hr)) { + return NULL; + } D2DGeometryContext* pathCtx = new D2DGeometryContext(); pathCtx->d2context = context; @@ -181,32 +184,51 @@ void AddPathArc(HANDLE ctx, D2D1_POINT_2F endPoint, D2D1_SIZE_F size, FLOAT swee pathContext->sink->AddArc(&seg); } -void DrawPath(HANDLE pathCtx, D2D1_COLOR_F strokeColor, FLOAT strokeWidth, D2D1_DASH_STYLE dashStyle) +void DrawGeometry(HANDLE geometryHandler, D2D1_COLOR_F strokeColor, FLOAT strokeWidth, D2D1_DASH_STYLE dashStyle) { - D2DPathContext* pathContext = reinterpret_cast(pathCtx); - D2DContext* context = pathContext->d2context; - + D2DGeometryContext* geoContext = reinterpret_cast(geometryHandler); + if (geoContext == NULL) { + return; + } + + D2DContext* context = geoContext->d2context; ID2D1Factory* factory = context->factory; ID2D1RenderTarget* renderTarget = context->renderTarget; ID2D1SolidColorBrush* strokeBrush = NULL; - renderTarget->CreateSolidColorBrush(strokeColor, &strokeBrush); + HRESULT hr = renderTarget->CreateSolidColorBrush(strokeColor, &strokeBrush); + if (!SUCCEEDED(hr) || strokeBrush == NULL) { + return; + } - ID2D1StrokeStyle *strokeStyle = NULL; + ID2D1StrokeStyle* strokeStyle = NULL; if (dashStyle != D2D1_DASH_STYLE_SOLID) { factory->CreateStrokeStyle(D2D1::StrokeStyleProperties( - D2D1_CAP_STYLE_FLAT, - D2D1_CAP_STYLE_FLAT, - D2D1_CAP_STYLE_ROUND, - D2D1_LINE_JOIN_MITER, - 10.0f, - dashStyle, - 0.0f), NULL, 0, &strokeStyle); + D2D1_CAP_STYLE_FLAT, + D2D1_CAP_STYLE_FLAT, + D2D1_CAP_STYLE_ROUND, + D2D1_LINE_JOIN_MITER, + 10.0f, + dashStyle, + 0.0f), NULL, 0, &strokeStyle); } - renderTarget->DrawGeometry(pathContext->path, strokeBrush, strokeWidth, strokeStyle); + switch(geoContext->geometryType) { + default: + // unknown type, abort to render + break; + + case GeometryType::GeoType_RectangleGeometry: + ID2D1RectangleGeometry* rectGeomtry = reinterpret_cast(geoContext->geometry); + renderTarget->DrawGeometry(rectGeomtry, strokeBrush, strokeWidth, strokeStyle); + break; + + case GeometryType::GeoType_PathGeometry: + D2DPathContext* pathContext = reinterpret_cast(geoContext); + renderTarget->DrawGeometry(pathContext->path, strokeBrush, strokeWidth, strokeStyle); + break; SafeRelease(&strokeBrush); SafeRelease(&strokeStyle);