Skip to content
The Chat Archive
gpt-3.5-turbo-0613· OpenAI WildChat-4.8M (AI2)

来一份wpf动态大屏幕的代码

  1. User

    来一份wpf动态大屏幕的代码

  2. Assistant

    下面是一个简单的WPF动态大屏幕的代码示例:

    XAML代码:

    <Window x:Class="DynamicScreen.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Dynamic Screen" Height="450" Width="800">
        <Canvas x:Name="canvas">
            <Canvas.Background>
                <SolidColorBrush Color="Black"/>
            </Canvas.Background>
        </Canvas>
    </Window>
    

    C#代码:

    using System;
    using System.Threading;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Shapes;
    
    namespace DynamicScreen
    {
        public partial class MainWindow : Window
        {
            private const int ScreenWidth = 800;
            private const int ScreenHeight = 450;
    
            public MainWindow()
            {
                InitializeComponent();
    
                // 创建一个线程用于更新屏幕内容
                Thread thread = new Thread(UpdateScreen);
                thread.Start();
            }
    
            private void UpdateScreen()
            {
                Random random = new Random();
    
                while (true)
                {
                    // 在屏幕上添加一个随机位置和大小的矩形
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        int width = random.Next(50, 200);
                        int height = random.Next(50, 200);
    
                        int left = random.Next(0, ScreenWidth - width);
                        int top = random.Next(0, ScreenHeight - height);
    
                        Rectangle rectangle = new Rectangle
                        {
                            Width = width,
                            Height = height,
                            Fill = new SolidColorBrush(Color.FromRgb((byte)random.Next(256), (byte)random.Next(256), (byte)random.Next(256))),
                            Stroke = Brushes.White,
                            StrokeThickness = 2
                        };
    
                        Canvas.SetLeft(rectangle, left);
                        Canvas.SetTop(rectangle, top);
    
                        canvas.Children.Add(rectangle);
                    });
    
                    Thread.Sleep(1000); // 每隔1秒更新一次屏幕
                }
            }
        }
    }
    

    在上述代码中,我们使用Canvas作为屏幕容器,通过在后台线程中不断创建随机位置和大小的矩形并将其添加到Canvas中,实现了动态的大屏幕效果。每隔1秒更新一次屏幕。

    请注意,由于该代码存在一个后台线程,因此需要在WPF应用程序的入口点进行多线程初始化。可以在App.xaml.cs文件中的Application_Startup方法中添加以下代码:

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        // 设置WPF应用程序支持多线程
        System.Windows.Forms.Application.EnableVisualStyles();
        System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
        System.Windows.Forms.Application.Run(new MainForm());
    }
    

    这样就能实现一个简单的WPF动态大屏幕的效果。当然,你也可以根据实际需求进行修改和扩展。