private void MenuItem_Click(object sender, RoutedEventArgs e) { MyMenuItem mi = sender as MyMenuItem; WriteLine("MenuItem_Click => event recieved with Name: " + mi.Name + "and Header: " + mi.Header); // Test if Context Menu is filled if (mi.PID() == 0 || (string)mi.Header == "No such process") { return; } MenuItemSelected(this.current_id, (string)mi.Header, mi.PID()); }
// DockPanel and ContextMenu private void Dock_MouseRightButtonUp(object sender, MouseButtonEventArgs e) { MyDockPanel dp = sender as MyDockPanel; dp.ContextMenu = new ContextMenu(); WriteLine("DockPanel_MouseRightButtonUp with ID: " + dp.Identifier()); // Process[] processes = Process.GetProcessesByName("Dofus"); Process[] processes = Process.GetProcesses(); if (processes.Length == 0) { this.current_id = -1; MyMenuItem mi = new MyMenuItem(0, "Error: Unable to find processes"); // MyMenuItem mi = new MyMenuItem(0, "No such process"); mi.Click += MenuItem_Click; dp.ContextMenu.Items.Add(mi); } else { this.current_id = dp.Identifier(); for (int i = 0; i < processes.Length; ++i) { if (processes[i].MainWindowTitle == "") { continue; } // Name = StackPanel Name + Process PID // Header = Process Title MyMenuItem mi = new MyMenuItem(processes[i].Id, processes[i].MainWindowTitle); mi.Click += MenuItem_Click; dp.ContextMenu.Items.Add(mi); } } }