您的位置:首页 > 编程语言 > Lua

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")


如图:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: