自定义右键单击菜单

用户控件具有RightClicked可以向其添加自定义事件的事件处理程序。此事件可以打开上下文菜单,但也可以执行其他操作。

Windows Forms

要自定义ScottPlot的右键单击菜单,请取消订阅默认的右键单击事件添加添加您自己的事件方法,以便在用户右键单击绘图时调用。

从GitHub下载这个例子

public Form1()
{
    InitializeComponent();
    
    // unsubscribe from the default right-click menu event
    formsPlot1.RightClicked -= formsPlot1.DefaultRightClickEvent;
    
    // add a custom right-click action
    formsPlot1.RightClicked += CustomRightClickEvent;
}

private void CustomRightClickEvent(object sender, EventArgs e)
{
    ContextMenuStrip customMenu = new ContextMenuStrip();
    customMenu.Items.Add(new ToolStripMenuItem("Add Sine Wave", null, new EventHandler(AddSine)));
    customMenu.Items.Add(new ToolStripMenuItem("Clear Plot", null, new EventHandler(ClearPlot)));
    customMenu.Show(Cursor.Position);
}

private void AddSine(object sender, EventArgs e)
{
    Random rand = new Random();
    double[] data = ScottPlot.DataGen.Sin(51, phase: rand.NextDouble() * 1000);
    formsPlot1.Plot.AddSignal(data);
    formsPlot1.Plot.AxisAuto();
    formsPlot1.Render();
}

private void ClearPlot(object sender, EventArgs e)
{
    formsPlot1.Plot.Clear();
    formsPlot1.Plot.AxisAuto();
    formsPlot1.Render();
}

WPF

要自定义ScottPlot的右键单击菜单,请取消订阅默认的右键单击事件添加添加您自己的事件方法,以便在用户右键单击绘图时调用。

从GitHub下载这个例子

public MainWindow()
{
    InitializeComponent();

    // unsubscribe from the default right-click menu event
    WpfPlot1.RightClicked -= WpfPlot1.DefaultRightClickEvent;

    // add a custom right-click action
    WpfPlot1.RightClicked += DeployCustomMenu;
}

private void DeployCustomMenu(object sender, EventArgs e)
{
    MenuItem addSinMenuItem = new MenuItem() { Header = "Add Sine Wave" };
    addSinMenuItem.Click += AddSine;

    MenuItem clearPlotMenuItem = new MenuItem() { Header = "Clear Plot" };
    clearPlotMenuItem.Click += ClearPlot;

    ContextMenu rightClickMenu = new ContextMenu();
    rightClickMenu.Items.Add(addSinMenuItem);
    rightClickMenu.Items.Add(clearPlotMenuItem);

    rightClickMenu.IsOpen = true;
}

private void AddSine(object sender, RoutedEventArgs e)
{
    Random rand = new Random();
    double[] data = ScottPlot.DataGen.Sin(51, phase: rand.NextDouble() * 1000);
    WpfPlot1.Plot.AddSignal(data);
    WpfPlot1.Plot.AxisAuto();
}

private void ClearPlot(object sender, RoutedEventArgs e)
{
    WpfPlot1.Plot.Clear();
    WpfPlot1.Plot.AxisAuto();
}