views:

3462

answers:

4

WPF doesn't provide the ability to have a window that allows resize but doesn't have maximize or minimize buttons. I'd like to able to make such a window so I can have resizable dialog boxes.

I'm aware the solution will mean using pinvoke but I'm not sure what to call and how. A search of pinvoke.net didn't turn up any thing that jumped out at me as what I needed, mainly I'm sure because Windows Forms does provide the CanMinimize and CanMaximize properties on its Windows.

Could someone point me towards or provide code (C# prefered) on how to do this?

+10  A: 

I've stolen some code I found on the MSDN forums and made an extension method on the Window class, like this:

internal static class WindowExtensions
{
    [DllImport("user32.dll")]
    internal extern static int SetWindowLong(IntPtr hwnd, int index, int value);

    [DllImport("user32.dll")]
    internal extern static int GetWindowLong(IntPtr hwnd, int index);

    internal static void HideMinimizeAndMaximizeButtons(this Window window)
    {
        const int GWL_STYLE = -16;

        IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(window).Handle;
        long value = GetWindowLong(hwnd, GWL_STYLE);

        SetWindowLong(hwnd, GWL_STYLE, (int)(value & -131073 & -65537));

    }
}

The only other thing to remember is that for some reason this doesn't work from a window's constructor. I got around that by chucking this into the constructor:

this.SourceInitialized += (x, y) =>
{
    this.HideMinimizeAndMaximizeButtons();
};

Hope this helps!

Matt Hamilton
Indeed it did! Thanks muchly!
Nidonocu
Perhaps it would make more sense to do this after the handle has been created, perhaps in a handler for the Initialized event on the Window?
Greg D
Nice workaround Mike. Works perfectly.
BrettRobi
+1  A: 
Gishu
This almost works, but its still possible to maximize or minimize the window if you double click the title bar, right click and use the control menu or the taskbar button if it is avalible. Plus of course it looks like a tool window, not a normal one.
Nidonocu
Right... but then again IMHO the constraint seems odd that the user is not allowed to maximise but can manually drag-enlarge the window by resizing. But it's your window.. your rules :)
Gishu
A: 

Code given above dint work for me. No change in the window display.....

A: 

You can set the ResizeMode="NoResize" of the window if you want to remove Minimize and Maximize button

Chinmay Behera