开发者

Mantain a control position fixed when scrolling a panel

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开发者_StackOverflow 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);
        }
    }
}


Well, it is technically possible, you just need to adjust the control position when the panel scrolls. For example:

  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
      mPicturePos = pictureBox1.Location;
    }
    Point mPicturePos;
    private void panel1_Scroll(object sender, ScrollEventArgs e) {
      pictureBox1.Location = mPicturePos;
    }
  }

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,

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


    public Form1()
    {
        this.InitializeComponent();
        panel2.Paint += new PaintEventHandler(panel2_Paint);
    }

    void panel2_Paint(object sender, PaintEventArgs e)
    {
        label1.Location = 
            new Point(-panel1.AutoScrollPosition.X, label1.Location.Y);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