Wpf拖动按钮实现(二)
生活随笔
收集整理的這篇文章主要介紹了
Wpf拖动按钮实现(二)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Main.xaml
<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);}} }昨天通宵 邊和妹子聊天編寫程序 順便把這個給完善了。。。
轉載于:https://www.cnblogs.com/haorensw/archive/2012/03/24/2415719.html
總結
以上是生活随笔為你收集整理的Wpf拖动按钮实现(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: itext库产生word文档示例(.do
- 下一篇: Android SimpleAdapte