滚动的文本内容库

zhhyit 7天前 128

封装一个实例成用户库,目前库有问题

单个实例

import win.ui;
import win.timer;
/*DSG{{*/
var winform = win.form(text="文本滚动示例";right=600;bottom=400;bgcolor=0x808000)
winform.add(
label={cls="static";left=20;top=30;right=580;bottom=60;bgcolor=0x000000;color=0x0000FF;fgcolor=16777215;font=LOGFONT(h=-21;name='微软雅黑');notify=1;z=1}
)
/*}}*/

// 获取标签控件
var label = winform.label;

// 设置标签样式 - 单行且不自动换行
label.style = 0x2000/*_SS_LEFTNOWORDWRAP*/;

// 初始化变量
var text = "输入的需要滚动的文本内容"; 
var scrollText = text + "     "; // 添加一些空格作为间隔
var scrollPos = 0; // 当前滚动位置
var maxChars = math.floor(label.getRect().width() / 14); // 估算字符数

// 创建定时器
var timer = win.timer( winform );
timer.setInterval(200) 
timer.onTimer = function(hwnd,msg,id,tick){
    // 计算新的文本显示内容
    scrollPos = scrollPos + 1;
    if(scrollPos > #scrollText) {
        scrollPos = 0;
    }
    
    // 显示部分文本(模拟滚动效果)
    var displayText = string.sub(scrollText, scrollPos) + string.sub(scrollText, 1, scrollPos);
    
    // 截取适当长度的文本以适应控件
/*
    if(#displayText > maxChars) {
        displayText = string.sub(displayText, 1, maxChars);
    }
    
*/
    label.text = displayText;	
} 

timer.enable();

// 显示窗口
winform.show();

// 进入消息循环
win.loopMessage();



封装库textScroller.aardio,失败

// textScroller.aardio
namespace textScroller {
    
    class scrollLabel {
        ctor(ctrl, text, interval, spacing) {
            this.ctrl = ctrl;
            this.originalText = text;
            this.interval = interval || 200;
            this.spacing = spacing || "     ";
            this.scrollPos = 0;
            this.timer = null;
            this.isScrolling = false;
            
            // 设置控件样式
            this.ctrl.style = 0x2000/*_SS_LEFTNOWORDWRAP*/;
            
            // 初始化滚动文本
            this.scrollText = text + this.spacing;
        }
        
        start = function() {
            if(this.isScrolling) return;
            
            if(!this.timer) {
                this.timer = win.timer(this.ctrl.parent);
                this.timer.setInterval(this.interval);
                var self = this;
                this.timer.onTimer = function() {
                    self.scrollPos = self.scrollPos + 1;
                    if(self.scrollPos > #self.scrollText) {
                        self.scrollPos = 0;
                    }
                    
                    var displayText = string.sub(self.scrollText, self.scrollPos) + string.sub(self.scrollText, 1, self.scrollPos);
                    self.ctrl.text = displayText;
                };
            }
            
            this.timer.enable();
            this.isScrolling = true;
        }
        
        stop = function() {
            if(this.timer && this.isScrolling) {
                this.timer.disable();
                this.isScrolling = false;
            }
        }
        
        setText = function(text) {
            this.originalText = text;
            this.scrollText = text + this.spacing;
            this.scrollPos = 0;
            this.ctrl.text = this.originalText;
        }
        
        setInterval = function(interval) {
            this.interval = interval;
            if(this.timer) {
                this.timer.setInterval(interval);
            }
        }
        
        setSpacing = function(spacing) {
            this.spacing = spacing;
            this.scrollText = this.originalText + spacing;
        }
    }
    
    // 创建滚动标签的便捷函数
    create = function(ctrl, text, interval, spacing) {
        return scrollLabel(ctrl, text, interval, spacing);
    }
}

库调用,失败

import win.ui;
import textScroller; // 导入我们的用户库

/*DSG{{*/
var winform = win.form(text="文本滚动示例";right=600;bottom=400;bgcolor=0x808000)
winform.add(
    label={cls="static";left=20;top=30;right=580;bottom=60;bgcolor=0x000000;color=0x0000FF;fgcolor=16777215;font=LOGFONT(h=-21;name='微软雅黑');notify=1;z=1}
)
/*}}*/

// 使用文本滚动库
var scroller = textScroller.scrollLabel(winform.label, "输入的需要滚动的文本内容");
scroller.start();

// 显示窗口
winform.show();

// 进入消息循环
win.loopMessage();


哪位大佬能风重新封装一下textScroller,方便使用

最新回复 (1)
  • demo 7天前
    0 2
    这个做跑马灯有实际用途,可惜都是失败555
返回