PDA

View Full Version : How do I force a messageBox to the forground in BasicMSI



Brandon Lowe
07-25-2012, 09:46 AM
I have a BasicMSI installation I'm working with. In a couple places I display a AskYesNo question and some times that will pop up under the forground window. Is they a command I can use right before the AskYesNo to force it to the top?

Thanks
Brandon

rguggisberg
07-25-2012, 01:11 PM
Ahhh yes... that subject comes up every now and then! Here's what I recently did. Add the custom action below and then just add the line
SetWindowFocus();
to your desired Custom Actions where you have this issue. Of course this is not goof proof because someone could be navigating willy-nilly around the screen after you have SetWindowFocus... but it solves the problem for most practical cases.
-----------------------------------------------------------
function SetWindowFocus()
HWND hMain;

begin
hMain = GetWindowHandle( HWND_INSTALL );
//Attach foreground window thread to installation thread
AttachThreadInput( GetWindowThreadProcessId( GetForegroundWindow(), NULL ), GetCurrentThreadId(), TRUE );

//Bring to foreground
SetWindowPos( hMain, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
SetForegroundWindow( hMain );

//Detach the attached thread
AttachThreadInput( GetWindowThreadProcessId( GetForegroundWindow(), NULL ), GetCurrentThreadId(), FALSE );
SetFocus( hMain );
end;
-----------------------------------------------------------

MichaelU
07-27-2012, 09:21 AM
For a Basic MSI, you should probably use MsiProcessMessage instead. This will allow Windows Installer to properly parent the dialog, and suppress it in scenarios that should not show UI. However the API will not work if the custom action is invoked in response to a ControlEvent (such as a button press on a dialog).