日期:2014-05-17  浏览次数:21005 次

WonForm 字幕滚动
这几天做一个WinForm的东西 出现了好多问题、
 现在做个字幕向上滚动的窗体、弄来弄始终达不到要求、在窗体上放了两个Panel、顶端那个显示标题、底端那个显示时间等等、中间的信息要向上循环滚动、我放在一个GroupBox里的、但是滚上去总会盖住顶端的那个Panel,我又在中间放了一个Panel,再把GroupBox放在Panel里、让他在Panel里滚动、但是滚上去了就没有了、怎么能循环滚动啊、上边滚完了、下边又出来继续滚,还有就是说 GroupBox.Width=窗体.Width或者Panel.Width=窗体.Width能不能这样设啊、怎么没反应啊 废话有点多、怕说不清楚 能不能说下具体思路啊、谢谢、、、、、

------解决方案--------------------
C# code
Action 委托方法 = () => { 移动字幕.Left = 移动字幕.Left - 1; if (移动字幕.Right < 0) 移动字幕.Left = this.Width; };
委托方法();

------解决方案--------------------
添加以下类至代码页:
C# code
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class ScrollStringLib
{
    private Control _Ct;
    private Color _TextColor;
    private Bitmap _TextLayer;
    private System.Timers.Timer withEventsField__Timer = new System.Timers.Timer();
    private System.Timers.Timer _Timer {
        get { return withEventsField__Timer; }
        set {
            if (withEventsField__Timer != null) {
                withEventsField__Timer.Elapsed -= _Timer_Elapsed;
            }
            withEventsField__Timer = value;
            if (withEventsField__Timer != null) {
                withEventsField__Timer.Elapsed += _Timer_Elapsed;
            }
        }
    }
    private int _ScrollnIndex = 0;
    private Bitmap _tempBmp;
    public void ScrollStop()
    {
        _Timer.Stop();
    }
    public void ScrollStart(Control Ct, string Text, Color TextColor, int Speed)
    {
        _Ct = Ct;
        if (_TextLayer != null)
            _TextLayer.Dispose();
        Size Sz = Ct.CreateGraphics.MeasureString(Text, _Ct.Font, _Ct.Width).ToSize;
        Sz = new Size(Sz.Width + 10, Sz.Height + 10);
        _TextLayer = new Bitmap(Sz.Width, Sz.Height);
        using (Graphics G = Graphics.FromImage(_TextLayer)) {
            G.DrawString(Text, _Ct.Font, new SolidBrush(TextColor), new Rectangle(0, 0, Sz.Width, Sz.Height));
        }
        _Timer.Interval = Speed;
        _Timer.Start();
    }

    private void _Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        try {
            if (_tempBmp != null)
                _tempBmp.Dispose();
            _tempBmp = new Bitmap(_Ct.Width, _Ct.Height);
            using (Graphics G = Graphics.FromImage(_tempBmp)) {
                _ScrollnIndex -= 1;
                if (_TextLayer != null) {
                    if (_ScrollnIndex <= -_TextLayer.Height)
                        _ScrollnIndex = 0;
                    G.DrawImage(_TextLayer, new Rectangle(0, _ScrollnIndex, _Ct.Width, _Ct.Height), new Rectangle(0, 0, _Ct.Width, _Ct.Height), GraphicsUnit.Pixel);
                }
            }
            _Ct.BackgroundImage = _tempBmp;
        } catch {
        }
    }
}

------解决方案--------------------
探讨
可是里边的内容不随着我的窗体变化而变化啊、内容的排放是根据窗体宽度排放的、也设了控件的宽度等于窗体的宽度、现在拉宽窗体、内容没什么反应啊、

------解决方案--------------------
if (滚完了)
{
重新开始;
}