滚动的文本内容库

zhhyit 21天前 291

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

单个实例

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,方便使用

最新回复 (4)
  • demo 21天前
    0 2
    这个做跑马灯有实际用途,可惜都是失败555
  • 阿法牛 2天前
    0 3
    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}
    )
    /*}}*/
    
    
    import raw
    import console
    
    // 获取标签控件
    var label = winform.label;
    
    // 设置标签样式 - 单行且不自动换行
    label.style = 0x2000/*_SS_LEFTNOWORDWRAP*/;
    
    // 初始化变量
    var text = "输入的需要a滚动c的b文本内容"; 
    console.log(#text)
    var scrollText = text + "     "; // 添加一些空格作为间隔
    var scrollPos = 0; // 当前滚动位置
    
    
    
    // 将字符串转换为字符数组(兼容方式处理Unicode)
    var chars = {};
    
    for m in string.gmatch(scrollText ,"<:>|<.>"){ 
        //console.log(m)
        table.push(chars,m)
    }
    console.dumpJson(chars)
    
    var totalChars = #chars;
    var maxChars = totalChars
    // 创建定时器
    var timer = win.timer( winform );
    timer.setInterval(200) 
    timer.onTimer = function(hwnd,msg,id,tick){
    	
        scrollPos = scrollPos + 1; // 中文每次滚动1个字符更合适
        if(scrollPos >= totalChars) {
            scrollPos = 0;
        }
        
        // 构建显示文本
        var displayChars = {};
        for(i=1;maxChars;1){
            var idx = (scrollPos + i) % totalChars;
            table.push(displayChars, chars[idx]); // 数组索引从1开始
        }
    
        var displayText = string.join(displayChars)
        label.text = displayText;
     
    } 
    
    timer.enable();
    
    // 显示窗口
    winform.show();
    
    // 进入消息循环
    win.loopMessage();


    但是我不会封装

  • 光庆 1天前
    0 4

    import win.ui;
    /*DSG{{*/
    var winform = win.form(text="aardio form";right=379;bottom=74)
    winform.add(
    plus={cls="plus";left=15;top=15;right=370;bottom=60;bgcolor=0xFFFFFF;font=LOGFONT(h=-21;name='黑体');z=1}
    )
    /*}}*/
    
    winform.show();
    
    var text = "我是滚动文本"
    var w,x = winform.plus.measureString(text).width,0;
    var brush = gdip.solidBrush(0xFFFF0000);
    var format = gdip.stringformat();
    format.lineAlign = 1/*_StringAlignmentCenter*/ ;
    
    winform.plus.onDrawContent = function(graphics,rc,txtColor,rcContent,foregroundColor,font){
    	graphics.clear(0xFFF0E68C);
    	graphics.textRenderingHint = 4/*_TextRenderingHintAntiAlias*/;
    	graphics.drawString(text,font,::RECTF(x,2,w,rc.bottom),format,brush);
    	x -= 1;
    	if x<-w x=rc.right;
    }
    
    winform.plus.setInterval( 
    	function(){
    		winform.plus.redraw()
    	},10 
    )
    
    win.loopMessage();


  • mfk 1天前
    0 5

返回