直接上代碼。
/// <summary>
/// MainWindow.xaml 的交互邏輯
/// </summary>
public partial class MainWindow : Window
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int RegisterWindowMessage(string msg);
private HwndSource _HwndSource = null;
private IntPtr _Handler = IntPtr.Zero;
private readonly int Message1 = RegisterWindowMessage("Message_1");
private readonly int Message2 = RegisterWindowMessage("Message_2");
private readonly int Message3 = RegisterWindowMessage("Message_3");
public MainWindow()
{
InitializeComponent();
this.SourceInitialized += new EventHandler(MainWindow_SourceInitialized);
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
this.LoadJumpList();
}
void MainWindow_SourceInitialized(object sender, EventArgs e)
{
this._HwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource;
this._Handler = this._HwndSource.Handle;
this._HwndSource.AddHook(this.WindowProc);
}
public IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == Message1)
{
MessageBox.Show("Message_1");
}
else if (msg == Message2)
{
MessageBox.Show("Message_2");
}
else if (msg == Message3)
{
MessageBox.Show("Message_3");
}
return IntPtr.Zero;
}
private void LoadJumpList()
{
JumpList jumpList = JumpList.GetJumpList(Application.Current);
if (jumpList != null
&& jumpList.JumpItems != null
&& jumpList.JumpItems.Count > 0)
jumpList.JumpItems.Clear();
JumpTask jtNotpad = new JumpTask()
{
ApplicationPath = "notepad.exe",
CustomCategory = "工具",
Description = "記事本",
Title = "記事本",
IconResourcePath = "notepad.exe",
IconResourceIndex = 0
};
JumpTask jtMsPaint = new JumpTask()
{
ApplicationPath = "mspaint.exe",
CustomCategory = "工具",
Description = "畫圖",
Title = "畫圖",
IconResourcePath = "mspaint.exe",
IconResourceIndex = 0
};
JumpTask jtCalc = new JumpTask()
{
ApplicationPath = "calc.exe",
CustomCategory = "工具",
Description = "計算器",
Title = "計算器",
IconResourcePath = "calc.exe",
IconResourceIndex = 0
};
int windowInt = this._Handler.ToInt32();
JumpTask jtMessage1 = new JumpTask()
{
CustomCategory = "Message",
Description = "Message1",
Title = "Message1",
ApplicationPath = Assembly.GetEntryAssembly().CodeBase,
Arguments = windowInt + " " + Message1
};
JumpTask jtMessage2 = new JumpTask()
{
CustomCategory = "Message",
Description = "Message2",
Title = "Message2",
ApplicationPath = Assembly.GetEntryAssembly().CodeBase,
Arguments = windowInt + " " + Message2
};
JumpTask jtMessage3 = new JumpTask()
{
CustomCategory = "Message",
Description = "Message3",
Title = "Message3",
ApplicationPath = Assembly.GetEntryAssembly().CodeBase,
Arguments = windowInt + " " + Message3
};
if (jumpList == null)
jumpList = new JumpList();
jumpList.JumpItems.Add(jtNotpad);
jumpList.JumpItems.Add(jtMsPaint);
jumpList.JumpItems.Add(jtCalc);
jumpList.JumpItems.Add(jtMessage1);
jumpList.JumpItems.Add(jtMessage2);
jumpList.JumpItems.Add(jtMessage3);
jumpList.ShowFrequentCategory = true;
jumpList.ShowRecentCategory = true;
jumpList.Apply();
this.ShowInTaskbar = true;
}
}
/// <summary>
/// App.xaml 的交互邏輯
/// </summary>
public partial class App : Application
{
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
public App()
{
string[] args = Environment.GetCommandLineArgs();
if (args.Length == 3)
{
//處理JumpList自定義按鈕操作
int handler = Int32.Parse(args[1]);
uint msg = UInt32.Parse(args[2]);
if (handler != 0 && msg != 0)
{
SendMessage((IntPtr)handler, msg, IntPtr.Zero, IntPtr.Zero);
}
Application.Current.Shutdown();
return;
}
}
}