Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28995 Discussions

getting a window handle based upon its title

chunky_lover_23
Beginner
1,273 Views

If I know the title of a win32 application window, could someone help with the function or do while loop where you can get the window handle ..

I'm trying to send a message from oneopengl application to another ...

0 Kudos
11 Replies
gregs_cvf
Beginner
1,273 Views
Quoting - chunky lover 23

If I know the title of a win32 application window, could someone help with the function or do while loop where you can get the window handle ..

I'm trying to send a message from oneopengl application to another ...

This is not as easy as it might seem. it requires use of EnumDesktopWindows(), and a teston all resulting windows for the desired title. I have attached a version that seems to work.

Greg

0 Kudos
gregs_cvf
Beginner
1,273 Views

Whoops, attachment follows

Greg

!GetWinHandle--Gets the handle of an active window with a matching title
!>>>>>>See also GetForegroundWindow() which may perform this same function!!!<<<<<
!This Function loops through all active windows until a matching title is found.
!Then it returns the handle of the window, or zero if no matching window title found.
!If more than one matching window is found it returns the handle of the first.or last
!matching window depending on optional argument Last. It also optionally may return
!the number of matching windows found.
!
!FUNCTION GetWinHandle(wTitle, Last, nMatches)
!***INPUT ARGS
!wTitle[C*(*)]=Window title to be searched for
!wTitle is case sensitive
!Comparison is only done out to LEN(wTitle) or out to Null character
!wTitle may or may not be a C-String.
!Last[L4][OPTIONAL]=If present, and .TRUE. then the last matching window is returned,
!Otherwise, the first matching window is returned
!***OUTPUT ARGS
!nMatches[I4][OPTIONAL]=Number of windows found with a matching title
!FullTitle[C*(*)][OPTIONAL]=Full title of matching window. May share storage with wTitle.
!***RETURNED FUNCTIONAL VALUE
!WinHandle[I4]=The handle of last window with a matching title if Last is
! present and is .TRUE. Otherwise it returns the handle of the first matching
! window title. =0 if no matching window titles are found

MODULE GetWinHandleMod
USE DFWIN, ONLY: EnumDesktopWindows, GetWindowText
IMPLICIT NONE
PRIVATE
PUBLIC GetWinHandle
INTEGER wHWnd, nMatch, Len1
CHARACTER*256 uTitle, vTitle
LOGICAL Last1

CONTAINS

FUNCTION GetWinHandle(wTitle, Last, FullTitle, nMatches) RESULT(WinHandle)
INTEGER:: WinHandle
CHARACTER*(*), INTENT(IN):: wTitle
LOGICAL, OPTIONAL, INTENT(IN):: Last
CHARACTER*(*), OPTIONAL, INTENT(OUT):: FullTitle
INTEGER, OPTIONAL, INTENT(OUT):: nMatches
!
INTEGER iRet
wHWnd = 0
nMatch = 0
Last1 = .FALSE.
IF(PRESENT(LAST))Last1 = Last
Len1 = index(wTitle, CHAR(0)) - 1
if(Len1 .GT. 0) then
uTitle = wTitle(1 : Len1)
else
uTitle = wTitle
Len1 = LEN(wTitle)
end if
!Enumerate (loop) through all the top-level windows and link to EnumWindowsProc
iRet = EnumDesktopWindows(0, Loc(EnumWindowsProc), 0)
WinHandle =wHWnd
IF(Present(FullTitle)) FullTitle = vTitle
IF(Present(nMatches)) nMatches = nMatch
END FUNCTION GetWinHandle

!EnumWindowsProc--Callback function to test if matching title is found
!This function is called iteratively for each active window by EnumDesktopWindows.
!The functional format is as required. The function returns 1 (TRUE) to continue
!the enumeration, 0 (FALSE) to stop.
INTEGER FUNCTION EnumWindowsProc(hWindow, lParam)
INTEGER, INTENT(IN) :: hWindow
INTEGER, INTENT(IN) :: lParam
!DEC$ ATTRIBUTES VALUE :: hWindow, lParam
!
CHARACTER(256) szTitle
integer iRet
EnumWindowsProc = 1
!Get the Windows Title bar text for this window
iRet = GetWindowText(hWindow, szTitle, Len(szTitle))
if (iret > 0) then
!write(*,*)' "' // sztitle(1:iRet) // '" ', len_trim(sztitle)
if (INDEX(szTitle(1 : iRet), uTitle(1 : Len1)) .EQ. 1) then
!Here if matching window found... set wHWnd and iterate nMatch
nMatch = nMatch + 1
if(Last1 .OR. nMatch .EQ. 1) then
wHWnd = hWindow
vTitle = szTitle(1 : iRet)
end if
!Use this to terminate enumeration
!EnumWindowsProc = 0
end if
endif
END FUNCTION EnumWindowsProc
END MODULE GetWinHandleMod

0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,273 Views
Quoting - gregs_cvf

Whoops, attachment follows

Greg

Greg, that was helluva complicated. FindWindow does the job in one step.

0 Kudos
gregs_cvf
Beginner
1,273 Views
Quoting - Jugoslav Dujic

Greg, that was helluva complicated. FindWindow does the job in one step.

Yes complicated, but I've never been able to figure out what the first arg of FindWindow is for common window types.

Help says it must be the "class name or an atom that identifies the class-name string". This is somewhat foreign to me.

Any suggestions?

Greg

0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,273 Views
Quoting - gregs_cvf

Yes complicated, but I've never been able to figure out what the first arg of FindWindow is for common window types.

Help says it must be the "class name or an atom that identifies the class-name string". This is somewhat foreign to me.

Any suggestions?

Greg

Quote, "If lpClassName is NULL, it finds any window whose title matches the lpWindowName parameter. ".

Jugoslav

0 Kudos
bearoflittlebrain_ol
1,273 Views
Quoting - Jugoslav Dujic

Quote, "If lpClassName is NULL, it finds any window whose title matches the lpWindowName parameter. ".

Jugoslav

0 Kudos
bearoflittlebrain_ol
1,273 Views
Quoting - Jugoslav Dujic

Quote, "If lpClassName is NULL, it finds any window whose title matches the lpWindowName parameter. ".

Jugoslav

Bother - hit the wrong button there! Jugoclav's quote is from the MSDN Library's online help, but not in the Help that came with my CVF6.6. The moral is to look online if stuck.

Bear

0 Kudos
gregs_cvf
Beginner
1,273 Views

Yes, FindWindow(NULL, ...) does indeed work. But be aware, the full exact window title must still be used.

The EnumDesktopWindows loop maystill be of usewhen the full title is too cumbersome, or whenyou only have access to an abbreviated form of the window title.

Greg

0 Kudos
bearoflittlebrain_ol
1,273 Views

BTW, is a negative handle value a sign of failure in any cases?

Bear

0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,272 Views

BTW, is a negative handle value a sign of failure in any cases?

Bear

No. You shouldn't hold any presumption about handle value, other than that it's different from zero and INVALID_HANDLE_VALUE (=-1 if I recall correctly).

0 Kudos
bearoflittlebrain_ol
1,272 Views

Jugoslav,

Many thanks - it's difficult to find in Help the answers to simple questions such as mine.

Bear of Little Brain

0 Kudos
Reply