Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce WinForms support for Direct2D/DirectWrite. #10

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions WInterop.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28729.10
# Visual Studio Version 17
VisualStudioVersion = 17.0.31717.71
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WInterop.Desktop", "src\WInterop.Desktop\WInterop.Desktop.csproj", "{AC918478-F510-44DD-92AE-6E0B013546F5}"
ProjectSection(ProjectDependencies) = postProject
Expand Down Expand Up @@ -126,6 +126,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Scratch", "src\Samples\OldN
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WInterop.SourceGenerators", "src\WInterop.SourceGenerators\WInterop.SourceGenerators.csproj", "{89E7FD50-13F6-45DA-8823-C3F6D38170C8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WInterop.Winforms", "Winterop.Winforms\WInterop.Winforms.csproj", "{2ADA4398-33F9-4163-A669-54C82310EA08}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "WinForms", "WinForms", "{D27AB6D6-1BE5-448C-9BAE-F0D2D89E6CDC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinFormsDirect2D", "WinFormsDirect2D\WinFormsDirect2D.csproj", "{C37EC799-BBB5-46C7-A469-66677C316F9B}"
EndProject
Global
GlobalSection(SharedMSBuildProjectFiles) = preSolution
src\Common\Common.projitems*{ac918478-f510-44dd-92ae-6e0b013546f5}*SharedItemsImports = 5
Expand Down Expand Up @@ -796,6 +802,38 @@ Global
{89E7FD50-13F6-45DA-8823-C3F6D38170C8}.Release|x64.Build.0 = Release|Any CPU
{89E7FD50-13F6-45DA-8823-C3F6D38170C8}.Release|x86.ActiveCfg = Release|Any CPU
{89E7FD50-13F6-45DA-8823-C3F6D38170C8}.Release|x86.Build.0 = Release|Any CPU
{2ADA4398-33F9-4163-A669-54C82310EA08}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2ADA4398-33F9-4163-A669-54C82310EA08}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2ADA4398-33F9-4163-A669-54C82310EA08}.Debug|ARM.ActiveCfg = Debug|Any CPU
{2ADA4398-33F9-4163-A669-54C82310EA08}.Debug|ARM.Build.0 = Debug|Any CPU
{2ADA4398-33F9-4163-A669-54C82310EA08}.Debug|x64.ActiveCfg = Debug|Any CPU
{2ADA4398-33F9-4163-A669-54C82310EA08}.Debug|x64.Build.0 = Debug|Any CPU
{2ADA4398-33F9-4163-A669-54C82310EA08}.Debug|x86.ActiveCfg = Debug|Any CPU
{2ADA4398-33F9-4163-A669-54C82310EA08}.Debug|x86.Build.0 = Debug|Any CPU
{2ADA4398-33F9-4163-A669-54C82310EA08}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2ADA4398-33F9-4163-A669-54C82310EA08}.Release|Any CPU.Build.0 = Release|Any CPU
{2ADA4398-33F9-4163-A669-54C82310EA08}.Release|ARM.ActiveCfg = Release|Any CPU
{2ADA4398-33F9-4163-A669-54C82310EA08}.Release|ARM.Build.0 = Release|Any CPU
{2ADA4398-33F9-4163-A669-54C82310EA08}.Release|x64.ActiveCfg = Release|Any CPU
{2ADA4398-33F9-4163-A669-54C82310EA08}.Release|x64.Build.0 = Release|Any CPU
{2ADA4398-33F9-4163-A669-54C82310EA08}.Release|x86.ActiveCfg = Release|Any CPU
{2ADA4398-33F9-4163-A669-54C82310EA08}.Release|x86.Build.0 = Release|Any CPU
{C37EC799-BBB5-46C7-A469-66677C316F9B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C37EC799-BBB5-46C7-A469-66677C316F9B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C37EC799-BBB5-46C7-A469-66677C316F9B}.Debug|ARM.ActiveCfg = Debug|Any CPU
{C37EC799-BBB5-46C7-A469-66677C316F9B}.Debug|ARM.Build.0 = Debug|Any CPU
{C37EC799-BBB5-46C7-A469-66677C316F9B}.Debug|x64.ActiveCfg = Debug|Any CPU
{C37EC799-BBB5-46C7-A469-66677C316F9B}.Debug|x64.Build.0 = Debug|Any CPU
{C37EC799-BBB5-46C7-A469-66677C316F9B}.Debug|x86.ActiveCfg = Debug|Any CPU
{C37EC799-BBB5-46C7-A469-66677C316F9B}.Debug|x86.Build.0 = Debug|Any CPU
{C37EC799-BBB5-46C7-A469-66677C316F9B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C37EC799-BBB5-46C7-A469-66677C316F9B}.Release|Any CPU.Build.0 = Release|Any CPU
{C37EC799-BBB5-46C7-A469-66677C316F9B}.Release|ARM.ActiveCfg = Release|Any CPU
{C37EC799-BBB5-46C7-A469-66677C316F9B}.Release|ARM.Build.0 = Release|Any CPU
{C37EC799-BBB5-46C7-A469-66677C316F9B}.Release|x64.ActiveCfg = Release|Any CPU
{C37EC799-BBB5-46C7-A469-66677C316F9B}.Release|x64.Build.0 = Release|Any CPU
{C37EC799-BBB5-46C7-A469-66677C316F9B}.Release|x86.ActiveCfg = Release|Any CPU
{C37EC799-BBB5-46C7-A469-66677C316F9B}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -843,6 +881,8 @@ Global
{A6FC8457-8223-4DBE-841B-EFC82C535680} = {9BFE01D1-0EF4-4CFC-8E20-254A7E39FB14}
{2A9BA0DC-C329-45AD-972B-83E063445289} = {F5D72A2B-8550-493A-8E91-A88FBB29643D}
{78444ABC-5425-457C-882C-4C928704971C} = {2A9BA0DC-C329-45AD-972B-83E063445289}
{D27AB6D6-1BE5-448C-9BAE-F0D2D89E6CDC} = {F5D72A2B-8550-493A-8E91-A88FBB29643D}
{C37EC799-BBB5-46C7-A469-66677C316F9B} = {D27AB6D6-1BE5-448C-9BAE-F0D2D89E6CDC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1D0C3A88-8F05-4858-97B6-0E554C1064BD}
Expand Down
55 changes: 55 additions & 0 deletions WinFormsDirect2D/DemoRenderer.HelloWorld.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using WInterop.Direct2d;
using System.Drawing;
using WInterop.Winforms;
using System.Diagnostics.CodeAnalysis;
using WInterop.DirectWrite;

