2

WPFでアプリを作成しています。

「System.Windows.Forms.NotifyIcon」を使用してSystrayアイコンを含めました

コードは次のようになります。

// Create WinForm notify icon
m_NotifyIcon = new System.Windows.Forms.NotifyIcon();
m_NotifyIcon.Icon = Properties.Resources.rocket;
m_NotifyIcon.Visible = true;
// Default Balloon title
m_NotifyIcon.BalloonTipTitle = "Greatest App ever";
m_NotifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu();

// Append default menu items
List<MenuItem> itemList = new List<MenuItem>();
itemList.Insert(0, new MenuItem("Exit", OnExit_Click));
itemList.Insert(0, new MenuItem("-"));
itemList.Insert(0, new MenuItem("Refresh", RefreshConsoleList));
itemList.Insert(0, new MenuItem("Filter: \"" + (String.IsNullOrEmpty(m_Filter) ? "NONE" : m_Filter) + "\"", ChangeFilter_Click));
itemList.Insert(0, new MenuItem("-"));
m_NotifyIcon.ContextMenu.MenuItems.AddRange(itemList.ToArray());

結果は次のようになります。

リフレッシュ前

更新の場合、私のアプリケーションは多くのエントリを取得し、これらのエントリを ContextMenu に追加します。メニューは次のようになります。

MenuItem のオーバーフロー

ご覧のとおり、MenuItem が多すぎて、オーバーフローのために 2 つの矢印が表示されます (ContextMenu の上部と下部)。

ユーザーがアプリケーションを終了したい場合は、下にスクロールして [終了] をクリックする必要があります。リストが非常に大きい場合、ユーザーを悩ませる可能性があります。

これを回避するために、ポップアップ時に ContextMenu が既に下にスクロールされていることを表示したい (最初の MenuItem が表示されている)

しかし、ContextMenu をプログラムでスクロール ダウンするために使用するイベントやコントロールが見つかりませんでした。

することは可能ですか?

よろしく

イヴ・デグロープ


PS: 評判のために画像を直接投稿できませんでした (これが私の最初の投稿です)。2 つ以上のリンクを投稿します。

3番目の例は次のとおりです。

yves.desgraupes.free.fr/shared/ContextMenu_OverflowScroll.png

4

1 に答える 1

0

あなたができることは、NotifyIcon の WPF 実装を使用することです。

次に、WPF メニューを用意して、メニューで必要な動作を実装する方法をより詳細に制御できます。

そのため、ContextMenu の Template を定義し、ContextMenu テンプレート内の ScrollViewer にアクセスして、その上で ScrollToEnd() を呼び出すことができます。

起動時に最後までスクロールされる WPF コンテキスト メニューを作成する方法を示す例を次に示します。これを WPF NotifyIcon で使用できます。ボタンを右クリックして、コンテキスト メニューを表示します。

出発点として提供したことに注意してください....コードを改善できるかもしれません..いくつかの繰り返しを減らします....しかし、それは機能します....たとえば、メニューが最初のときにグリッチを回避するために最後までスクロールしないと表示され、SizeChanged イベントをトラップする必要がありました。

<Window x:Class="WpfApplication15.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="{x:Type ContextMenu}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ContextMenu}">
                        <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
                            <ScrollViewer x:Name="scrollviewer"
Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}"
CanContentScroll="True" >
                                <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="{TemplateBinding Padding}"
KeyboardNavigation.DirectionalNavigation="Cycle"/>
                            </ScrollViewer>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Button x:Name="mybutton">
            <Button.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Test1"/>
                </ContextMenu>
            </Button.ContextMenu>
            Test Button
        </Button>       
    </Grid>
</Window>

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.Windows.Controls.Primitives;

namespace WpfApplication15
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public static T FindVisualChild<T>(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T)
                    {
                        return (T)child;
                    }

                    T childItem = FindVisualChild<T>(child);
                    if (childItem != null) return childItem;
                }
            }
            return null;
        }

        public MainWindow()
        {
            InitializeComponent();

            ContextMenu contextmenu = mybutton.ContextMenu;

            // Add 100 more items

            for (int i = 2; i <= 100; i++)
            {
                MenuItem mi = new MenuItem();
                mi.Header = "Item" + i.ToString();

                contextmenu.Items.Add(mi);
            }

            contextmenu.SizeChanged += new SizeChangedEventHandler(contextmenu_SizeChanged);
            contextmenu.Loaded += new RoutedEventHandler(contextmenu_Loaded);

            mybutton.ContextMenuOpening += new ContextMenuEventHandler(mybutton_ContextMenuOpening);
        }

        void contextmenu_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            ContextMenu contextmenu = mybutton.ContextMenu;

            ScrollViewer sv = FindVisualChild<ScrollViewer>(contextmenu);
            if (sv != null)
            {
                sv.ScrollToEnd();
            }
        }

        void contextmenu_Loaded(object sender, RoutedEventArgs e)
        {
            ContextMenu contextmenu = mybutton.ContextMenu;

            ScrollViewer sv = FindVisualChild<ScrollViewer>(contextmenu);
            if (sv != null)
            {
                sv.ScrollToEnd();
            }
        }

        void mybutton_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            ContextMenu contextmenu = mybutton.ContextMenu;
            ScrollViewer sv = FindVisualChild<ScrollViewer>(contextmenu);
            if (sv != null)
            {
                sv.ScrollToEnd();
            }
        }
    }
}

補足として....ここで MaxHeight...demo を設定して、ContextMenu の高さを制限したい場合があります。

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid>  
  <Button>
  <Button.ContextMenu>
  <ContextMenu MaxHeight="100">
  <MenuItem Header="Test1"/>
  <MenuItem Header="Test2"/>
  <MenuItem Header="Test3"/>
  <MenuItem Header="Test4"/>
  <MenuItem Header="Test5"/>
  <MenuItem Header="Test6"/>
  <MenuItem Header="Test7"/>
  <MenuItem Header="Test8"/>
  <MenuItem Header="Test9"/>
  </ContextMenu>
  </Button.ContextMenu>
  Test Button</Button>
  </Grid>
</Page>
于 2012-09-11T22:53:31.387 に答える