Skip to content

Commit

Permalink
#5: Ensure we don't execute WM_NCPAINT and WM_ERASEBKGND when active.
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-englert committed Mar 4, 2022
1 parent 90384c2 commit d87ceed
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/RegionToShare/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Window x:Class="RegionToShare.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:local="clr-namespace:RegionToShare"
Title="Region to Share" Height="450" Width="800" Background="Black" Foreground="White"
MouseLeftButtonDown="Window_MouseLeftButtonDown">
<Grid x:Name="ClientArea">
Expand All @@ -15,7 +15,7 @@
</Grid>
</Grid>
<WindowsFormsHost x:Name="RenderTargetHost" Visibility="Collapsed">
<wf:Control x:Name="RenderTargetWindow" Visible="False" MouseClick="RenderTargetWindow_OnMouseClick" />
<local:RenderTarget x:Name="RenderTargetWindow" MouseClick="RenderTargetWindow_OnMouseClick" />
</WindowsFormsHost>
</Grid>
</Window>
18 changes: 18 additions & 0 deletions src/RegionToShare/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Threading;
using TomsToolbox.Wpf;
Expand All @@ -22,6 +23,9 @@ protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);

var messageSource = (HwndSource)PresentationSource.FromDependencyObject(this);
messageSource.AddHook(WindowProc);

_windowHandle = this.GetWindowHandle();

var separationLayerWindow = new Window() { Background = (Brush)FindResource("HatchBrush"), WindowStyle = WindowStyle.None, ResizeMode = ResizeMode.NoResize, Title = "Region to Share - Separation Layer", ShowInTaskbar = false };
Expand Down Expand Up @@ -102,4 +106,18 @@ private void SendToBack()
NativeMethods.SetWindowPos(_separationLayerHandle, NativeMethods.HWND_BOTTOM, 0, 0, 0, 0, NativeMethods.SWP_NOMOVE | NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOACTIVATE | NativeMethods.SWP_SHOWWINDOW);
NativeMethods.SetWindowPos(_windowHandle, _separationLayerHandle, 0, 0, 0, 0, NativeMethods.SWP_NOMOVE | NativeMethods.SWP_NOSIZE | NativeMethods.SWP_NOACTIVATE);
}

private IntPtr WindowProc(IntPtr windowHandle, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
switch (msg)
{
case NativeMethods.WM_NCPAINT:
// case NativeMethods.WM_PAINT:
case NativeMethods.WM_ERASEBKGND:
handled = _recordingWindow != null;
break;
}

return IntPtr.Zero;
}
}
3 changes: 3 additions & 0 deletions src/RegionToShare/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ internal static class NativeMethods
[DllImport("user32.dll", SetLastError = true)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);

public const int WM_PAINT = 0x000F;
public const int WM_ERASEBKGND = 0x0014;
public const int WM_NCPAINT = 0x0085;
public const int WM_NCHITTEST = 0x0084;
public const int CURSOR_SHOWING = 0x00000001;

Expand Down
20 changes: 20 additions & 0 deletions src/RegionToShare/RenderTarget.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Windows.Forms;

namespace RegionToShare
{
public class RenderTarget : Control
{
public override bool PreProcessMessage(ref Message msg)
{
switch (msg.Msg)
{
case NativeMethods.WM_NCPAINT:
// case NativeMethods.WM_PAINT:
case NativeMethods.WM_ERASEBKGND:
return true;
}

return base.PreProcessMessage(ref msg);
}
}
}

0 comments on commit d87ceed

Please sign in to comment.