别人的弹出公告会显示我也想要 点击就会进入对应功能是怎么做的
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-07-22 12:58:04 |
👁️ 阅读: 14
要实现弹出公告点击进入对应功能,主要使用富文本控件和超链事件监听。以下是实现步骤:
## 1. 创建支持超链的富文本控件
```lua
-- 创建父节点
local parent = GUI:Win_Create("公告父节点", 0, 0, 0, 0, false, false, true, false, false, false, 1)
-- 创建支持<font>和<a>标签的富文本控件
local richText = GUI:RichText_Create(parent, "richText", 100, 100, "", 1000, 20, "#ffffff")
-- 设置富文本内容(包含超链)
local message = "<font color='#FF33FF'>系统公告:</font><a href='功能#1'><font color='#00FF00'>点击这里进入功能1</font></a>"
GUI:RichText_setString(richText, message)
```
## 2. 注册超链点击事件
```lua
-- 注册超链点击事件监听
SL:RegisterLUAEvent(LUA_EVENT_RICHTEXT_OPEN_URL, "公告处理", function(...)
local args = {...}
local href = args[1] or "" -- 获取超链地址
SL:Print("点击了超链:", href)
-- 解析超链参数
if string.find(href, "功能#") then
local funcId = string.match(href, "功能#(%d+)")
if funcId then
-- 根据功能ID执行对应操作
if funcId == "1" then
-- 打开功能1界面
SL:OpenCommonTipsUI({
str = "进入功能1界面",
btnType = 1,
btnDesc = {"确定"}
})
elseif funcId == "2" then
-- 打开功能2界面
SL:PlaySound(1001) -- 播放音效
end
end
end
end)
```
## 3. 服务端发送带超链的消息
```lua
-- 服务端脚本示例(QFunction-0.lua)
function triggerchat(actor, sMsg, chat,...)
-- 发送带超链的聊天消息
sendmsg(actor, 2, '{"Msg":"<a href=\'功能#1\'><font color=\'#FF33FF\'>点击进入功能1</font></a>","Type":1}')
end
```
## 4. 公告弹窗实现
```lua
-- 创建公告弹窗背景
local bg = GUI:Image_Create(parent, "公告背景", 300, 300, "res/public_win32/1900000610.png")
-- 添加确认按钮
local btn = GUI:Button_Create(bg, "确认按钮", 200, 50, "res/public_win32/1900000612.png")
GUI:Button_setTitleText(btn, "我知道了")
GUI:addOnClickEvent(btn, function()
GUI:removeFromParent(bg) -- 关闭弹窗
end)
```
## 关键点说明:
1. **RichText_Create**:必须使用这个函数创建富文本,才能支持`<a>`和`<font>`标签
2. **超链格式**:`<a href='功能#ID'>描述文本</a>`
3. **事件监听**:使用`SL:RegisterLUAEvent`监听`LUA_EVENT_RICHTEXT_OPEN_URL`事件
4. **参数解析**:从超链地址中解析功能ID,执行对应操作
这样就能实现点击公告中的链接进入对应功能的效果了。
← 返回问答库