Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools


Microsoft changed the rules with Win98 and Windows 2000 . The SetForegroundWindow API can no longer be used directly to take focus away from another application. Here is the code to implement ForceForeGround window functionality.

Here I have used VB function that uses AttachThreadInput together with SetForegroundWindow. Making a window the foreground window requires more than just calling the SetForegroundWindow API. You must first determine the foreground thread and attach it to your window, using AttachThreadInput, then call SetForegroundWindow. That way they can share input states.

Step-By-Step Example

- Create a standard exe project
- Add one commandbutton, one timer and one checkbox control on the form1
- Add the following code in Form1

Form1.frm

Click here to copy the following block
Const DEMO_TIME = 10  'seconds
Dim nSecondPassed As Integer

Private Sub Command1_Click()
  If Check1.Value = 1 Then Me.WindowState = vbMinimized
  Timer1.Enabled = True
  Command1.Enabled = False
End Sub

Private Sub Form_Load()
  Command1.Caption = "<< Start Demo"
  Check1.Caption = "Run demo with Minimized Window"
  Timer1.Interval = 1000
  Timer1.Enabled = False
End Sub

Private Sub Timer1_Timer()
  If nSecondPassed >= DEMO_TIME Then
    ForceForegroundWindow Me.hWnd
    nSecondPassed = 0
    Timer1.Enabled = False
    Command1.Enabled = True
    Me.Caption = "I am on the Top"
  Else
    nSecondPassed = nSecondPassed + 1
    Me.Caption = "[ " & nSecondPassed & " ] Second(s) passed"
  End If
End Sub

- Add the following code in Module1

Module1.bas

Click here to copy the following block
Option Explicit

Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function IsIconic Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long

Private Const SW_SHOW = 5
Private Const SW_RESTORE = 9

Public Function ForceForegroundWindow(ByVal hWnd As Long) As Boolean
  Dim ThreadID1 As Long
  Dim ThreadID2 As Long
  Dim nRet As Long

  ' Nothing to do if already in foreground.
  If hWnd = GetForegroundWindow() Then
    ForceForegroundWindow = True
  Else
    ' First need to get the thread responsible for this window,
    ' and the thread for the foreground window.
    ThreadID1 = GetWindowThreadProcessId(GetForegroundWindow, ByVal 0&)
    ThreadID2 = GetWindowThreadProcessId(hWnd, ByVal 0&)

    ' By sharing input state, threads share their concept of
    ' the active window.
    If ThreadID1 <> ThreadID2 Then
      Call AttachThreadInput(ThreadID1, ThreadID2, True)
      nRet = SetForegroundWindow(hWnd)
      Call AttachThreadInput(ThreadID1, ThreadID2, False)
    Else
      nRet = SetForegroundWindow(hWnd)
    End If
    '
    ' Restore and repaint
    '
    If IsIconic(hWnd) Then
      Call ShowWindow(hWnd, SW_RESTORE)
    Else
      Call ShowWindow(hWnd, SW_SHOW)
    End If

    ' SetForegroundWindow return success.
    ForceForegroundWindow = CBool(nRet)
  End If
End Function

- Press F5 to run the demo
- You can run this demo in minimized mode or normal mode. When you click on "Start Demo" button after 10 seconds your window will come to the front.


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.