WPF 中使用Hook
public DocumentWindow()
{
InitializeComponent();
this.SourceInitialized += new EventHandler(OnSourceInitialized);
}
void OnSourceInitialized(object sender, EventArgs e)
{
HwndSource source = (HwndSource)PresentationSource.FromVisual(this);
source.AddHook(new HwndSourceHook(HandleMessages));
}
private IntPtr HandleMessages(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
//KeyboardHook 一个钩子类
KeyboardHook hk = new KeyboardHook();
hk.KeyDown += new System.Windows.Forms.KeyEventHandler(kh_KeyDown);
return System.IntPtr.Zero;
}
void kh_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.F5)
{
System.Windows.Forms.MessageBox.Show("f5");
e.Handled = true;
Microsoft.Office.Interop.PowerPoint._Presentation myPres =
(Microsoft.Office.Interop.PowerPoint.Presentation)framer.ActiveDocument;
myPres.SlideShowSettings.RangeType = PpSlideShowRangeType.ppShowSlideRange;
var mm = myPres.SlideShowSettings;
var app = myPres.Application;
mm.Run();
}
else if (e.KeyCode == Keys.Escape)
{
//System.Windows.Forms.MessageBox.Show("退?出?");
e.Handled = true;
this.Close();
}
if (e.KeyCode == Keys.Right)
{
// e.Handled = true;
// Microsoft.Office.Interop.PowerPoint._Presentation myPres =
// (Microsoft.Office.Interop.PowerPoint.Presentation)framer.ActiveDocument;
// myPres.SlideShowSettings.RangeType = PpSlideShowRangeType.ppShowSlideRange;
// var mm = myPres.SlideShowSettings;
// mm.LoopUntilStopped=Microsoft.Office.Core.MsoTriState.msoTrue;
////myPres.SlideShowWindow.View.LastSlideViewed
//// var app = myPres.Application;
// mm.Run();
}
//if (e.KeyCode == Keys.F1 && !fInCall)
//{
// e.Handled = true;
// fInCall = true;
// tmrDelayedCall = new Timer();
// tmrDelayedCall.Interval = 1;
// tmrDelayedCall.Tick += new
// EventHandler(tmrDelayedCall_Tick);
// tmrDelayedCall.Start();
//}
}
hook 类实现 IDisposable接口