Skip to content

Commit da1981f

Browse files
committed
IGraphics: removed Geometry methods, only keep for D2DGraphics
1 parent 7063c90 commit da1981f

File tree

4 files changed

+157
-339
lines changed

4 files changed

+157
-339
lines changed

Source/DXControl/Graphics/D2DGraphics.cs

Lines changed: 131 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
/*
22
MIT License
3-
Copyright (C) 2022 DUONG DIEU PHAP
3+
Copyright (C) 2022 - 2023 DUONG DIEU PHAP
44
Project & license info: https://github.com/d2phap/DXControl
55
*/
66
using DirectN;
7-
using System.Drawing;
87
using System.Runtime.InteropServices;
98

109
namespace D2Phap;
@@ -303,121 +302,6 @@ public void DrawRectangle(RectangleF rect, float radius, Color borderColor, Colo
303302
#endregion // Draw/Fill Rectangle
304303

305304

306-
#region Draw / Fill Geometry
307-
308-
public GeometryObject GetCombinedRectanglesGeometry(RectangleF rect1, RectangleF rect2,
309-
float rect1Radius, float rect2Radius, CombineMode combineMode)
310-
{
311-
// create rounded rectangle 1
312-
var roundedRect1 = new D2D1_ROUNDED_RECT()
313-
{
314-
rect = new D2D_RECT_F(rect1.Left, rect1.Top, rect1.Right, rect1.Bottom),
315-
radiusX = rect1Radius,
316-
radiusY = rect1Radius,
317-
};
318-
319-
// create rounded rectangle 2
320-
var roundedRect2 = new D2D1_ROUNDED_RECT()
321-
{
322-
rect = new D2D_RECT_F(rect2.Left, rect2.Top, rect2.Right, rect2.Bottom),
323-
radiusX = rect2Radius,
324-
radiusY = rect2Radius,
325-
};
326-
327-
328-
// create geometries
329-
var shape1Geo = D2DFactory.CreateRoundedRectangleGeometry(roundedRect1);
330-
var shape22Geo = D2DFactory.CreateRoundedRectangleGeometry(roundedRect2);
331-
332-
// create path geometry to get the combined shape
333-
var pathGeo = D2DFactory.CreatePathGeometry();
334-
pathGeo.Object.Open(out var pathGeoSink).ThrowOnError();
335-
336-
337-
// combine 2 geometry shapes
338-
shape1Geo.Object.CombineWithGeometry(shape22Geo.Object, (D2D1_COMBINE_MODE)combineMode, IntPtr.Zero, 0, pathGeoSink).ThrowOnError();
339-
340-
pathGeoSink.Close();
341-
Marshal.ReleaseComObject(pathGeoSink);
342-
shape1Geo.Dispose();
343-
shape22Geo.Dispose();
344-
345-
return new GeometryObject() { D2DGeometry = pathGeo };
346-
}
347-
348-
349-
public GeometryObject GetCombinedEllipsesGeometry(RectangleF rect1, RectangleF rect2, CombineMode combineMode)
350-
{
351-
// create ellipse 1
352-
var ellipse1 = new D2D1_ELLIPSE(rect1.X + rect1.Width / 2, rect1.Y + rect1.Height / 2, rect1.Width / 2, rect1.Height / 2);
353-
354-
// create ellipse 2
355-
var ellipse2 = new D2D1_ELLIPSE(rect2.X + rect2.Width / 2, rect2.Y + rect2.Height / 2, rect2.Width / 2, rect2.Height / 2);
356-
357-
358-
// create geometries
359-
var shape1Geo = D2DFactory.CreateEllipseGeometry(ellipse1);
360-
var shape2Geo = D2DFactory.CreateEllipseGeometry(ellipse2);
361-
362-
// create path geometry to get the combined shape
363-
var pathGeo = D2DFactory.CreatePathGeometry();
364-
pathGeo.Object.Open(out var pathGeoSink).ThrowOnError();
365-
366-
367-
// combine 2 geometry shapes
368-
shape1Geo.Object.CombineWithGeometry(shape2Geo.Object, (D2D1_COMBINE_MODE)combineMode, IntPtr.Zero, 0, pathGeoSink).ThrowOnError();
369-
370-
pathGeoSink.Close();
371-
Marshal.ReleaseComObject(pathGeoSink);
372-
shape1Geo.Dispose();
373-
shape2Geo.Dispose();
374-
375-
376-
return new GeometryObject() { D2DGeometry = pathGeo };
377-
}
378-
379-
380-
public void DrawGeometry(GeometryObject geoObj, Color borderColor, Color? fillColor = null, float strokeWidth = 1f)
381-
{
382-
if (geoObj.D2DGeometry is not IComObject<ID2D1Geometry> geometry) return;
383-
384-
// draw background color -----------------------------------
385-
if (fillColor != null)
386-
{
387-
var bgColor = DXHelper.FromColor(fillColor.Value);
388-
var bgBrushStylePtr = new D2D1_BRUSH_PROPERTIES()
389-
{
390-
opacity = 1f,
391-
}.StructureToPtr();
392-
DeviceContext.CreateSolidColorBrush(bgColor, bgBrushStylePtr, out var bgBrush);
393-
394-
// fill the combined geometry
395-
DeviceContext.FillGeometry(geometry.Object, bgBrush);
396-
397-
Marshal.FreeHGlobal(bgBrushStylePtr);
398-
}
399-
400-
401-
// draw border color ----------------------------------------
402-
if (borderColor != Color.Transparent)
403-
{
404-
var bdColor = DXHelper.FromColor(borderColor);
405-
var borderBrushStylePtr = new D2D1_BRUSH_PROPERTIES()
406-
{
407-
opacity = 1f,
408-
}.StructureToPtr();
409-
DeviceContext.CreateSolidColorBrush(bdColor, borderBrushStylePtr, out var borderBrush);
410-
411-
// draw the combined geometry
412-
DeviceContext.DrawGeometry(geometry.Object, borderBrush, strokeWidth);
413-
414-
Marshal.FreeHGlobal(borderBrushStylePtr);
415-
}
416-
}
417-
418-
#endregion // Draw / Fill Geometry
419-
420-
421305
#region Draw / Measure text
422306

423307
public void DrawText(string text, string fontFamilyName, float fontSize, float x, float y, Color c, float? textDpi = null, StringAlignment hAlign = StringAlignment.Near, StringAlignment vAlign = StringAlignment.Near, bool isBold = false, bool isItalic = false)
@@ -556,4 +440,134 @@ public void ClearBackground(Color color)
556440

557441
#endregion // Others
558442

443+
444+
// D2DGraphics-only methods
445+
#region D2DGraphics-only methods
446+
447+
#region Draw / Fill Geometry
448+
449+
/// <summary>
450+
/// Get geometry from a combined 2 rectangles.
451+
/// </summary>
452+
public IComObject<ID2D1Geometry>? GetCombinedRectanglesGeometry(RectangleF rect1, RectangleF rect2,
453+
float rect1Radius, float rect2Radius, D2D1_COMBINE_MODE combineMode)
454+
{
455+
// create rounded rectangle 1
456+
var roundedRect1 = new D2D1_ROUNDED_RECT()
457+
{
458+
rect = new D2D_RECT_F(rect1.Left, rect1.Top, rect1.Right, rect1.Bottom),
459+
radiusX = rect1Radius,
460+
radiusY = rect1Radius,
461+
};
462+
463+
// create rounded rectangle 2
464+
var roundedRect2 = new D2D1_ROUNDED_RECT()
465+
{
466+
rect = new D2D_RECT_F(rect2.Left, rect2.Top, rect2.Right, rect2.Bottom),
467+
radiusX = rect2Radius,
468+
radiusY = rect2Radius,
469+
};
470+
471+
472+
// create geometries
473+
var shape1Geo = D2DFactory.CreateRoundedRectangleGeometry(roundedRect1);
474+
var shape22Geo = D2DFactory.CreateRoundedRectangleGeometry(roundedRect2);
475+
476+
// create path geometry to get the combined shape
477+
var pathGeo = D2DFactory.CreatePathGeometry();
478+
pathGeo.Object.Open(out var pathGeoSink).ThrowOnError();
479+
480+
481+
// combine 2 geometry shapes
482+
shape1Geo.Object.CombineWithGeometry(shape22Geo.Object, combineMode, IntPtr.Zero, 0, pathGeoSink).ThrowOnError();
483+
484+
pathGeoSink.Close();
485+
Marshal.ReleaseComObject(pathGeoSink);
486+
shape1Geo.Dispose();
487+
shape22Geo.Dispose();
488+
489+
return pathGeo;
490+
}
491+
492+
493+
/// <summary>
494+
/// Get geometry from a combined 2 ellipses.
495+
/// </summary>
496+
public IComObject<ID2D1Geometry>? GetCombinedEllipsesGeometry(RectangleF rect1, RectangleF rect2, D2D1_COMBINE_MODE combineMode)
497+
{
498+
// create ellipse 1
499+
var ellipse1 = new D2D1_ELLIPSE(rect1.X + rect1.Width / 2, rect1.Y + rect1.Height / 2, rect1.Width / 2, rect1.Height / 2);
500+
501+
// create ellipse 2
502+
var ellipse2 = new D2D1_ELLIPSE(rect2.X + rect2.Width / 2, rect2.Y + rect2.Height / 2, rect2.Width / 2, rect2.Height / 2);
503+
504+
505+
// create geometries
506+
var shape1Geo = D2DFactory.CreateEllipseGeometry(ellipse1);
507+
var shape2Geo = D2DFactory.CreateEllipseGeometry(ellipse2);
508+
509+
// create path geometry to get the combined shape
510+
var pathGeo = D2DFactory.CreatePathGeometry();
511+
pathGeo.Object.Open(out var pathGeoSink).ThrowOnError();
512+
513+
514+
// combine 2 geometry shapes
515+
shape1Geo.Object.CombineWithGeometry(shape2Geo.Object, combineMode, IntPtr.Zero, 0, pathGeoSink).ThrowOnError();
516+
517+
pathGeoSink.Close();
518+
Marshal.ReleaseComObject(pathGeoSink);
519+
shape1Geo.Dispose();
520+
shape2Geo.Dispose();
521+
522+
523+
return pathGeo;
524+
}
525+
526+
527+
/// <summary>
528+
/// Draw geometry.
529+
/// </summary>
530+
public void DrawGeometry(IComObject<ID2D1Geometry>? geo, Color borderColor, Color? fillColor = null, float strokeWidth = 1f)
531+
{
532+
if (geo is not IComObject<ID2D1Geometry> geometry) return;
533+
534+
// draw background color -----------------------------------
535+
if (fillColor != null)
536+
{
537+
var bgColor = DXHelper.FromColor(fillColor.Value);
538+
var bgBrushStylePtr = new D2D1_BRUSH_PROPERTIES()
539+
{
540+
opacity = 1f,
541+
}.StructureToPtr();
542+
DeviceContext.CreateSolidColorBrush(bgColor, bgBrushStylePtr, out var bgBrush);
543+
544+
// fill the combined geometry
545+
DeviceContext.FillGeometry(geometry.Object, bgBrush);
546+
547+
Marshal.FreeHGlobal(bgBrushStylePtr);
548+
}
549+
550+
551+
// draw border color ----------------------------------------
552+
if (borderColor != Color.Transparent)
553+
{
554+
var bdColor = DXHelper.FromColor(borderColor);
555+
var borderBrushStylePtr = new D2D1_BRUSH_PROPERTIES()
556+
{
557+
opacity = 1f,
558+
}.StructureToPtr();
559+
DeviceContext.CreateSolidColorBrush(bdColor, borderBrushStylePtr, out var borderBrush);
560+
561+
// draw the combined geometry
562+
DeviceContext.DrawGeometry(geometry.Object, borderBrush, strokeWidth);
563+
564+
Marshal.FreeHGlobal(borderBrushStylePtr);
565+
}
566+
}
567+
568+
#endregion // Draw / Fill Geometry
569+
570+
571+
#endregion
572+
559573
}

