Wierd focus problem renders windows out of focus and clickability

Posted on 2005-02-26
Medium Priority
Last Modified: 2008-02-01
I have created a piece of software which uses windows calls to give focus to different windows at different points of time. Clicking a button in one application may trigger another application to gain focus.

This actually works pretty well, however some times it results in the window getting focus however none of the buttons can be pressed, it seems as it is a window out of focus however it is the top one. Now the really wierd part is that by minimizing one of the other Applications the focus to the one with the problem is restored and one can continue.

Does anyone have any idea what could be the cause of this ?

This is the code which causes the conflict

Private Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long
Public Function switchToCash(billToOpen As Long, Optional person_id As Long)
    On Error Resume Next
    Unload frmFront
    On Error GoTo here
    Dim nrs As New ADODB.Recordset
    If person_id <> 0 Then
        frmFront.storedPID = person_id
    ElseIf frmBusinessView.currentSetup.persLog Then
        If frmBusinessView.currentSetup.persCheckedIN Then
            nrs.Open "select personale_navn,personale_id from personale_db where personale_id in(select person_id from checks where checkedOut is null)", frmFront.bookingCnxn
            nrs.Open "select personale_navn,personale_id from personale_db", frmFront.bookingCnxn
        End If
        If nrs.EOF Then
            BringWindowToTop frmBusinessView.hwnd
            MsgBox "Der er intet personale checket ind"
            GoTo thisPlace
        End If
        BringWindowToTop frmPersLogin.hwnd
        frmFront.storedPID = frmPersLogin.create(frmBusinessView)
    End If
    frmFront.tableID = -1
    frmFront.currentBill = billToOpen
    BringWindowToTop frmFront.hwnd
    frmFront.Hide ''1, frmBusinessView
    frmFront.Show 1, frmBusinessView
    Exit Function
    MsgBox "Der opstod en fejl..." & vbNewLine & Err.Description
End Function
Question by:mSchmidt
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
LVL 32

Accepted Solution

Erick37 earned 2000 total points
ID: 13410632
"Microsoft changed the rules with Win98 and Windows 2000 ("The OS formerly known as NT5"). The SetForegroundWindow API can no longer be used directly to take focus away from another application."


To bring a window in another process to the top and in focus, use the ForceForegroundWindow function from the link above.  Place the following code in a standard module, and call the function like this:

Call ForceForegroundWindow(hwnd)

'=========Standard Module code========

Option Explicit

' *********************************************************************
'  Copyright ©1998 Karl E. Peterson, All Rights Reserved
'  http://www.mvps.org/vb
' *********************************************************************
'  You are free to use this code within your own applications, but you
'  are expressly forbidden from selling or otherwise distributing this
'  source code without prior written consent.
' *********************************************************************
' 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
      ' 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)
         nRet = SetForegroundWindow(hWnd)
      End If
      ' Restore and repaint
      If IsIconic(hWnd) Then
         Call ShowWindow(hWnd, SW_RESTORE)
         Call ShowWindow(hWnd, SW_SHOW)
      End If
      ' SetForegroundWindow return accurately reflects success.
      ForceForegroundWindow = CBool(nRet)
   End If
End Function

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question