namespace WinFormsDirect2D
{
internal partial class DemoRenderer
{
private class HelloWorld : IRenderItem
{
protected ITextFormat? _textFormat;
protected ITextLayout? _textLayout;
protected ITypography? _typography;

protected ISolidColorBrush? _blackBrush;

public HelloWorld()
{
}

[AllowNull]
public ID2DFactoryProvider D2DFactoryProvider { get; set; }

public void InitializeComponent()
{
_textFormat = D2DFactoryProvider.GetDirectWriteFactory().CreateTextFormat("Gabriola", fontSize: 64);
_textFormat.SetTextAlignment(TextAlignment.Center);
_textFormat.SetParagraphAlignment(ParagraphAlignment.Center);
}

public void CreateD2DResources(IRenderTarget renderTarget)
{
string text = "Hello World From ... DirectWrite!";
_blackBrush = renderTarget.CreateSolidColorBrush(Color.Black);
_textLayout = D2DFactoryProvider.GetDirectWriteFactory().CreateTextLayout(text, _textFormat, renderTarget.GetSize());

// (21, 12) is the range around "DirectWrite!"
_textLayout.SetFontSize(100, (21, 12));
_typography = D2DFactoryProvider.GetDirectWriteFactory().CreateTypography();
_typography.AddFontFeature(new FontFeature(FontFeatureTag.StylisticSet7, 1));
_textLayout.SetTypography(_typography, (0, text.Length));
_textLayout.SetUnderline(true, (21, 12));
_textLayout.SetFontWeight(FontWeight.Bold, (21, 12));
}

public void D2DPaint(IRenderTarget renderTarget)
{
renderTarget.Clear(Color.CornflowerBlue);
renderTarget.DrawTextLayout(default, _textLayout, _blackBrush);
}
}
}
}
67 changes: 67 additions & 0 deletions WinFormsDirect2D/DemoRenderer.SimpleTestPicture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using WInterop.Direct2d;
using System.Drawing;
using WInterop.Winforms;
using System.Diagnostics.CodeAnalysis;

