Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 鼠标活动减慢WPF图形演示_C#_Wpf - Fatal编程技术网

C# 鼠标活动减慢WPF图形演示

C# 鼠标活动减慢WPF图形演示,c#,wpf,C#,Wpf,下面是WPF中的一个小图形演示。它看起来像这样: 当它运行时,如果我在窗口的主要区域移动鼠标,动画就会减慢(至少在我的系统上是这样)。如果我将鼠标指针完全移出窗口,动画就会恢复速度 有没有关于如何防止鼠标移动干扰动画速度的建议 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Contro

下面是WPF中的一个小图形演示。它看起来像这样:

当它运行时,如果我在窗口的主要区域移动鼠标,动画就会减慢(至少在我的系统上是这样)。如果我将鼠标指针完全移出窗口,动画就会恢复速度

有没有关于如何防止鼠标移动干扰动画速度的建议

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 System.Threading;
using System.Windows.Threading;

namespace WPF_Golden_Section
{
    class DrawingVisualElement : FrameworkElement
    {
        public DrawingVisual visual;

        public DrawingVisualElement() { visual = new DrawingVisual(); }

        protected override int VisualChildrenCount { get { return 1; } }

        protected override Visual GetVisualChild(int index) { return visual; }
    }

    public static class CanvasUtils
    {
        public static Canvas SetCoordinateSystem(this Canvas canvas, Double xMin, Double xMax, Double yMin, Double yMax)
        {
            var width = xMax - xMin;
            var height = yMax - yMin;

            var translateX = -xMin;
            var translateY = height + yMin;

            var group = new TransformGroup();

            group.Children.Add(new TranslateTransform(translateX, -translateY));
            group.Children.Add(new ScaleTransform(canvas.ActualWidth / width, canvas.ActualHeight / -height));

            canvas.RenderTransform = group;

            return canvas;
        }
    }

    public static class ColorUtils
    {
        public static Color Write(this Color color)
        {
            Console.Write("Color[{0} {1} {2} {3}]", color.A, color.R, color.G, color.B);

            return color;
        }

        static byte ColorComponentToByte(double n)
        {
            return 
                (byte)
                    Math.Round(Math.Min(Math.Max(n, 0), 1) * 255);
        }

        public static Color Rgb(double r, double g, double b)
        {
            return
                Color.FromRgb(
                    ColorComponentToByte(r),
                    ColorComponentToByte(g),
                    ColorComponentToByte(b));
        }

        public static Color SetArgb(this Color color, double a, double r, double g, double b)
        {
            color.A = ColorComponentToByte(a);
            color.R = ColorComponentToByte(r);
            color.G = ColorComponentToByte(g);
            color.B = ColorComponentToByte(b);

            return color;
        }
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            Width = 600;
            Height = 600;

            var dockPanel = new DockPanel();

            Content = dockPanel;

            var slider =
                new Slider()
                {
                    Minimum = 0.1,
                    Maximum = 10,
                    LargeChange = 0.001,
                    Value = 1
                };

            dockPanel.Children.Add(slider);

            DockPanel.SetDock(slider, Dock.Top);

            var incrementSlider =
                new Slider()
                {
                    Maximum = 0.001,
                    Minimum = 0.00001,
                    Value = 0.0001
                };

            dockPanel.Children.Add(incrementSlider);


            DockPanel.SetDock(incrementSlider, Dock.Top);

            var pauseButton = new Button() { Content = "Pause" };

            dockPanel.Children.Add(pauseButton);

            DockPanel.SetDock(pauseButton, Dock.Top);

            var canvas = new Canvas();

            dockPanel.Children.Add(canvas);

            DockPanel.SetDock(canvas, Dock.Top);

            SizeChanged += (s, e) => canvas.SetCoordinateSystem(-400, 400, -400, 400);

            var element = new DrawingVisualElement();

            Action draw = () =>
                {
                    canvas.Children.Clear();

                    // var element = new DrawingVisualElement();

                    using (var dc = element.visual.RenderOpen())
                    {
                        for (var i = 0.0; i < 720.0; i += slider.Value)
                        {
                            var radius = Math.Sin(i / 720.0 * Math.PI) * 30;

                            var phi = (1 + Math.Sqrt(5)) / 2;

                            var omega = 2 * Math.PI * (phi - 1) * i;

                            var x = Math.Cos(omega) * 0.5 * i;
                            var y = Math.Sin(omega) * 0.5 * i;

                            dc.DrawEllipse(
                                new SolidColorBrush(ColorUtils.Rgb(i / 360.0, i / 360.0, 0.25)),
                                new Pen(Brushes.Black, Math.Max(radius / 6, 1)),
                                new Point(x, y),
                                radius / 2,
                                radius / 2);
                        }
                    }

                    canvas.Children.Add(element);
                };

            slider.ValueChanged += (s, e) =>
            {
                Title = slider.Value.ToString() + "     " + incrementSlider.Value;
                draw();
            };

            var timer = new DispatcherTimer();

