Click to See Complete Forum and Search --> : ~Focus~


Starcraft
January 27th, 2000, 11:59 PM
From my program, how would I make another program the active window, such as Microsoft paint or notepad?

Chris Eastwood
January 28th, 2000, 03:37 AM
It depends on which version of Windows you are running. If you are running Windows95, or NT4 then you can use this code :


option Explicit
'
public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(byval hWnd1 as Long, byval hWnd2 as Long, byval lpsz1 as string, _
byval lpsz2 as string) as Long
public Declare Function SetForegroundWindow Lib "user32" _
(byval hwnd as Long) as Long
'
public Sub FindNotepad()
Dim lHwnd as Long
'
lHwnd = FindWindowEx(0, 0, "Notepad", vbNullString)
If lHwnd > 0 then
SetForegroundWindow lHwnd
End If
End Sub




- in a module.

If you use Win98 or Win2K, Microsoft have 'changed the rules' so that an application can't just jump to the front when it feels like it - although Karl Peterson has some excellent code to get around this problem at :

http://www.mvps.org/vb/index2.html?samples.htm - look for the ForceFore.zip sample - be warned though, it's quite heavy going if you don't know anything about the API.


Chris Eastwood

CodeGuru - the website for developers
http://codeguru.developer.com/vb