Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 769
  • Last Modified:

Scrollbar for canvas area

Hi,
I have set the canvas for whole Client Rectangle size.
When i resize the form, my form's clientrectangle size gets smaller than canvas area size and some of the graphic objects on screen gets hidden inside. Im creating a scrollbar horizontal/vertical (which ever is required) inorder to view the hidden area of the canvas. But when i scroll i couldn't make the form to move to show the hidden area.
I tried the below code,
// when horizontal scrollbar thumb is moved
        void hsb_ValueChanged(object sender, EventArgs e)
        {
            isHsbValChanged = true;
            Invalidate(); // calling invalidate to raise paint event
         }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            if (canvas.IsCanvasExists())
            {
                canvas.g.FillRectangle(new SolidBrush(SystemColors.Control), e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height); // g is graphic object created in canvas form
                canvas.Render(e.Graphics);
                if (isHsbValChanged)
                {
                    canvas.ScrollRender(canvas.g, 10, 0);
                    isHsbValChanged = false;
                }
            }
        }

// Render function at canvas form
        public void Render(Graphics g)
        {
            if (bmp != null)  //bmp is empty bitmap created at runtime
                // width & height are the canvas width & height
                g.DrawImage(bmp, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel);
        }

// Scroll Render function at canvas form
        public void ScrollRender(Graphics g,int hVal, int vVal)
        {
            if (bmp != null)
                // hVal is horizontal movement & vVal is vertical movement, values are taken just for example
                g.DrawImage(bmp, new Rectangle(0, 0, width, height), hVal, vVal, width + hVal, height + vVal, GraphicsUnit.Pixel);
        }


But this code doesn't seems to work, i dont see the form area is moving.
How could i connect scrollbar to form's display(visible) area?
Any suggession would be of great help.
Thanks in advance.
Rajmahesh
0
Rajmahesh
Asked:
Rajmahesh
  • 4
  • 3
1 Solution
 
AlexFMCommented:
Try to set Form.AutoScroll Property to true, this must solve the problem without any additional code.
0
 
RajmaheshAuthor Commented:
Alex, I tried setting that, but that doesn't seem to be working.
Im not sure why it should not work. But the scrollbars doesn't appear when i resize the form.
0
 
AlexFMCommented:
Try to set AutoScrollMinSize to initial size of the canvas.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
RajmaheshAuthor Commented:
Got AutoScroll working after setting AutoScrollMinSize.
But the problem now is
I have grid lines in the form. When i resize i get the scrollbar. If i move the scrollbar rightside to some extent, during that time the form gets repainted and after that the visible client rectangle's top left corner becomes 0,0 and my grid lines are aligning relative to the visible client rectangle. I want only the view to get shifted not the grid even during repaint. The grid and any graphic objects in the form should be in the same position relative to the actual form's top left corner and not to form's visible area's top left corner. I guess this explains my problem. Please let me know if you require more detail.
Thanks
0
 
AlexFMCommented:
I didn't understand exactly your requirements, but this problem should be solved using form AutoScrollPosition Property. If returns current scrollbars position, and you need to make correction with it. If some object is moved by scrollbar, and you want to keep it in the place, change it's position: X -= AutoScrollPosition.X, Y -= AutoScrollPosition.Y (pseudpcode).
If some object is not moved by scrollbar, and you want to move it, change it's position by the following way: X += AutoScrollPosition.X, Y += AutoScrollPosition.Y.
0
 
RajmaheshAuthor Commented:
Here is my requirement:
I have a canvas drawn on the form. On top of it I have horizontal & vertical lines drawn at 50 pixels each. So i get the grid format. On top of it i have some graphic objects like Rectangle. When i drag the rectangle, it moves aligning to grid as like snaplines.
You got my problem right. But if the number of graphic objects (say rectangle) is less, then this would be feasible. But if the count is in hundreds (but in realcase it would not cross hundred) it would be a tough thing to set it for each object. I guess there should be an easy way to do this, since it is a common problem for all those who works on gdi+/graphic programming. Any suggessions. I will try the pseudocode you had given now.
0
 
RajmaheshAuthor Commented:
Hi Alex,
Sorry I wasnt available for a week, thats y late reply.
Thanks for your time to help me.
I did the calculation for AutoScrollPosition in the wrapper class for each graphic object itself. So dint need any much work to fix it.
Thanks again.
Rajmahesh
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now