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
Solved

Scrollbar for canvas area

Posted on 2006-11-05
7
756 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 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