65.9K
CodeProject 正在变化。 阅读更多。
Home

C# 滚动文本控件

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.13/5 (13投票s)

2003年2月28日

2分钟阅读

viewsIcon

177903

downloadIcon

9935

本文档展示了如何在 C# 中创建一个滚动文本/滚动标题控件。

The DougScrollingTextCtrl in an app.

引言

本文旨在展示创建 C# 滚动文本控件是多么容易。

背景

在阅读了 Alexandr Khilov 的文章 编写自定义控件:循序渐进 之后,我想尝试自己制作一个 C# 控件。我对 C# 和 Visual Studio.NET 来说都是新手,所以最好的学习方法就是直接开始。这是我制作的第一个控件,如果我有更多时间,我会发布其他的。

代码

要创建自己的自定义控件,请打开 Visual Studio 并启动一个新项目。您的项目必须基于 C# Windows 控件库模板。将您的项目命名为 DougScrollingTextCtrl(或者您想要的任何名称),然后单击“确定”。

以下是完整的 DougScrollingTextCtrl 代码列表。

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace DougScrollingText
{
    /// <summary>
    /// Summary description for DougScrollingTextCtrl.
    /// </summary>
    
    //////////////////////////////////////////////////////////////////
    //
    // Function: class DougScrollingTextCtrl.
    //
    // By: Doug 
    //
    // Date: 2/27/03
    //
    // Description: Create the control and derive 
    // it from System.Windows.Forms.Control.
    //
    ///////////////////////////////////////////////////////////////////
    //
    public class DougScrollingTextCtrl : System.Windows.Forms.Control
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null; 
        private Color m_Color1 = Color.Black;  // First default color.
        private Color m_Color2 = Color.Gold;   // Second default color.
        private Font m_MyFont;   // For the font. 
        protected Timer m_Timer; // Timer for text animation.
        protected string sScrollText = null; // Text to be displayed 
                      // in the control.
        
        /// <summary>
        /// Add member variables.
        /// </summary> 


        ///////////////////////////////////////////////////////////////////
        //
        // Function: public DougScrollingTextCtrl()
        //
        // By: Doug 
        //
        // Date: 2/27/03
        //
        // Description: Constructor.
        //
        /////////////////////////////////////////////////////////////////// 
        //
        public DougScrollingTextCtrl()
        {
            m_Timer = new Timer(); 
            // Set the timer speed and properties.
            m_Timer.Interval = 250;
            m_Timer.Enabled = true;
            m_Timer.Tick += new EventHandler( Animate );
        } 
        // Add a color property.
        public Color DougScrollingTextColor1
        {
            get { return m_Color1; }
            set 
            {
                m_Color1 = value; 
                Invalidate();
            }
        } 
        // Add a color property.
        public Color DougScrollingTextColor2
        {
            get { return m_Color2; }
            set 
            {
                m_Color2 = value; 
                Invalidate();
            }
        } 
        ////////////////////////////////////////////////////////////////////
        //
        // Function: Animate( object sender, EventArgs e )
        //
        // By: Doug 
        //
        // Date: 2/27/03
        //
        // Description: Sets up the animation of the text.
        //
        /////////////////////////////////////////////////////////////////
        //
        void Animate( object sender, EventArgs e )
        {
            // sScrollText string is from the Text 
            // property, add 4 spaces after the string so 
            // everything is not bunche together.
            if( sScrollText == null )
            {
                sScrollText = Text + "    ";
            } 
            // Scroll text by triming one character at a time 
            // from the left, then adding that character to the 
           // right side of the control to make it look like scrolling text.
            sScrollText = sScrollText.Substring( 1, 
                sScrollText.Length-1 ) + sScrollText.Substring( 0, 1 );
            
            // Call Invalidate() to tell the windows form that
           // our control needs to be repainted.
            Invalidate();
        } 
        ///////////////////////////////////////////////////////////////////
        //
        // Function: StartStop( object sender, EventArgs e )
        //
        // By: Doug 
        //
        // Date: 2/27/03
        //
        // Description: Start and stop the timer.
        //
        /////////////////////////////////////////////////////////////////
        //
        void StartStop( object sender, EventArgs e )
        {
            m_Timer.Enabled = !m_Timer.Enabled;
        } 
        ////////////////////////////////////////////////////////////////////
        //
        // Function: protected override void OnTextChanged( EventArgs e )
        //
        // By: Doug 
        //
        // Date: 2/27/03
        //
        // Description: If/when the string text is changed, 
        // I need to update the sScrollText string.
        //
        ////////////////////////////////////////////////////////////////////
        //
        protected override void OnTextChanged( EventArgs e )
        {
            sScrollText = null; 
            base.OnTextChanged( e );
        } 
        ////////////////////////////////////////////////////////////////////
        //
        // Function: protected override void OnClick( EventArgs e )
        //
        // By: Doug 
        //
        // Date: 2/27/03
        //
        // Description: Handle the click event of the DougScrollingTextCtrl.
        //
        /////////////////////////////////////////////////////////////////////
        //
        protected override void OnClick( EventArgs e )
        {
            m_Timer.Enabled = !m_Timer.Enabled; 
            base.OnClick( e );
        } 
        //////////////////////////////////////////////////////////////////
        //
        // Function: protected override void OnPaint( PaintEventArgs pe )
        //
        // By: Doug 
        //
        // Date: 2/27/03
        //
        // Description: Paint the DougScrollingTextCtrl.
        //
        ////////////////////////////////////////////////////////////////
        //
        protected override void OnPaint( PaintEventArgs pe )
        {
            // This is a fancy brush that draws graded colors.
            Brush MyBrush = 
                new System.Drawing.Drawing2D.LinearGradientBrush( 
                  ClientRectangle, m_Color1, m_Color2, 10 ); 
            // Get the font and use it to draw text in the control.  
            // Resize to the height of the control if possible.
            m_MyFont = new Font( Font.Name, (Height*3)/4, 
                Font.Style, GraphicsUnit.Pixel ); 
            // Draw the text string in the control.
            pe.Graphics.DrawString( sScrollText, m_MyFont, MyBrush, 0, 0 ); 
            base.OnPaint (pe); 
            // Clean up variables..
            MyBrush.Dispose(); 
            m_MyFont.Dispose();
        }
    }
} 

