在自定义窗口上打开Windows系统菜单的启动窗口

10

当您单击应用程序图标或右键单击应用程序标题栏时,我想调用ContextMenu

这就是我指的ContextMenu

enter image description here

我需要它,因为我制作了一个类似于窗口的自定义控件。
我需要这种行为来完成我的控件。

编辑:
Leo Lorenzo Luis要求我提供代码:

https://skydrive.live.com/?cid=c3392940f5cf5f74&id=C3392940F5CF5F74%21107&authkey=!APd2X3tDxWRfpL4

或:

My MainWindow.xaml:

    <!--<Grid>
        <Border Name="TopBorder" BorderThickness="0.5,0,0,0" BorderBrush="Blue"/>
        <Border Name="RightBorder" BorderThickness="0,0.5,0,0" BorderBrush="Red"/>
        <Border Name="BottomBorder" BorderThickness="0,0,0.5,0" BorderBrush="Green"/>
        <Border Name="LeftBorder" BorderThickness="0,0,0,0.5" BorderBrush="Orange"/>
        <Grid Margin="0.5">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <StatusBar Background="Transparent" MouseDoubleClick="TriggerMaximize" MouseDown="StatusBar_MouseDown">
                    <Image Margin="5,0,0,0" VerticalAlignment="Center" Width="16" Height="16" Source="{Binding Icon, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" RenderOptions.BitmapScalingMode="NearestNeighbor" RenderOptions.EdgeMode="Aliased"/>
                    <Label VerticalAlignment="Center" FontSize="14" Content="{Binding Title, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>
                </StatusBar>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Grid.Column="1">
                    <Button x:Name="Minimize" ToolTip="Minimize" Content="0" Style="{DynamicResource TitleBarButton}" Click="TriggerMinimize"/>
                    <Button x:Name="Restore" ToolTip="Restore" Content="2" Style="{DynamicResource TitleBarButton}"  Visibility="Collapsed" Click="TriggerMaximize"/>
                    <Button x:Name="Maximize" ToolTip="Maximize" Content="1" Style="{DynamicResource TitleBarButton}" Click="TriggerMaximize"/>
                    <Button x:Name="Close" ToolTip="Close" Content="r" Style="{DynamicResource TitleBarButton}" Click="TriggerClose"/>
                </StackPanel>
            </Grid>
        </Grid>
    </Grid>-->

    <DockPanel LastChildFill="true">
        <Border Name="TopBorder" DockPanel.Dock="Top" BorderBrush ="#007ACC" BorderThickness="0.5"/>
        <Border Name="RightBorder" DockPanel.Dock="Right" BorderBrush ="#007ACC" BorderThickness="0.5"/>
        <Border Name="BottomBorder" DockPanel.Dock="Bottom" BorderBrush ="#007ACC" BorderThickness="0.5"/>
        <Border Name="LeftBorder" DockPanel.Dock="Left" BorderBrush="#007ACC" BorderThickness="0.5"/>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <StatusBar Background="Transparent" MouseDoubleClick="TriggerMaximize" MouseDown="StatusBar_MouseDown">
                    <Image Margin="5,0,0,0" Width="16" Height="16" Source="{Binding Icon, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" RenderOptions.BitmapScalingMode="NearestNeighbor" RenderOptions.EdgeMode="Aliased"/>
                    <Label VerticalAlignment="Center" FontSize="14" Content="{Binding Title, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>
                </StatusBar>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Grid.Column="1">
                    <Button x:Name="Minimize" ToolTip="Minimize" Content="0" Style="{DynamicResource TitleBarButton}" Click="TriggerMinimize"/>
                    <Button x:Name="Restore" ToolTip="Restore" Content="2" Style="{DynamicResource TitleBarButton}"  Visibility="Collapsed" Click="TriggerMaximize"/>
                    <Button x:Name="Maximize" ToolTip="Maximize" Content="1" Style="{DynamicResource TitleBarButton}" Click="TriggerMaximize"/>
                    <Button x:Name="Close" ToolTip="Close" Content="r" Style="{DynamicResource TitleBarButton}" Click="TriggerClose"/>
                </StackPanel>
            </Grid>
        </Grid>
    </DockPanel>
</Window>

我的MainWindow.cs(代码后台):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
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.Interop;
using System.Windows.Forms;

