Mr. Cluey : How To ...? : Captions


How do I determine if a window has a caption?

One problem with Partner's implementation of captions is that, in the absence of a caption on the control, the text prior to the control is used. If you are trying to detect blank buttons, for instance, GetCaption () hides exactly the information that you are hoping to verify.

Time to look for a back door. We can't get the prior text of a control at runtime, but we can check for the existance of a control which just happens to have the prior text we seek.

The quick answer is to GetCaption the control we are trying to verify, convert the returned text to a prior text tag (using the ^ prefix). If that control exists, we almost certainly have a button with no tag. You can see this for yourself by using the record declarations dialog - you will almost never see a labeled button with the same string in the prior text tag.

Our first pass at this function looks like this:

boolean HasCaption ( window w )
{
	string TempCaption = w.GetCaption () ;
	WNDTAG PriorText = "^" + TempCaption ;
	
	window wTarget = w.GetParent().@([string]ClassOf(w))(PriorText) ;

	if wTarget.Exists () 
		return ( false ) ; // the caption comes frfom some other control
	else
		return ( true )  ; // the caption did come from this control
}

wTarget deserves some explanation. ClassOf() returns the DATACLASS of a window, which can then be cast to a string. We dereference this using the @ operator to get a window again. GetParent() is used to be certain that we look in the right place - GetParent is used rather than WindowParent because we are concerned with finding controls that may only exist at run time.

This is certainly sufficient for casual use, but isn't really as safe as we would like. Some other control might have prior text which matches the caption on the window we are looking at. We need to be sure that w and wTarget are the same.

Well, window handles are unique; if A.hWnd == B.hWnd, it follows that A == B. So we will take advantage of that. As windows have handles only when they exist, to avoid throwing an exception we leave in the wTarget.Exists() test.

The final implementation of this function would look like this:

boolean HasCaption ( window w )
{
	string TempCaption = w.GetCaption () ;
	string PriorText = "^" + TempCaption ;
	
	window wTarget = w.GetParent().@([string]ClassOf(w)) (PriorText ) ;
	boolean bRetVal = true ;
	
	if wTarget.Exists ()
		if ( wTarget.hWnd == w.hWnd  )
			bRetVal = false ;

	return ( bRetVal ) ;
}

Now, there is one tiny bug with this implementation - if a button is adjacent to text field with matching captions (an OK button next to a static text field that reads "OK") this function will report that the button doesn't have a caption.


Mr. Cluey : How To ...? : Captions

Return to ATS Automated Testing Resources Page