lua终端控制输出不同颜色

windows下:

1.调用系统函数: os.execute([command])

    This function is equivalent to the C functionsystem. It passescommand to be executed by an operating system shell. It returns a status code, which is system-dependent. Ifcommand is absent, then it returns nonzero if a shell is available and zero otherwise.

颜色属性由两位十六进制数字指定,第一个为背景色,第二个则为前景色。
每个数字可以为以下任何值之一:   

    0 = 黑色        8 = 灰色
    1 = 蓝色        9 = 淡蓝色
    2 = 绿色        A = 淡绿色
    3 = 湖蓝色     B = 淡浅绿色
    4 = 红色        C = 淡红色
    5 = 紫色        D = 淡紫色
    6 = 黄色        E  = 淡黄色
    7 = 白色        F  = 亮白色

os.execute("color 0C") 只能全屏改变颜色

  1. -- 设置绿色  
  2. os.execute("color 0A")  
  3. print("test start")  
  4.   
  5. -- 设置紫色  
  6. os.execute("color 0D")  
  7. print("test resume")  
  8.   
  9. os.execute("pause")  
-- 设置绿色
os.execute("color 0A")
print("test start")

-- 设置紫色
os.execute("color 0D")
print("test resume")

os.execute("pause")


如图:

       
 
2.调用win API:
  1. #include <Windows.h>   
  2. #include <lua.hpp>   
  3.   
  4. #pragma comment(lib, "lua5.1.lib")   
  5.   
  6. static int set_color(lua_State *L)  
  7. {  
  8.     WORD color = (WORD)luaL_checknumber(L, -1);  
  9.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);  
  10.     return 0;  
  11. }  
  12.   
  13. static luaL_Reg libs[] = {  
  14.     {"set_color", set_color},  
  15.     {NULL, NULL}  
  16. };  
  17.   
  18. extern "C"  
  19. _declspec(dllexport)  
  20. int luaopen_test(lua_State *L)  
  21. {  
  22.     luaL_register(L, "test", libs);  
  23.     return 1;  
  24. }  
#include <Windows.h>
#include <lua.hpp>

#pragma comment(lib, "lua5.1.lib")

static int set_color(lua_State *L)
{
	WORD color = (WORD)luaL_checknumber(L, -1);
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
	return 0;
}

static luaL_Reg libs[] = {
	{"set_color", set_color},
	{NULL, NULL}
};

extern "C"
_declspec(dllexport)
int luaopen_test(lua_State *L)
{
	luaL_register(L, "test", libs);
	return 1;
}

 编译为test.dll  然后和lua代码放在一起执行:

lua example:(续前)

  1. -- 设置绿色  
  2. os.execute("color 0A")  
  3. print("test start")  
  4.   
  5. -- 设置紫色  
  6. os.execute("color 0D")  
  7. print("test resume")  
  8.   
  9. require"test"  
  10. -- 浅绿色  
  11. test.set_color(0x000B)  
  12.   
  13. print("calll dll print")  
  14.   
  15. os.execute("pause")  
-- 设置绿色
os.execute("color 0A")
print("test start")

-- 设置紫色
os.execute("color 0D")
print("test resume")

require"test"
-- 浅绿色
test.set_color(0x000B)

print("calll dll print")

os.execute("pause")


如图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值