I've got a window where I use ScrollWindow and I'm curious if there's an easy way to determine if various controls on the window are viewable from the ScrollWindow's current position. My window only scrolls vertically, so I'm guessing I could check the location of the control and see if its currently in the range being displayed, but I thought IsWindowVisible (tried this, didn't work) or something similar existed for this situation (but couldn't find it).
Announcement
Collapse
No announcement yet.
ScrollWindow... Is a control visible?
Collapse
X
-
Try the IntersectRect WinApi function using the client area of the screen and window rect of the control. If any areas are in common (a rect is returned) the control is at least partially visible.
MCM
ADDED:
Not tested but I think it will work.
Code:FUNCTION IsControlInVisibleArea (hWndparent, %ID_CONTROL) AS LONG LOCAL rParent AS RECT, rCtrl AS RECT, rDest AS RECT, iRet AS LONG ' Get RECT of parent Window GetClientRect hwndParent, rHwnd ' client coodinates ' -------------------------------------------------- ' convert client coordinates to screen coordinates ' -------------------------------------------------- MapWindowPoints hWndParent, %HWND_DESKTOP, BYVAL VARPTR(rParent), BYVAL 2& 'when using RECT cPoints=2 ' ------------------------------------------------ ' Get screen coordinates of the Control's RECT ' ------------------------------------------------ GetWindowRect GetDlgItem (hWndParent, %ID_CONTROL), rCtrl ' screen coordinates ' -------------------------------- ' See if the two RECTs intersect. ' -------------------------------- iRet = IntersectRect (rDest, rParent, rCtrl) FUNCTION = ISTRUE (iret) ' if true is returned the RECTs intersect END FUNCTION
Last edited by Michael Mattias; 20 Apr 2008, 11:43 AM.Michael Mattias
Tal Systems (retired)
Port Washington WI USA
[email protected]
http://www.talsystems.com
-
Hey John,
for the fun of it, here is a little one using
GetClientRect, GetWindowRect, MapWindowPoints and PtInRect.
Code:#COMPILE EXE '#Win 8.04# #DIM ALL #INCLUDE "WIN32API.INC" '#2005-01-27# %LABEL = 200 '______________________________________________________________________________ CALLBACK FUNCTION DlgProc() AS LONG LOCAL si AS SCROLLINFO LOCAL DlgRect AS RECT LOCAL CtrlRect AS RECT LOCAL hControl AS DWORD LOCAL oldPos AS LONG LOCAL WmScroll AS LONG LOCAL iSeeYou AS LONG SELECT CASE CBMSG CASE %WM_INITDIALOG si.cbSize = LEN(si) si.fMask = %SIF_ALL si.nMin = 0 si.nMax = 800 si.nPage = 50 si.nPos = 0 SetScrollInfo CBHNDL, %SB_VERT, si, 1 SetScrollInfo CBHNDL, %SB_HORZ, si, 1 CASE %WM_HSCROLL, %WM_VSCROLL si.cbSize = SIZEOF(si) si.fMask = %SIF_ALL IF CBMSG = %WM_HSCROLL THEN GetScrollInfo CBHNDL, %SB_HORZ, si oldPos = si.nPos SELECT CASE LOWRD(CBWPARAM) CASE %SB_LINELEFT : si.nPos = si.nPos - 5 CASE %SB_PAGELEFT : si.nPos = si.nPos - si.nPage CASE %SB_LINERIGHT : si.nPos = si.nPos + 5 CASE %SB_PAGERIGHT : si.nPos = si.nPos + si.nPage CASE %SB_THUMBTRACK : si.nPos = HIWRD(CBWPARAM) CASE ELSE : EXIT FUNCTION END SELECT si.fMask = %SIF_POS SetScrollInfo CBHNDL, %SB_HORZ, si, 1 ScrollWindow CBHNDL, oldPos - si.nPos, 0, BYVAL %NULL, BYVAL %NULL ELSE GetScrollInfo CBHNDL, %SB_VERT, si oldPos = si.nPos SELECT CASE LOWRD(CBWPARAM) CASE %SB_LINEUP : si.nPos = si.nPos - 5 CASE %SB_PAGEUP : si.nPos = si.nPos - si.nPage CASE %SB_LINEDOWN : si.nPos = si.nPos + 5 CASE %SB_PAGEDOWN : si.nPos = si.nPos + si.nPage CASE %SB_THUMBTRACK : si.nPos = HIWRD(CBWPARAM) CASE ELSE : EXIT FUNCTION END SELECT si.fMask = %SIF_POS SetScrollInfo CBHNDL, %SB_VERT, si, 1 ScrollWindow CBHNDL, 0, oldPos - si.nPos, BYVAL %NULL, BYVAL %NULL END IF FUNCTION = 1 GetClientRect(CBHNDL, DlgRect) hControl = GetDlgItem(CBHNDL, %LABEL) GetWindowRect(hControl, CtrlRect) MapWindowPoints %HWND_DESKTOP, CBHNDL, CtrlRect, 2 iSeeYou = 0 IF PtInRect(DlgRect, CtrlRect.nLeft, CtrlRect.nTop ) THEN INCR iSeeYou IF PtInRect(DlgRect, CtrlRect.nRight, CtrlRect.nTop) THEN INCR iSeeYou IF PtInRect(DlgRect, CtrlRect.nLeft, CtrlRect.nBottom) THEN INCR iSeeYou IF PtInRect(DlgRect, CtrlRect.nRight, CtrlRect.nBottom) THEN INCR iSeeYou IF iSeeYou = 4 THEN SetWindowText CBHNDL, "Control is fully viewable" ELSEIF iSeeYou = 0 THEN SetWindowText CBHNDL, "Control is not viewable" ELSE SetWindowText CBHNDL, "Control is partially viewable" END IF END SELECT END FUNCTION '______________________________________________________________________________ FUNCTION PBMAIN () AS LONG LOCAL hDlg AS DWORD DIALOG NEW %HWND_DESKTOP, "isControlViewable",,, 450, 250, _ %WS_CAPTION OR %WS_SYSMENU OR %WS_MAXIMIZEBOX OR %WS_MINIMIZEBOX, TO hDlg DIALOG SET COLOR hDlg, -1, RGB(75, 165, 125) SetClassLong hDlg, %GCL_HICON, LoadIcon(BYVAL %NULL, BYVAL %IDI_INFORMATION) 'Set a nice dialog icon CONTROL ADD LABEL, hDlg, %LABEL, "Keep scrolling !", 100, 100, 300, 200, _ %SS_CENTER OR %SS_CENTERIMAGE, %WS_EX_LEFT CONTROL SET COLOR hDlg, %LABEL, -1, RGB(25, 125, 255) DIALOG SHOW MODAL hDlg CALL DlgProc END FUNCTION '______________________________________________________________________________ '
Last edited by Pierre Bellisle; 17 Apr 2021, 12:03 AM.
Comment
-
Code:GetClientRect(CBHNDL, DlgRect) ... GetWindowRect(hControl, CtrlRect)
MCMMichael Mattias
Tal Systems (retired)
Port Washington WI USA
[email protected]
http://www.talsystems.com
Comment
-
Originally posted by Michael Mattias View PostCode:GetClientRect(CBHNDL, DlgRect) ... GetWindowRect(hControl, CtrlRect)
MCM
Code:Call GetClientRect CBHNDL, DlgRect
Code:GetClientRect(CBHNDL, DlgRect)
Code:GetClientRect CBHNDL, DlgRect
Rgds, Dave
Comment
-
Yes, I think I said that correctly.
Without CALL, a DECLARE is required and no parens are permitted.
With CALL, DECLARE is not required (but is permitted) and parens are mandatory.
The above is the "corrrect" syntax. What different versions of the compiler of the compiler do... well that's what defines bugs.
>The bug was that the PBWin7x compiler would accept use of the Call verb without parens. Wasn't it?
Actually two bugs..
- Accepted parens when CALL was not used. (But it worked as though the parens weren't there)
- This also 'worked' :
Code:Value = FunctionName param, param, param
Last edited by Michael Mattias; 21 Apr 2008, 10:01 AM.Michael Mattias
Tal Systems (retired)
Port Washington WI USA
[email protected]
http://www.talsystems.com
Comment
-
GetClientRect(CBHNDL, DlgRect) compiles and runs in both PBWin703 and PBWin804.
Well, I'll be dipped... this compiles in 8.03, too:
Code:FUNCTION TestParens() AS LONG LOCAL hWnd AS LONG, R AS RECT GetClientRect (hWnd, R) END FUNCTION
Oh, wait a minute, the help file changed. These are all shown as valid under CALL:
CALL MyProc (parm1, parm2)
MyProc (parm1, Parm2)
MyProc parm1, parm2
This updated syntax now permits macros to be called using the SUB-style convention if/when the macros expand directly to Function calls. For example:
PowerBASIC now supports parentheses around parameters in calls to Subs (and functions that are being called as Subs) when the CALL keyword is omitted. For example, the following lines demonstrate valid syntax:
MCMMichael Mattias
Tal Systems (retired)
Port Washington WI USA
[email protected]
http://www.talsystems.com
Comment
Comment