|
1 | 1 | /* |
2 | 2 | MIT License |
3 | | -Copyright (C) 2022 DUONG DIEU PHAP |
| 3 | +Copyright (C) 2022 - 2023 DUONG DIEU PHAP |
4 | 4 | Project & license info: https://github.com/d2phap/DXControl |
5 | 5 | */ |
6 | 6 | using DirectN; |
7 | | -using System.Drawing; |
8 | 7 | using System.Runtime.InteropServices; |
9 | 8 |
|
10 | 9 | namespace D2Phap; |
@@ -303,121 +302,6 @@ public void DrawRectangle(RectangleF rect, float radius, Color borderColor, Colo |
303 | 302 | #endregion // Draw/Fill Rectangle |
304 | 303 |
|
305 | 304 |
|
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 | | - |
421 | 305 | #region Draw / Measure text |
422 | 306 |
|
423 | 307 | 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) |
556 | 440 |
|
557 | 441 | #endregion // Others |
558 | 442 |
|
| 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 | + |
559 | 573 | } |
0 commit comments