Lua 中改变console的输出字体颜色
2015-12-31 16:56
459 查看
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”) 只能全屏改变颜色!
2. 调用win API:
#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) { // lua5.1API luaL_register(L, "test", libs); return 1; }
3. 编译为test.dll 然后和lua代码放在一起执行:
lua example:(续前) -- 设置绿色 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")
如图:
相关文章推荐
- 在Windows visual studio环境下编译Lua
- another lua vm
- Lua封装延时执行函数
- Lua封装将数字转换成万显示
- Lua封装打印Table函数
- Lua格式化字符串中的空格
- lua vm test
- 关于lua中的loadstring
- ALUA,AA,多路径
- freeswitch lua会议模块
- lua math.floor()的一个BUG
- LUA string库详解
- 【Lua】将Lua5.1的setfenv迁移到Lua5.2的_ENV
- lua 中 常用的 string 函数
- lua堆栈操作
- 关于lua中如何修改引用的值
- Lua简易教程
- Visual Sutdio2015 C++嵌入Lua脚本环境配置
- 在lua中如何remove掉table里面的数据
- C/C++,VS下使用luajit(环境配置)