Source/DXControl/Graphics/GdipGraphics.cs

Lines changed: 1 addition & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
MIT License
3-
Copyright (C) 2022 DUONG DIEU PHAP
3+
Copyright (C) 2022 - 2023 DUONG DIEU PHAP
44
Project & license info: https://github.com/d2phap/DXControl
55
*/
66
using System.Drawing.Drawing2D;
@@ -193,112 +193,6 @@ public void DrawRectangle(RectangleF rect, float radius, Color borderColor, Colo
193193
#endregion // Draw / Fill Rectangle
194194

195195

196-
#region Draw / Fill Geometry
197-
198-
public GeometryObject GetCombinedRectanglesGeometry(RectangleF rect1, RectangleF rect2,
199-
float rect1Radius, float rect2Radius, CombineMode combineMode)
200-
{
201-
var path1 = GetRoundRectanglePath(rect1, rect1Radius);
202-
var path2 = GetRoundRectanglePath(rect2, rect2Radius);
203-
var region = new Region(path1);
204-
205-
if (combineMode == CombineMode.Union)
206-
{
207-
region.Union(path2);
208-
}
209-
else if (combineMode == CombineMode.Intesect)
210-
{
211-
region.Intersect(path2);
212-
}
213-
else if (combineMode == CombineMode.Xor)
214-
{
215-
region.Xor(path2);
216-
}
217-
else if (combineMode == CombineMode.Exclude)
218-
{
219-
region.Exclude(path2);
220-
}
221-
222-
return new GeometryObject()
223-
{
224-
GdiRegion = region,
225-
GdiPath = path1,
226-
};
227-
}
228-
229-
230-
public GeometryObject GetCombinedEllipsesGeometry(RectangleF rect1, RectangleF rect2, CombineMode combineMode)
231-
{
232-
// create ellipse paths
233-
var path1 = new GraphicsPath();
234-
path1.AddEllipse(rect1);
235-
236-
var path2 = new GraphicsPath();
237-
path2.AddEllipse(rect2);
238-
239-
240-
// combine 2 paths
241-
var region = new Region(path1);
242-
243-
if (combineMode == CombineMode.Union)
244-
{
245-
region.Union(path2);
246-
}
247-
else if (combineMode == CombineMode.Intesect)
248-
{
249-
region.Intersect(path2);
250-
}
251-
else if (combineMode == CombineMode.Xor)
252-
{
253-
region.Xor(path2);
254-
}
255-
else if (combineMode == CombineMode.Exclude)
256-
{
257-
region.Exclude(path2);
258-
}
259-
260-
return new GeometryObject()
261-
{
262-
GdiRegion = region,
263-
GdiPath = path1,
264-
};
265-
}
266-
267-
268-
/// <summary>
269-
/// Draw geometry.
270-
/// <para>
271-
/// <c>**Note:</c>
272-
/// Currently GDI+ only draws correctly for 2 mode: <see cref="CombineMode.Intesect"/> and <see cref="CombineMode.Exclude"/>
273-
/// </para>
274-
/// </summary>
275-
public void DrawGeometry(GeometryObject geoObj, Color borderColor, Color? fillColor = null, float strokeWidth = 1)
276-
{
277-
if (geoObj.GdiRegion is not Region region) return;
278-
if (geoObj.GdiPath is not GraphicsPath path) return;
279-
280-
Graphics.Clip = region;
281-
282-
// fill background
283-
if (fillColor != null)
284-
{
285-
using var bgBrush = new SolidBrush(fillColor.Value);
286-
Graphics.FillPath(bgBrush, path);
287-
}
288-
289-
// draw border
290-
if (borderColor != Color.Transparent)
291-
{
292-
using var borderPen = new Pen(borderColor, strokeWidth);
293-
Graphics.DrawPath(borderPen, path);
294-
}
295-
296-
Graphics.ResetClip();
297-
}
298-
299-
#endregion // Draw / Fill Geometry
300-
301-
302196
#region Draw / Measure text
303197

304198
public void DrawText(string text, string fontFamilyName, float fontSize, float x, float y, Color c, float? textDpi = null, StringAlignment hAlign = StringAlignment.Near, StringAlignment vAlign = StringAlignment.Near, bool isBold = false, bool isItalic = false)

0 commit comments

Comments
 (0)