44 lines
2.9 KiB
C#
44 lines
2.9 KiB
C#
using System;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using PlotDirector.Companion.Protocol;
|
|
|
|
namespace PlotDirector.WordAddIn
|
|
{
|
|
public sealed partial class WordAdapter
|
|
{
|
|
private delegate IntPtr FocusHook(int code,IntPtr removed,IntPtr message);
|
|
[StructLayout(LayoutKind.Sequential)]private struct FocusMessage{public IntPtr Window;public uint Message;public UIntPtr WParam;public IntPtr LParam;public uint Time;public int X,Y;public uint Private;}
|
|
[DllImport("user32.dll",SetLastError=true)]private static extern IntPtr SetWindowsHookEx(int kind,FocusHook callback,IntPtr module,uint thread);
|
|
[DllImport("user32.dll")]private static extern bool UnhookWindowsHookEx(IntPtr hook);
|
|
[DllImport("user32.dll")]private static extern IntPtr CallNextHookEx(IntPtr hook,int code,IntPtr removed,IntPtr message);
|
|
[DllImport("user32.dll")]private static extern IntPtr GetAncestor(IntPtr window,uint flag);
|
|
[DllImport("user32.dll",CharSet=CharSet.Unicode)]private static extern int GetClassName(IntPtr window,StringBuilder text,int capacity);
|
|
[DllImport("kernel32.dll")]private static extern uint GetCurrentThreadId();
|
|
private readonly FocusInputState focusInput=new FocusInputState();private readonly System.Diagnostics.Stopwatch focusClock=System.Diagnostics.Stopwatch.StartNew();
|
|
private FocusHook focusHookCallback;private IntPtr focusHook;
|
|
private void StartFocusInput(){focusHookCallback=ObserveFocusInput;focusHook=SetWindowsHookEx(3,focusHookCallback,IntPtr.Zero,GetCurrentThreadId());}
|
|
private IntPtr ObserveFocusInput(int code,IntPtr removed,IntPtr message)
|
|
{
|
|
// Thread-local, metadata only, no COM in the hook. Always continue Office's chain.
|
|
try{if(code>=0&&removed==new IntPtr(1)){
|
|
var input=(FocusMessage)Marshal.PtrToStructure(message,typeof(FocusMessage));
|
|
if(applyingFocus)focusInput.Clear();
|
|
else if(input.Message==0x0201||input.Message==0x0100){
|
|
uint process;var root=GetAncestor(input.Window,2);GetWindowThreadProcessId(root,out process);
|
|
if(process==focusProcessId&&root==GetAncestor(GetForegroundWindow(),2)){
|
|
var key=(int)input.WParam.ToUInt64();var mouse=input.Message==0x0201;
|
|
if(mouse||key>=33&&key<=40||key==9||key==13){
|
|
var name=new StringBuilder(64);GetClassName(input.Window,name,name.Capacity);var body=name.ToString()=="_WwG";
|
|
if(!mouse&&key==13&&body)focusInput.Clear();
|
|
else{focusInput.Record(root.ToInt32(),focusClock.ElapsedMilliseconds,mouse&&body);if(mouse&&body)ScheduleSelection();}
|
|
}else focusInput.Clear();
|
|
}
|
|
}else if(input.Message==0x0102||input.Message==0x010D||input.Message==0x020A||input.Message==0x020E)focusInput.Clear();
|
|
}}catch{focusInput.Clear();}
|
|
return CallNextHookEx(focusHook,code,removed,message);
|
|
}
|
|
private void StopFocusInput(){focusInput.Clear();if(focusHook!=IntPtr.Zero){UnhookWindowsHookEx(focusHook);focusHook=IntPtr.Zero;}focusHookCallback=null;}
|
|
}
|
|
}
|