This is not a duplicate
Related (Working on windows 10, 7):
- Disable Maximize Button c++ console application
New Problem:
After reading the coments the new problem seems to get the correct window to modify the menu because ‘GetConsoleWindow’ seems to not work.
In Windows 10 you were able to disable / gray out windows Minimize, Naximize and Close buttons but the same methods dont work on windows 11 if anyone has a new working mehtod / alternative or information if its even posible any hint would be apreciated.
Methods i have tryed:
- DeleteMenu + GetConsoleWindow + GetSystemMenu
- EnableMenuItem + GetConsoleWindow + GetSystemMenu
- SetWindowLong + GetConsoleWindow
Mehods to update window i have tryed:
- DrawMenu
- SetWindowPos (SWP_NOSIZE|SWP_NOMOVE|SWP_FRAMECHANGED)
Expectet:
- Grayed out Maximize, Minimize, Close Button / Disabled (not clickable)
Errors Encountered:
-
With FormatMessage and GetLastError i always get either no error or a Invalid Menu Handle Error
-
But no result both ways not visualy (grayed out) or interactivly (can still use buttons)
Resources i have used:
https://learn.microsoft.com/en-us/windows/console/getconsolewindow
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getsystemmenu
https://learn.microsoft.com/en-us/windows/console/getstdhandle
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enablemenuitem
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowlongptra
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-deletemenu
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowpos
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-drawmenubar
EDIT Add Code (Minimal C# Console App to Disable Maximize, Minimize using Delete Menu Method):
class Program
{
private const int MF_BYCOMMAND = 0x00000000;
public const int SC_CLOSE = 0xF060;
public const int SC_MINIMIZE = 0xF020;
public const int SC_MAXIMIZE = 0xF030;
[DllImport("user32.dll")]
public static extern int DeleteMenu (IntPtr hMenu, int nPosition, int wFlags);
[DllImport("user32.dll")]
private static extern IntPtr GetSystemMenu (IntPtr hWnd, bool bRevert);
[DllImport("kernel32.dll", ExactSpelling = true)]
private static extern IntPtr GetConsoleWindow ();
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DrawMenuBar (IntPtr consoleWindow);
static void Main (string[] args)
{
DeleteMenu(GetSystemMenu(GetConsoleWindow(), false), SC_CLOSE, MF_BYCOMMAND);
DeleteMenu(GetSystemMenu(GetConsoleWindow(), false), SC_MINIMIZE, MF_BYCOMMAND);
DeleteMenu(GetSystemMenu(GetConsoleWindow(), false), SC_MAXIMIZE, MF_BYCOMMAND);
DrawMenuBar(GetConsoleWindow());
Console.Read();
}
}
Code Example (Disable Maximize Button using SetWindowLong Method):
class Program
{
// Importing necessary user32.dll functions
[DllImport("kernel32.dll")]
private static extern IntPtr GetConsoleWindow ();
[DllImport("user32.dll")]
private static extern long GetWindowLong (IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong (IntPtr hWnd, int nIndex, long dwNewLong);
[DllImport("user32.dll")]
private static extern bool SetWindowPos (IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
// Constants for window styles
const int GWL_STYLE = -16;
const long WS_MAXIMIZEBOX = 0x00010000;
const uint SWP_NOSIZE = 0x0001;
const uint SWP_NOMOVE = 0x0002;
const uint SWP_FRAMECHANGED = 0x0020;
static void Main ()
{
IntPtr hwnd = GetConsoleWindow();
long style = GetWindowLong(hwnd, GWL_STYLE);
style &= unchecked(~WS_MAXIMIZEBOX);
SetWindowLong(hwnd, GWL_STYLE, style);
SetWindowPos(hwnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_FRAMECHANGED);
Console.WriteLine("Maximize box has been disabled.");
Console.ReadLine();
}
}
FireDragon91245 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
10