Skip to content

Instantly share code, notes, and snippets.

@nmfisher
Created November 1, 2017 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmfisher/08a7574f759da308a99de7826adfc4d7 to your computer and use it in GitHub Desktop.
Save nmfisher/08a7574f759da308a99de7826adfc4d7 to your computer and use it in GitHub Desktop.
var hwndSource = HwndSource.FromHwnd(handle);
if (hwndSource != null)
{
hwndSource.AddHook(WindowProc);
}
WinAPI.ShowWindowAsync(new WindowInteropHelper(OverlayForm).Handle, 0);
private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
switch (msg)
{
case 0x0003:
uint uFlags = WinAPI.SWP_NOACTIVATE | WinAPI.SWP_NOZORDER;
int parent = Application.ActiveWindow.Hwnd;
System.Drawing.Rectangle rect = new System.Drawing.Rectangle();
WinAPI.GetWindowRect((IntPtr)parent, ref rect);
WinAPI.SetWindowPos((uint)hwnd.ToInt32(), parent, 0, 0, rect.Right, rect.Bottom, uFlags);
break;
}
return (IntPtr)0;
}
@nmfisher
Copy link
Author

nmfisher commented Nov 1, 2017

var lpwndpl = new WINDOWPLACEMENT();
lpwndpl.rcNormalPosition = new System.Drawing.Rectangle(rect.Left, rect.Top, rect.Width, rect.Height);
lpwndpl.length = System.Runtime.InteropServices.Marshal.SizeOf(lpwndpl);
WinAPI.SetWindowPlacement((uint)handle.ToInt32(), ref lpwndpl);

@nmfisher
Copy link
Author

nmfisher commented Nov 2, 2017

WinAPI.HookProc HookProcedure = new WinAPI.HookProc(ThisAddIn.HookProc);
WinAPI.SetWindowsHookEx(0x0003, HookProcedure, (IntPtr)0, AppDomain.GetCurrentThreadId());

public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);

public static IntPtr ParentProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) {
public static int HookProc(int nCode, IntPtr wParam, IntPtr lParam) {
System.Diagnostics.Debug.WriteLine(nCode);
return 0;
}

@nmfisher
Copy link
Author

nmfisher commented Nov 2, 2017

[DllImport("user32.dll")]
public static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hWnd, int id);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment