How To center a logical drawing space much bigger than your screen?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • raylopez99

    How To center a logical drawing space much bigger than your screen?

    I have a logical drawing space much bigger than the viewport (the
    screen) and I'd like to center the viewport (the screen) to be at the
    center of the logical drawing space. After following the excellent
    transforms specified on Bob Powell's site, I still wonder if there's
    an easier way of centering it than the following procedure?

    Here is what I do now (it's awkward but it works):

    1) I follow the transforms specified on Bob Powell's site, which are
    required to get the scroll bars to work correctly, and the mouse
    coordinates to work with the viewport (screen) and also to zoom.

    2) to get the scrollbars to zoom to the very end of the logical
    drawing space, I create a series of three tiny buttons (size (1,1) and
    invisible) in the constructor of the form (make sure they are declared
    before InitializeCompo nent(); in your partial class or you might get a
    runtime error): button 1 = Top left corner of logical drawing space;
    button 2 = bottom right corner, and button 3 = midway of your logical
    drawing space, which you have to know the dimensions of your logical
    drawing space in pixels ahead of time. I also adjusted the button 3 a
    bit using some logic to center it better, especially if you are, as
    here, using a split panel.

    3) In the constructor, and ONLY in the constructor (I have no idea
    why), you do this:

    button3Center = new Point(xCenterof Board, yCenterofBoard) ; //calculate
    center of board from your logical drawing space ahead of time
    button1= new Point(0,0);
    button1= new Size(1,1);
    //etc for button2...

    this.mySplitCon tainer1.Panel2. Controls.Add(th is.button1);
    this.splitConta iner1.Panel2.Co ntrols.Add(this .button3Center; )
    this.splitConta iner1.Panel2.Co ntrols.Add(this .button2);

    //need next two if statements together to ensure stable
    behavior
    //you can also add the below if statements when resizing, in a On
    Resize handler, but only the two below if statements, not any of the
    above

    if (button1.CanFoc us)
    {
    button1.Focus() ;
    }

    if (button3Center. CanFocus)
    {
    button3Center.F ocus();
    }
    //

    //now, the form when it loads up will have the logical drawing space
    that is near button3Center show up on the screen. This way the
    viewport (screen) is 'centered' at around the middle or center of the
    logical drawing space.

    For some reason you cannot put this logic anywhere else (like Resize
    or Scroll). Further, you cannot set any 'anchor' properties for your
    three buttons, such as: button1.Anchor =
    System.Windows. Forms.AnchorSty les.None; // this will mess up your
    centering.

    Anyway, the above works fine, though I'd like to know if somebody has
    a cleaner way. I tried playing around in the OnPaint handler with
    Matrix mx = new Matrix (1,0,0,0,client size.Width/2, clientsize.Heig ht/
    2); and variations (such as making clientsize negative, which had
    promise as it shifted the logical drawing space but I could not quite
    get the mouse coordinates to work, see Bob Powell's site on this).

    RL
  • raylopez99

    #2
    Re: How To center a logical drawing space much bigger than yourscreen?

    On Nov 9, 5:31 pm, raylopez99 <raylope...@yah oo.comwrote:
    button3Center = new Point(xCenterof Board, yCenterofBoard) ; //calculate
    center of board from your logical drawing space ahead of time
    button1= new Point(0,0);
    button1= new Size(1,1);
    //etc for button2...
    >
    Small correction: above should read:

    button3Center.l ocation = new Point(xCenterof Board, yCenterofBoard) ;
    button1.locatio n = new Point(0,0);

    //etc

    RL

    Comment

    • raylopez99

      #3
      Re: How To center a logical drawing space much bigger than yourscreen?

      Further, if you have several resolutions, such as with Zoom (i.e. 50%,
      75%, 100%, 150%), you have to set up a complicated Case statement and
      setup buttons for each such resolution every time you switch
      resolution, using :
      this.mysplitCon tainer.Panel`.C ontrols.Add(But ton1);
      and .Controls.Remov e(...), for each pairs of these buttons.

      There has to be a better way of doing this I would imagine.

      RL

      Comment

      • Bob Powell [MVP]

        #4
        Re: How To center a logical drawing space much bigger than your screen?

        You're working too hard..
        What is that with the buttons all about? I have no clue what you're
        explaining...

        How can percentages require a complicated case statement?? you divide by 100
        and multiply by whatever percentage you like...

        See after my signature for some inspiration...


        --
        Bob Powell [MVP]
        Visual C#, System.Drawing

        Ramuseco Limited .NET consulting


        Find great Windows Forms articles in Windows Forms Tips and Tricks


        Answer those GDI+ questions with the GDI+ FAQ


        All new articles provide code in C# and VB.NET.
        Subscribe to the RSS feeds provided and never miss a new article.





        /// <summary>
        /// The canvas class
        /// </summary>
        [
        ToolboxItem(tru e),
        ToolboxBitmap(t ypeof(Canvas)," CanvasIcon.bmp" )
        ]
        public class Canvas : ScrollableContr ol
        {

        /// <summary>
        /// Constructor
        /// </summary>
        public Canvas()
        {
        this.SetStyle(
        ControlStyles.A llPaintingInWmP aint |
        ControlStyles.U serPaint |
        ControlStyles.R esizeRedraw ,true);
        }

        private bool _doubleBuffer;

        [
        Category("Behav ior"),
        Description("Se t true to enable double buffering")
        ]
        public bool DoubleBuffer
        {
        get{return _doubleBuffer;}
        set
        {
        _doubleBuffer=v alue;
        if(value)
        {
        SetStyle(Contro lStyles.DoubleB uffer,true);
        }
        else
        {
        SetStyle(Contro lStyles.DoubleB uffer,false);
        }
        Invalidate();
        }
        }

        private Color _pageColor=Colo r.White;

        [
        Category("Appea rance"),
        Description("Th e base color of the page")
        ]
        public Color PageColor
        {
        get{return _pageColor;}
        set
        {
        _pageColor=valu e;
        Invalidate();
        }
        }

        private bool _clipToPage;

        [
        Category("Behav ior"),
        Description("Ge ts or sets the clipping flag. When true no drawing is
        allowed outside page boundaries")
        ]
        public bool ClipToPage
        {
        get{return _clipToPage;}
        set
        {
        _clipToPage=val ue;
        Invalidate();
        }
        }

        private Size _pageSize=new Size(640,480);

        [
        Category("Appea rance"),
        Description("Th e size of the virtual page")
        ]
        public Size PageSize
        {
        get{return _pageSize;}
        set
        {
        _pageSize=value ;
        OnPageSizeChang ed(EventArgs.Em pty);
        }
        }

        protected virtual void OnPageSizeChang ed(EventArgs e)
        {
        CalcScroll();
        if(PageSizeChan ged!=null)
        PageSizeChanged (this,e);
        }

        public event EventHandler PageSizeChanged ;

        private float _zoom=1.0f;

        [Category("Behav ior"),
        Description("Th e multiplying factor for the zoom level"),
        DefaultValue(1. 0f)]
        public float Zoom
        {
        get{return _zoom;}
        set{
        _zoom = value;
        //A zoom may not be negative
        if(_zoom<0)
        _zoom=Math.Abs( _zoom);
        //a zoom may be very small but never zero.
        if(_zoom==0)
        _zoom=0.000001f ;
        //The scrollbars should be recalculated
        CalcScroll();
        //and the host application code informed if needs be
        OnZoomChanged(E ventArgs.Empty) ;
        }
        }

        void CalcScroll()
        {
        Size cs = new
        Size((int)(this ._pageSize.Widt h*_zoom),(int)( this._pageSize. Height*_zoom));
        this.AutoScroll MinSize=cs;
        Invalidate();
        }

        protected override void OnSizeChanged(E ventArgs e)
        {
        CalcScroll();
        base.OnSizeChan ged(e);
        }

        protected override void OnPaint(PaintEv entArgs e)
        {
        base.OnPaintBac kground(e);

        Matrix mx=new Matrix(_zoom,0, 0,_zoom,0,0);

        Size s=new
        Size((int)(this .ClientSize.Wid th*(1f/_zoom)),(int)(t his.ClientSize. Height*(1f/_zoom)));

        if(s.Width>Page Size.Width)
        mx.Translate((s .Width/2)-(_pageSize.Widt h/2),0);
        else
        mx.Translate((f loat)this.AutoS crollPosition.X *(1f/_zoom),0);

        if(s.Height>Pag eSize.Height)
        mx.Translate(0, (s.Height/2)-(this._pageSize .Height/2)+(this.AutoSc rollPosition.Y) );
        else
        mx.Translate(0, (float)this.Aut oScrollPosition .Y*(1f/_zoom));

        e.Graphics.Tran sform=mx;

        SolidBrush b=new SolidBrush(Colo r.FromArgb(64,C olor.Black));
        e.Graphics.Fill Rectangle(b,new Rectangle(new Point(20,20),Pa geSize));
        b.Color=PageCol or;
        e.Graphics.Fill Rectangle(b,new Rectangle(new Point(0,0),Page Size));

        if(ClipToPage)
        e.Graphics.SetC lip(new Rectangle(0,0,P ageSize.Width,P ageSize.Height) );

        base.OnPaint(e) ;

        }

        public event EventHandler ZoomChanged;

        protected virtual void OnZoomChanged(E ventArgs e)
        {
        if(this.ZoomCha nged!=null)
        ZoomChanged(thi s,e);
        Invalidate();
        }

        protected virtual MouseEventArgs BacktrackMouse( MouseEventArgs e)
        {
        Matrix mx=new Matrix(_zoom,0, 0,_zoom,0,0);

        Size s=new Size( (int)(this.Clie ntSize.Width*(1 f/_zoom)),
        (int)(this.Clie ntSize.Height*( 1f/_zoom)));

        if(s.Width>Page Size.Width)
        mx.Translate((s .Width/2)-(_pageSize.Widt h/2),0);
        else
        mx.Translate((f loat)this.AutoS crollPosition.X *(1f/_zoom),0);

        if(s.Height>Pag eSize.Height)
        mx.Translate(0, (s.Height/2)-(this._pageSize .Height/2)+
        (this.AutoScrol lPosition.Y));
        else
        mx.Translate(0, (float)this.Aut oScrollPosition .Y*(1f/_zoom));

        mx.Invert();

        Point[] px=new Point[]{new Point(e.X,e.Y)} ;

        mx.TransformPoi nts(px);

        MouseEventArgs et=new
        MouseEventArgs( e.Button,e.Clic ks,px[0].X,px[0].Y,e.Delta);

        return et;
        }

        protected override void OnMouseMove(Mou seEventArgs e)
        {
        MouseEventArgs et=this.Backtra ckMouse(e);
        base.OnMouseMov e(et);
        }

        protected override void OnMouseUp(Mouse EventArgs e)
        {
        MouseEventArgs et=this.Backtra ckMouse(e);
        base.OnMouseUp( et);
        }

        protected override void OnMouseDown(Mou seEventArgs e)
        {
        MouseEventArgs et=this.Backtra ckMouse(e);
        base.OnMouseDow n(et);
        }
        }








        "raylopez99 " <raylopez99@yah oo.comwrote in message
        news:1c5ccfb9-fac8-4258-9740-f0bffe47a945@q2 6g2000prq.googl egroups.com...
        Further, if you have several resolutions, such as with Zoom (i.e. 50%,
        75%, 100%, 150%), you have to set up a complicated Case statement and
        setup buttons for each such resolution every time you switch
        resolution, using :
        this.mysplitCon tainer.Panel`.C ontrols.Add(But ton1);
        and .Controls.Remov e(...), for each pairs of these buttons.
        >
        There has to be a better way of doing this I would imagine.
        >
        RL

        Comment

        • raylopez99

          #5
          Re: How To center a logical drawing space much bigger than yourscreen?

          On Nov 10, 3:00 pm, "Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net >
          wrote:
          You're working too hard..
          What is that with the buttons all about? I have no clue what you're
          explaining...
          >
          How can percentages require a complicated case statement?? you divide by 100
          and multiply by whatever percentage you like...
          >
          See after my signature for some inspiration...
          >
          --
          Bob Powell [MVP]
          Visual C#, System.Drawing

          WOW! Bob Powell has emailed me! This is cool. Bob you are a real
          inspiration and my program would not work without your BacktrackMouse
          method, which I got from your site. I notice in your example above
          (which I will compile and examine later) it's also present.

          WHat I notice (just looking at your code) is that the OnPaint and
          Mouse handlers have the same matrix transform, which is great, and
          same as in my program. You have to sync screen and mouse.

          What I do, which you also do with different and better logic, is to
          try and get the scroll bars to work to scroll past my drawing object:
          a big rectangle. I have a huge rectangle that is bigger than the
          screen. How to get to the bottom of this rect? Well as you know if
          you just paint it, it will be truncated (only the upper left corner
          will be shown). So to get to the lower right corner, I do the
          following: place a small invisible button (create it, add it) at the
          lower right corner. This works. But, if you change the _zoom factor
          (I have a series of set zooms, e.g. a normal, a small, a large, extra
          large, etc), you must .remove this button and .add it again to the new
          rect., otherwise you cannot scroll to the lower right corner of the
          new rectangle.

          It's complicated (involving numerous enums and bit flags) but it
          works. I can add any number of fixed (set) resolutions this way:
          i.e., 25% of normal, 75% of normal, 100% (default) and 200%, etc, but
          I cannot do a "variable" resolution using a slider bar for example--is
          this what your example shows? If so, your way is clearly superior.

          Any words appreciated. Thanks again Bob! I think MSDN should pay you
          money for providing your service--it's really helpful and MSDN had
          nothing on Matrices (as easy to understand as yours, or I could not
          find it).

          RL

          PS--besides getting to the bottom of the rectangle I also want the
          viewing area (viewport) to be centered at the center of the
          rectangle. I do this by adding yet another small invisible button to
          the center of the rectangle, using .add, .remove as necessary,
          everytime the resolution is changed. If you have any better way of
          doing this please let me know. Thanks again!

          I plan on looking at your code but feel free to comment anyway--just a
          few words of encouragement is all I need.


          Comment

          • Bob Powell [MVP]

            #6
            Re: How To center a logical drawing space much bigger than your screen?

            If you look at the canvas code I sent you, the control is quite happy to
            deal with large images.

            The scroll bars are set using the info from my "understand ing autoscroll"
            article.

            Centering on the screen is easy. Just set the origin of the picture to:

            center of screen - width/height of picture /2


            --
            --
            Bob Powell [MVP]
            Visual C#, System.Drawing

            Ramuseco Limited .NET consulting


            Find great Windows Forms articles in Windows Forms Tips and Tricks


            Answer those GDI+ questions with the GDI+ FAQ


            All new articles provide code in C# and VB.NET.
            Subscribe to the RSS feeds provided and never miss a new article.


            "raylopez99 " <raylopez99@yah oo.comwrote in message
            news:bcdd13c6-4d8f-45eb-888c-b834ad5897d8@u1 8g2000pro.googl egroups.com...
            On Nov 10, 3:00 pm, "Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net >
            wrote:
            You're working too hard..
            What is that with the buttons all about? I have no clue what you're
            explaining...
            >
            How can percentages require a complicated case statement?? you divide by
            100
            and multiply by whatever percentage you like...
            >
            See after my signature for some inspiration...
            >
            --
            Bob Powell [MVP]
            Visual C#, System.Drawing

            WOW! Bob Powell has emailed me! This is cool. Bob you are a real
            inspiration and my program would not work without your BacktrackMouse
            method, which I got from your site. I notice in your example above
            (which I will compile and examine later) it's also present.

            WHat I notice (just looking at your code) is that the OnPaint and
            Mouse handlers have the same matrix transform, which is great, and
            same as in my program. You have to sync screen and mouse.

            What I do, which you also do with different and better logic, is to
            try and get the scroll bars to work to scroll past my drawing object:
            a big rectangle. I have a huge rectangle that is bigger than the
            screen. How to get to the bottom of this rect? Well as you know if
            you just paint it, it will be truncated (only the upper left corner
            will be shown). So to get to the lower right corner, I do the
            following: place a small invisible button (create it, add it) at the
            lower right corner. This works. But, if you change the _zoom factor
            (I have a series of set zooms, e.g. a normal, a small, a large, extra
            large, etc), you must .remove this button and .add it again to the new
            rect., otherwise you cannot scroll to the lower right corner of the
            new rectangle.

            It's complicated (involving numerous enums and bit flags) but it
            works. I can add any number of fixed (set) resolutions this way:
            i.e., 25% of normal, 75% of normal, 100% (default) and 200%, etc, but
            I cannot do a "variable" resolution using a slider bar for example--is
            this what your example shows? If so, your way is clearly superior.

            Any words appreciated. Thanks again Bob! I think MSDN should pay you
            money for providing your service--it's really helpful and MSDN had
            nothing on Matrices (as easy to understand as yours, or I could not
            find it).

            RL

            PS--besides getting to the bottom of the rectangle I also want the
            viewing area (viewport) to be centered at the center of the
            rectangle. I do this by adding yet another small invisible button to
            the center of the rectangle, using .add, .remove as necessary,
            everytime the resolution is changed. If you have any better way of
            doing this please let me know. Thanks again!

            I plan on looking at your code but feel free to comment anyway--just a
            few words of encouragement is all I need.


            Comment

            • raylopez99

              #7
              Re: How To center a logical drawing space much bigger than yourscreen?

              Bob,

              Thanks again. I tried to compile your code in your OP this morning,
              and though it compiled I could not get it to work as a 'form' (I tried
              making it a form and could not get it to load as a form, after trying
              for example Canvas myCanvasForm = new Canvas();. It might be missing
              some resource files or something (I notice it references some image
              file).

              Anyway, it did compile, and I printed it out and will go through it.
              Any further comments appreciated. I really do need one complete
              example for my library (for future reference--right now my kludge way
              of doing things 'works' and that's 'fine for now').

              Cheers from a time zone about -7 hours behind yours...

              RL

              On Nov 12, 11:37 pm, "Bob Powell [MVP]" <b...@spamkille rbobpowell.net>
              wrote:
              If you look at the canvas code I sent you, the control is quite happy to
              deal with large images.
              >
              The scroll bars are set using the info from my "understand ing autoscroll"
              article.
              >
              Centering on the screen is easy. Just set the origin of the picture to:
              >
              center of screen - width/height of picture /2
              >
              --
              --
              Bob Powell [MVP]
              Visual C#, System.Drawing

              Comment

              • raylopez99

                #8
                Re: How To center a logical drawing space much bigger than yourscreen?

                On Nov 12, 11:37 pm, "Bob Powell [MVP]" <b...@spamkille rbobpowell.net>
                wrote:
                If you look at the canvas code I sent you, the control is quite happy to
                deal with large images.
                No, it did not work, though I finally got it to work past just
                compilation. Reproduced below. It did give a nice white screen with
                a grey border that can be resized to any arbitrary size, using the
                _zoom, but if you set _zoom to a very large number, say 2.0F, then no
                scroll bars show up (despite, using the Designer Wizard, I did check
                the scrollbars property to True).
                >
                The scroll bars are set using the info from my "understand ing autoscroll"
                article.
                Is that in your canvas code?
                >
                Centering on the screen is easy. Just set the origin of the picture to:
                >
                center of screen - width/height of picture /2
                >
                --
                Where do I add this? In what function call / event handler?

                --
                Bob Powell [MVP]
                Visual C#, System.Drawing
                Con permission, I have a few more optional questions (you can feel
                free to answer as many or few as you please; I appreciate your time):

                1) below is my code. I notice that the Color.White background seems
                to not allow a very bright overlay of say a red rectangle (see the
                rectangle I draw below). It comes out (or so it seems) a bit pink,
                like the white background is bleeding through.

                2) How to use scrollbars, as I mentioned above. AutoScrollPosit ion is
                shown throughout the code, but, unlike my working example in the
                original thread (which was inspired by your website examples), the one
                that uses hidden tiny buttons, this time the form doesn't scroll. If
                you can point out in your code where this feature is, maybe I can
                check that I didn't copy and paste wrong--since to make the program
                work past the compile stage I had to create a form from scratch and
                copy and paste selectively, until everything (I hope) was copied from
                your code.

                3). Minor but important for me: this line made things work without
                the invalidation problem: "this.SetSt yle
                ( ControlStyles.A llPaintingInWmP aint | ControlStyles.U serPaint |
                ControlStyles.R esizeRedraw ,true); "
                How to set this using the Wizard (I like to use the wizard whenever
                possible)?

                Thank you.

                I like your "canvas" example for my library because, though the
                scrollbars don't work, for _zoom < 1.0F you can resize the window by
                grapping a corner, and the white canvas with grey border will
                automatically stay in the middle of the resized window, and, of
                course, the BackTrackMouse function and Matrix will correctly adjust
                your mouse coordinates (I added a function for right-button down to
                test this, as you can see below). Very nice, but I do like the idea of
                scrollbars and want to add them.

                RL

                using System;
                using System.Collecti ons.Generic;
                using System.Componen tModel;
                using System.Data;
                using System.Drawing;
                using System.Linq;
                using System.Text;
                using System.Windows. Forms;
                using System.Drawing. Drawing2D;

                namespace Canvas2
                {
                public partial class Canvas : Form
                {
                private Size _pageSize; // = new Size(640, 480);
                private bool _clipToPage;
                private float _zoom;
                private Color _pageColor; // = Color.White;

                public Canvas()
                {
                InitializeCompo nent();
                _pageSize = new Size(640, 480);
                _zoom = 0.5F;
                _pageColor = Color.White; //Color.Transpare nt //
                Color.Empty; //do not work, give dark background, use Color.White
                //

                this.SetStyle( ControlStyles.A llPaintingInWmP aint |
                ControlStyles.U serPaint | ControlStyles.R esizeRedraw ,true);

                }

                // private Color _pageColor = Color.White;
                [
                Category("Appea rance"),
                Description("Th e base color of the page")
                ]
                public Color PageColor
                {
                get { return _pageColor; }
                set
                {
                _pageColor = value;
                Invalidate();
                }
                }



                // private bool _clipToPage;
                [
                Category("Behav ior"),
                Description("Ge ts or sets the clipping flag. When true no
                drawing is allowed outside page boundaries")
                ]
                public bool ClipToPage
                {
                get { return _clipToPage; }
                set
                {
                _clipToPage = value;
                Invalidate();
                }
                }
                // private Size _pageSize = new Size(640, 480);
                [
                Category("Appea rance"),
                Description("Th e size of the virtual page")
                ]
                public Size PageSize
                {
                get { return _pageSize; }
                set
                {
                _pageSize = value;
                OnPageSizeChang ed(EventArgs.Em pty);
                }
                }

                protected virtual void OnPageSizeChang ed(EventArgs e)
                {
                CalcScroll();
                if (PageSizeChange d != null)
                PageSizeChanged (this, e);
                }
                public event EventHandler PageSizeChanged ;

                void CalcScroll()
                {
                Size cs = new Size((int)(this ._pageSize.Widt h * _zoom),
                (int)(this._pag eSize.Height * _zoom));
                this.AutoScroll MinSize = cs;
                Invalidate();
                }

                [Category("Behav ior"),
                Description("Th e multiplying factor for the zoom level"),
                DefaultValue(1. 0f)]
                public float Zoom
                {
                get { return _zoom; }
                set
                {
                _zoom = value;
                //A zoom may not be negative
                if (_zoom < 0)
                _zoom = Math.Abs(_zoom) ;
                //a zoom may be very small but never zero.
                if (_zoom == 0)
                _zoom = 0.000001f;
                //The scrollbars should be recalculated
                CalcScroll();
                //and the host application code informed if needs be
                OnZoomChanged(E ventArgs.Empty) ;
                }
                }

                public event EventHandler ZoomChanged;
                protected virtual void OnZoomChanged(E ventArgs e)
                {
                if (this.ZoomChang ed != null)
                ZoomChanged(thi s, e);
                Invalidate();
                }

                protected override void OnPaint(PaintEv entArgs e)
                {
                base.OnPaintBac kground(e);

                Matrix mx = new Matrix(_zoom, 0, 0, _zoom, 0, 0);
                Size s = new Size((int)(this .ClientSize.Wid th * (1f /
                _zoom)), (int)(this.Clie ntSize.Height * (1f / _zoom)));
                if (s.Width PageSize.Width)
                mx.Translate((s .Width / 2) - (_pageSize.Widt h / 2),
                0);
                else
                mx.Translate((f loat)this.AutoS crollPosition.X * (1f /
                _zoom), 0);
                if (s.Height PageSize.Height )
                mx.Translate(0, (s.Height / 2) -
                (this._pageSize .Height / 2) + (this.AutoScrol lPosition.Y));
                else
                mx.Translate(0, (float)this.Aut oScrollPosition .Y *
                (1f / _zoom));
                e.Graphics.Tran sform = mx;
                SolidBrush b = new SolidBrush(Colo r.FromArgb(64,
                Color.Black));
                e.Graphics.Fill Rectangle(b, new Rectangle(new Point(20,
                20), PageSize));
                b.Color = PageColor;
                e.Graphics.Fill Rectangle(b, new Rectangle(new Point(0, 0),
                PageSize));
                if (ClipToPage)
                e.Graphics.SetC lip(new Rectangle(0, 0, PageSize.Width,
                PageSize.Height ));

                //SolidBrush b1 = new SolidBrush(Colo r.FromArgb(64,
                Color.Red));
                //e.Graphics.Fill Rectangle(b1, new Rectangle(new Point(75,
                75), new Size(20, 30)));

                base.OnPaint(e) ;
                }


                protected virtual MouseEventArgs BacktrackMouse( MouseEventArgs e)
                {
                Matrix mx=new Matrix(_zoom,0, 0,_zoom,0,0);
                Size s=new Size( (int)(this.Clie ntSize.Width*(1 f/_zoom)),
                (int)(this.Clie ntSize.Height*( 1f/_zoom)));
                if(s.Width>Page Size.Width)
                mx.Translate((s .Width/2)-(_pageSize.Widt h/2),0);
                else
                mx.Translate((f loat)this.AutoS crollPosition.X *(1f/_zoom),0);
                if(s.Height>Pag eSize.Height)
                mx.Translate(0, (s.Height/2)-(this._pageSize .Height/2)+
                (this.AutoScrol lPosition.Y));
                else
                mx.Translate(0, (float)this.Aut oScrollPosition .Y*(1f/_zoom));
                mx.Invert();
                Point[] px=new Point[]{new Point(e.X,e.Y)} ;
                mx.TransformPoi nts(px);
                MouseEventArgs et=new
                MouseEventArgs( e.Button,e.Clic ks,px[0].X,px[0].Y,e.Delta);
                return et;
                }
                protected override void OnMouseMove(Mou seEventArgs e)
                {
                MouseEventArgs et=this.Backtra ckMouse(e);
                base.OnMouseMov e(et);
                }
                protected override void OnMouseUp(Mouse EventArgs e)
                {
                MouseEventArgs et=this.Backtra ckMouse(e);
                base.OnMouseUp( et);
                }
                protected override void OnMouseDown(Mou seEventArgs e)
                {
                MouseEventArgs et=this.Backtra ckMouse(e);
                base.OnMouseDow n(et);
                }

                private void Canvas_Paint(ob ject sender, PaintEventArgs e)
                {
                SolidBrush b1 = new SolidBrush(Colo r.FromArgb(64, Color.Red));
                e.Graphics.Fill Rectangle(b1, new Rectangle(new Point(75, 75),
                new Size(20, 30)));
                }

                private void Canvas_MouseUp( object sender, MouseEventArgs e)
                {
                if (e.Button == MouseButtons.Ri ght)
                {
                MessageBox.Show ("pt.X,Y = " + e.X.ToString() + "," +
                e.Y.ToString()) ;
                }
                }
                }


                }




                Comment

                Working...