I have a utility WinForm application that generally runs minimized. It is used on a PC in a manufacturing plant. I would like to have the application become not visible when minimized to prevent accidently closing the form or playing with some of the settings. Not meant to be foolproof, just a bit of caution.
However I cannot get the application’s main and only form to reappear sending it a WM_SYSCOMMAND
, SC_RESTORE
message from another instance of the same application.
I have the Resize_Event
set up to set Visible
back to true
when not minimized.
I’ve tried SendMessage
and PostMessage
. If I allow the form to minimize or maximize without setting form’s Visible
property to false, the call works as expected and the target form resizes.
Is the form’s message pump is not running in this state so it ignores the message?
Should I create another thread that checks a message queue every so often? How is that done?
Sample code in the app that is restoring the form:
...
Process current = Process.GetCurrentProcess();
foreach (Process process in Process.GetProcessesByName("MyApp")) { //current.ProcessName)) {
if (process.Id != current.Id) {
SendMessage(process.MainWindowHandle, WM_SYSCOMMAND, (IntPtr)SC_RESTORE, (IntPtr)0);
Interaction.AppActivate(process.Id); //Most reliable way to focus target
SetForegroundWindow(process.MainWindowHandle);
break;
}
}
...
Code in target form that restores visibility.
// If form is minimized then hide it
if (WindowState == FormWindowState.Minimized) {
this.Visible = false;
}
else {
this.Visible = true;
}