namespace WpfApplication16
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.SourceInitialized += new EventHandler(win_SourceInitialized);
        }

        private void TriggerMaximize(object sender, MouseButtonEventArgs e)
        {
            TriggerMaximize();
        }

        private void TriggerMaximize(object sender, RoutedEventArgs e)
        {
            TriggerMaximize();
        }

        private void TriggerMaximize()
        {
            if (WindowState == System.Windows.WindowState.Maximized)
            {
                WindowState = System.Windows.WindowState.Normal;
                Restore.Visibility = Visibility.Collapsed;
                Maximize.Visibility = Visibility.Visible;
            }
            else if (WindowState == System.Windows.WindowState.Normal)
            {
                WindowState = System.Windows.WindowState.Maximized;
                Maximize.Visibility = Visibility.Collapsed;
                Restore.Visibility = Visibility.Visible;
            }
        }

        private void Window_LocationChanged(object sender, EventArgs e)
        {
            TriggerBorderChanges();
        }


        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            TriggerBorderChanges();
        }

        private void TriggerBorderChanges()
        {
            TopBorder.BorderThickness = new Thickness(0.5);
            RightBorder.BorderThickness = new Thickness(0.5);
            BottomBorder.BorderThickness = new Thickness(0.5);
            LeftBorder.BorderThickness = new Thickness(0.5);

            if (Top == 0)
            {
                TopBorder.BorderThickness = new Thickness(0);
                BottomBorder.BorderThickness = new Thickness(0);
            }

            if (Left == 0)
            {
                LeftBorder.BorderThickness = new Thickness(0);
            }

            // need to test in dual view -if not needed, remove drawing and windows.forms (from refereance and from the using)
            //Screen currentScreen = Screen.FromPoint(System.Windows.Forms.Cursor.Position);
            //if (Left == (currentScreen.WorkArea.Width - Width))
            if (Left == (System.Windows.SystemParameters.WorkArea.Width - 1 - Width))
            {
                RightBorder.BorderThickness = new Thickness(0);
            }
        } 

        private void TriggerClose(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void TriggerMinimize(object sender, RoutedEventArgs e)
        {
            WindowState = System.Windows.WindowState.Minimized;
        }


        private void StatusBar_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
                this.DragMove();
        }


        void win_SourceInitialized(object sender, EventArgs e)
        {
            System.IntPtr handle = (new WindowInteropHelper(this)).Handle;
            HwndSource.FromHwnd(handle).AddHook(new HwndSourceHook(WindowProc));
        }

        /// <summary>
        /// POINT aka POINTAPI
        /// </summary>5
        [StructLayout(LayoutKind.Sequential)]
        public struct POINT
        {
            /// <summary>
            /// x coordinate of point.
            /// </summary>
            public int x;
            /// <summary>
            /// y coordinate of point.
            /// </summary>
            public int y;

            /// <summary>
            /// Construct a point of coordinates (x,y).
            /// </summary>
            public POINT(int x, int y)
            {
                this.x = x;
                this.y = y;
            }
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct MINMAXINFO
        {
            public POINT ptReserved;
            public POINT ptMaxSize;
            public POINT ptMaxPosition;
            public POINT ptMinTrackSize;
            public POINT ptMaxTrackSize;
        };

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public class MONITORINFO
        {
            /// <summary>
            /// </summary>            
            public int cbSize = Marshal.SizeOf(typeof(MONITORINFO));

            /// <summary>
            /// </summary>            
            public RECT rcMonitor = new RECT();

            /// <summary>
            /// </summary>            
            public RECT rcWork = new RECT();

            /// <summary>
            /// </summary>            
            public int dwFlags = 0;
        }

        /// <summary> Win32 </summary>
        [StructLayout(LayoutKind.Sequential, Pack = 0)]
        public struct RECT
        {
            /// <summary> Win32 </summary>
            public int left;
            /// <summary> Win32 </summary>
            public int top;
            /// <summary> Win32 </summary>
            public int right;
            /// <summary> Win32 </summary>
            public int bottom;

            /// <summary> Win32 </summary>
            public static readonly RECT Empty = new RECT();

            /// <summary> Win32 </summary>
            public int Width
            {
                get { return Math.Abs(right - left); }  // Abs needed for BIDI OS
            }

            /// <summary> Win32 </summary>
            public int Height
            {
                get { return bottom - top; }
            }

            /// <summary> Win32 </summary>
            public RECT(int left, int top, int right, int bottom)
            {
                this.left = left;
                this.top = top;
                this.right = right;
                this.bottom = bottom;
            }

            /// <summary> Win32 </summary>
            public RECT(RECT rcSrc)
            {
                this.left = rcSrc.left;
                this.top = rcSrc.top;
                this.right = rcSrc.right;
                this.bottom = rcSrc.bottom;
            }

            /// <summary> Win32 </summary>
            public bool IsEmpty
            {
                get
                {
                    // BUGBUG : On Bidi OS (hebrew arabic) left > right
                    return left >= right || top >= bottom;
                }
            }

            /// <summary> Return a user friendly representation of this struct </summary>
            public override string ToString()
            {
                if (this == RECT.Empty) { return "RECT {Empty}"; }
                return "RECT { left : " + left + " / top : " + top + " / right : " + right + " / bottom : " + bottom + " }";
            }

            /// <summary> Determine if 2 RECT are equal (deep compare) </summary>
            public override bool Equals(object obj)
            {
                if (!(obj is Rect)) { return false; }
                return (this == (RECT)obj);
            }

            /// <summary>Return the HashCode for this struct (not garanteed to be unique)</summary>
            public override int GetHashCode()
            {
                return left.GetHashCode() + top.GetHashCode() + right.GetHashCode() + bottom.GetHashCode();
            }

            /// <summary> Determine if 2 RECT are equal (deep compare)</summary>
            public static bool operator ==(RECT rect1, RECT rect2)
            {
                return (rect1.left == rect2.left && rect1.top == rect2.top && rect1.right == rect2.right && rect1.bottom == rect2.bottom);
            }

            /// <summary> Determine if 2 RECT are different(deep compare)</summary>
            public static bool operator !=(RECT rect1, RECT rect2)
            {
                return !(rect1 == rect2);
            }
        }

        [DllImport("user32")]
        internal static extern bool GetMonitorInfo(IntPtr hMonitor, MONITORINFO lpmi);

        [DllImport("User32")]
        internal static extern IntPtr MonitorFromWindow(IntPtr handle, int flags);

        private static System.IntPtr WindowProc(
                System.IntPtr hwnd,
                int msg,
                System.IntPtr wParam,
                System.IntPtr lParam,
                ref bool handled)
        {
            switch (msg)
            {
                case 0x0024:
                    WmGetMinMaxInfo(hwnd, lParam);
                    handled = true;
                    break;
            }

            return (System.IntPtr)0;
        }

        private static void WmGetMinMaxInfo(System.IntPtr hwnd, System.IntPtr lParam)
        {
            MINMAXINFO mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));

            // Adjust the maximized size and position to fit the work area of the correct monitor
            int MONITOR_DEFAULTTONEAREST = 0x00000002;
            System.IntPtr monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);

            if (monitor != System.IntPtr.Zero)
            {

                MONITORINFO monitorInfo = new MONITORINFO();
                GetMonitorInfo(monitor, monitorInfo);
                RECT rcWorkArea = monitorInfo.rcWork;
                RECT rcMonitorArea = monitorInfo.rcMonitor;
                mmi.ptMaxPosition.x = Math.Abs(rcWorkArea.left - rcMonitorArea.left);
                mmi.ptMaxPosition.y = Math.Abs(rcWorkArea.top - rcMonitorArea.top);
                mmi.ptMaxSize.x = Math.Abs(rcWorkArea.right - rcWorkArea.left);
                mmi.ptMaxSize.y = Math.Abs(rcWorkArea.bottom - rcWorkArea.top);
            }

            Marshal.StructureToPtr(mmi, lParam, true);
        }
    }
}

