wpf 热键问题
以下是代码,运行起来热键没用,求解决的方法using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Windows.Interop;
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint control,Key key);
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
public static int WM_HOTKEY = 0x0312;
public Window1()
{
InitializeComponent();
this.Closed += new EventHandler(Window1_Closed);
}
void Window1_Closed(object sender, EventArgs e)
{
IntPtr hWnd = new WindowInteropHelper(this).Handle;
UnregisterHotKey(hWnd, 123);
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
IntPtr hWnd = new WindowInteropHelper(this).Handle;
RegisterHotKey(hWnd, 123, 0, System.Windows.Input.Key.Escape);// System.Windows.Forms.Keys.EscapeKey.Escape
HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
source.AddHook(WndProc);
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_HOTKEY)
{
MessageBox.Show("OK");
if (wParam.ToInt32() == 123)
{
MessageBox.Show("OK");
handled = true;
}
}
return IntPtr.Zero;
}
}
}
补充:.NET技术 , C#