WPF滚动
- <UserControlx:Class="WpfApplication1.UserControl1"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- mc:Ignorable="d"
- d:DesignHeight="30"d:DesignWidth="300"Loaded="UserControl_Loaded">
- <CanvasClipToBounds="True"x:Name="canvas">
- <Canvas.Resources>
- <Storyboardx:Key="stdUp">
- <DoubleAnimationDuration="0:0:1.5"Storyboard.TargetName="content"Storyboard.TargetProperty="RenderTransform.Y"/>
- </Storyboard>
- <Storyboardx:Key="stdLeft">
- <DoubleAnimationDuration="0:0:1.5"Storyboard.TargetName="content"Storyboard.TargetProperty="RenderTransform.X"/>
- </Storyboard>
- </Canvas.Resources>
- <StackPanelx:Name="content">
- <StackPanel.RenderTransform>
- <TranslateTransform/>
- </StackPanel.RenderTransform>
- <TextBlockx:Name="txtItem"Foreground="Black"/>
- </StackPanel>
- </Canvas>
- </UserControl>
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingSystem.Windows;
- usingSystem.Windows.Controls;
- usingSystem.Windows.Data;
- usingSystem.Windows.Documents;
- usingSystem.Windows.Input;
- usingSystem.Windows.Media;
- usingSystem.Windows.Media.Animation;
- usingSystem.Windows.Media.Imaging;
- usingSystem.Windows.Navigation;
- usingSystem.Windows.Shapes;
- namespaceWpfApplication1
- {
- /// <summary>
- /// UserControl1.xaml 的交互逻辑
- /// </summary>
- public partial classUserControl1:UserControl
- {
- publicUserControl1()
- {
- InitializeComponent();
- }
- Storyboard std = null;
- DoubleAnimation animation = null;
- int index, total;
- //public int FontSize
- //{
- // get { return (int)this.GetValue(FontSizeProperty); }
- // set { this.SetValue(FontSizeProperty, value); }
- //}
- publicMarqueeTypeShowType
- {
- get {return(MarqueeType)this.GetValue(ShowTypeProperty);}
- set{this.SetValue(ShowTypeProperty, value);}
- }
- publicstatic readonly DependencyPropertyShowTypeProperty=DependencyProperty.Register("ShowType",typeof(MarqueeType),typeof(UserControl1),newPropertyMetadata(MarqueeType.Up));
- publicdoubleSpeed
- {
- get {return(double)this.GetValue(SpeedProperty);}
- set{this.SetValue(SpeedProperty, value);}
- }
- publicstatic readonly DependencyPropertySpeedProperty=DependencyProperty.Register("Speed",typeof(double),typeof(UserControl1),newPropertyMetadata(1.5));
- privatevoidUserControl_Loaded(object sender,RoutedEventArgs e)
- {
- if(ShowType==MarqueeType.Up||ShowType==MarqueeType.Down)
- {
- std =(Storyboard)canvas.Resources["stdUp"];
- content.Width= canvas.ActualWidth;
- txtItem.TextWrapping=TextWrapping.Wrap;
- }
- if(ShowType==MarqueeType.Left||ShowType==MarqueeType.Right)
- {
- std =(Storyboard)canvas.Resources["stdLeft"];
- content.Height= canvas.ActualHeight;
- }
- animation =(DoubleAnimation)std.Children[0];
- std.Completed+=(t, r)=> changeItem();
- }
- privateList<string> itemsSource;
- publicList<string>ItemsSource
- {
- get {return itemsSource;}
- set
- {
- this.Dispatcher.BeginInvoke(newAction(()=>
- {
- if(std != null)
- {
- std.Stop();
- txtItem.Text="";
- itemsSource = value;
- if(itemsSource != null >> itemsSource.Count>0)
- {
- index =0;
- total = value.Count;
- changeItem();
- }
- }
- }));
- }
- }
- privatevoid changeItem()
- {
- txtItem.Text= itemsSource[index].ToString();
- txtItem.FontSize=FontSize;
- txtItem.UpdateLayout();
- double canvasWidth = canvas.ActualWidth;
- double canvasHeight = canvas.ActualHeight;
- double txtWidth = txtItem.ActualWidth;
- double txtHeight = txtItem.ActualHeight;
- if(ShowType==MarqueeType.Up)
- {
- animation.From= canvasHeight;
- animation.To=-txtHeight;
- }
- elseif(ShowType==MarqueeType.Down)
- {
- animation.From=-txtHeight;
- animation.To= canvasHeight;
- }
- elseif(ShowType==MarqueeType.Left)
- {
- animation.From= canvasWidth;
- animation.To=-txtWidth;
- }
- elseif(ShowType==MarqueeType.Right)
- {
- animation.From=-txtWidth;
- animation.To= canvasWidth;
- }
- int time =0;
- if(ShowType==MarqueeType.Up||ShowType==MarqueeType.Down)
- {
- time =(int)(txtHeight / canvasHeight *Speed);
- }
- if(ShowType==MarqueeType.Left||ShowType==MarqueeType.Right)
- {
- time =(int)(txtWidth / canvasWidth *Speed);
- }
- if(time <2) time =2;
- animation.Duration=newDuration(newTimeSpan(0,0, time));
- index++;
- if(index == total) index =0;
- if(std != null)
- {
- std.Begin();
- }
- }
- }
- publicenumMarqueeType
- {
- Up,
- Down,
- Left,
- Right
- }
- }
- <Windowx:Class="WpfApplication1.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Background="Transparent"
- Opacity="1"
- WindowStyle="None"
- AllowsTransparency="True"
- Title="MainWindow"Height="350"Width="525"Loaded="Window_Loaded">
- </Window>
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using WebSocketSharp;
- namespace WpfApplication1
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- // 设置全屏
- this.WindowState = System.Windows.WindowState.Normal;
- this.WindowStyle = System.Windows.WindowStyle.None;
- this.ResizeMode = System.Windows.ResizeMode.NoResize;
- this.Topmost = true;
- this.Left = 0.0;
- this.Top = 0.0;
- this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
- this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
- }
- private void Window_Loaded(object sender, RoutedEventArgs e)
- {
- UserControl1 uc = new UserControl1();
- //uc.itemsSource = new List<String>();
- uc.FlowDirection = System.Windows.FlowDirection.RightToLeft;
- uc.ShowType = MarqueeType.Right;
- uc.FontSize = 40;
- uc.TranslatePoint(new Point(1000, 300), this);
- uc.ItemsSource = new List<string>() { "你好" };
- this.AddChild(uc);
- this.SetValue(Canvas.TopProperty, 10d);
- try
- {
- WebSocket ws;
- ws = new WebSocket("ws://localhost:9090/");
- ws.OnMessage += (s, erg) =>
- {
- };
- ws.Connect();
- }
- catch (Exception ex)
- {
- throw;
- }
- }
- }
- }
============ 欢迎各位老板打赏~ ===========
与本文相关的文章
- · The instance of entity type ‘Customer’ cannot be tracked because another instance with the same key value for {‘Id’} is already being tracked.
- · .NET8实时更新nginx ip地址归属地
- · 解决.NET Blazor子组件不刷新问题
- · .NET8如何在普通类库中引用 Microsoft.AspNetCore
- · .NET8 Mysql SSL error
- · ASP.NET Core MVC的Razor视图渲染中文乱码的问题
- · .NETCORE 依赖注入服务生命周期
- · asp.net zero改mysql
- · .NET5面试汇总
- · .Net连接Mysql数据库的Convert Zero Datetime日期问题
- · vue使用element-ui中的Message 、MessageBox 、Notification
- · Asp.Net Core Filter 深入浅出的那些事-AOP