• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 470
  • Last Modified:

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:

hnd=GetActiveWindow
Form.Show
SetFocus(hnd)

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
                Console.WriteLine(p.MainWindowTitle)
            End If
        Next p

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

Please Help

Greetinz -=TR2=-
0
VBtorment
Asked:
VBtorment
1 Solution
 
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
        Get
            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

        MyBase.WndProc(m)
    End Sub

End Class
0
 
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
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now