Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Scrollbar for canvas area

Posted on 2006-11-05
7
Medium Priority
?
764 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 600 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

715 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