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); } } }
昨天通宵 边和妹子聊天编写程序 顺便把这个给完善了。。。