on 1:APPACTIVE:echo mIRC active status: $appactive
The above event triggers whenever mIRC's active status has changed. The $appactive identifier returns $true if mIRC is active, or $false if it isn't.
this combined with the window state will get you what you want I think