如何在页面滚动时获取嵌套面板的相对坐标 [英] How to get relative coordinates of a nested panel as page scrolls

查看:28
本文介绍了如何在页面滚动时获取嵌套面板的相对坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,如果这对 WinForms 用户来说是一个微不足道的问题,我来自游戏编程背景,WinForms API 与我习惯在游戏中做 UI 的方式非常不同,这是我第一次使用 WinForms.

Excuse me if this is a trivial question to WinForms users, I come from a background in game programming, and the WinForms API is very different than I'm used to for doing the UI in games, and this is my first time with WinForms.

我有一个 UserControl,附加到一个面板,附加到该面板的是一个 TableLayoutPanel,有很多行和 1 列,在每一行中都有一个显示图像的面板.层次结构如下所示:

I have a UserControl, attached to that is a Panel, attached to that panel is a TableLayoutPanel with many rows and 1 column, and in each of those rows is a Panel that displays an image. Hierarchy looks like this:

UserControl
|- Panel (I use this one to scroll the child of this)
   |- TableLayoutPanel (many rows, 1 column, one panel per row)
      |- Panel1 (contains an image)
      |- Panel2 (contains an image)
      ...
      |- PanelN (contains an image)

每一行以及其中的面板都是在运行时创建的.

Each of the rows, as well as the panel within them, is created at runtime.

现在,由于 TableLayoutPanel 通常太高而无法在一个屏幕上显示,我将其父面板设置为滚动.

Now, since that TableLayoutPanel is usually too tall to fit on one screen, I have its parent panel set up to scroll.

所以这是我的问题:我需要能够搜索所有叶面板(Panel1 - PanelN),并找到当前显示在屏幕上的那些.我试图通过获取每个面板的位置 (Panel1.Location) 并查看哪个 y 坐标最接近 0 并将其用作我的起点来做到这一点.问题是我似乎无法找到对 PointToScreen、RectangleToClient、RectangleToScreen 等的正确调用组合来获取有用的数据.无论我从这些调用中得到哪个点/矩形,它们要么具有恒定的 y 值(即相对于它们的父 TableLayoutPanel 定义,不移动)或者它们确实随着滚动条移动,但有一些基于窗口在屏幕上的位置或其相对于 UserControl 上方的其他一些父元素的位置.

So here is my problem: I need to be able to search all of the leaf panels (Panel1 - PanelN), and find the ones currently showing on the screen. I am trying to do this by taking each Panel's location (Panel1.Location), and seeing which y-coordinate is closest to 0, and using that as my starting point. The problem is that I can't seem to find the right combination of calls to PointToScreen, RectangleToClient, RectangleToScreen, etc to get useful data. Regardless of which Point/Rectangle I get from those calls, they either have y-values that are constant (i.e. defined relative to their parent TableLayoutPanel, not moving) or they do move with the scroll bar, but there is some constant added based on either the window's position on the screen or its position relative to some other parent elements that are above the UserControl.

当滚动条(在面板上)一直在页面顶部时,我需要进行哪些调用才能使 Panel1 的位置返回 (x, 0),并开始返回负数 (x,y <0) 当用户向下滚动时?

What calls do I need to make to get the position of Panel1 to return (x, 0) when the scroll bar (on Panel) is all the way at the top of the page, and start returning negative numbers (x, y < 0) as the user scrolls down?

我查看了这些 stackoverflow 问题(以及很多其他资源),但其中的代码对我的问题没有帮助:获取控件相对于整个控件的位置屏幕? ,c#控件在屏幕上的绝对位置如何获得组框控件内控件的屏幕位置?

I've looked at these stackoverflow questions (and quite a few other resources), but the code in them did not help with my problem: Getting The Location Of A Control Relative To The Entire Screen? , c# absolute position of control on screen , How to get the Screen position of a control inside a group box control?

推荐答案

Try Panel1.Top + TableLayout1.Top - Panel.VerticalScroll.Value

这篇关于如何在页面滚动时获取嵌套面板的相对坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