我的 App.xaml:

<Application
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" x:Class="WpfApplication16.App"
             StartupUri="MainWindow.xaml">
    <Application.Resources>


        <Style x:Key="TitleBarButton" TargetType="Button">
            <Setter Property="Foreground" Value="Black"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="Padding" Value="12,7"/>
            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
            <Setter Property="FontFamily" Value="Marlett"/>
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}">
                            <Grid>
                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" TextBlock.FontFamily="{TemplateBinding FontFamily}" TextBlock.FontSize="{TemplateBinding FontSize}" />
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Background" Value="#EFEFF2" />
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter Property="Background" Value="#007ACC"/>
                                <Setter Property="Foreground" Value="White"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </Application.Resources>
</Application>

希望得到您的帮助。


在您分享的自定义控件中,您能否将 Visual Studio(图标)制作成一个按钮,并在单击时从那里显示 ContextMenu? - 123 456 789 0
是的。我只需要知道如何调用那个特定的上下文菜单。我需要自己制作一个还是可以直接调用这个?(我的控件基于窗口控件) - Ron
请从您的问题中删除额外的代码,这将有助于其他人快速获取问题。 - Nitin
3个回答

11

您想要显示的菜单是系统ContextMenu。要使用它,您需要像下面代码中所示导入一些user32函数。我已经在按钮点击时启动了系统菜单,您可以在任何操作、右键单击等时启动它。

GetSystemMenu获取系统菜单,TrackPopupMenuEx用于显示它。在菜单项单击时,PostMessage会发送系统命令。

public partial class Window3 : Window
{

