编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

winform 滚动播放Txt 歌词(winform textbox滚动条)

wxchong 2024-07-20 08:16:26 开源技术 16 ℃ 0 评论

最新在做一个播放投屏的功能。

主要是播放投屏TXT内容,进行滚动播放。

下面先看下效果:

过程1. 首先界面创建一个panel 一个rickbox

        private System.Windows.Forms.RichTextBox richTextBox1;
        private System.Windows.Forms.Panel panel1;

设置panel1 的dock 为fill 设置panel在主窗体里。停靠在主窗体里。

this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;

设置richtexbox anchor 上、 左、右 注意这里不要设置dock 为fill

this.richTextBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 

            | System.Windows.Forms.AnchorStyles.Right)));

设置richtextbox的背景色和文字颜色:

   this.richTextBox1.BackColor = System.Drawing.SystemColors.InfoText;
            this.richTextBox1.ForeColor = System.Drawing.Color.YellowGreen;

设置的ContentsResized 事件

 private void richTextBox1_ContentsResized(object sender, ContentsResizedEventArgs e)
        {
            //根据不同情况也可以:
            if (!this.IsHandleCreated)
            {
                return;
            }
            this.richTextBox1.BeginInvoke(new Action(() =>
            {
                richTextBox1.Height = e.NewRectangle.Height + 10;
                richTextBox1.Width = this.panel1.Width;//设置richTextBox1与外层panel1宽度一直

            }));
         
        }

下面放一下全部代码:

form窗体:

namespace Z_Test
{
    partial class Form8
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form8));
            this.richTextBox1 = new System.Windows.Forms.RichTextBox();
            this.panel1 = new System.Windows.Forms.Panel();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // richTextBox1
            // 
            this.richTextBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.richTextBox1.BackColor = System.Drawing.SystemColors.InfoText;
            this.richTextBox1.ForeColor = System.Drawing.Color.YellowGreen;
            this.richTextBox1.Location = new System.Drawing.Point(3, 0);
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.None;
            this.richTextBox1.Size = new System.Drawing.Size(1047, 188);
            this.richTextBox1.TabIndex = 2;
            this.richTextBox1.Text = resources.GetString("richTextBox1.Text");
            this.richTextBox1.ContentsResized += new System.Windows.Forms.ContentsResizedEventHandler(this.richTextBox1_ContentsResized);
            // 
            // panel1
            // 
            this.panel1.BackColor = System.Drawing.SystemColors.InactiveCaption;
            this.panel1.Controls.Add(this.richTextBox1);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(1053, 532);
            this.panel1.TabIndex = 4;
            // 
            // Form8
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(1053, 532);
            this.Controls.Add(this.panel1);
            this.Name = "Form8";
            this.Text = "Form8";
            this.Load += new System.EventHandler(this.Form8_Load);
            this.Resize += new System.EventHandler(this.Form8_Resize);
            this.panel1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion
        private System.Windows.Forms.RichTextBox richTextBox1;
        private System.Windows.Forms.Panel panel1;
    }
}

功能代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Z_Test
{
    public partial class Form8 : Form
    {
        public Form8()
        {
            InitializeComponent();

            initData();

            richTextBox1.Location = this.panel1.Location;//设置richTextBox1与外层panel1起始位置一致

           // richTextBox1.Width = this.panel1.Width;//设置richTextBox1与外层panel1宽度一直

            //richTextBox1.Height = 1500;

            //设置richTextBox1的高度,此处需要估算出完全展示完滚动内容的richTextBox1高度

            System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();//创建定时器

            myTimer.Interval = 50;//间隔时间是以毫秒为单位的

            myTimer.Tag = richTextBox1;//该属性用于传递要移动的对象(此处可以不要)

            myTimer.Start();//启动定时器

          
            myTimer.Tick += myTimerTest_Tick;
        }
        private void myTimerTest_Tick(object sender, EventArgs e)
        {
            richTextBox1.Top = richTextBox1.Top - 2;//向上移动2
            if (richTextBox1.Bottom < 0)
            {
                richTextBox1.Top = this.Height;
                //当richTextBox1底部移动出panel1的上边沿时,保证richTextBox1从panel1下边沿出现
            }
        }
        private void Form8_Load(object sender, EventArgs e)
        {
            
        }



        private void richTextBox1_ContentsResized(object sender, ContentsResizedEventArgs e)
        {
            //根据不同情况也可以:
            if (!this.IsHandleCreated)
            {
                return;
            }
            this.richTextBox1.BeginInvoke(new Action(() =>
            {
                richTextBox1.Height = e.NewRectangle.Height + 10;
                richTextBox1.Width = this.panel1.Width;//设置richTextBox1与外层panel1宽度一直

            }));
         
        }

        private void initData() {
            string txtContent = File.ReadAllText(@"D:\Desktop\资本论.txt");
            this.richTextBox1.Text = txtContent;
        }

        private void Form8_Resize(object sender, EventArgs e)
        {
            //根据不同情况也可以:
            if (!this.IsHandleCreated)
            {
                return;
            }
            this.richTextBox1.BeginInvoke(new Action(()=>
            {
                richTextBox1.Width = this.panel1.Width;//设置richTextBox1与外层panel1宽度一直
                richTextBox1.Refresh();
            }));

        }
    }
}

这里测试使用的Txt大小为4M。

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表