2013-01-14 49 views
1

我需要寫點東西來連續自動滾動單行文字TextBox。我是否需要滾動到插入符,然後將其設置爲第一個字符並重新開始?如何自動滾動單行文本框連續?

+0

[你有什麼試過?](http://whathaveyoutried.com) – Blachshma

+1

'PathText.SelectionStart = PathText.TextLength; PathText.ScrollToCaret(); PathText.Refresh();'問題是,它不是一個滾動的文本,它只是跳到結束,我不知道如何開始 –

+0

所以你正在嘗試做一個滾動滾動? –

回答

1

所以,我發現了一個大帳篷控制並對其進行修改一點點,並用它來代替文本框

public class Marquee : System.Windows.Forms.UserControl 
{ 
    private System.ComponentModel.IContainer components; 
    private System.Windows.Forms.Timer timer1; 

    private int currentPos = 0; 
    private bool mBorder; 
    private string mText; 

    public string MarqueeText 
    { 
     get { return mText; } 
     set { mText = value; } 
    } 

    public bool Border 
    { 
     get { return mBorder; } 
     set { mBorder = value; } 
    } 

    public int Interval 
    { 
     get { return timer1.Interval * 10; } 
     set { timer1.Interval = value/10; } 
    } 


    public Marquee() 
    { 
     // This call is required by the Windows.Forms Form Designer. 
     InitializeComponent(); 

     // TODO: Add any initialization after the InitForm call 
     this.Size = new Size(this.Width, this.Font.Height); 
    } 

    /// <summary> 
    /// Clean up any resources being used. 
    /// </summary> 
    protected override void Dispose(bool disposing) 
    { 
     if (disposing) 
     { 
      if (components != null) 
       components.Dispose(); 
     } 
     base.Dispose(disposing); 
    } 

    #region Component 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.components = new System.ComponentModel.Container(); 
     this.timer1 = new System.Windows.Forms.Timer(this.components); 
     // 
     // timer1 
     // 
     this.timer1.Enabled = true; 
     this.timer1.Interval = 1000; 
     this.timer1.Tick += new System.EventHandler(this.timer1_Tick); 
     // 
     // Marquee 
     // 
     this.Name = "Marquee"; 
     this.Size = new System.Drawing.Size(150, 136); 
     this.Resize += new System.EventHandler(this.Marquee_Resize); 

    } 
    #endregion 

    private void Marquee_Resize(object sender, System.EventArgs e) 
    { 
     this.Height = this.Font.Height; 
    } 

    private void timer1_Tick(object sender, System.EventArgs e) 
    { 
     this.Invalidate(); 
    } 

    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) 
    { 

     if (mBorder) 
     { 
      e.Graphics.DrawRectangle(new Pen(this.ForeColor), 0, 0, this.Width - 1, this.Height - 1); 
     } 

     float dif = e.Graphics.MeasureString(mText, this.Font).Width- this.Width; 
     if (this.Width < e.Graphics.MeasureString(mText, this.Font).Width) 
     { 
      e.Graphics.DrawString(mText, this.Font, new SolidBrush(this.ForeColor), 
       currentPos, 0); 
      e.Graphics.DrawString(mText, this.Font, new SolidBrush(this.ForeColor), 
       this.Width + currentPos + dif, 0); 

      currentPos--; 

      if ((currentPos < 0) && (Math.Abs(currentPos) >= e.Graphics.MeasureString(mText, this.Font).Width)) 
       currentPos = this.Width + currentPos; 
     } 
     else 
     { 
      e.Graphics.DrawString(mText, this.Font, new SolidBrush(this.ForeColor),-dif/2, 0); 
     } 

    } 
} 

因此,如果文本寬度比控制的寬度長的文本將在別的靜態的文字會自動滾動