winform滚动字幕

来源:互联网 发布:网络架构是什么 编辑:程序博客网 时间:2024/06/10 03:45

最近因项目需要,需在winform中实现文字从右向左,从下向上,滚动效果,其中试过在PictureBox,RichTexBox,Label组件中实现文字滚动,发现Label中效果最好,PictureBox次之,RichTexBox中不能实现。



现分享如下:Label控件中实现从下向上滚动效果,PictureBox组件中实现从右向左滚动效果,winform工程中添加Label,PictureBox组件,各创建一个Label,PictureBox的继承类,实现如下:

Label继承类:

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

namespace gundong
{
    class NewLabel : Label
    {
        public NewLabel()
        {

            this.updateTimer = new Timer();
            this.updateTimer.Interval = 30;
            this.updateTimer.Enabled = true;
            this.updateTimer.Tick += delegate { this.OnTick(); };
            //drawRect.X = x;
            drawRect.X = this.width + stringSize.Width;
            drawRect.Y = y;
            drawRect.Width = width;
            //drawRect.Height = height;
            drawRect.Height = GetHeight();
            //x = 0 - stringSize.Width;
            //x = this.width + stringSize.Width;
        }

        private Timer updateTimer;
        String drawString = "文字很长时,就会超出这个矩形框,我想实现的功能是。我给你看看。。尽量不要在重绘的时候赋图片。重绘一般都画图神马的。。程序写好了不会导致闪烁的。状态栏走马灯似的移动文本,重绘代码发上来。我给你看看。";
        Font drawFont = new Font("宋体", 12);
        SizeF stringSize = new SizeF();
        SolidBrush drawBrush = new SolidBrush(Color.Black);
        float x = 0;
        float y = 200;
        //float width = 562;
        float width = 283;
        float height = 90;
       
        RectangleF drawRect = new RectangleF();
        //从左到右
        // private void OnTick()
        // {
        //    x++;
        //     if (x > this.width + stringSize.Width)
        //        x = 0 - stringSize.Width;
        //    drawRect.X = x;
        //    this.Invalidate();
        //}

        //从右到左
        private void OnTick()
        {
            //x--;
            y--;
            if (y < -drawRect.Height)
                y = 0 + 200 /*+ stringSize.Width*/;
            drawRect.X = x;
            drawRect.Y = y;
            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            stringSize = e.Graphics.MeasureString(drawString, drawFont);
            e.Graphics.DrawRectangle(new Pen(Color.Red, 1), drawRect.X, drawRect.Y, drawRect.Width, drawRect.Height);
            e.Graphics.DrawString(drawString, drawFont, drawBrush, drawRect);
            base.OnPaint(e);
        }

        private  float GetHeight()
        {
            Graphics g = this.CreateGraphics();

            Font font = new Font("宋体", 12);

            //设方框固定为600

            SizeF size = new SizeF(283, float.MaxValue);

            //打印的起点

            PointF p = new PointF(10, 10);

            int charNum, lineNum;
            float height = 0;

            //测量需要画几行

            g.MeasureString(drawString, font, size, StringFormat.GenericTypographic, out charNum, out lineNum);

            //获取字体的高度

            float fontHeight = font.GetHeight(g);

            //size = new SizeF(600, lineNum * fontHeight);

            //g.DrawRectangle(Pens.Red, 10, 10, size.Width, size.Height);

            //g.DrawString(content, font, Brushes.Blue, new RectangleF(p, size), StringFormat.GenericTypographic);
            height = (lineNum+1) * fontHeight;
            return height;


        }
    }
}



PictureBox继承类:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace gundong
{
    class NewPictureBox : System.Windows.Forms.PictureBox
    {
        public NewPictureBox()
        {

            this.updateTimer = new Timer();
            this.updateTimer.Interval = 30;
            this.updateTimer.Enabled = true;
            this.updateTimer.Tick += delegate { this.OnTick(); };
            //drawRect.X = x;
            drawRect.X = this.width + stringSize.Width;
            drawRect.Y = y;
            drawRect.Width = width;
            drawRect.Height = height;
            //x = 0 - stringSize.Width;
            x = this.width + stringSize.Width;
        }


        private Timer updateTimer;
        String drawString = "文字很长时,就会超出这个矩形框,我想实现的功能是,重绘代码发上来。我给你看看。";
        Font drawFont = new Font("宋体", 12);
        SizeF stringSize = new SizeF();
        SolidBrush drawBrush = new SolidBrush(Color.Black);
        float x = 0;
        float y = 2;
        //float width = 562;
        float width = 210;
        float height = 17;
        RectangleF drawRect = new RectangleF();
        //从左到右
        // private void OnTick()
        // {
        //    x++;
        //     if (x > this.width + stringSize.Width)
        //        x = 0 - stringSize.Width;
        //    drawRect.X = x;
        //    this.Invalidate();
        //}

        //从右到左
        private void OnTick()
        {
            x--;
            if (x < -stringSize.Width)
                x = 0 + this.width /*+ stringSize.Width*/;
            drawRect.X = x;
            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            stringSize = e.Graphics.MeasureString(drawString, drawFont);
            e.Graphics.DrawRectangle(new Pen(Color.Red, 1), drawRect.X, drawRect.Y, stringSize.Width, stringSize.Height);
            drawRect.Width = stringSize.Width;
            e.Graphics.DrawString(drawString, drawFont, drawBrush, drawRect);
            base.OnPaint(e);
        }
    }
}


Form1.Designer.cs

namespace gundong
{
    partial class Form1
    {
        /// <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()
        {
            this.newPictureBox = new gundong.NewPictureBox();
            this.newLabel = new gundong.NewLabel();
            ((System.ComponentModel.ISupportInitialize)(this.newPictureBox)).BeginInit();
            this.SuspendLayout();
            //
            // newPictureBox
            //
            this.newPictureBox.BackColor = System.Drawing.Color.White;
            this.newPictureBox.Location = new System.Drawing.Point(38, 32);
            this.newPictureBox.Name = "newPictureBox";
            this.newPictureBox.Size = new System.Drawing.Size(151, 218);
            this.newPictureBox.TabIndex = 2;
            this.newPictureBox.TabStop = false;
            //
            // newLabel
            //
            this.newLabel.BackColor = System.Drawing.Color.White;
            this.newLabel.Location = new System.Drawing.Point(337, 32);
            this.newLabel.Name = "newLabel";
            this.newLabel.Size = new System.Drawing.Size(284, 218);
            this.newLabel.TabIndex = 1;
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(664, 306);
            this.Controls.Add(this.newPictureBox);
            this.Controls.Add(this.newLabel);
            this.Name = "Form1";
            this.Text = "Form1";
            ((System.ComponentModel.ISupportInitialize)(this.newPictureBox)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        //private System.Windows.Forms.Label label1;
        private NewLabel newLabel;
        private NewPictureBox newPictureBox;

    }
}


效果图如下:



0 0
原创粉丝点击