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); } } }
相关文章推荐
- WPF学习——制作一个简单的录入界面(2):用C#编程实现所有控件的功能
- 井位坐标添加到Las文件功能——Tkinter界面开发学习笔记一
- cocos2d-x 3.0 学习笔记: 一个可以拖拽的Label及schedule的应用
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)
- Android开发学习笔记(十)实现一个简易的开场界面
- 【WPF学习笔记】之如何传递第一个登录界面的值到下一个页面显示:动画系列之(三)
- WPF and Silverlight 学习笔记(六):WPF窗体
- WPF and Silverlight 学习笔记(六):WPF窗体
- 用WPF做一个像QQ登陆界面一样的窗体
- 微软企业库5.0学习笔记(八)windows窗体和WPF应用
- 创建一个windows窗体--学习笔记一
- C#学习笔记——MDI窗体(多文档界面)
- 【TOF-3D】学习笔记(1)-上层界面的功能设计
- 一个基于qt的桌面聊天系统的学习笔记(1)-邮件模块(1)邮件界面的初始化流程
- java学习笔记之使用MyEclipse写一个简单的登陆界面(二)
- C#学习笔记——MDI窗体(多文档界面)
- jQuery 学习笔记3 点击弹出一个div并允许拖拽移动
- ExtJS 学习笔记 示例1-点击按钮弹出一个新窗体,避免重复创建
- wpf学习笔记-数据绑定功能总结
- 黑马程序员--wpf学习笔记三---窗体类小结