Setfocus to active window when it looses it

I have a application that checks mail every 10 minutes
when mail is found it pops up a window above the tray.(i don't like the windows balloon :))
The popup may not open is the active window is Maximized (IsZoomed), this is for if i'm playing a game.
The problem is that this window gets the focus.

lets say i'm working in MSWord and my program fires the popupscreen, i loose the focus on MSWord
i must either click MSWord or wait for the popup to disapear after 10 seconds

is there a way to popup a window that does not recieve focus or after opening the window, give the focus back to the the active window.

i tried the following things:


This doesn't work because these api's only work on the apllications thread

Then i tried

        Dim p As Process

        For Each p In Process.GetProcesses(System.Environment.MachineName)
            Dim fw As Integer = FindWindow(vbNullString, p.MainWindowTitle)
            If IsZoomed(fw) <> 0 Then
            End If
        Next p

But now i cannot see wich of the FindWindows is the active one in Windows :(

Please Help

Greetinz -=TR2=-
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is how to create a form that does not steal the focus.  Set the TopMost property to true.

Imports System.Runtime.InteropServices

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Const WS_EX_NOACTIVATE = &H8000000
    Private Const WM_MOVING = &H216

    Private Structure RECT
        Public Left As Integer
        Public Top As Integer
        Public Right As Integer
        Public Bottom As Integer
    End Structure

    Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams
            Dim cp As CreateParams = MyBase.CreateParams
            cp.ExStyle = cp.ExStyle Or WS_EX_NOACTIVATE
            Return cp
        End Get
    End Property

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = WM_MOVING Then
            Dim r As RECT
            r = DirectCast(Marshal.PtrToStructure(m.LParam, GetType(RECT)), RECT)
            Me.Location = New Point(r.Left, r.Top)
        End If

    End Sub

End Class

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VBtormentAuthor Commented:
Thank you very much, it works :)

I don't understand the code tho
but i will figure it out later

Points are yors...

Thnks Torment
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.