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


Have you ever tried to use SetForegroundWindow function in Windows 2000/ME ? In windows 9x its very easy to set foreground window, just pass window handle to SetForegroundWindow and done... But in Windows 2000 and ME microsoft has changed the process for setting foreground window. In windows 2000 and ME foreground application must grant SetForeground Window Permission to the process who wants to be foreground window by calling AllowSetForegroundWindow API.

Step-By-Step Example

Project1

- Create a standard exe Project
- Add a timer control on the form1
- Place the following code in form1 code window

Project1 - > Form1.frm

Click here to copy the following block
Private Declare Function SetForegroundWindow Lib "user32" _
    (ByVal hwnd As Long) As Long

Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long

Private Declare Function SetProp Lib "user32" Alias "SetPropA" _
    (ByVal hwnd As Long, _
    ByVal lpString As String, _
    ByVal hData As Long) As Long

Private Sub Form_Load()
  Me.Left = 100
  Me.Top = 100
  Me.Caption = "I wanna be foreground window"
  Timer1.Enabled = True

  '//Store ProcessId as a Property of this window so other guys can easily get
  '//ProcessId of this Process by just using window handle
  SetProp Me.hwnd, "ProcessId", GetCurrentProcessId

  '//Every 10 seconds we will call SetForegroundWindow to set foreground window
  Timer1.Interval = 10000  'set interval to 5 seconds
End Sub

Private Sub Timer1_Timer()
'Now try to call SetForegroundWindow to set our form as a foreground window
'NOTE: if you are running Win9x then no problem but for Win ME/2000 SetForegroundWindow will fail
'    under Win 2000/ME foreground window has to call AllowSetForegroundWindow to give SetForegroundWindow
'    permission for our application if you dont have permission then you cant set foreground window,
'    it will only flash the titlebar if no SetForeGroundWindow permission

  ret = SetForegroundWindow(Me.hwnd)

End Sub

Now we need another application which will grant SetForeGround permission to Project1

Project2

- Create a standard exe Project
- Add 1 textbox control and 1 command button on the form1
- Place the following code in form1 code window

Project2 - > Form1.frm

Click here to copy the following block
Private Declare Function GetProp Lib "user32" Alias "GetPropA" (ByVal hwnd As Long, ByVal lpString As String) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function AllowSetForegroundWindow Lib "user32.dll" (ByVal dwProcessId As Long) As Long
Private Sub Form_Load()
  Command1.Caption = "Grant SetForeground Permission"
  Text1 = "I wanna be foreground window"
  Me.Caption = "Grant SetForeground Permission using AllowSetForegroundWindow API"
End Sub
Private Sub Command1_Click()
  Dim mWnd As Long, hProcessID As Long
  mWnd = FindWindow(vbNullString, "I wanna be foreground window")
  If mWnd = 0 Then
    MsgBox "Couldn't find other window!"
    Exit Sub
  End If
  hProcessID = GetProp(mWnd, "ProcessID")
  ret = AllowSetForegroundWindow(hProcessID)
  If ret Then
    Me.Caption = "SetForeGround Permission Granted to ProcessId [" & hProcessID & "]"
  Else
    MsgBox "Error occurred : Error#" & Err.LastDllError
  End If
End Sub

- Now run Project1 and then Project2 so Project2 window stay on the top.
- Notice that if you dont press grant SetForeGround permission and you are running on Win 2k/ME then Project1 window cant set itself to foreground window, it will simly flash the title bar every 10 seconds.
- Now press the "Grant SetForeGround Permission" button and watch what happens.... Now project one can set it self as foreground window because Project2 which was Foreground window has granted the permission to Project1 window.


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.