w0lfshad3
November 18th, 2007, 01:45 AM
solved mostly: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2423864&SiteID=1&mode=1
I have this functionality i want to port from WinForms but i run into a few problems and i can't continue. What it does is move a panel(canvas) around as i click and hold and drag the mouse). I saw the sample using TranlateTransform(XAML) but i can't get any code-behind example, this is dynamic and interactive. I managed to make some out only need the translation solved(and possibly a good approach on using this with Margin, wich seems the only tool i got to moving the canvas around?).
#region Pan tool
private bool mMovingPanel;
private Point mMousePos;
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
mMovingPanel = e.Button == MouseButtons.Left;
// prev mouse pos
mMousePos = panel1.PointToScreen(e.Location);
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (mMovingPanel)
{
// "this mouse pos"
Point pos = panel1.PointToScreen(e.Location);
// "this mouse pos" - "prev mouse pos" = offset
Size adj = new Size(pos.X - mMousePos.X, pos.Y - mMousePos.Y);
// Location + offset = Location (final - unassigned)
Point loc = panel1.Location + adj;
if (panel1.Width >= this.ClientSize.Width && panel1.Height >= this.ClientSize.Height)
{
if (loc.X > 0) loc.X = 0;
if (loc.X + panel1.Width < this.ClientSize.Width) loc.X = this.ClientSize.Width - panel1.Width;
if (loc.Y > 0) loc.Y = 0;
if (loc.Y + panel1.Height < this.ClientSize.Height) loc.Y = this.ClientSize.Height - panel1.Height;
}
else if (panel1.Height >= this.ClientSize.Height)
{
if (loc.X < 0) loc.X = 0;
if (loc.X + panel1.Width > this.ClientSize.Width) loc.X = this.ClientSize.Width - panel1.Width;
if (loc.Y > 0) loc.Y = 0;
if (loc.Y + panel1.Height < this.ClientSize.Height) loc.Y = this.ClientSize.Height - panel1.Height;
}
else
{
if (loc.X < 0) loc.X = 0;
if (loc.X + panel1.Width > this.ClientSize.Width) loc.X = this.ClientSize.Width - panel1.Width;
if (loc.Y < 0) loc.Y = 0;
if (loc.Y + panel1.Height > this.ClientSize.Height) loc.Y = this.ClientSize.Height - panel1.Height;
}
// Location(final - assigned)
panel1.Location = loc;
// "this mouse pos" set aside as "prev mouse pos"
mMousePos = pos;
}
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
mMovingPanel = false;
}
#endregion
This is my attempt, i know it lacks but hopefully it will speed things up:
#region Pan tool
private bool mMovingPanel;
private Point mMousePos;
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
mMovingPanel = Mouse.LeftButton == MouseButtonState.Pressed;
// prev mouse pos
mMousePos = canvas1.PointToScreen(Mouse.GetPosition(canvas1));
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (mMovingPanel)
{
// "this mouse pos"
Point pos = canvas1.PointToScreen(Mouse.GetPosition(canvas1));
// "this mouse pos" - "prev mouse pos" = offset
Vector adj = new Vector(pos.X - mMousePos.X, pos.Y - mMousePos.Y);
// Location + offset = Location (final - unassigned)
Thickness currentThickness = new Thickness();
Thickness loc = new Thickness(currentThickness.Left + adj.X, currentThickness.Top + adj.Y,
currentThickness.Right, currentThickness.Bottom);
if (canvas1.ActualWidth >= this.ActualWidth && canvas1.ActualHeight >= this.ActualHeight)
{
if (loc.Left > 0) loc.Left = 0;
if (loc.Left + canvas1.ActualWidth < this.ActualWidth) loc.Left = this.ActualWidth - canvas1.ActualWidth;
if (loc.Top > 0) loc.Top = 0;
if (loc.Top + canvas1.ActualHeight < this.ActualHeight) loc.Top = this.ActualHeight - canvas1.ActualHeight;
}
else if (canvas1.ActualHeight >= this.ActualHeight)
{
if (loc.Left < 0) loc.Left = 0;
if (loc.Left + canvas1.ActualWidth > this.ActualWidth) loc.Left = this.ActualWidth - canvas1.ActualWidth;
if (loc.Top > 0) loc.Top = 0;
if (loc.Top + canvas1.ActualHeight < this.ActualHeight) loc.Top = this.ActualHeight - canvas1.ActualHeight;
}
else
{
if (loc.Left < 0) loc.Left = 0;
if (loc.Left + canvas1.ActualWidth > this.ActualWidth) loc.Left = this.ActualWidth - canvas1.ActualWidth;
if (loc.Top < 0) loc.Top = 0;
if (loc.Top + canvas1.ActualHeight > this.ActualHeight) loc.Top = this.ActualHeight - canvas1.ActualHeight;
}
// Location(final - assigned)
canvas1.Margin = loc;
// "this mouse pos" set aside as "prev mouse pos"
mMousePos = pos;
}
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
mMovingPanel = false;
}
#endregion
I have this functionality i want to port from WinForms but i run into a few problems and i can't continue. What it does is move a panel(canvas) around as i click and hold and drag the mouse). I saw the sample using TranlateTransform(XAML) but i can't get any code-behind example, this is dynamic and interactive. I managed to make some out only need the translation solved(and possibly a good approach on using this with Margin, wich seems the only tool i got to moving the canvas around?).
#region Pan tool
private bool mMovingPanel;
private Point mMousePos;
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
mMovingPanel = e.Button == MouseButtons.Left;
// prev mouse pos
mMousePos = panel1.PointToScreen(e.Location);
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (mMovingPanel)
{
// "this mouse pos"
Point pos = panel1.PointToScreen(e.Location);
// "this mouse pos" - "prev mouse pos" = offset
Size adj = new Size(pos.X - mMousePos.X, pos.Y - mMousePos.Y);
// Location + offset = Location (final - unassigned)
Point loc = panel1.Location + adj;
if (panel1.Width >= this.ClientSize.Width && panel1.Height >= this.ClientSize.Height)
{
if (loc.X > 0) loc.X = 0;
if (loc.X + panel1.Width < this.ClientSize.Width) loc.X = this.ClientSize.Width - panel1.Width;
if (loc.Y > 0) loc.Y = 0;
if (loc.Y + panel1.Height < this.ClientSize.Height) loc.Y = this.ClientSize.Height - panel1.Height;
}
else if (panel1.Height >= this.ClientSize.Height)
{
if (loc.X < 0) loc.X = 0;
if (loc.X + panel1.Width > this.ClientSize.Width) loc.X = this.ClientSize.Width - panel1.Width;
if (loc.Y > 0) loc.Y = 0;
if (loc.Y + panel1.Height < this.ClientSize.Height) loc.Y = this.ClientSize.Height - panel1.Height;
}
else
{
if (loc.X < 0) loc.X = 0;
if (loc.X + panel1.Width > this.ClientSize.Width) loc.X = this.ClientSize.Width - panel1.Width;
if (loc.Y < 0) loc.Y = 0;
if (loc.Y + panel1.Height > this.ClientSize.Height) loc.Y = this.ClientSize.Height - panel1.Height;
}
// Location(final - assigned)
panel1.Location = loc;
// "this mouse pos" set aside as "prev mouse pos"
mMousePos = pos;
}
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
mMovingPanel = false;
}
#endregion
This is my attempt, i know it lacks but hopefully it will speed things up:
#region Pan tool
private bool mMovingPanel;
private Point mMousePos;
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
mMovingPanel = Mouse.LeftButton == MouseButtonState.Pressed;
// prev mouse pos
mMousePos = canvas1.PointToScreen(Mouse.GetPosition(canvas1));
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (mMovingPanel)
{
// "this mouse pos"
Point pos = canvas1.PointToScreen(Mouse.GetPosition(canvas1));
// "this mouse pos" - "prev mouse pos" = offset
Vector adj = new Vector(pos.X - mMousePos.X, pos.Y - mMousePos.Y);
// Location + offset = Location (final - unassigned)
Thickness currentThickness = new Thickness();
Thickness loc = new Thickness(currentThickness.Left + adj.X, currentThickness.Top + adj.Y,
currentThickness.Right, currentThickness.Bottom);
if (canvas1.ActualWidth >= this.ActualWidth && canvas1.ActualHeight >= this.ActualHeight)
{
if (loc.Left > 0) loc.Left = 0;
if (loc.Left + canvas1.ActualWidth < this.ActualWidth) loc.Left = this.ActualWidth - canvas1.ActualWidth;
if (loc.Top > 0) loc.Top = 0;
if (loc.Top + canvas1.ActualHeight < this.ActualHeight) loc.Top = this.ActualHeight - canvas1.ActualHeight;
}
else if (canvas1.ActualHeight >= this.ActualHeight)
{
if (loc.Left < 0) loc.Left = 0;
if (loc.Left + canvas1.ActualWidth > this.ActualWidth) loc.Left = this.ActualWidth - canvas1.ActualWidth;
if (loc.Top > 0) loc.Top = 0;
if (loc.Top + canvas1.ActualHeight < this.ActualHeight) loc.Top = this.ActualHeight - canvas1.ActualHeight;
}
else
{
if (loc.Left < 0) loc.Left = 0;
if (loc.Left + canvas1.ActualWidth > this.ActualWidth) loc.Left = this.ActualWidth - canvas1.ActualWidth;
if (loc.Top < 0) loc.Top = 0;
if (loc.Top + canvas1.ActualHeight > this.ActualHeight) loc.Top = this.ActualHeight - canvas1.ActualHeight;
}
// Location(final - assigned)
canvas1.Margin = loc;
// "this mouse pos" set aside as "prev mouse pos"
mMousePos = pos;
}
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
mMovingPanel = false;
}
#endregion