Home > Software engineering >  Opening title bar/program icon context menu in a custom location [WPF]
Opening title bar/program icon context menu in a custom location [WPF]

Time:12-17

I was trying to create an extended title bar in my program, I got the basic functionality, such as the extension from the glass frame, the actual moving of the window on click, but I still don't know where to start when mimicking the context menu that appears when right clicking the Windows title bar.

Of course, I could probably throw down some self-made context menu that looks exactly the same, but that would be quite cheaty, inconsistent, and not native.

Here's the code I am currently using:


private void Navbar_MouseDown(object sender, MouseButtonEventArgs e)
{
    if (e.ChangedButton == MouseButton.Left)
    {
        while (e.ButtonState == MouseButtonState.Pressed)
        {
            this.DragMove();
        }
    }
}

private void Navbar_MouseUp(object sender, MouseButtonEventArgs e)
{
    if (e.ChangedButton == MouseButton.Right)
    {
        // open the title bar context menu
    }
}

Any help would be appreciated!

ALSO, I didn't try anything YET, because I don't know where to start, and everywhere I looked for, everything was talking about modifying the context menu, not even describing how to display it programmatically.

I even found a Stack Overflow post that asked for almost the same thing, but instead talking about modifying it, which was not what I wanted. One of it's tags were wpf but it got closed as a duplicate to a post asking how to do that in WinForms, with another answer talking about WinForms, even though the question had the wpf tag.

Stack Overflow's automatic "Do any of these posts answer your question?" tool gave results that were completely unrelated to my question, and brings us back to square one.

CodePudding user response:

You could use the WindowChrome.CaptionHeight property to specify the area at the top of your custom window that enables the system behaviors typically associated with the title bar, such as for example displaying the ContextMenu.

  • Related