Solved

Scrollbar for canvas area

Posted on 2006-11-05
7
753 Views
Last Modified: 2009-07-29
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
Comment
Question by:Rajmahesh
  • 4
  • 3
7 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 17879285
Try to set Form.AutoScroll Property to true, this must solve the problem without any additional code.
0
 
LVL 1

Author Comment

by:Rajmahesh
ID: 17879402
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
 
LVL 48

Expert Comment

by:AlexFM
ID: 17879421
Try to set AutoScrollMinSize to initial size of the canvas.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 1

Author Comment

by:Rajmahesh
ID: 17879766
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
 
LVL 48

Accepted Solution

by:
AlexFM earned 150 total points
ID: 17879840
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
 
LVL 1

Author Comment

by:Rajmahesh
ID: 17879969
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
 
LVL 1

Author Comment

by:Rajmahesh
ID: 17937723
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now