- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 ...
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Whoops, attachment follows
Greg
Greg, that was helluva complicated. FindWindow does the job in one step.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quote, "If lpClassName is NULL, it finds any window whose title matches the lpWindowName parameter. ".
Jugoslav
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
BTW, is a negative handle value a sign of failure in any cases?
Bear
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Jugoslav,
Many thanks - it's difficult to find in Help the answers to simple questions such as mine.
Bear of Little Brain

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page