✅ How can i disable maximizing the form

Hi! I am using C# .netframework 4.7.2 and i was trying to disable maximizing and my FormBorderStyle is None, im using my own buttons but actual problem is when i press Win + Up it maximizes again how can i disable this?
75 Replies
Omnissiah
Omnissiah2y ago
have you also disabled maximize button?
TravestyOfCode
I think the simplest solution would be to set your Form's MaximumSize value.
ThєSαvícs ↝ darKSaviC
ok i made it its works fine but another problem is when i press Win + Up it goes top left side its need to be do nothing how can i remove win + up combination on form
ThєSαvícs ↝ darKSaviC
i cant pull down
TravestyOfCode
Change the FormBorderStyle to FixedSingle with no Text value.
ThєSαvícs ↝ darKSaviC
there is no way to make in None? i dont wanna see FixedSingle's tabs
TravestyOfCode
What tabs?
ThєSαvícs ↝ darKSaviC
sorry for my bad english this thing
TravestyOfCode
Set the Form's Text property to nothing
ThєSαvícs ↝ darKSaviC
its still there
TravestyOfCode
Also check if ControlBox is set to false
ThєSαvícs ↝ darKSaviC
why it has a border?
TravestyOfCode
You have text and control box not set properly
ThєSαvícs ↝ darKSaviC
wait im still checking but text is blank and control box is false
TravestyOfCode
And FormBorderStyle is FixedToolWindow?
ThєSαvícs ↝ darKSaviC
wat but you said fixed single
TravestyOfCode
FixedSingle works for me as well
ThєSαvícs ↝ darKSaviC
fixedtoolwindows works i think it seems works but it has a little border
TravestyOfCode
Your screenshot shows that there is still text for the form.
ThєSαvícs ↝ darKSaviC
its label
TravestyOfCode
Yeah, if you don't want the small border, you have to use FormBorderStyle None. You'll need to use the code from https://stackoverflow.com/questions/3213606/how-to-suppress-task-switch-keys-winkey-alt-tab-alt-esc-ctrl-esc-using-low
Stack Overflow
How to Suppress task switch keys (winkey, alt-tab, alt-esc, ctrl-es...
Can anyone please tell me how to disable the task switch keys using c#
ThєSαvícs ↝ darKSaviC
i was using None first time
TravestyOfCode
You can change the objKeyInfo.key checks for the keys you care about. You have to hook into low level keyboard input events to disable the winkey stuff. You probably only need if (objKeyInfo.key == Keys.RWin || objKeyInfo.key == Keys.LWin) instead of the others as those deal with Alt-Tab and other combinations.
ThєSαvícs ↝ darKSaviC
well which ones i need to paste in my project
TravestyOfCode
You shouldn't just paste code in. You should read it and try to understand what it's doing.
ThєSαvícs ↝ darKSaviC
the thing is i cant understand very well
ThєSαvícs ↝ darKSaviC
example what is it
TravestyOfCode
What does the comment above that section say?
ThєSαvícs ↝ darKSaviC
i dont know what is this dll do //System level functions to be used for hook and unhook keyboard input private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr SetWindowsHookEx(int id, LowLevelKeyboardProc callback, IntPtr hMod, uint dwThreadId); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern bool UnhookWindowsHookEx(IntPtr hook); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr CallNextHookEx(IntPtr hook, int nCode, IntPtr wp, IntPtr lp); [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern IntPtr GetModuleHandle(string name); [DllImport("user32.dll", CharSet = CharSet.Auto)] private static extern short GetAsyncKeyState(Keys key); //Declaring Global objects private IntPtr ptrHook; private LowLevelKeyboardProc objKeyboardProcess; this dlls needs to be in my project folder? ok forget what i said where am i need to write this?
TravestyOfCode
It needs to be accessed from the code in your Form_Load event, so probably somewhere in the Form.cs.
ThєSαvícs ↝ darKSaviC
but its gave me too much errors
TravestyOfCode
Where did you put the code?
ThєSαvícs ↝ darKSaviC
in Form_Load event
ThєSαvícs ↝ darKSaviC
my form name is ggg k i put other code it says
TravestyOfCode
The stackoverflow post shows two sections of code. The second says it goes in the Form_Load(). The first does not go in the Form_Load()
ThєSαvícs ↝ darKSaviC
Chatset isnt found i think i need to put some using thing
TravestyOfCode
System.Runtime.InteropServices
ThєSαvícs ↝ darKSaviC
this thing ? i found it Diagnostics its works veryyy well but i need to change something win + up isnt working at all but i want to make alt tab alt tab is good shortcut damn i cant do it when i was discord NOO
TravestyOfCode
That's all handled by the section labled // Disabling Windows keys of the captureKey function You choose which keys and/or combination of keys to ignore.
ThєSαvícs ↝ darKSaviC
ok thanks im going to test it its like a virus u cant use any windows thing damn wait but when i try to change it its allows win + up and i cant make win + r to open "run" i just need to disable win + up
TravestyOfCode
If you only want to disable Win+Up I think something like if ((objKeyInfo.Key == Keys.Rwin && Keys.Up) || (objKeyInfo.Key == Keys.Lwin && Keys.Up)) but I haven't tested it.
TravestyOfCode
I don't speak.. Turkish? So I have no idea what those errors are saying.
ThєSαvícs ↝ darKSaviC
oh hm wait i cant change vs's lang but i have translate Severity Code Description Project File Line Hide Status Error CS1061 'GGG.KBDLLHOOKSTRUCT' does not contain a definition of 'Key' and there is no accessible 'Key' expansion method that accepts a first argument of type 'GGG.KBDLLHOOKSTRUCT' (maybe you're missing a using folders or tutorial?) i solve the problem but its still allow wait
TravestyOfCode
Did you add the code from the second stackoverflow post to your form_load?
ThєSαvícs ↝ darKSaviC
yep i forgot to change it with your code wait im trying to solve this problem CS0019 The '&&' operator cannot be applied to operands of type 'bool' and 'Keys'.
TravestyOfCode
I did my code directly in discord so Im sure it has some typos. LIke I said, I didn't test it.
ThєSαvícs ↝ darKSaviC
i changed your code like this if (objKeyInfo.key == Keys.RWin && Keys.Up || objKeyInfo.key == Keys.LWin && Keys.Up && (ModifierKeys & Keys.Control) == Keys.Control) its says you cannot use bool's && symbol on Keys i think i translated very well 😄 the problem is which thing we need to use to add new combo like win + up, the code errors when we use &&
TravestyOfCode
From testing it also seems like the Win Key combination isn't sent as a combination of keys. I think I found a solution that I'm testing.
ThєSαvícs ↝ darKSaviC
ow good luck
TravestyOfCode
Here's the code that I think should work.
// Disabling Windows keys
if(objKeyInfo.key == Keys.Up)
{
if(GetAsyncKeyState(Keys.LWin) < 0 || GetAsyncKeyState(Keys.RWin) < 0)
{
return (IntPtr)1;
}
}