            timer.Tick += (s, e) =>
                {
                    if (slider.Value < 10.0)
                        slider.Value += incrementSlider.Value;
                };

            timer.Start();

            pauseButton.Click += (s, e) =>
                {
                    if (timer.IsEnabled)
                        timer.Stop();
                    else
                        timer.Start();
                };

            slider.Value = 3.5;
            incrementSlider.Value = 0.00001;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用系统线程;
使用System.Windows.Threading;
名称空间WPF_黄金分割
{
类DrawingVisualElement:FrameworkElement
{
公众绘画;
公共DrawingVisualElement(){visual=new DrawingVisual();}
受保护的重写int VisualChildrenCount{get{return 1;}}
受保护的重写Visual GetVisualChild(int索引){return Visual;}
}
公共静态类画布
{
公共静态画布SetCoordinateSystem(此画布画布,双xMin,双xMax,双yMin,双yMax)
{
变量宽度=xMax-xMin;
var高度=yMax-yMin;
var translateX=-xMin;
var translateY=高度+yMin;
var group=新的TransformGroup();
添加(新的TranslateTransform(translateX,-translateY));
添加(新的ScaleTransform(canvas.ActualWidth/width,canvas.ActualHeight/-height));
canvas.RenderTransform=组;
返回画布;
}
}
公共静态类ColorUtils
{
公共静态颜色写入(此颜色)
{
编写(“Color[{0}{1}{2}{3}]”,Color.A,Color.R,Color.G,Color.B);
返回颜色;
}
静态字节ColorComponentToByte(双n)
{
返回
(字节)
数学四舍五入(数学最小值(数学最大值(n,0),1)*255);
}
公共静态彩色Rgb(双r、双g、双b)
{
返回
Color.FromRgb(
比色组分红细胞(r),
比色组分(g),
显色组分(b);
}
公共静态颜色设置argb(此颜色,双a、双r、双g、双b)
{
color.A=颜色成分到字节(A);
color.R=色度成分比(R);
color.G=色度组分比(G);
color.B=色度成分比(B);
返回颜色;
}
}
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
宽度=600;
高度=600;
var dockPanel=new dockPanel();
内容=dockPanel;
变量滑块=
新滑块()
{
最小值=0.1,
最大值=10,
大变化=0.001,
值=1
};
dockPanel.Children.Add(滑块);
DockPanel.SetDock(滑块,Dock.Top);
变量递增滑块=
新滑块()
{
最大值=0.001,
最小值=0.00001,
值=0.0001
};
dockPanel.Children.Add(递增滑块);
DockPanel.SetDock(递增滑块,Dock.Top);
var pauseButton=new Button(){Content=“Pause”};
dockPanel.Children.Add(暂停按钮);
DockPanel.SetDock(暂停按钮,Dock.Top);
var canvas=newcanvas();
dockPanel.Children.Add(画布);
DockPanel.SetDock(画布,Dock.Top);
SizeChanged+=(s,e)=>canvas.SetCoordinateSystem(-400400,-400400);
var元素=新的DrawingVisualElement();
动作画=()=>
{
canvas.Children.Clear();
//var元素=新的DrawingVisualElement();
使用(var dc=element.visual.renderropen())
{
对于(变量i=0.0;i<720.0;i+=slider.Value)
{
变量半径=数学Sin(i/720.0*数学PI)*30;
var phi=(1+数学Sqrt(5))/2;
var omega=2*Math.PI*(φ-1)*i;
var x=数学Cos(ω)*0.5*i;
var y=数学Sin(ω)*0.5*i;
付款人(
新的SolidColorBrush(ColorUtils.Rgb(i/360.0,i/360.0,0.25)),
新笔(画笔。黑色,数学最大值(半径/6,1)),
新点(x,y),
半径/2,
半径(1/2);
}
}
canvas.Children.Add(元素);
};
slider.ValueChanged+=(s,e)=>
{
Title=slider.Value.ToString()+“”+incrementSlider.Value;
draw();
};
var timer=newdispatchermer();
计时器.勾号+=(s,e)=>
{
如果(滑块值<10.0)
slider.Value+=递增slider.Value;
};
timer.Start();
暂停按钮。单击+=(s,e)=>
{
如果(计时器已启用)
timer.Stop();
其他的
        slider.Value = 3.5;
        incrementSlider.Value = 0.00001;

        EventHandler renderer = (s, e) =>
        {
            if (slider.Value < 10.0)
                slider.Value += incrementSlider.Value;

            //Title = slider.Value.ToString() + "     " + incrementSlider.Value;
            draw();
            _frameCounter++;
        };

        CompositionTarget.Rendering += renderer;
        pauseButton.Click += (sender, args) => CompositionTarget.Rendering -= renderer;

        var timer = new DispatcherTimer();
        timer.Interval = TimeSpan.FromMilliseconds(1000);
        timer.Tick += (s, e) =>
                          {
                              Title = string.Format("fps: {0}", _frameCounter);
                              _frameCounter = 0;
                          };

        timer.Start();