Reputation: 376
I'm trying to disable the maximize button of my WPF window at runtime: when I click on a "disable" button the maximize button should be disabled.
To accomplish this I'm using the following code:
private const int GWL_STYLE = -16;
private const int WS_MAXIMIZEBOX = 0x10000;
[DllImport( "user32.dll" )]
private static extern int GetWindowLong( IntPtr hWnd, int nIndex );
[DllImport( "user32.dll" )]
private static extern int SetWindowLong( IntPtr hWnd, int nIndex, int dwNewLong );
public static void DisableMaximizeOf( Window window )
{
IntPtr winHandle = new WindowInteropHelper( window ).Handle;
// Get the current style and remove the WS_MAXIMIZEBOX bits
int style = GetWindowLong( winHandle, GWL_STYLE );
SetWindowLong( winHandle, GWL_STYLE, style & ~WS_MAXIMIZEBOX );
}
After DisableMaximizeOf(...) was called the maximize-functionality is correctly disabled - the only problem is that the maximize button still looks like before:
If I minimize this window and restore it again, the button is correctly displayed:
So I tried to refresh the GUI, but nothing seemed to work. Is there a solution for this? Any help would be appreciated.
Upvotes: 0
Views: 921
Reputation: 48
1) Go to the MainWindow.xaml page where you can view and design your form.
2) Open the XAML window and paste this code below in but replace "your_namespace" with your program's namespace (refer to the image attached)
xmlns:local="clr-namespace:your_namespace" ResizeMode="CanMinimize"
This might be a better alternative for you due to it's simplicity.
Upvotes: 0
Reputation: 431
This is taken directly from PInvoke.net
Certain window data is cached, so changes you make using SetWindowLong will not take effect until you call the SetWindowPos function. Specifically, if you change any of the frame styles, you must call SetWindowPos with the SWP_FRAMECHANGED flag for the cache to be updated properly.
Upvotes: 1