#include <stdio.h>
#include <string.h>
#include <unistd.h> // usleep
void scroll_left_array(char *buf, size_t len, unsigned int delay_ms) {
char temp[len + 1];
strcpy(temp, buf);
while (1) {
printf("\r%s", temp);
fflush(stdout);
usleep(delay_ms * 1000);
// 左移一位
char first = temp[0];
memmove(temp, temp + 1, len - 1);
temp[len - 1] = first;
temp[len] = '\0';
}
}
int main() {
char msg[] = "Hello, World! Welcome to C programming. ";
scroll_left_array(msg, strlen(msg), 200);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <unistd.h>
void scroll_left_ring(const char *msg, size_t msg_len, unsigned int delay_ms) {
size_t pos = 0;
while (1) {
// 輸出從 pos 開始的 msg_len 字符 (環形)
for (size_t i = 0; i < msg_len; ++i) {
putchar(msg[(pos + i) % msg_len]);
}
printf("\r");
fflush(stdout);
usleep(delay_ms * 1000);
pos = (pos + 1) % msg_len;
}
}
#include <stdio.h>
#include <unistd.h>
void scroll_left_ansi(const char *msg, size_t width, unsigned int delay_ms) {
// 隱藏游標
printf("\033[?25l");
// 清除行並移至行首
printf("\033[2K\r");
size_t len = strlen(msg);
size_t pos = 0;
while (1) {
printf("\033[2K\r"); // 清除整行
// 輸出可見寬度內的字符
for (size_t i = 0; i < width; ++i) {
putchar(msg[(pos + i) % len]);
}
fflush(stdout);
usleep(delay_ms * 1000);
pos = (pos + 1) % len;
}
// 顯示游標 (程式結束時)
// printf("\033[?25h");
}
Qt: `QScrollBar` + `QTimer` / `QPropertyAnimation` 對 `QLabel` 位移。
Win32: `CreateWindowEx(WS_EX_CLIENTEDGE, "STATIC", ..., SS_LEFT | SS_OWNERDRAW)` + `WM_TIMER` + `TextOut` / `DrawText` 繪製偏移文本。