Wpf拖动按钮实现(二)
2012-03-24 18:33
423 查看
Main.xaml
Main.xaml.cs
MouseIndex.xaml
Mouse.xaml.cs
昨天通宵 边和妹子聊天编写程序 顺便把这个给完善了。。。
<Grid x:Name="LayoutRoot" Background="Transparent"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.RowDefinitions> <!--TitlePanel 包含应用程序的名称和页标题--> <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> <TextBlock x:Name="ApplicationTitle" Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}"/> <TextBlock x:Name="PageTitle" Text="页面名称" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> </StackPanel> <!--ContentPanel - 在此处放置其他内容--> <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <Button Content="Button" Height="72" HorizontalAlignment="Left" Margin="135,138,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" /> </Grid> </Grid>
Main.xaml.cs
public partial class MainPage : PhoneApplicationPage { // 构造函数 public MainPage() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { this.NavigationService.Navigate(new Uri("/MouseIndex.xaml", UriKind.Relative)); } }
MouseIndex.xaml
<!--LayoutRoot 是包含所有页面内容的根网格--> <Canvas x:Name="LayoutRoot" MouseLeave="LayoutRoot_MouseLeave" MouseMove="LayoutRoot_MouseMove"> <Canvas.Background> <ImageBrush Stretch="Fill" ImageSource="./Image/Backgroup.jpg"/> </Canvas.Background> <TextBlock x:Name="msgBlock" Grid.Row="0" Grid.Column="0" VerticalAlignment="Bottom" Canvas.Left="500" Canvas.Top="48" /> <TextBlock x:Name="mouseMsg" Grid.Row="0" Grid.Column="1" VerticalAlignment="Bottom" Canvas.Left="528" Canvas.Top="148" /> <!--TitlePanel 包含应用程序的名称和页标题--> <StackPanel x:Name="TitlePanel" Margin="140,17,12,234" Canvas.Left="419" Canvas.Top="161" Height="96" Width="96"> <Image x:Name="RightButton" MouseLeftButtonDown="RightButton_MouseLeftButtonDown" MouseLeftButtonUp="RightButton_MouseLeftButtonUp" Stretch="Fill" Source="./Image/doubleClick.png"></Image> </StackPanel> <StackPanel Margin="82,128,67,118" x:Name="stackPanel2" Canvas.Left="432" Canvas.Top="193"> <Image x:Name="LeftButton" MouseLeftButtonDown="LeftButton_MouseLeftButtonDown" MouseLeftButtonUp="LeftButton_MouseLeftButtonUp" Stretch="Fill" Source="./Image/click.png" Height="100" Width="100" /> </StackPanel> <!--ContentPanel - 在此处放置其他内容--> <Canvas x:Name="ContentPent" Width="450" Height="450" Canvas.Left="12" Canvas.Top="18"> <Image x:Name="MouseButton" Stretch="Fill" Source="./Image/Button.png" Height="120" Width="120" Canvas.Top="165" Canvas.Left="165" MouseLeftButtonDown="MouseButton_MouseLeftButtonDown" MouseLeave="MouseButton_MouseLeave"/> </Canvas> <TextBlock x:Name="haha" Canvas.Left="82" Canvas.Top="48" Height="30" Text="TextBlock" /> </Canvas>
Mouse.xaml.cs
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using Microsoft.Phone.Controls; using Model; using Alien.NetWork; namespace WP7Face { public partial class MouseIndex : PhoneApplicationPage { bool IsMove = false; Point centerP = new Point(125, 125); Point mousePoint; const double hahax = 225;//按钮中心据conver顶部的距离 const double hahay = 225;//按钮中心据conver左边的距离 private double CentX { get { return (double)MouseButton.GetValue(Canvas.LeftProperty) + MouseButton.Width / 2; } set { value = value - MouseButton.Width / 2; MouseButton.SetValue(Canvas.LeftProperty, value); } } private double CentY { get { return (double)MouseButton.GetValue(Canvas.TopProperty) + MouseButton.Height / 2; } set { value = value - MouseButton.Height / 2; MouseButton.SetValue(Canvas.TopProperty, value); } } public MouseIndex() { InitializeComponent(); } private void MouseButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { IsMove = true; haha.Text = string.Format("{0}:{1}", (int)CentX, (int)CentY); } private void MouseButton_MouseLeave(object sender, MouseEventArgs e) { Point mousePoint = e.GetPosition(ContentPent); } //提交命令 public void Submit(string ordor, double submitX, double submitY) { Message message=new Message(); message.Type = "mouseContorl"; string x = submitX.ToString(); string y = submitY.ToString(); message.Data = "mov:" + x + ":" + y; SocketHelper.Send(message); } //设置按钮回归原点 private void ContentPent_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { IsMove = false; CentX = hahax; CentY = hahay; } private void LayoutRoot_MouseLeave(object sender, MouseEventArgs e) { IsMove = false; CentX = hahax; CentY = hahay; } private void LayoutRoot_MouseMove(object sender, MouseEventArgs e) { mousePoint = e.GetPosition(ContentPent); //获取鼠标触点 const int control = 40;//按钮移动范围 if (IsMove) { double lengh = Math.Sqrt((hahax - mousePoint.X) * (hahax - mousePoint.X) + (hahay - mousePoint.Y) * (hahay - mousePoint.Y)); //计算触点到原点的距离 int v = (int)lengh / 100; //设置速度 //根据范围 分别计算按钮移动的范围 if (lengh < control) { CentX = mousePoint.X; CentY = mousePoint.Y; } else if (mousePoint.X < hahax && mousePoint.Y < hahay) { CentX = hahax - control * Math.Abs(hahax - mousePoint.X) / lengh; CentY = hahay - control * Math.Abs(hahay - mousePoint.Y) / lengh; } else if (mousePoint.X > hahax && mousePoint.Y < hahay) { CentX = hahax + control * Math.Abs(hahax - mousePoint.X) / lengh; CentY = hahay - control * Math.Abs(hahay - mousePoint.Y) / lengh; } else if (mousePoint.X < hahax && mousePoint.Y > hahay) { CentX = hahax - control * Math.Abs(hahax - mousePoint.X) / lengh; CentY = hahay + control * Math.Abs(hahay - mousePoint.Y) / lengh; } else if (mousePoint.X > hahax && mousePoint.Y > hahay) { CentX = hahax + control * Math.Abs(hahax - mousePoint.X) / lengh; CentY = hahay + control * Math.Abs(hahay - mousePoint.Y) / lengh; } int x = (int)(hahax - mousePoint.X) / 10; int y = (int)(hahay - mousePoint.Y) / 10; if (v >= 1) { x *= v; y *= v; Submit("mov", x, y); } else { Submit("mov", x, y); } } } private void RightButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Submit("rid", 0, 0); } private void RightButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { Submit("riu", 0, 0); } private void LeftButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Submit("led", 0, 0); } private void LeftButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { Submit("leu", 0, 0); } } }
昨天通宵 边和妹子聊天编写程序 顺便把这个给完善了。。。
相关文章推荐
- WPF拖动按钮实现(一)
- wpf ListBox拖动排序实现
- WPF外发光按钮的实现(转)
- WPF/Silverligh实现图片的放大缩小拖动
- 在WPF中的Canvas上实现控件的拖动、缩放
- 【WPF】使用 XAML 的 Trigger 系统实现三态按钮
- wpf,后台触发按钮点击以及拖动
- WPF中拖动控件,实现位置随意摆放[1]
- WPF中实现砖块拖动的方法(1)
- 在WPF中的Canvas上实现控件的拖动、缩放
- WPF 去除头部,实现拖动
- 实现了当移近按钮的时候,按钮显现外发光的特效,用WPF实现比较容易。
- WPF下实现控件的拖动操作
- 基于BootStrap的Metronic框架实现页面链接收藏夹功能按钮移动收藏记录(使用Sortable进行拖动排序)
- iOS之实现按钮拖动/tableViewcell多选,拖动/collectionviewcell多选,拖动(交换位置)
- android 应用内部悬浮可拖动按钮简单实现代码
- android实现可拖动按钮
- 【C#/WPF】WPF应用程序,另一类Button按钮(图片)的实现方法。
- 安卓实现按钮可随意拖动(同时解决click和touch事件的冲突)
- WPF界面设计技巧(3)—实现不规则动画按钮