您的位置:首页 > 其它

WPF学习笔记--一个具有拖拽、平移、放大等界面导航功能的窗体

2013-05-31 22:46 387 查看
前台代码

<Window x:Class="Main.MainWindow" Name="GPS_Main"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="GPS_Main_Loaded">
<Grid Name="mainArea" Margin="0,0,0,0"  >
<DockPanel LastChildFill="True">
<Grid Name="MenuTop" VerticalAlignment="Top" Height="30" Panel.ZIndex="100"></Grid>
<Grid Name="Center" VerticalAlignment="Top" Margin="0,30,0,0" Panel.ZIndex="100">
<Canvas x:Name="CI" Background="#FFE5EBEF"
HorizontalAlignment="Left"
MouseUp="CI_MouseUp" MouseDown="CI_MouseDown"
MouseWheel="CI_MouseWheel"
MouseMove="CI_MouseMove"
Canvas.Left="0" Canvas.Top="0">
<Canvas.RenderTransform>
<TransformGroup>
<ScaleTransform x:Name="STF"></ScaleTransform>
<MatrixTransform x:Name="MTF"></MatrixTransform>
</TransformGroup>
</Canvas.RenderTransform>
<!--巷道图存放位置-->
<Image  Panel.ZIndex="1"/>
<Canvas x:Name="ICOS" Panel.ZIndex="4"></Canvas>
</Canvas>

</Grid>
</DockPanel>
</Grid>
</Window>


后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Configuration;
using System.Diagnostics;
using System.Reflection;
namespace Main
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
//旋转中心点
Point RotatePoint = new Point(-999, -999);
bool DrawPoint = true;
DateTime LastClick;
Point LastPoint;
bool CanRotate = false;
public MainWindow()
{
InitializeComponent();
}
/// <summary>
/// 根据配置文件配置UI的主要参数
/// </summary>
void SetUIByConfigfile()
{
double Height_Main = Convert.ToDouble(ConfigurationManager.AppSettings["Height_Main"]);
double Width_Main = Convert.ToDouble(ConfigurationManager.AppSettings["Width_Main"]);
double Height_MenuTop = Convert.ToDouble(ConfigurationManager.AppSettings["Height_MenuTop"]);
double Width_MenuTop = Convert.ToDouble(ConfigurationManager.AppSettings["Width_MenuTop"]);
double Height_CI = Convert.ToDouble(ConfigurationManager.AppSettings["Height_CI"]);
double Width_CI = Convert.ToDouble(ConfigurationManager.AppSettings["Width_CI"]);
GPS_Main.Height = Height_Main;
GPS_Main.Width = Width_Main;
MenuTop.Height = Height_MenuTop;
MenuTop.Width = Width_MenuTop;
ICOS.Height = CI.Height = Height_CI;
ICOS.Width = CI.Width = Width_CI;
string ICOS_BackColor = ConfigurationManager.AppSettings["ICOS_BackColor"];
PropertyInfo propertyPkid = typeof(System.Windows.Media.Brushes).GetProperty(ICOS_BackColor);
ICOS.Background = (System.Windows.Media.SolidColorBrush)propertyPkid.GetValue(new System.Windows.Media.SolidColorBrush());

ICOS.Background = new System.Windows.Media.SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(ICOS_BackColor));

}
private void GPS_Main_Loaded(object sender, RoutedEventArgs e)
{
SetUIByConfigfile();
}

void drawPoint(double X, double Y, byte Type)
{
if (!DrawPoint)
return;
ICOS.Children.Clear();
Ellipse myEllipse = new Ellipse();
myEllipse.Stroke = System.Windows.Media.Brushes.Black;
myEllipse.StrokeThickness = 1;
myEllipse.Fill = System.Windows.Media.Brushes.Black;
myEllipse.HorizontalAlignment = HorizontalAlignment.Left;
myEllipse.VerticalAlignment = VerticalAlignment.Center;
myEllipse.Width = 10;
myEllipse.Height = 10;
Canvas.SetLeft(myEllipse, X - myEllipse.Width / 2);
Canvas.SetTop(myEllipse, Y - myEllipse.Height / 2);
ICOS.Children.Add(myEllipse);
}
private void CI_MouseUp(object sender, MouseButtonEventArgs e)
{
RotatePoint = new Point(-999, -999);
ICOS.Children.Clear();
if (CanRotate)
{
CanRotate = false;
}
}

