R
raylopez99
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 InitializeComponent(); 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(xCenterofBoard, 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.mySplitContainer1.Panel2.Controls.Add(this.button1);
this.splitContainer1.Panel2.Controls.Add(this.button3Center
this.splitContainer1.Panel2.Controls.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.CanFocus)
{
button1.Focus();
}
if (button3Center.CanFocus)
{
button3Center.Focus();
}
//
//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.AnchorStyles.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,clientsize.Width/2, clientsize.Height/
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
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 InitializeComponent(); 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(xCenterofBoard, 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.mySplitContainer1.Panel2.Controls.Add(this.button1);
this.splitContainer1.Panel2.Controls.Add(this.button3Center

this.splitContainer1.Panel2.Controls.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.CanFocus)
{
button1.Focus();
}
if (button3Center.CanFocus)
{
button3Center.Focus();
}
//
//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.AnchorStyles.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,clientsize.Width/2, clientsize.Height/
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