在面板中滚动形状

发布于 2024-12-02 20:40:10 字数 185 浏览 0 评论 0原文

我正在开发一个在面板上绘制形状的程序。我向面板添加了一个垂直滚动条,但是当滚动面板时,形状保持在原来的位置并且不滚动。如何使这些形状滚动?
我在绘制处理程序中使用此代码来更新这些形状:

e.Graphics.FillRectangle(Brushes.Red, selectedRect);

I'm working on a program which draws shapes on a panel. I added a vertical scroll bar to the panel but when scrolling the panel, the shapes stays where they are and don't scroll. How can I make these shapes scroll?
I'm using this code in the paint handler to update these shapes:

e.Graphics.FillRectangle(Brushes.Red, selectedRect);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-12-09 20:40:10

滚动时,您应该注意面板的实际滚动位置,并在再次绘制形状时考虑此偏移。

网上有这样的例子(不完全是你的情况,但可以帮助识别问题):

private int count; 
private ArrayList arrayctl = new ArrayList();

private void button1_Click(object sender, System.EventArgs e) 
{ 
    TextBox newtxt = new TextBox(); 
    newtxt.Text = count.ToString(); 
    count++; arrayctl.Add(newtxt); 
    DrawControls(); 
} 

private void DrawControls() 
{ 
    System.Drawing.Point CurrentPoint; CurrentPoint = panel1.AutoScrollPosition; 
    int i = 0; 
    panel1.Controls.Clear(); 
    panel1.SuspendLayout(); 
    foreach (TextBox txt in arrayctl) 
    {
     panel1.Controls.Add(txt); 
        txt.Width = panel1.ClientRectangle.Width; 
        txt.Top = i; i += txt.Height; 
    } 
    panel1.ResumeLayout(); 
    panel1.AutoScrollPosition = new Point(Math.Abs(panel1.AutoScrollPosition.X), Math.Abs(CurrentPoint.Y)); 
}

我也在SO中找到了这个: 使用自动滚动控制 Windows.Forms.Panel 内的位置

on scrolling you should take care of the actual scroll position of the panel and consider this offset when painting your shapes again.

there are examples like this online (not exactly your case but could help to identify the problem):

private int count; 
private ArrayList arrayctl = new ArrayList();

private void button1_Click(object sender, System.EventArgs e) 
{ 
    TextBox newtxt = new TextBox(); 
    newtxt.Text = count.ToString(); 
    count++; arrayctl.Add(newtxt); 
    DrawControls(); 
} 

private void DrawControls() 
{ 
    System.Drawing.Point CurrentPoint; CurrentPoint = panel1.AutoScrollPosition; 
    int i = 0; 
    panel1.Controls.Clear(); 
    panel1.SuspendLayout(); 
    foreach (TextBox txt in arrayctl) 
    {
     panel1.Controls.Add(txt); 
        txt.Width = panel1.ClientRectangle.Width; 
        txt.Top = i; i += txt.Height; 
    } 
    panel1.ResumeLayout(); 
    panel1.AutoScrollPosition = new Point(Math.Abs(panel1.AutoScrollPosition.X), Math.Abs(CurrentPoint.Y)); 
}

I have also found this here in SO: Control position inside Windows.Forms.Panel with autoscroll

不知在何时 2024-12-09 20:40:10

不要将 VerticalScrollBar 添加到面板中。当您设置 AutoScrollMinSize 属性时,面板已经自行处理滚动:

Rectangle selectedRect = new Rectangle(16, 16, 64, 28);

private void Form1_Load(object sender, EventArgs e)
{
  panel1.AutoScrollMinSize = new Size(panel1.ClientRectangle.Width - SystemInformation.VerticalScrollBarWidth, 1200);
}

private void panel1_Paint(object sender, PaintEventArgs e)
{
  e.Graphics.TranslateTransform(panel1.AutoScrollPosition.X, panel1.AutoScrollPosition.Y);
  e.Graphics.FillRectangle(Brushes.Red, selectedRect);
}

private void panel1_Scroll(object sender, ScrollEventArgs e)
{
  panel1.Invalidate();
}

Don't add a VerticalScrollBar to the panel. The Panel already handles scrolling itself when you set the AutoScrollMinSize property:

Rectangle selectedRect = new Rectangle(16, 16, 64, 28);

private void Form1_Load(object sender, EventArgs e)
{
  panel1.AutoScrollMinSize = new Size(panel1.ClientRectangle.Width - SystemInformation.VerticalScrollBarWidth, 1200);
}

private void panel1_Paint(object sender, PaintEventArgs e)
{
  e.Graphics.TranslateTransform(panel1.AutoScrollPosition.X, panel1.AutoScrollPosition.Y);
  e.Graphics.FillRectangle(Brushes.Red, selectedRect);
}

private void panel1_Scroll(object sender, ScrollEventArgs e)
{
  panel1.Invalidate();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文