private void CI_MouseDown(object sender, MouseButtonEventArgs e)
{
Point p = e.MouseDevice.GetPosition(CI);
if ((Mouse.MiddleButton == MouseButtonState.Pressed && RotatePoint.X == -999 && RotatePoint.Y == -999))
{
RotatePoint = p;
drawPoint(p.X, p.Y, 2);

}
else if (Mouse.LeftButton == MouseButtonState.Pressed)
{

if ((DateTime.Now - LastClick).TotalMilliseconds < 300)
{
drawPoint(p.X, p.Y, 1);
Matrix m = CI.RenderTransform.Value;
m.ScaleAtPrepend(1.1, 1.1, p.X, p.Y);
CI.RenderTransform = new MatrixTransform(m);

}
LastClick = DateTime.Now;
LastPoint = p;
}
else if (Mouse.RightButton == MouseButtonState.Pressed)
{

RotatePoint = new Point(CI.Width / 2, CI.Height / 2);
drawPoint(RotatePoint.X, RotatePoint.Y, 1);
CanRotate = true;
LastPoint = p;
}
}

private void CI_MouseMove(object sender, MouseEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
Point NowPoint = e.GetPosition(CI);
if (e.LeftButton == MouseButtonState.Pressed)
{
double resultY = NowPoint.Y - LastPoint.Y + (double)CI.GetValue(Canvas.TopProperty);
double resultX = NowPoint.X - LastPoint.X + (double)CI.GetValue(Canvas.LeftProperty);
Matrix m = CI.RenderTransform.Value;
m.TranslatePrepend(resultX, resultY);
CI.RenderTransform = new MatrixTransform(m);
element.Cursor = Cursors.Hand;
}
else
if (Mouse.MiddleButton == MouseButtonState.Pressed && RotatePoint.X != -999 && RotatePoint.Y != -999)
{
Matrix m = CI.RenderTransform.Value;

if (NowPoint.X > RotatePoint.X)
{
m.RotateAtPrepend(0.1, RotatePoint.X, RotatePoint.Y);

}
else if (NowPoint.X < RotatePoint.X)
{
m.RotateAtPrepend(-0.1, RotatePoint.X, RotatePoint.Y);
}
Debug.WriteLine(RotatePoint.X + "|" + RotatePoint.Y);
CI.RenderTransform = new MatrixTransform(m);
LastPoint = NowPoint;

}
else if (Mouse.RightButton == MouseButtonState.Pressed)
{
Matrix m = CI.RenderTransform.Value;
if (NowPoint.X > RotatePoint.X)
{
m.RotateAtPrepend(0.1, RotatePoint.X, RotatePoint.Y);

}
else if (NowPoint.X < RotatePoint.X)
{
m.RotateAtPrepend(-0.1, RotatePoint.X, RotatePoint.Y);
}
Debug.WriteLine(RotatePoint.X + "|" + RotatePoint.Y);
CI.RenderTransform = new MatrixTransform(m);
LastPoint = NowPoint;

}
else
{
element.Cursor = null;
}
}

private void CI_MouseWheel(object sender, MouseWheelEventArgs e)
{
Point p = e.MouseDevice.GetPosition(CI);
drawPoint(p.X, p.Y, 4);

Matrix m = CI.RenderTransform.Value;
if (e.Delta > 0)
m.ScaleAtPrepend(1.1, 1.1, p.X, p.Y);
else
m.ScaleAtPrepend(1 / 1.1, 1 / 1.1, p.X, p.Y);
CI.RenderTransform = new MatrixTransform(m);
}
}
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