二维码生成控件:自动版本选择、前景/背景色、中心 Logo 嵌入。
QRCode 控件生成并显示二维码:
• Value:编码内容(URL/文本/JSON)
• 自动版本:根据数据长度自动选择 QR 版本(1-40)
• 颜色:前景色(模块)+ 背景色
• Logo:中心嵌入图标(Surface + 尺寸)
• Padding:二维码周围留白
| 函数 | 说明 |
|---|---|
int xuiQrCodeCreate(pCtx, ppW, pDesc) | 创建二维码 |
int xuiQrCodeSetValue(pW, sValue) | 设置内容 |
const char* xuiQrCodeGetValue(pW) | 获取内容 |
int xuiQrCodeSetColors(pW, iFg, iBg) | 前景/背景色 |
int xuiQrCodeSetPadding(pW, fPadding) | 留白 |
int xuiQrCodeSetIcon(pW, pSurface, tSrc, fSize) | 中心 Logo |
xui_surface xuiQrCodeGetIcon(pW) | 获取 Logo |
float xuiQrCodeGetIconSize(pW) | Logo 尺寸 |
gcc -O2 -I. -o ch189.exe ch189_main.c src\xui_*.c build\xge.lib -lm -lws2_32 -liphlpapi -lgdi32 -luser32 -lshell32 -lopengl32 -lole32 -lwinmm -lavrt
.\ch189.exe
xui_qrcode_desc_t desc = { 0 };
xui_widget pQR;
xuiQrCodeCreate(pCtx, &pQR, &desc);
// 编码 URL
xuiQrCodeSetValue(pQR, "https://xge.dev/download");
// 深色前景 + 白色背景
xuiQrCodeSetColors(pQR, 0x1A1A2EFF, 0xFFFFFFFF);
// 留白 8px
xuiQrCodeSetPadding(pQR, 8.0f);
// 中心 Logo(占 QR 宽度 20%)
xui_rect_t tSrc = { 0, 0, 64, 64 };
xuiQrCodeSetIcon(pQR, pLogoSurface, tSrc, 0.2f);
xuiWidgetAddChild(pPanel, pQR);将以下代码保存为 ch189_main.c,编译后运行即可看到二维码生成效果:
#include "xge.h"
#include "xui.h"
static xui_context g_pCtx;
static int onFrame(void* pUser) {
(void)pUser;
xgeClear(0xFFFFFFFF);
xui_vec2_t pos;
xgeMouseGet(&pos.x, &pos.y);
xuiInputPointerMove(g_pCtx, pos.x, pos.y);
if (xgeMouseDown(XGE_MOUSE_LEFT))
xuiInputPointerDown(g_pCtx, pos.x, pos.y, 0);
else
xuiInputPointerUp(g_pCtx, pos.x, pos.y, 0);
xuiUpdate(g_pCtx, xgeGetDelta());
xuiLayout(g_pCtx);
xuiRender(g_pCtx, NULL, NULL, 0);
return XGE_OK;
}
int main(void) {
xge_desc_t d = { 0 };
d.iWidth = 400; d.iHeight = 400;
d.sTitle = "ch189 - QRCode";
d.iFlags = XGE_INIT_VSYNC;
xgeInit(&d);
xuiCreate(&g_pCtx);
extern const xui_proxy_t tXgeProxy;
xui_proxy_t tXgeProxy = xuiProxyXge();
xuiSetProxy(g_pCtx, &tXgeProxy);
xuiSetViewportSize(g_pCtx, 400, 400);
xui_qrcode_desc_t qdesc = { 0 };
xui_widget pQR;
xuiQrCodeCreate(g_pCtx, &pQR, &qdesc);
// 编码 URL
xuiQrCodeSetValue(pQR, "https://xge.dev/download");
// 深色前景 + 白色背景
xuiQrCodeSetColors(pQR, 0x1A1A2EFF, 0xFFFFFFFF);
// 留白 8px
xuiQrCodeSetPadding(pQR, 8.0f);
xui_widget pRoot = xuiGetRootWidget(g_pCtx);
xuiWidgetAddChild(pRoot, pQR);
xuiWidgetSetRect(pQR, (xui_rect_t){0, 0, 300, 300});
/* 位置已含于上方 SetRect */
xgeRun(onFrame, NULL);
xuiDestroy(g_pCtx);
xgeUnit();
return 0;
}