要编译您的新控件,只需按 ctrl+shift+b。您会看到您的新控件现在已准备好添加到应用程序中。

使用代码

实现您的新控件/代码应该非常容易。首先,在您的目标 C# 应用程序打开时,右键单击工具箱,选择“自定义工具箱”,选择“.NET Framework 组件”选项卡,单击“浏览”并找到控件库 DLL,例如:C:\\ ... DougScrollingTextCtrl\ ... \DougScrollingTextCtrl.dll。 组件 DougScrollingTextCtrl 现在将出现在工具箱中。现在您所要做的就是找到工具箱底部的 DougScrollingTextCtrl 并将其拖到您的应用程序上,或者您可以直接将以下代码添加到您的 form1.cs 类中。

如果您手动对控件进行硬编码(而不是从工具箱拖动它),请使用下面的代码。如果您从工具箱拖动控件,则忽略此代码片段。

// Add this line to the control decloration of your app.
private DougScrollingText.DougScrollingTextCtrl m_DougScrollingTextCtrl;

//Add these lines to your InitializeComponent() function call.
this.m_DougScrollingTextCtrl = 
  new DougScrollingText.DougScrollingTextCtrl(); 
//
// m_DougScrollingTextCtrl
//
this.m_DougScrollingTextCtrl.DougScrollingTextColor1 = 
  System.Drawing.Color.Crimson;
this.m_DougScrollingTextCtrl.DougScrollingTextColor2 = 
  System.Drawing.Color.Gold;
this.m_DougScrollingTextCtrl.Location = new System.Drawing.Point(24, 264);
this.m_DougScrollingTextCtrl.Name = "m_DougScrollingTextCtrl";
this.m_DougScrollingTextCtrl.Size = new System.Drawing.Size(240, 32);
this.m_DougScrollingTextCtrl.TabIndex = 7;
this.m_DougScrollingTextCtrl.Text = "Go Redskins!"; 
// Don't forget to add the control to your apps 
// this.Controls.AddRange(new System.Windows.Forms.Control[]{ 
// listing.
m_DougScrollingTextCtrl 

关注点

在 C# 中创建自己的控件时,请记住添加属性,以便您自己和其他人都可以轻松使用它们。

历史

  • 初始版本。DougScrollingTextCtrl 版本 1.0.0 2/28/03
© . All rights reserved.