Sunday, July 23, 2017

Find any Window by Title in Visual Foxpro



** FINDTITL.PRG
PROCEDURE FINDTITL
PARAMETERS zcTitle, zlExact, zlUpper

** Checks to see if the Title sent in (zcTitle) matches
**  the title of any active desktop window.
** The second parameter (zlExact) tells us to check the
**  beginning of the title for an exact match or check
**  anywhere in the title for zcTitle.
**
** zcTitle is CASE SENSITIVE unless zlUpper is .T.

** If the title is found then that window is brought
**  forward and .T. is returned.

IF TYPE("ZCTITLE") != "C" OR TYPE("ZLEXACT") != "L" ;
    OR TYPE("ZLUPPER") != "L"
   ** Bad parameter(s).
   RETURN .F.
ENDIF

IF zlUpper
   zcTitle = UPPER(zcTitle)
ENDIF

#DEFINE GW_HWNDNEXT              2
#DEFINE GW_CHILD                 5
#DEFINE SW_RESTORE               9

DECLARE INTEGER GetWindow IN USER32 INTEGER, INTEGER
DECLARE INTEGER GetDesktopWindow IN USER32
DECLARE INTEGER GetWindowText IN USER32 INTEGER, ;
   STRING @, INTEGER
DECLARE INTEGER IsIconic IN USER32 INTEGER
DECLARE INTEGER SetActiveWindow IN USER32 INTEGER
DECLARE INTEGER ShowWindow IN USER32 INTEGER, INTEGER

PRIVATE qnHwnd, qlReturn
qlReturn = .F.
** Get the handle of the first window on the Desktop.
qnHwnd = GetWindow(GetDesktopWindow(), GW_CHILD)

PRIVATE qcTitle, qnLen
** Loop through all active windows.
DO WHILE qnHwnd > 0
   qcTitle = REPL(CHR(0), 128)
   qnLen = GetWindowText(qnHwnd, @qcTitle, 128)
   qnLen = MIN(qnLen, 128)
   qcTitle = SUBSTR(qcTitle, 1, qnLen)
   IF zlUpper
      qcTitle = UPPER(qcTitle)   
   ENDIF
   IF ! EMPTY(qcTitle)
      IF zlExact ;
          AND zcTitle==SUBSTR(qcTitle,1,LEN(zcTitle))
         qlReturn = .T.
         EXIT
      ENDIF
      IF ! zlExact AND zcTitle $ qcTitle
         qlReturn = .T.
         EXIT
      ENDIF
   ENDIF
   
   ** Get the next window handle.
   qnHwnd = GetWindow(qnHwnd, GW_HWNDNEXT)

ENDDO
IF qlReturn
   ** Attempt to bring the found window forward.
   IF IsIconic(qnHwnd) > 0
      =ShowWindow(qnHwnd, SW_RESTORE)
   ENDIF
   =SetActiveWindow(qnHwnd)
ENDIF

RETURN qlReturn

}

No comments:

Post a Comment