Setfocus to active window when it looses it

Posted on 2004-11-20
Last Modified: 2010-04-24
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=-
Question by:VBtorment
    LVL 85

    Accepted Solution

    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
    LVL 1

    Author Comment

    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

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    This video discusses moving either the default database or any database to a new volume.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now