I'm wondering if there's a good way to prevent any application from Minimizing my Delphi form? I currently have a borderless VCL form and the WindowState is set to wsMaximized. I'd like to keep it in the maximized state permanently. Any advice on how to do this would be most wonderful. Thank you
Asked
Active
Viewed 169 times
1
-
1Sensing an XY question, do consider perhaps an [embedded](https://download.microsoft.com/download/C/E/5/CE5DAF5E-86E1-4AEE-AFF0-1E0975ABB1DE/Windows%20Embedded%20Version%20Overview.pdf) or [kiosk mode](https://learn.microsoft.com/en-us/windows/configuration/kiosk-single-app) deployment for this application. It sounds like that's what you want. – J... Mar 02 '20 at 20:50
-
As per possible duplicate https://stackoverflow.com/questions/943551/dont-want-form-to-minimize , you can remove the biMinimize BorderIcon. – Sertac Akyuz Mar 03 '20 at 13:24
1 Answers
5
Override your Form's virtual WndProc()
method and have it discard any WM_SYSCOMMAND
messages whose wParam
contains SC_MINIMIZE
or SC_RESTORE
.

Remy Lebeau
- 555,201
- 31
- 458
- 770
-
This works fine from within my own window but other programs are still able to minimize my window. I've just tested with Process Explorer by selecting my process, right-clicking and using the built-in Window menu to Minimize. – WayOfTheWright Mar 02 '20 at 20:30
-
var lpOldWndProc: PVOID = nil; function NewWndProc(Window: HWND; Msg, WP: Integer; LP: Integer): Integer; stdcall; begin if (wP = SC_MINIMIZE) or (wP = SC_RESTORE) then result := 0 else result := CallWindowProc(PVOID(lpOldWndProc), Window, Msg, WP, LP) end; procedure TForm1.FormCreate(Sender: TObject); begin lpOldWndProc := PVOID(SetWindowLongPtr(Form1.Handle, GWL_WNDPROC, NativeInt(@NewWndProc))); end; – WayOfTheWright Mar 02 '20 at 20:32
-
1
-
-
@WayOfTheWright you need to check `Msg = WM_SYSCOMMAND` before looking at `WP`. But even if you don't `override` the `WndProc()` method like I suggested, you can use the `WindowProc` property instead of using `SetWindowLongPtr()` manually (and even then, `SetWindowSubclass()` would have been more preferred). But yes, the end effect would be the same no matter how you subclass the window procedure. Try intercepting the [`WM_WINDOWPOSCHANGING`](https://learn.microsoft.com/en-us/windows/win32/winmsg/wm-windowposchanging) message instead and see what happens – Remy Lebeau Mar 02 '20 at 20:36
-
Yes, the effect would be the same. So use the clean method and not the hack that breaks in the face of window recreation. – David Heffernan Mar 02 '20 at 20:37
-
That would be true only if I were dynamically creating forms, but I'm not in this case. Form1 remains statically created so it's never recreated. I will keep this in mind though for future reference. – WayOfTheWright Mar 02 '20 at 20:57
-
3I said window recreation and not form recreation. The window can be recreated in certain circumstances and your code will break. Don't use your hack. Do it either of the ways Remy said. – David Heffernan Mar 02 '20 at 21:28
-
Understood. I've since used the virtual WndProc as per his suggestion. "The window can be recreated in certain circumstances" I've literally never observed this happening. This window you refer to would be accessed from Form1.Handle in my case and that handle remains the same throughout the lifetime of my "Form" so I have no idea what you're referring to. Also, if a window was recreated wouldn't that trigger the VCL to again call event handlers such as FormCreate() again? That would be dangerous – WayOfTheWright Mar 02 '20 at 21:50
-
1@WayOfTheWright "*This window you refer to would be accessed from Form1.Handle*" - yes. The HWND assigned to the `Handle` can be recreated by the VCL dynamically, creating a new HWND. It is less likely for a Form, more likely for child controls, but it *can* happen for a Form, too. "*if a window was recreated wouldn't that trigger the VCL to again call event handlers such as FormCreate() again?*" - no, because such events are Form events not window events. The Form is not being recreated, just its HWND. – Remy Lebeau Mar 03 '20 at 00:10
-
You might never have observed window recreation for your forms, but it can and does happen. So if you must apply customisations to your window then use `CreateParams` or `CreateWnd/DestroyWindowHandle`. – David Heffernan Mar 03 '20 at 09:18
-
Note that `SetWindowPlacement`, `ShowWindow` etc.. do not generate system commands but they can minimize a window. – Sertac Akyuz Mar 03 '20 at 13:13
-
Ok, thanks to all 3 of you (David, Remy and Sertac) for the great information – WayOfTheWright Mar 03 '20 at 20:05