//if (objKeyInfo.key == Keys.RWin || objKeyInfo.key == Keys.LWin || objKeyInfo.key == Keys.Tab && HasAltModifier(objKeyInfo.flags) || objKeyInfo.key == Keys.Escape && (ModifierKeys & Keys.Control) == Keys.Control)
//{
// return (IntPtr)1; // if 0 is returned then All the above keys will be enabled
//}
// Disabling Windows keys
if(objKeyInfo.key == Keys.Up)
{
if(GetAsyncKeyState(Keys.LWin) < 0 || GetAsyncKeyState(Keys.RWin) < 0)
{
return (IntPtr)1;
}
}

//if (objKeyInfo.key == Keys.RWin || objKeyInfo.key == Keys.LWin || objKeyInfo.key == Keys.Tab && HasAltModifier(objKeyInfo.flags) || objKeyInfo.key == Keys.Escape && (ModifierKeys & Keys.Control) == Keys.Control)
//{
// return (IntPtr)1; // if 0 is returned then All the above keys will be enabled
//}
ThєSαvícs ↝ darKSaviC
bro you are the best coder ive ever seen thanks
TravestyOfCode
BTW, if you're just starting learning, it might be better to swtich away from WinForms. It's old tech and not really being updated/used. WPF is more current.
ThєSαvícs ↝ darKSaviC
you'r right but for now im making simple programs. Btw im going to add new help i think because i need to make daily log system for this project my opinion is when my dad's customers came in our place and when my dad isn't there i have to note it on paper but i dont wanna note it because it tooks too long for me thats why im doing this i have to log it in txt but i need to make like this. Example today is 06.25.2023 right? folder name is 06.25.2023 inside of the folder is need to log.txt its need to change everyday when the date is changed and when the program was started its need to create a new folder but first i need to go wc soo see u later and thanks for helping me 💜
TravestyOfCode
Good luck!
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server