Skip to content

Commit

Permalink
Host WPF UserControl (#2)
Browse files Browse the repository at this point in the history
* add WPF UserControl
  • Loading branch information
josef-poetzl authored Apr 14, 2024
1 parent d38b612 commit eb1cddb
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 24 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# VbeUserControlHost
.net UserControl host for VBIDE (VBE Add-ins)
.NET UserControl host for VBIDE (VBE Add-ins)

COM registration of VbeUserControlHost required

Expand All @@ -11,7 +11,7 @@ COM registration of VbeUserControlHost required

```
var vbeControl = new VbeUserControl<TypeOfUserControlToHost>(
AddIn,
VbeAddIn,
"VBE Window Caption",
UserControlToHost.PositionGuid,
InstanceOfUserControlToHost);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,13 @@
<HintPath>..\packages\ThammimTech.Microsoft.Vbe.Interop.15.0.0\lib\net20\Microsoft.Vbe.Interop.dll</HintPath>
<EmbedInteropTypes>True</EmbedInteropTypes>
</Reference>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="WindowsBase" />
<Reference Include="WindowsFormsIntegration" />
</ItemGroup>
<ItemGroup>
<Compile Include="Config.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;


namespace AccessCodeLib.Common.VBIDETools
{
[ComVisible(true)]
[Guid("0EEEA3E7-68D6-49BA-8536-572E69CCCEF0")]
public interface IVbeUserControlHost
[Guid("007610E2-E9A0-498E-867E-36AA8BC4D71C")]
public interface IVbeUserControlHost : IVbeWindowsFormsUserControlHost, IVbeWPFUserControlHost
{
}

[ComVisible(true)]
[Guid("326CB39E-C621-41F6-833D-BACE54D26763")]
public interface IVbeWindowsFormsUserControlHost
{
void HostUserControl(UserControl UserControlToHost);
void HostUserControl(System.Windows.Forms.UserControl UserControlToHost);
}

[ComVisible(true)]
[Guid("A5014EE3-82C7-4790-B12F-4EE2C95092D2")]
public interface IVbeWPFUserControlHost
{
void HostUserControl(System.Windows.Controls.UserControl UserControlToHost);
}


}
44 changes: 29 additions & 15 deletions source/AccessCodeLib.Common.VbeUserControlHost/VbeUserControl.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Forms = System.Windows.Forms;
using WPF = System.Windows.Controls;
using Microsoft.Vbe.Interop;

namespace AccessCodeLib.Common.VBIDETools
{
public class VbeUserControl<TControl> : IDisposable
where TControl : UserControl
{
private readonly TControl _control;
private readonly Window _vbeWindow;
Expand All @@ -21,14 +21,28 @@ public VbeUserControl(AddIn addIn, string caption, string positionGuid,
caption, positionGuid, ref docObj);
_vbeWindow.Visible = true;

if (!(docObj is IVbeUserControlHost userControlHost))
_control = controlToHost;
if (_control is Forms.UserControl winFormControl)
{
throw new InvalidComObjectException(string.Format("docObj cannot be casted to IVbeUserControlHost"));
if (!(docObj is IVbeWindowsFormsUserControlHost userControlHost))
{
throw new InvalidComObjectException(string.Format("docObj cannot be casted to IVbeWindowsFormsUserControlHost"));
}
userControlHost.HostUserControl(winFormControl);
}

_control = controlToHost;
userControlHost.HostUserControl(_control);

else if (_control is WPF.UserControl wpfControl)
{
if (!(docObj is IVbeWPFUserControlHost userControlHost))
{
throw new InvalidComObjectException(string.Format("docObj cannot be casted to IVbeWPFUserControlHost"));
}
userControlHost.HostUserControl(wpfControl);
}
else
{
throw new ArgumentException("controlToHost must be a System.Windows.Forms.UserControl or a System.Windows.Controls.UserControl");
}

if (!visible)
{
_vbeWindow.Visible = false;
Expand Down Expand Up @@ -99,7 +113,7 @@ public void Dispose()
#endregion
}

internal class SubClassingResizeWindow : NativeWindow
internal class SubClassingResizeWindow : Forms.NativeWindow
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
Expand All @@ -120,11 +134,11 @@ struct RECT
public int Bottom;
}

private readonly Control _userControl;
private readonly Microsoft.Vbe.Interop.Window _vbeWindow;
private readonly Forms.Control _userControl;
private readonly Window _vbeWindow;
private Size _lastSize;

public SubClassingResizeWindow(Control userControl, Microsoft.Vbe.Interop.Window vbeWindow)
public SubClassingResizeWindow(Forms.Control userControl, Window vbeWindow)
{
_userControl = userControl;
_vbeWindow = vbeWindow;
Expand All @@ -138,7 +152,7 @@ public SubClassingResizeWindow(Control userControl, Microsoft.Vbe.Interop.Window
CheckSize();
}

private static IntPtr FindVbeWindowHostHwnd(Microsoft.Vbe.Interop.Window vbeWindow)
private static IntPtr FindVbeWindowHostHwnd(Window vbeWindow)
{
const string DockedWindowClass = "wndclass_desked_gsk";
const string FloatingWindowClass = "VBFloatingPalette";
Expand All @@ -157,7 +171,7 @@ private static IntPtr FindVbeWindowHostHwnd(Microsoft.Vbe.Interop.Window vbeWind
return hWnd;
}

private static bool IsDockedWindow(Microsoft.Vbe.Interop.Window vbeWindow)
private static bool IsDockedWindow(Window vbeWindow)
{
return vbeWindow.LinkedWindowFrame.Type == vbext_WindowType.vbext_wt_MainWindow;
}
Expand All @@ -174,7 +188,7 @@ private void CheckSize()
}
}

protected override void WndProc(ref Message m)
protected override void WndProc(ref Forms.Message m)
{
const int WM_SIZE = 0x0005;

Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Windows.Forms.Integration;

namespace AccessCodeLib.Common.VBIDETools
{
Expand All @@ -28,6 +29,17 @@ public void HostUserControl(UserControl UserControlToHost)
UserControlToHost.Dock = DockStyle.Fill;
}

public void HostUserControl(System.Windows.Controls.UserControl UserControlToHost)
{
_resizeWindow.Init(this, GetParentVbeWindowHandle());
var host = new ElementHost();
host.Size = new Size(300, 200);
host.BackColor = Color.DarkCyan;
host.Dock = DockStyle.Fill;
host.Child = UserControlToHost;
Controls.Add(host);
}

private IntPtr GetParentVbeWindowHandle()
{
return GetParent(this.Handle);
Expand Down

0 comments on commit eb1cddb

Please sign in to comment.