|
|
|
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 |
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 ) |
|
|