Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make Window Top Most......But..

Status
Not open for further replies.

SQLwannabe

Programmer
May 3, 2001
16
0
0
US
I've looked around and found the code to make a window top most, but the code I've found also makes it so it is top most even if you activate another program. I'd like to keep it so it becomes top most when activated, but also allow other windows in front of it if they are activated.

How would you do this?

Thanks!
 
Hi,

In win98 is easy; use the setForgroundWindow API.

However in newer window versions it is only possible to bring a window to the front from the application currently in focus. This can be circumvented by attaching to the currently active appication and then bringing your window to the front. This snipplet does the job (downloaded from -------------------------------------------------------
Option Explicit
'
' Required Win32 API Declarations
'
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
'
' Constants used with APIs
'
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
' the foreground window, then the thread running
' the passed 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 accurately reflects
' success.
ForceForegroundWindow = CBool(nRet)
End If
End Function
-------------------------------------------------------


Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top