     private const int WM_SYSCOMMAND = 0x112;
    uint TPM_LEFTALIGN = 0x0000;
    uint TPM_RETURNCMD = 0x0100;
     const UInt32 MF_ENABLED = 0x00000000;
     const UInt32 MF_GRAYED = 0x00000001;
     internal const UInt32 SC_MAXIMIZE = 0xF030;
     internal const UInt32 SC_RESTORE = 0xF120;

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

    [DllImport("user32.dll")]
    static extern int TrackPopupMenuEx(IntPtr hmenu, uint fuFlags,
      int x, int y, IntPtr hwnd, IntPtr lptpm);

    [DllImport("user32.dll")]
    public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

    [DllImport("user32.dll")]
    static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem,
       uint uEnable);

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        WindowInteropHelper helper = new WindowInteropHelper(this);
        IntPtr callingWindow = helper.Handle;
        IntPtr wMenu = GetSystemMenu(callingWindow, false);
        // Display the menu
        if (this.WindowState == System.Windows.WindowState.Maximized)
        {
            EnableMenuItem(wMenu, SC_MAXIMIZE, MF_GRAYED);
        }
        else
        {
            EnableMenuItem(wMenu, SC_MAXIMIZE, MF_ENABLED);
        }

        int command = TrackPopupMenuEx(wMenu, TPM_LEFTALIGN | TPM_RETURNCMD, 100, 100, callingWindow, IntPtr.Zero);
        if (command == 0)
            return;

        PostMessage(callingWindow, WM_SYSCOMMAND, new IntPtr(command), IntPtr.Zero);
    }

非常接近我想要的了。我有一个问题——即使窗口已经最大化,最大化选项仍然可用,恢复选项(当它没有最大化时)也是如此——我该如何强制它按照应有的方式运行? - Ron
更新了最大化菜单项的启用/禁用答案...同样,您还需要处理其他项目。 - Nitin
工作得很好,虽然我希望它能自动检测,但只要它能正常工作就行了。非常感谢! - Ron
有没有一种方法可以在默认位置打开菜单? - elios264

0
如果您正在进行MVVM应用程序开发,在ViewModel中,您只需要声明窗口的属性和命令即可:
public Window Window { get; set; }
public ICommand MenuCommand { get; set; }

接下来是一个Point属性,用于获取鼠标光标相对于屏幕的位置:

public Point MousePosition
    {
        get
        {
            return Application.Current.MainWindow.PointToScreen(Mouse.GetPosition(Window));
        }
        
    }

在你的类的构造函数中最后一步是:
public WindowViewModel(Window window)
{
    MenuCommand = new RelayCommand(() => SystemCommands.ShowSystemMenu(Window, MousePosition));
}

在我的RelayCommand类中,我只是使用了ICommand接口来执行以下操作:
public class RelayCommand : ICommand
{
    
    private Action mAction;
    
    public event EventHandler? CanExecuteChanged = (sender, e) => { };
    
    public RelayCommand(Action action)
    {
        mAction = action;
    }
    
    public bool CanExecute(object? parameter)
    {
        return true;
    }

    public void Execute(object? parameter)
    {
        mAction();
    }
}

0
在您的自定义控件中,像您分享的图片一样,将Visual Studio(图标)设置为imagebutton/button,并在单击时从那里显示ContextMenu。
<Button Click="SomeEventHandler">
  <Button.ContextMenu>
     <ContextMenu>
      <!-- DO WHATEVER -->
     </ContextMenu>
  </Button.ContextMenu>
</Button>

然后在单击处理程序中,只需说 buttonName.ContextMenu.IsOpen = true

有关如何实现此操作的更多信息可以在这里找到

已经有一个依赖属性可以设置以显示上下文菜单。我不确定你所说的“你需要自己制作还是可以直接调用这个”是什么意思

编辑:我不明白为什么你要重新创建窗口行为,而不是将自定义窗口继承到窗口类并覆盖需要自定义的内容。


是的,我可以看出你没有正确理解我的意思。我知道如何制作ContextMenu并调用它。我不想制作自己的ContextMenu,而是使用窗口的ContextMenu(包括还原、移动、大小等项目)- 我相信有一种方法,但我不知道怎么做... - Ron
看到了,首先我不确定我知道如何继承。其次,我覆盖了整个设计(基于Windows 8设计制作窗口http://stackoverflow.com/questions/19335343/wpf-net-framework-3-5-window-metro-style - 因为他们的目标是框架4+,所以我不得不自己制作)+ 覆盖了一些功能(例如:在未最大化时添加边框)。 - Ron
你能给我展示一下你的自定义控件吗?包括XAML和后台代码(如果有的话)。 - 123 456 789 0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接