期末作業 應用程式(App) TouchAndDrawCircles(觸控繪製圖) 資工三乙 499g0027 吳俊磊 介面操作 這是一個 App 應用程式,功能是你可以隨意地畫 出不同大小的圓,而且每次顏色都會不同,亂數 選到的顏色,可以在上面創作,非常有趣! 執行畫面: 程式部分: MainPage.xaml.cs 部分 using System; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Windows.Shapes; using Microsoft.Phone.Controls; namespace TouchAndDrawCircles { public partial class MainPage : PhoneApplicationPage { Random rand = new Random(); bool isDrawing, isDragging; Path path; EllipseGeometry ellipseGeo; public MainPage() { InitializeComponent(); } protected override void OnManipulationStarted(ManipulationStartedEventArgs args) { if (isDrawing || isDragging) return; if (args.OriginalSource is Path) { ellipseGeo = (args.OriginalSource as Path).Data as EllipseGeometry; isDragging = true; args.ManipulationContainer = ContentPanel; args.Handled = true; } else if (args.OriginalSource == ContentPanel) { ellipseGeo = new EllipseGeometry(); ellipseGeo.Center = args.ManipulationOrigin; path = new Path(); path.Stroke = this.Resources["PhoneForegroundBrush"] as Brush; path.Data = ellipseGeo; ContentPanel.Children.Add(path); isDrawing = true; args.Handled = true; } base.OnManipulationStarted(args); } protected override void OnManipulationDelta(ManipulationDeltaEventArgs args) { if (isDragging) { Point center = ellipseGeo.Center; center.X += args.DeltaManipulation.Translation.X; center.Y += args.DeltaManipulation.Translation.Y; ellipseGeo.Center = center; args.Handled = true; } else if (isDrawing) { Point translation = args.CumulativeManipulation.Translation; double radius = Math.Max(Math.Abs(translation.X), Math.Abs(translation.Y)); ellipseGeo.RadiusX = radius; ellipseGeo.RadiusY = radius; args.Handled = true; } base.OnManipulationDelta(args); } protected override void OnManipulationCompleted(ManipulationCompletedEventArgs args) { if (isDragging) { isDragging = false; args.Handled = true; } else if (isDrawing) { Color clr = Color.FromArgb(255, (byte)rand.Next(256), (byte)rand.Next(256), (byte)rand.Next(256)); path.Fill = new SolidColorBrush(clr); isDrawing = false; args.Handled = true; } base.OnManipulationCompleted(args); } } } App.xaml.cs 部分 using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Navigation; using System.Windows.Shapes; using Microsoft.Phone.Controls; using Microsoft.Phone.Shell; namespace TouchAndDrawCircles { public partial class App : Application { public PhoneApplicationFrame RootFrame { get; private set; } public App() { UnhandledException += Application_UnhandledException; if (System.Diagnostics.Debugger.IsAttached) { } InitializeComponent(); InitializePhoneApplication(); } private void Application_Launching(object sender, LaunchingEventArgs e) { } private void Application_Activated(object sender, ActivatedEventArgs e) { } private void Application_Deactivated(object sender, DeactivatedEventArgs e) { } private void Application_Closing(object sender, ClosingEventArgs e) { } private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e) { if (System.Diagnostics.Debugger.IsAttached) { System.Diagnostics.Debugger.Break(); } } private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) { if (System.Diagnostics.Debugger.IsAttached) { System.Diagnostics.Debugger.Break(); } } #region Phone application initialization private bool phoneApplicationInitialized = false; private void InitializePhoneApplication() { if (phoneApplicationInitialized) return; RootFrame = new PhoneApplicationFrame(); RootFrame.Navigated += CompleteInitializePhoneApplication; RootFrame.NavigationFailed += RootFrame_NavigationFailed; phoneApplicationInitialized = true; } private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e) { if (RootVisual != RootFrame) RootVisual = RootFrame; RootFrame.Navigated -= CompleteInitializePhoneApplication; } #endregion } }