namespace WinFormsDirect2D
{
internal partial class DemoRenderer
{
private class SimpleTestPicture : IRenderItem
{
private ISolidColorBrush? _lightSlateGrayBrush;
private ISolidColorBrush? _cornflowerBlueBrush;

public SimpleTestPicture()
{
}

[AllowNull]
public ID2DFactoryProvider D2DFactoryProvider { get; set; }

public void CreateD2DResources(IRenderTarget renderTarget)
{
_lightSlateGrayBrush = renderTarget.CreateSolidColorBrush(Color.LightSlateGray);
_cornflowerBlueBrush = renderTarget.CreateSolidColorBrush(Color.CornflowerBlue);
}

public void D2DPaint(IRenderTarget renderTarget)
{
renderTarget.SetTransform();
renderTarget.Clear(Color.White);
Size size = renderTarget.GetSize().ToSize();

_lightSlateGrayBrush!.GetColor(out ColorF color);

for (int x = 0; x < size.Width; x += 10)
{
renderTarget.DrawLine(
new Point(x, 0), new Point(x, size.Height),
_lightSlateGrayBrush, 0.5f);
}

for (int y = 0; y < size.Height; y += 10)
{
renderTarget.DrawLine(
new Point(0, y), new Point(size.Width, y),
_lightSlateGrayBrush, 0.5f);
}

Rectangle rectangle1 = Rectangle.FromLTRB(
size.Width / 2 - 50,
size.Height / 2 - 50,
size.Width / 2 + 50,
size.Height / 2 + 50);

Rectangle rectangle2 = Rectangle.FromLTRB(
size.Width / 2 - 100,
size.Height / 2 - 100,
size.Width / 2 + 100,
size.Height / 2 + 100);

renderTarget.FillRectangle(rectangle1, _lightSlateGrayBrush);
renderTarget.DrawRectangle(rectangle2, _cornflowerBlueBrush);
}
}
}
}
32 changes: 32 additions & 0 deletions WinFormsDirect2D/DemoRenderer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using WInterop.Winforms;

namespace WinFormsDirect2D
{
internal partial class DemoRenderer
{
private readonly ImmutableDictionary<string, IRenderItem> _demos;

public DemoRenderer(ID2DFactoryProvider factoryProvider)
{
var temp = new Dictionary<string, IRenderItem>()
{
{
nameof(SimpleTestPicture),
new SimpleTestPicture() { D2DFactoryProvider = factoryProvider }
},
{
nameof(HelloWorld),
new HelloWorld() { D2DFactoryProvider = factoryProvider }
},

};

_demos = temp.ToImmutableDictionary();
}

public ImmutableDictionary<string, IRenderItem> Demos
=> _demos;
}
}
111 changes: 111 additions & 0 deletions WinFormsDirect2D/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions WinFormsDirect2D/MainForm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Windows.Forms;
using WInterop.Winforms;

namespace WinFormsDirect2D
{
public partial class MainForm : Form
{
private bool _resourcesCreated;
private DemoRenderer _demoRenderer;
private IRenderItem? _currentRenderItem;

public MainForm()
{
InitializeComponent();
_demoRenderer = new DemoRenderer(d2DPanel);

foreach (var item in _demoRenderer.Demos)
{
ToolStripMenuItem menuItem = new()
{
Text = item.Key,
Tag = item.Value
};

menuItem.Click += MenuItem_Click;
viewToolStripMenuItem.DropDownItems.Add(menuItem);
}
}

private void MenuItem_Click(object? sender, System.EventArgs e)
{
_currentRenderItem = (IRenderItem)((ToolStripMenuItem)sender!).Tag;
_resourcesCreated = false;
d2DPanel.Reset();
}

private void D2DPanel_D2DPaint(object sender, WInterop.Winforms.D2DPaintEventArgs e)
{
if (!_resourcesCreated || _currentRenderItem is null)
{
return;
}

_currentRenderItem.D2DPaint(e.RenderTarget);
}

private void D2DPanel_CreateD2DResources(object sender, WInterop.Winforms.D2DPaintEventArgs e)
{
_resourcesCreated = true;
if (_currentRenderItem is null)
{
return;
}

_currentRenderItem.CreateD2DResources(e.RenderTarget);
}
}
}
Loading