分类目录

链接

2018年八月
« 7月    
 12345
6789101112
13141516171819
20212223242526
2728293031  

近期文章

热门标签

博主推荐

现在位置:    首页 > .NET > 正文
WPF滚动
.NET 暂无评论 阅读(10)
  1. <UserControlx:Class="WpfApplication1.UserControl1"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  5. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  6. mc:Ignorable="d"
  7. d:DesignHeight="30"d:DesignWidth="300"Loaded="UserControl_Loaded">
  8. <CanvasClipToBounds="True"x:Name="canvas">
  9. <Canvas.Resources>
  10. <Storyboardx:Key="stdUp">
  11. <DoubleAnimationDuration="0:0:1.5"Storyboard.TargetName="content"Storyboard.TargetProperty="RenderTransform.Y"/>
  12. </Storyboard>
  13. <Storyboardx:Key="stdLeft">
  14. <DoubleAnimationDuration="0:0:1.5"Storyboard.TargetName="content"Storyboard.TargetProperty="RenderTransform.X"/>
  15. </Storyboard>
  16. </Canvas.Resources>
  17. <StackPanelx:Name="content">
  18. <StackPanel.RenderTransform>
  19. <TranslateTransform/>
  20. </StackPanel.RenderTransform>
  21. <TextBlockx:Name="txtItem"Foreground="Black"/>
  22. </StackPanel>
  23. </Canvas>
  24. </UserControl>

 

 

  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Text;
  5. usingSystem.Windows;
  6. usingSystem.Windows.Controls;
  7. usingSystem.Windows.Data;
  8. usingSystem.Windows.Documents;
  9. usingSystem.Windows.Input;
  10. usingSystem.Windows.Media;
  11. usingSystem.Windows.Media.Animation;
  12. usingSystem.Windows.Media.Imaging;
  13. usingSystem.Windows.Navigation;
  14. usingSystem.Windows.Shapes;
  15. namespaceWpfApplication1
  16. {
  17. /// <summary>
  18. /// UserControl1.xaml 的交互逻辑
  19. /// </summary>
  20. public partial classUserControl1:UserControl
  21. {
  22. publicUserControl1()
  23. {
  24. InitializeComponent();
  25. }
  26. Storyboard std = null;
  27. DoubleAnimation animation = null;
  28. int index, total;
  29. //public int FontSize
  30. //{
  31. //    get { return (int)this.GetValue(FontSizeProperty); }
  32. //    set { this.SetValue(FontSizeProperty, value); }
  33. //}
  34. publicMarqueeTypeShowType
  35. {
  36.             get {return(MarqueeType)this.GetValue(ShowTypeProperty);}
  37. set{this.SetValue(ShowTypeProperty, value);}
  38. }
  39. publicstatic readonly DependencyPropertyShowTypeProperty=DependencyProperty.Register("ShowType",typeof(MarqueeType),typeof(UserControl1),newPropertyMetadata(MarqueeType.Up));
  40. publicdoubleSpeed
  41. {
  42.             get {return(double)this.GetValue(SpeedProperty);}
  43. set{this.SetValue(SpeedProperty, value);}
  44. }
  45. publicstatic readonly DependencyPropertySpeedProperty=DependencyProperty.Register("Speed",typeof(double),typeof(UserControl1),newPropertyMetadata(1.5));
  46. privatevoidUserControl_Loaded(object sender,RoutedEventArgs e)
  47. {
  48. if(ShowType==MarqueeType.Up||ShowType==MarqueeType.Down)
  49. {
  50.                 std =(Storyboard)canvas.Resources["stdUp"];
  51.                 content.Width= canvas.ActualWidth;
  52.                 txtItem.TextWrapping=TextWrapping.Wrap;
  53. }
  54. if(ShowType==MarqueeType.Left||ShowType==MarqueeType.Right)
  55. {
  56.                 std =(Storyboard)canvas.Resources["stdLeft"];
  57.                 content.Height= canvas.ActualHeight;
  58. }
  59.             animation =(DoubleAnimation)std.Children[0];
  60.             std.Completed+=(t, r)=> changeItem();
  61. }
  62. privateList<string> itemsSource;
  63. publicList<string>ItemsSource
  64. {
  65.             get {return itemsSource;}
  66. set
  67. {
  68. this.Dispatcher.BeginInvoke(newAction(()=>
  69. {
  70. if(std != null)
  71. {
  72.                         std.Stop();
  73.                         txtItem.Text="";
  74.                         itemsSource = value;
  75. if(itemsSource != null >> itemsSource.Count>0)
  76. {
  77.                             index =0;
  78.                             total = value.Count;
  79.                             changeItem();
  80. }
  81. }
  82. }));
  83. }
  84. }
  85. privatevoid changeItem()
  86. {
  87.             txtItem.Text= itemsSource[index].ToString();
  88.             txtItem.FontSize=FontSize;
  89.             txtItem.UpdateLayout();
  90. double canvasWidth = canvas.ActualWidth;
  91. double canvasHeight = canvas.ActualHeight;
  92. double txtWidth = txtItem.ActualWidth;
  93. double txtHeight = txtItem.ActualHeight;
  94. if(ShowType==MarqueeType.Up)
  95. {
  96.                 animation.From= canvasHeight;
  97.                 animation.To=-txtHeight;
  98. }
  99. elseif(ShowType==MarqueeType.Down)
  100. {
  101.                 animation.From=-txtHeight;
  102.                 animation.To= canvasHeight;
  103. }
  104. elseif(ShowType==MarqueeType.Left)
  105. {
  106.                 animation.From= canvasWidth;
  107.                 animation.To=-txtWidth;
  108. }
  109. elseif(ShowType==MarqueeType.Right)
  110. {
  111.                 animation.From=-txtWidth;
  112.                 animation.To= canvasWidth;
  113. }
  114. int time =0;
  115. if(ShowType==MarqueeType.Up||ShowType==MarqueeType.Down)
  116. {
  117.                 time =(int)(txtHeight / canvasHeight *Speed);
  118. }
  119. if(ShowType==MarqueeType.Left||ShowType==MarqueeType.Right)
  120. {
  121.                 time =(int)(txtWidth / canvasWidth *Speed);
  122. }
  123. if(time <2) time =2;
  124.             animation.Duration=newDuration(newTimeSpan(0,0, time));
  125.             index++;
  126. if(index == total) index =0;
  127. if(std != null)
  128. {
  129.                 std.Begin();
  130. }
  131. }
  132. }
  133. publicenumMarqueeType
  134. {
  135. Up,
  136. Down,
  137. Left,
  138. Right
  139. }
  140. }

 

  1. <Windowx:Class="WpfApplication1.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. Background="Transparent"
  5. Opacity="1"
  6. WindowStyle="None"
  7. AllowsTransparency="True"
  8. Title="MainWindow"Height="350"Width="525"Loaded="Window_Loaded">
  9. </Window>
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using WebSocketSharp;
  15.  
  16. namespace WpfApplication1
  17. {
  18.     /// <summary>
  19.     /// MainWindow.xaml 的交互逻辑
  20.     /// </summary>
  21.     public partial class MainWindow : Window
  22.     {
  23.         public MainWindow()
  24.         {
  25.             InitializeComponent();
  26.             // 设置全屏  
  27.             this.WindowState = System.Windows.WindowState.Normal;
  28.             this.WindowStyle = System.Windows.WindowStyle.None;
  29.             this.ResizeMode = System.Windows.ResizeMode.NoResize;
  30.             this.Topmost = true;
  31.  
  32.             this.Left = 0.0;
  33.             this.Top = 0.0;
  34.             this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
  35.             this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
  36.  
  37.         }
  38.  
  39.         private void Window_Loaded(object sender, RoutedEventArgs e)
  40.         {
  41.             UserControl1 uc = new UserControl1();
  42.             //uc.itemsSource = new List<String>();
  43.             uc.FlowDirection = System.Windows.FlowDirection.RightToLeft;
  44.             uc.ShowType = MarqueeType.Right;
  45.             uc.FontSize = 40;
  46.             uc.TranslatePoint(new Point(1000, 300), this); 
  47.             uc.ItemsSource = new List<string>() { "你好" };
  48.             this.AddChild(uc);
  49.             this.SetValue(Canvas.TopProperty, 10d);
  50.  
  51.  
  52.  
  53.             try
  54.             {
  55.                 WebSocket ws;
  56.  
  57.                 ws = new WebSocket("ws://localhost:9090/");
  58.  
  59.                 ws.OnMessage += (s, erg) =>
  60.                 {
  61.                 };
  62.  
  63.                 ws.Connect();
  64.             }
  65.             catch (Exception ex)
  66.             {
  67.  
  68.                 throw;
  69.             }
  70.  
  71.  
  72.         }
  73.     }
  74. }

本文版权归数据库之家所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:WPF滚动 | 数据库之家