滚动面板时保持固定的控制位置
我在大型自定义面板 panel2 的中间左侧有一个标签 label1,该面板在父面板 panel1 中滚动。
替代文本 http://lh4.ggpht.com/_1TPOP7DzY1E/ szNN2g9Sv4I/AAAAAAAC1U/A_LlLOoejX8/s800/formScroll.png
我会将标签1始终保持在面板2的可见左侧中间,即使在滚动时也是如此。
真实的例子,我的面板是一个用户控件,在其左侧生成一些标签。面板滚动,但我需要保持标签始终可见。
如何才能实现呢?
这是我的代码:
public partial class Form1 : Form
{
public Form1()
{
this.InitializeComponent();
}
protected Point clickPosition;
protected Point scrollPosition;
private void panel2_MouseDown(object sender, MouseEventArgs e)
{
this.clickPosition = e.Location;
}
private void panel2_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.SuspendLayout();
this.scrollPosition += (Size)clickPosition - (Size)e.Location;
this.panel1.AutoScrollPosition = scrollPosition;
this.ResumeLayout(false);
}
}
}
I have a label label1 in the middle left of a large custom panel panel2 that is scrolled in a parent panel panel1.
alt text http://lh4.ggpht.com/_1TPOP7DzY1E/SzNN2g9Sv4I/AAAAAAAAC1U/A_LlLOoejX8/s800/formScroll.png
I would keep the label1 always in the visible left middle of the panel2, even when scrolling.
In the real example, my panel is a user control that generates the some labels in its left side. The panel scrolls, but I need to keep the labels always visible.
How could it be achieved?
this is my code:
public partial class Form1 : Form
{
public Form1()
{
this.InitializeComponent();
}
protected Point clickPosition;
protected Point scrollPosition;
private void panel2_MouseDown(object sender, MouseEventArgs e)
{
this.clickPosition = e.Location;
}
private void panel2_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.SuspendLayout();
this.scrollPosition += (Size)clickPosition - (Size)e.Location;
this.panel1.AutoScrollPosition = scrollPosition;
this.ResumeLayout(false);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,技术上是可以的,你只需要在面板滚动时调整控制位置即可。例如:
但是,您会看到当您滚动面板时控件将开始执行 pogo。这里的问题是 Windows 太有帮助了。它使用快速 bitblt 滚动窗口本身的内容,然后仅针对窗口中需要重新绘制的部分发送绘制请求。
它通过名为“拖动时显示窗口内容”的系统选项来执行此操作,该选项位于“控制面板”中“显示”小程序的“外观+效果”对话框中。您无法合理地关闭此选项,它会对系统范围产生影响。在Win7上它甚至不再暴露了。
除了一个简单的方法之外,没有好的解决方法:不要将控件放在面板中。只需确保它位于面板顶部即可。这在设计器中可能有点棘手,将其放在面板旁边(如有必要,将其置于前面)并手动编辑 Location 属性。
Well, it is technically possible, you just need to adjust the control position when the panel scrolls. For example:
However, you'll see that the control will start doing the pogo when you scroll the panel. The problem here is that Windows is being too helpful. It scrolls the content of the window itself with a fast bitblt, then sends a paint request for only the parts of the window that needs to be repainted.
It does this directed by a system option named "Show window contents while dragging", available in Appearance + Effects dialog of the Display applet in Control Panel. You cannot reasonably turn this option off, it has system-wide effects. On Win7 it isn't even exposed anymore.
There is no good workaround for this, other than a simple one: don't put the control in the panel. Just make sure it is located on top of the panel. That can be a bit tricky in the designer, put it next to the panel (Bring To Front if necessary) and edit the Location property by hand.
Hai serhio,
看看这个,它与你的问题有关..但我不知道它是否能解决你的问题,无论如何尝试维护表单中控件的大小和位置
和这个
维护树视图的滚动位置
Hai serhio,
Have a look at this it is related to ur question.. But i have no idea whether it would solve your prob anyhow give a try Maintaining the size and position of the Control in the form
and this one
Maintain scroll position of treeview