7 Commits

Author SHA1 Message Date
霜霜
e4851e16f4 Update version 2.1.0 release date in CHANGELOG
Updated release date for version 2.1.0 and clarified features.
2025-10-27 15:00:44 +08:00
Ysm-04
dcf13895da feat: add a new awesome feature 2025-10-27 14:59:29 +08:00
Ysm-04
95149238e2 feat: add a new awesome feature 2025-10-03 16:34:58 +08:00
Ysm-04
3509b2bc39 feat: add a new awesome feature 2025-10-01 00:57:41 +08:00
Ysm-04
26f30cee39 feat: add a new awesome feature 2025-09-30 19:26:01 +08:00
Ysm-04
741987e48b feat: add a new awesome feature 2025-09-22 20:39:34 +08:00
Ysm-04
9226deaf85 feat: add a new awesome feature 2025-09-22 16:51:14 +08:00
30 changed files with 3927 additions and 442 deletions

685
API 文档.md Normal file
View File

@@ -0,0 +1,685 @@
# 中文 API 文档
[English API Documentation](English API Documentation.md)
下面是 **StellarX GUI 框架** 各主要类和函数的 API 文档。每个类的用途、接口、参数和返回值等详细信息如下:
### Control 类 (抽象基类)
**描述:** `Control` 是所有控件的抽象基类,定义了通用的属性和接口,包括位置、尺寸、重绘标记等。它提供了绘图状态保存与恢复机制,确保控件绘制不影响全局状态。`Control` 本身不能直接实例化。
- **主要属性:**
- `x, y`:控件左上角坐标。
- `width, height`:控件宽度和高度。
- `dirty`:是否需要重绘的标志。
- `show`:控件是否可见。
- `rouRectangleSize``StellarX::RouRectangle` 结构,保存圆角矩形控件圆角的宽高半径。
- **注意:** `Control` 会在内部维护当前绘图状态(字体、颜色、线型等)的备份指针,用于绘制前保存状态、绘制后恢复。
- **主要接口方法:**
- `virtual void draw() = 0;`
**描述:** 纯虚函数,绘制控件内容。具体控件类需实现自己的绘制逻辑。
- `virtual bool handleEvent(const ExMessage& msg) = 0;`
**描述:** 纯虚函数,处理事件消息(如鼠标、键盘事件)。返回值表示事件是否被该控件消费(`true` 则不再向其他控件传播)。
- `void saveBackground(int x, int y, int w, int h);`
**描述:** 保存控件区域 `(x, y, w, h)` 的背景图像快照,以便需要时恢复背景。常用于控件需要暂时覆盖背景(如弹出框)。
- `void restBackground();`
**描述:** 恢复最近保存的背景快照,将之前保存的背景图像绘制回控件原位置。典型用于控件隐藏或重绘前清除旧内容。
- `void discardBackground();`
**描述:** 丢弃当前保存的背景快照并释放相关资源。当窗口重绘或尺寸变化导致背景失效时调用,避免错误使用旧快照。
- *属性访问和设置:*
- `int getX() const, getY() const, getWidth() const, getHeight() const` 等:获取控件位置和尺寸各属性。
- `int getRight() const, getBottom() const`:获取控件右边界和下边界坐标(`x + width``y + height`)。
- `void setX(int nx), setY(int ny), setWidth(int w), setHeight(int h)`:设置控件位置或尺寸,对应属性改变后自动将控件标记为需要重绘 (`dirty = true`)。
- `void setDirty(bool d)`:手动设置控件的重绘标志。
- `void setShow(bool visible)`:设置控件可见性。如果设为 `false`,则控件将不绘制自身内容。
- `bool isVisible() const`:返回控件当前可见状态(`show` 标志)。**注意:** 控件被标记为隐藏时,其 `draw()` 通常不会被调用。
- *其他:*
- `virtual bool model() const = 0;`
**描述:** 纯虚函数,用于检查控件是否为“模态”。仅对话框控件需要实现(模态对话框返回 `true`),其他非对话框控件可忽略(返回 `false`)。窗口事件循环根据此函数区分模态对话框与普通控件的事件处理优先级。
- `void saveStyle(), restoreStyle();` *(受保护方法)*
**描述:** 保存当前全局绘图样式(字体、颜色、线型等)并恢复。控件在 `draw()` 中应先调用 `saveStyle()` 备份当前样式,绘制完毕后调用 `restoreStyle()` 以免影响其他绘制操作。
### Window 类 (应用主窗口)
**描述:** `Window` 类表示应用程序的主窗口,负责窗口的创建、消息循环、控件管理和整体渲染。一个应用通常创建一个 Window 实例作为 GUI 的根容器。
- **构造函数:**
- `Window(int width, int height, int mode, COLORREF bkColor = ..., std::string headline = "窗口")`
创建一个指定宽、高的主窗口。`mode` 参数指定图形模式如是否双缓冲EasyX 使用 NULL 或 `INIT_RENDERMANUAL` 等模式),`bkColor` 是窗口背景色,`headline` 是窗口标题文本。构造时不会立即显示窗口,需调用 `draw()`
- **主要方法:**
- `void draw();`
**描述:** 初始化并显示窗口,创建绘图界面。将使用设定的模式创建绘图窗口(调用 EasyX 的 `initgraph`),并应用窗口标题和背景色。然后绘制已添加的控件。通常在创建 Window 后立即调用一次。
- `void draw(std::string pImgFile);`
**描述:** 与无参版本类似,但使用指定路径的图像文件作为窗口背景图。该方法会加载图像并按照窗口尺寸绘制为背景,然后再绘制子控件。
- `void runEventLoop();`
**描述:** 进入窗口消息处理循环。该循环持续获取用户输入事件 (`peekmessage`) 并将其分发给子控件或对话框处理:
- 优先将事件传递给所有已打开的非模态对话框(`Dialog``model() == false``isVisible() == true`)。一旦某个对话框的 `handleEvent` 返回 `true`(事件被消费),停止继续传递。
- 若事件未被对话框消费,再按添加顺序将事件传递给普通控件列表中的各控件的 `handleEvent`(通常 Container 控件会向其子控件继续传播)。
- 处理完事件后,如果有对话框正在显示(或刚关闭),或者 `dialogClose` 标志为真,则强制重绘一帧界面:包括所有普通控件和对话框,以确保界面更新。
- 循环每帧暂停 10 毫秒,防止 CPU 占用过高。
- **注意:** 该函数在调用后不会返回,直到窗口接收到关闭消息 (`WM_CLOSE`),此时函数内部会跳出循环结束。
- `void setBkImage(IMAGE* img)` / `void setBkImage(std::string filePath);`
**描述:** 更改窗口背景图像。可以直接传入一个已加载的 `IMAGE` 指针,或提供图像文件路径让函数内部加载。调用此函数会触发窗口重绘当前所有控件和对话框以应用新的背景。
- `void setBkcolor(COLORREF c);`
**描述:** 设置窗口背景颜色,并立即用该颜色清除屏幕背景(不销毁已有背景图,仅覆盖绘制)。
- `void setHeadline(std::string title);`
**描述:** 设置窗口标题文本。窗口创建后可随时更改标题;如果窗口已显示,则通过 WinAPI `SetWindowText` 立即更新窗口标题栏。
- `void addControl(std::unique_ptr<Control> control);`
**描述:** 向窗口添加一个普通控件。Window 维护一个控件列表,`draw()``runEventLoop()` 会据此绘制及派发事件。此方法接收智能指针,调用后 Window 接管控件的生命周期。
- `void addDialog(std::unique_ptr<Control> dialog);`
**描述:** 向窗口添加一个对话框控件(一般为 `Dialog` 类型)。对话框与普通控件分开管理,其事件和绘制有独立逻辑。
- `bool hasNonModalDialogWithCaption(const std::string& caption) const;`
**描述:** 检查当前是否已有**非模态**对话框其标题caption与给定字符串匹配。如果存在返回 `true`。通常用于避免重复打开相同对话框。
**重载版本:** `bool hasNonModalDialogWithCaption(const std::string& caption, const std::string& text) const;`
除了标题,还同时比较对话框内容文本,以更严格地判断重复。`MessageBox::showAsync` 内部使用此方法防止弹出重复的提示框。
- *信息获取方法:*
- `HWND getHwnd() const;` 获取底层窗口句柄EasyX 初始化返回的 HWND
- `int getWidth() const, getHeight() const;` 获取窗口宽度和高度。
- `std::string getHeadline() const;` 获取当前窗口标题。
- `COLORREF getBkcolor() const;` 获取窗口背景色。
- `IMAGE* getBkImage() const;` 获取当前窗口背景图像对象指针(如果有)。
- `std::vector<std::unique_ptr<Control>>& getControls();` 引用返回窗口维护的控件列表(可以对其遍历,但一般不手动修改)。
### Canvas 类 (容器控件)
**描述:** `Canvas` 是一种可包含子控件的画布容器用于将其他控件分组、统一背景和边框样式以及实现复杂布局。Canvas 自身也是一个控件,继承自 `Control`
- **特性:**
- 支持四种矩形形状背景(普通矩形/圆角矩形,各有无边框和有边框版本),可通过 `setShape` 设置。
- 可自定义容器背景颜色 (`canvasBkColor`)、边框颜色 (`canvasBorderColor`)、边框线型 (`canvasLineStyle`)、填充模式 (`canvasFillMode`,纯色/图案/无填充) 等。
- Canvas 能自动管理子控件的生命周期(使用 `addControl` 添加的子控件在 Canvas 销毁时自动释放)。
- Canvas 会在绘制时遍历并绘制所有添加的子控件并在处理事件时优先将事件传递给子控件列表中最后添加的控件Z 顺序)。
- **重要数据成员:**
- `std::vector<std::unique_ptr<Control>> controls;` 子控件列表。
- `StellarX::ControlShape shape;` 容器背景形状(默认矩形)。
- `StellarX::FillMode canvasFillMode;` 背景填充模式(默认实色填充)。
- `StellarX::LineStyle canvasLineStyle;` 边框线型(默认实线)。
- `int canvaslinewidth;` 边框线宽像素值。
- `COLORREF canvasBorderColor, canvasBkColor;` 容器边框颜色与背景色。
- `StellarX::LayoutKind Kind;` (预留)布局管理类型,当前未深入实现,可用于标识布局策略(如绝对布局、水平/垂直布局等)。
- **注意:** Canvas 重载了 `isVisible()` 始终返回 false因为 Canvas 通常不作为独立可见单元(事件循环中不直接处理 Canvas而由其子控件处理事件。但这不影响 Canvas 作为容器的绘制和子控件事件派发。
- **主要方法:**
- `Canvas(); Canvas(int x, int y, int w, int h);`
**描述:** 构造一个 Canvas 容器,可以指定位置 `(x,y)` 及初始尺寸。
- `void addControl(std::unique_ptr<Control> control);`
**描述:** 添加子控件到 Canvas。被添加控件将成为 Canvas 内容的一部分,其坐标相对于 Canvas 左上角Canvas 并不主动调整子控件位置,需在添加前设置好子控件位置)。添加后 Canvas 会将自身标记为需要重绘。
- `void setShape(StellarX::ControlShape shape);`
**描述:** 设置 Canvas 背景形状。支持 `RECTANGLE``B_RECTANGLE`(无边框矩形)、`ROUND_RECTANGLE`(圆角矩形)、`B_ROUND_RECTANGLE`无边框圆角矩形。如果传入圆形或椭圆形CIRCLE/ELLIPSE等Canvas 不支持,将自动按矩形处理。
- `void setCanvasFillMode(StellarX::FillMode mode);`
**描述:** 设置背景填充模式,如纯色 (`Solid`)、无填充 (`Null`)、预定义图案填充 (`Hatched`) 或自定义图案/图像填充 (`Pattern/DibPattern`),需结合 `setfillstyle` 使用。默认为纯色。
- `void setBorderColor(COLORREF color);` / `void setCanvasBkColor(COLORREF color);` / `void setCanvasLineStyle(StellarX::LineStyle style);` / `void setLinewidth(int width);`
**描述:** 分别设置容器边框颜色、背景色、边框线型和线宽。这些改变都会使 Canvas 需要重绘。
- `void draw() override;`
**描述:** 绘制 Canvas 背景和其所有子控件。Canvas 绘制步骤:
1. 根据设定的背景颜色/填充模式和形状绘制容器背景(例如填充矩形或圆角矩形区域)。
2. 遍历子控件列表,调用每个子控件的 `draw()` 方法绘制它们。绘制前对每个子控件先设置其 `dirty = true`,确保子控件按需重绘。
3. 恢复绘图样式,标记自身为已绘制(`dirty = false`)。
*实现细节:* Canvas 使用 `saveStyle()/restoreStyle()` 保护全局绘图状态,并依赖 EasyX 的填充/绘制函数根据 `shape` 绘制矩形或圆角矩形背景。
- `bool handleEvent(const ExMessage& msg) override;`
**描述:** 将事件按照子控件的添加顺序逆序传递(后添加的子控件先处理)。对 `controls` 列表从末尾向前调用每个子控件的 `handleEvent`,若某子控件返回 `true`(表示事件已被处理),则停止传递并返回 `true`;若所有子控件都未消费事件,则返回 `false` 表示未处理。
*用途:* 使得堆叠在顶层的子控件(通常列表后面的)有更高优先权处理事件,例如多个重叠控件的情形。
- `void clearAllControls();` *(protected 方法)*
**描述:** 清除并移除所有子控件。会释放智能指针列表中所有控件对象。一般在容器销毁或重置时使用。普通情况下不直接调用,由容器析构自动完成。
### Label 类 (静态文本标签)
**描述:** `Label` 用于显示一段静态文本,可设置文字颜色、背景透明/不透明及样式等。Label 不处理用户输入事件(纯展示)。
- **构造函数:**
- `Label(); Label(int x, int y, std::string text = "标签", COLORREF textColor = BLACK, COLORREF bkColor = WHITE);`
**描述:** 创建一个 Label 控件。可指定初始位置 `(x,y)`、显示文本内容和文本颜色、背景色等。默认文本为“标签”,默认文字黑色、背景白色。
- **主要属性:**
- `std::string text;` 显示的文字内容。
- `COLORREF textColor;` 文本颜色。
- `COLORREF textBkColor;` 文本背景色(在背景不透明时生效)。
- `bool textBkDisap;` 是否背景透明显示文本。若为 `true`Label 绘制文字时使用透明背景模式。
- `StellarX::ControlText textStyle;` 文字样式结构,包括字体、高度、粗细等属性(继承自 Control 默认值,例如 `字体:微软雅黑`)。
- **注意:** Label 大小 (`width, height`) 初始为 0通常 Label 大小根据文本自动适应无需手动设置宽高。Label 绘制时会截取背景图像以覆盖文字区域刷新。
- **主要方法:**
- `void setTextdisap(bool transparent);`
**描述:** 设置文本背景是否透明显示。传入 `true` 则文字背景透明,显示时不遮挡底下背景;`false` 则文字背景为 `textBkColor` 实色矩形。
- `void setTextColor(COLORREF color);` / `void setTextBkColor(COLORREF color);`
**描述:** 设置文字颜色或背景色。更改后会标记 Label 需要重绘。
- `void setText(std::string text);`
**描述:** 更改显示的文本内容,并标记需要重绘。支持中文字符串。
- `void draw() override;`
**描述:** 绘制文本标签。根据 `textBkDisap` 决定调用 EasyX 的 `setbkmode(TRANSPARENT)``OPAQUE`,并设置背景色 `setbkcolor(textBkColor)`。然后设置文本颜色和样式,调用 `outtextxy(x, y, text)` 输出文字。为防止文字重绘留痕迹,`Label::draw` 会先保存文字区域背景 (`saveBackground`),绘制完成后再恢复样式。首次绘制后将 `dirty` 置为 false。
- `bool handleEvent(...) override;`
**描述:** Label 不处理任何事件,始终返回 false。
- `void hide();`
**描述:** 将 Label 内容隐藏。其实现是调用 `restBackground()` 恢复先前保存的背景,然后 `discardBackground()` 释放快照,并将 `dirty` 设为 false。这通常在 Label 用作 Tooltip 提示框时,由宿主控件调用,用于移除提示文本的显示而不重绘整个界面。
- **使用场景:** Label 常用于界面中的静态说明文字、标题、状态栏信息等。通过配合 `textStyle` 可以修改字体、大小、是否加粗等。默认情况下 Label 背景不透明白底,如需与窗口背景融合可调用 `setTextdisap(true)` 使背景透明。
### Button 类 (按钮控件)
**描述:** `Button` 提供普通按钮和切换按钮(两态)的功能,可响应点击和悬停事件。支持设置各种样式(形状、颜色)和点击回调,是交互的主要控件之一。
- **工作模式:** 由 `StellarX::ButtonMode` 枚举定义:
- `NORMAL` 普通按钮:每次点击都会触发一次动作,按钮本身不保持按下状态。
- `TOGGLE` 切换按钮:每次点击切换自身状态(选中/未选中),并触发不同的回调。
- `DISABLED` 禁用按钮:不响应用户点击,显示为灰色且文字带删除线。
- 可通过 `setButtonMode(ButtonMode mode)` 改变按钮模式。
- **外观形状:** 由 `StellarX::ControlShape` 定义:
- 支持矩形 (`RECTANGLE`/`B_RECTANGLE`)、圆角矩形 (`ROUND_RECTANGLE`/`B_ROUND_RECTANGLE`)、圆形 (`CIRCLE`/`B_CIRCLE`)、椭圆 (`ELLIPSE`/`B_ELLIPSE`) 八种形状B_ 前缀表示无边框填充)。通过 `setButtonShape(ControlShape shape)` 设置按钮形状。
- 注意:在切换 `ControlShape` 时,某些尺寸下圆形/椭圆以控件的 `width``height` 计算,圆形半径取较小的半径,椭圆按照矩形外接框绘制。
- **主要可配置属性:**
- 颜色:`buttonTrueColor`(被点击时颜色),`buttonFalseColor`(常态颜色),`buttonHoverColor`(鼠标悬停颜色),`buttonBorderColor`(边框颜色)。
- 填充:`buttonFillMode`(填充模式,实色/图案/图像等),`buttonFillIma`(图案填充样式,如果 `FillMode == Hatched``buttonFileIMAGE`(指向自定义填充图像的指针,如果 `FillMode == DibPattern`)。
- 文本:按钮显示的文字 `text` 及其样式 `textStyle`(字体、字号、颜色等)。另外提供 `cutText` 字符串用于文本过长时显示裁剪的内容。
- Tooltip`tipEnabled`(是否启用悬停提示),`tipTextClick`NORMAL模式提示文本`tipTextOn/Off`TOGGLE模式下两种状态的提示文本`tipFollowCursor`(提示框是否跟随鼠标移动),`tipDelayMs`(提示延迟毫秒),`tipOffsetX/Y`(提示框相对位置偏移),`tipLabel`(内部使用的 Label 控件呈现提示文本)。
- 圆角大小:通过继承自 `Control``rouRectangleSize` 成员控制圆角矩形圆角的半径大小。提供 `setRoundRectangleWidth(int)``setRoundRectangleHeight(int)` 接口修改。
- **主要方法:**
- `void setOnClickListener(function<void()>&& callback);`
**描述:** 设置按钮普通点击时的回调函数。当按钮模式为 NORMAL在每次鼠标释放点击时调用该回调当模式为 TOGGLE则仅在每次点击状态发生变化时调用取决于 toggle 逻辑)。
- `void setOnToggleOnListener(...)` / `void setOnToggleOffListener(...);`
**描述:** 设置按钮在 TOGGLE 模式下由未选中变为选中、以及由选中变为未选中时分别触发的回调函数。当按钮模式为 NORMAL 或 DISABLED 时,这两个回调不会被调用。
- `void setButtonText(const char* text)` / `void setButtonText(std::string text);`
**描述:** 更改按钮上的显示文本。支持 C 风格字符串或 `std::string`。更改文本后会重新计算文本宽高,并标记按钮需要重绘。
- `void setButtonBorder(COLORREF border)` / `void setButtonFalseColor(COLORREF color)`
**描述:** 设置按钮边框颜色、常态下背景填充颜色。
- `void setFillMode(FillMode mode)` / `void setFillPattern(FillStyle pattern)` / `void setFillImage(const std::string& path)`
**描述:** 设置按钮背景填充方式:
- `setFillMode` 改变填充模式(纯色、图案、位图等)。
- 若填充模式为图案 (`Pattern`),需调用 `setFillPattern` 设置具体的图案样式 (`StellarX::FillStyle`)。
- 若填充模式为位图 (`DibPattern`),可调用 `setFillImage` 加载指定路径的图像并用作填充(会自动调整图像大小与按钮一致)。
- `void setButtonClick(bool click);`
**描述:** 外部设置按钮的“点击状态”。对于 TOGGLE 模式,`setButtonClick(true)` 会将按钮设为选中状态并触发 onToggleOn 回调,`false` 则触发 onToggleOff 回调。对于 NORMAL 模式,传入 true 将临时触发一次 onClick 回调(随后状态立即复位为未点击)。无论哪种模式,调用都会引起按钮重绘。**注意:** DISABLED 模式调用此函数没有效果。
- `bool isClicked() const;`
**描述:** 返回按钮当前是否处于按下状态(仅对 TOGGLE 模式有意义,表示按钮选中状态)。
- 其他状态获取方法如 `getButtonText()`, `getButtonMode()`, `getButtonShape()`, `getFillMode()`, `getFillPattern()`, `getFillImage()`, `getButtonBorder()`, `getButtonTextColor()`, `getButtonTextStyle()`, `getButtonWidth()`, `getButtonHeight()` 一一提供,返回对应属性值。
- `void draw() override;`
**描述:** 绘制按钮外观。根据按钮当前状态选择填充颜色:若禁用则用 `DISABLEDCOLOUR` 灰色并给文字加删除线效果,否则 `click` 状态优先,其次 `hover` 状态,最后默认未点击颜色。然后设置透明背景、边框颜色和文本样式,计算文字应绘制的位置并绘制文字。
如果按钮文本长度超出按钮宽度,`draw()` 会自动调用 `cutButtonText()` 对文本进行裁剪,并用省略号表示超出部分,以确保文字在按钮内完整显示。绘制结束后恢复样式并将 `dirty` 置为 false。
- `bool handleEvent(const ExMessage& msg) override;`
**描述:** 处理与按钮相关的鼠标事件,包括鼠标按下、弹起、移动等:
- 检测鼠标是否悬停在按钮区域内(根据按钮形状调用内部 `isMouseInCircle/Ellipse` 辅助函数)。
- 处理 `WM_LBUTTONDOWN`(鼠标按下):若按钮未禁用且鼠标位于按钮上,在 NORMAL 模式下将按钮标记为点击状态(按下),在 TOGGLE 模式下不改变状态(等待弹起时处理)。
- 处理 `WM_LBUTTONUP`(鼠标弹起):若鼠标在按钮上且按钮可点击:
- NORMAL 模式:如果之前已记录按下,则认为一次有效点击,调用 `onClickCallback`,然后清除点击状态。
- TOGGLE 模式:切换按钮状态 `click = !click`。根据切换后的状态调用 `onToggleOnCallback``onToggleOffCallback`
- 每次有效点击后,都会通过 `flushmessage(EX_MOUSE|EX_KEY)` 清空输入消息队列,防止此次点击产生的消息(例如弹起)重复被处理。
- 处理鼠标移出(在 `WM_MOUSEMOVE` 中检测):如果 NORMAL 模式下拖出按钮区域则取消按下状态,任何模式下 hover 状态变化都将使按钮重绘。
- Tooltip 提示:如果启用了 `tipEnabled``handleEvent` 内会管理 `tipLabel` 的显示:
- 当鼠标首次移入按钮区域,启动一个定时(根据 `tipDelayMs`)。
- 当鼠标离开或按钮点击后,立即隐藏提示(调用 `hideTooltip()`)。
- 当鼠标在按钮上停留超过延迟且按钮仍显示,则设置 `tipVisible = true` 并初始化 `tipLabel` 的文本和位置准备显示提示。`tipFollowCursor` 决定提示框锚定鼠标位置还是按钮下方。
- `refreshTooltipTextForState()` 用于当按钮为 TOGGLE 模式且未自定义提示文本时,选择显示 `tipTextOn``tipTextOff`
- 每帧事件处理后,如提示应显示,则调用 `tipLabel.draw()` 绘制提示文本。
- 函数最后,如果按钮 `hover` 状态或 `click` 状态有变化,则将自身标记为 `dirty` 并立即调用 `draw()` 重绘自身,实现交互的即时反馈。
`handleEvent` 返回值为 `true` 当且仅当按钮按下鼠标弹起事件被处理时,表示该点击事件已被消费,其它控件不应再处理。
- **使用说明:**
开发者通常通过设置按钮的各种监听器来处理点击事件。例如:
```
auto btn = std::make_unique<Button>(50, 50, 80, 30, "确定");
btn->setOnClickListener([](){ /* 点击时执行 */ });
```
对于 TOGGLE 按钮:
```
btn->setButtonMode(ButtonMode::TOGGLE);
btn->setOnToggleOnListener([](){ /* 开启时执行 */ });
btn->setOnToggleOffListener([](){ /* 取消时执行 */ });
```
也可以控制按钮的外观,如:
```
btn->textStyle.color = RGB(255,255,255); // 白色文字
btn->setButtonFalseColor(RGB(100,150,200)); // 默认背景色
btn->setButtonBorder(RGB(80,80,80)); // 边框灰色
btn->setButtonShape(StellarX::ControlShape::RECTANGLE);
```
若要提示说明按钮功能,可:
```
btn->tipEnabled = true;
btn->tipTextClick = "点击执行确定操作";
// (TOGGLE模式则设置 tipTextOn/off)
```
### TextBox 类 (文本框控件)
**描述:** `TextBox` 用于显示和输入单行文本。可配置为可编辑或只读模式。TextBox 提供基本的文本输入框功能,当用户点击输入模式的文本框时,会弹出一个输入对话框接受用户输入(利用 EasyX 的 `InputBox` 实现),并将结果显示在文本框中。
- **模式:** 由 `StellarX::TextBoxmode` 指定:
- `INPUT_MODE` 可输入模式:用户点击文本框可输入新内容。
- `READONLY_MODE` 只读模式:用户点击不会更改文本,典型地可用于展示不可修改的信息。
- **外观:**
- 支持矩形和圆角矩形四种形状(无边框或有边框),通过 `setTextBoxShape(ControlShape)` 设置。圆形/椭圆对文本框来说不常用,一律按矩形处理。
- 文本框默认有黑色边框和白色背景,可用 `setTextBoxBorder` 和 `setTextBoxBk` 改变。
- 文字样式通过 `textStyle` 调整,包含字体、大小、颜色等属性。
- **主要属性:**
- `std::string text;` 当前文本框显示的文本内容。
- `TextBoxmode mode;` 当前模式(决定是否允许输入)。
- `ControlShape shape;` 当前文本框边框形状(矩形或圆角矩形,默认 RECTANGLE
- `bool click;` 内部使用标志,表示是否刚被点击(用于触发输入弹窗,处理完后即重置)。
- `size_t maxCharLen;` 最大允许输入字符数。默认 255。
- `COLORREF textBoxBorderColor, textBoxBkColor;` 边框颜色和背景填充颜色。
- `StellarX::ControlText textStyle;` 文本样式(高度、宽度、字体、颜色等)。
- **主要方法:**
- `void setMode(StellarX::TextBoxmode mode);`
**描述:** 切换文本框模式。可选 INPUT_MODE 或 READONLY_MODE。更改模式不会清除已有文本但只读模式下用户输入将被忽略。
- `void setMaxCharLen(size_t len);`
**描述:** 设置文本框最大字符长度。仅当 `len > 0` 时有效。超出长度的输入会被截断。
- `void setTextBoxShape(StellarX::ControlShape shape);`
**描述:** 设置文本框形状。支持矩形和圆角矩形,与 Button 类似。如果传入非法形状(圆/椭圆),会自动改为矩形。设置后文本框将 `dirty`,下次绘制按新形状渲染。
- `void setTextBoxBorder(COLORREF color);` / `void setTextBoxBk(COLORREF color);`
**描述:** 设置边框颜色和背景颜色。
- `void setText(std::string text);`
**描述:** 更新文本框内容文本。如果新文本长度超过 `maxCharLen`,将自动截断。设置后标记 `dirty` 并立即调用 `draw()` 重绘,使更改即时可见。
- `std::string getText() const;`
**描述:** 获取当前文本框内容字符串。
- `void draw() override;`
**描述:** 绘制文本框背景和文字。根据 `textStyle` 设置字体,如字体高度/宽度大于控件尺寸则裁剪适应;使用透明背景模式绘制文字,以便背景色通过 `fill...` 函数填充。
- 绘制顺序:先 `fillrectangle/roundrect` 绘制背景(按 `textBoxBkColor`),然后 `outtextxy` 绘制文本,文本在 X 方向留出 10 像素左右内边距Y 方向垂直居中。
- 如果文本长度超出控件宽度,不自动裁剪(与 Button 不同),而是可能显示不下或被遮盖。通常应通过设置 `maxCharLen` 控制输入长度,或设计足够宽度的文本框。
- 绘制完成后 `dirty` 置 false。
- `bool handleEvent(const ExMessage& msg) override;`
**描述:** 处理鼠标事件:
- 判断鼠标是否在文本框区域 (`hover`)。
- 当 `WM_LBUTTONUP` 且鼠标在区域内发生:若模式为 INPUT_MODE则调用 EasyX 的 `InputBox` 弹出输入对话框,允许用户输入文本(初始值为当前文本框内容),用户提交后 `InputBox` 返回新文本,将此文本赋值并标记 `dirty` 以便重绘显示。若模式为 READONLY_MODE则弹出一个只读提示框不会改变文本并不修改 `dirty` 状态。
- 其它鼠标事件不改变文本框外观,仅根据需要返回 false未消费事件
- 当完成一次输入或点击,文本框会调用自身 `draw()` 更新显示。READONLY_MODE 下点击会简单弹出提示(使用 InputBox 显示原文本并提示输入无效)。
- 函数返回 true 表示在 INPUT_MODE 下点击事件被消费因为弹出了输入框READONLY_MODE 下也返回 true提示已显示从而阻止事件继续传播。
- **用法注意:**
- 由于 EasyX 的 `InputBox` 是模态阻塞函数,`runEventLoop` 本身是非阻塞循环,**在 UI 线程直接调用 InputBox 会暂停整个消息循环** 直到输入完成。StellarX 目前采用这种简单方式处理文本输入(不是异步 UI 输入)。因此在 `TextBox::handleEvent` 中使用 `InputBox` 来得到用户输入。这简化了实现,但 UI 线程会暂时挂起。开发者应避免在非常频繁交互时滥用 InputBox。
- 可以通过 `maxCharLen` 控制可输入最大字符数,例如对于数值输入框可以设定长度防止超出范围。
- 使用 `textStyle` 可调整显示字体(如等宽字体用于代码输入等),`textStyle.color` 也可单独设置文字颜色。
### Dialog 类 (对话框控件)
**描述:** `Dialog` 提供模态/非模态对话框功能可在应用中弹出提示、确认或输入等对话界面。Dialog 本质上是一个特殊的 Canvas 容器,内部包含文本消息和按钮等子控件,并与 Window 紧密配合实现模态阻塞效果。
- **创建与模式:**
- 构造函数 `Dialog(Window& parent, std::string title, std::string message = "对话框", MessageBoxType type = OK, bool modal = true)`
**描述:** 创建一个关联到 `parent` 窗口的对话框。`title` 将作为对话框标题文本显示在对话框顶部,`message` 是正文消息内容。`type` 参数指定对话框包含的按钮组合类型(见 MessageBoxType 枚举),例如 OK 只有一个确定按钮YesNo 包含“是”和“否”两个按钮等。`modal` 决定对话框是模态还是非模态。构造后对话框初始为隐藏状态,需要调用 `show()` 显示。
- `MessageBoxType` 枚举定义了常见的对话框按钮组合:
- `OK`:一个“确定”按钮。
- `OKCancel`:“确定”和“取消”按钮。
- `YesNo`:“是”和“否”按钮。
- `YesNoCancel`:“是”、“否”和“取消”按钮。
- `RetryCancel`:“重试”和“取消”。
- `AbortRetryIgnore`:“中止”、“重试”、“忽略”按钮。
- 模态 (`modal=true`) 对话框:调用 `show()` 时会阻塞当前线程运行,直到对话框关闭后才返回。这期间应用的其他部分暂停响应输入。适用于必须先处理用户响应的关键情景。模态对话框关闭时,`show()` 返回后可通过 `getResult()` 获取用户点击结果。
- 非模态 (`modal=false`) 对话框:调用 `show()` 则立即返回,应用程序继续运行,用户可以在背景窗口和对话框之间自由交互。需要通过回调函数或轮询 `getResult()` 来获取对话框的结果。
- **组成元素:**
- 标题文本(使用内部的 Label 控件 `title` 实现,显示在对话框顶部)。
- 消息正文(支持多行文本,`Dialog` 自动按行分割存储在 `lines` 向量中,并在绘制时逐行居中输出)。
- 功能按钮(根据 `MessageBoxType`包含1到3个 Button 控件,位置自动计算居于对话框底部)。
- 关闭按钮(右上角的 “×” 按钮,一个小的 Button 控件,点击相当于取消/关闭)。
- 背景样式Dialog 采用圆角矩形背景(如 Canvas shape = ROUND_RECTANGLE并有半透明效果具体通过 saveBackground 恢复背景实现视觉模态效果,但在 EasyX 简化模型下实际只是截屏背景,无模糊透明实现)。
- **主要属性:**
- `bool modal;` 是否模态对话框。
- `titleText`:标题栏文本,`message`:消息内容文本。
- `MessageBoxType type;` 对话框类型(按钮组合)。
- `std::vector<std::string> lines;` 分割后的多行消息文本。
- `std::unique_ptr<Label> title;` 用于显示标题的 Label 控件。
- `Button* closeButton;` 关闭按钮指针(“×”)。
- `Window& hWnd;` 引用父窗口,用于操作对话框关闭后刷新背景等。
- `MessageBoxResult result;` 用户选择结果枚举值对应哪个按钮被按下。常用值OK=1, Cancel=2, Yes=6, No=7 等(见 CoreTypes 定义)。
- `std::function<void(MessageBoxResult)> resultCallback;` 非模态对话框的结果回调函数指针。如果设置,将在对话框关闭且 `modal == false` 时被调用,参数为用户最终点击结果。
- **主要方法:**
- `void show();`
**描述:** 显示对话框。如果是模态对话框,此函数将阻塞,内部运行一个事件处理循环,使该对话框独占输入焦点,直到对话框关闭为止;如果是非模态,则此函数立即返回(但对话框窗口已可见,输入由主 Window 继续分发)。`show()` 不返回任何值,但可以在模态情况下函数返回后调用 `getResult()` 获取结果。
- `void closeDialog();`
**描述:** 关闭对话框。会将对话框隐藏并清理资源。如果有设置非模态回调且对话框为非模态,则在此过程中调用 `resultCallback(result)` 通知结果。通常不直接调用,由内部逻辑根据按钮按下触发。
- `MessageBoxResult getResult() const;`
**描述:** 获取对话框的返回结果(用户按下了哪个按钮)。对模态对话框,`show()` 返回后调用此函数获取结果;对非模态,可在回调或稍后某时获取。
- `void setTitle(const std::string& title);` / `void setMessage(const std::string& msg);`
**描述:** 更改对话框标题或内容文本。更改内容会重新分割 `lines` 并调整文本区域大小,需要重新布局。
- `void setType(MessageBoxType type);`
**描述:** 更改对话框类型(按钮组合)。将重新创建按钮布局。
- `void setModal(bool modal);`
**描述:** 切换对话框模态属性(必须在 show() 前调用)。一般不会在运行中切换。
- `void setResult(MessageBoxResult res);`
**描述:** 设置对话框结果代码。通常由内部在按钮按下时调用,开发者无需手动调用。
- `void setInitialization(bool init);`
**描述:** 若传入 true则提前计算和设置对话框尺寸并保存背景快照。**用途:** 在通过 `MessageBox` 静态函数创建对话框时,会先调用此函数以准备好对话框以便正确显示。
- `void setResultCallback(function<void(MessageBoxResult)> cb);`
**描述:** 为非模态对话框设置结果回调函数。用户关闭对话框时将异步调用此函数,将结果传出。
- *内部实现在 `Dialog` 类私有方法中:*
- `initButtons()`:根据 `type` 创建对应数量的 Button 控件并设置其回调,将它们添加到 Dialog 容器。
- `initCloseButton()`:创建右上角关闭按钮(文本为 “×”),点击后触发取消逻辑。
- `initTitle()`:创建标题 Label 控件。
- `splitMessageLines()`:将 `message` 根据换行符分割填充到 `lines` 数组。
- `getTextSize()`:计算 `lines` 中最长行宽度和单行高度,以确定文本区域所需尺寸。
- `initDialogSize()`:综合计算对话框宽高(考虑文本区域和按钮区域的宽度、高度),然后居中放置对话框于窗口。最后调用上述 init** 方法创建各子控件。
- `performDelayedCleanup()`用于延迟销毁对话框时释放资源。Dialog 设计上为了避免背景快照不匹配,需要在对话框关闭且完全隐去后再恢复背景并释放子控件资源;此函数执行该清理流程,由 Window 的事件循环或 `handleEvent` 在适当时机调用。
- `void draw() override;`
**描述:** 绘制对话框界面。Dialog 继承自 Canvas通过调用 `Canvas::draw()` 绘制背景和子控件框架,然后另外绘制消息文本内容。绘制步骤:
1. 如果首次显示,对话框先保存覆盖区域的屏幕背景(便于关闭时恢复)。
2. 设置 Canvas 的背景颜色、边框等样式为对话框特定值(通常圆角矩形)。
3. 将所有子控件标记为 `dirty` 并调用 `Canvas::draw()` 绘制对话框背景和子控件包括按钮、标题Label等
4. 使用对话框的 `textStyle` 设置文字样式,然后将每行 `lines` 文本居中绘制在对话框内部合适的位置(计算基于对话框宽度)。
5. 恢复样式,设置 `dirty = false`。
对话框作为一个整体,由 Window 管理,其绘制顺序通常在 Window::runEventLoop 强制刷新时绘制。模态对话框在自己的 loop 内绘制。
- `bool handleEvent(const ExMessage& msg) override;`
**描述:** 处理对话框相关事件:
- 如果对话框未显示 (`show == false`),则检查 `pendingCleanup` 标志,如果有延迟清理任务且当前未在清理,则调用 `performDelayedCleanup()` 执行清理,然后不消费事件直接返回 false。
- 如果对话框正在清理或标记延迟清理,则不处理任何事件直接返回 false。
- 若为模态对话框 (`modal == true`),并检测到用户点击了对话框区域外部(窗口背景区域)的鼠标弹起事件,则发出提示声音 (`\a`)并返回 true**吞噬事件**),防止用户点击窗体其他部分。
- 将事件传递给 Dialog 继承的 Canvas即其子控件处理`consume = Canvas::handleEvent(msg)`,这样对话框内部的按钮可以响应事件。如果子控件处理了事件则 consume 为 true。
- 最后,如果对话框有延迟清理任务,则调用 `performDelayedCleanup()` 处理。返回值为 whether 消费了事件。
- **模态对话框特别说明:** Window 主循环在有模态对话框显示时,会将背景点击等事件吃掉(上面逻辑已实现),同时对话框 `show()` 方法内部启动自己的循环处理事件,使模态对话框独占用户输入。非模态对话框则依赖 Window 主循环分发事件,由 handleEvent 参与处理。
- **使用示例:**
`Dialog` 通常不直接使用,而是通过 `StellarX::MessageBox` 工具调用快速创建并显示:
- 模态对话框:
```
StellarX::MessageBoxResult res = StellarX::MessageBox::showModal(window,
"文件已保存。是否关闭程序?", "提示",
StellarX::MessageBoxType::YesNoCancel);
if (res == StellarX::MessageBoxResult::Yes) { /* 用户选择是 */ }
```
`showModal` 内部创建 `Dialog` 并以模态方式显示,直到用户点击“是/否/取消”其中一个按钮,函数返回对应的枚举值。
- 非模态对话框:
```
StellarX::MessageBox::showAsync(window,
"处理完成!", "通知",
StellarX::MessageBoxType::OK,
[](StellarX::MessageBoxResult){ /* 用户点了OK后的处理 */ });
```
这样弹出的对话框不会阻塞程序,它包含一个“确定”按钮。用户点击后,对话框关闭并自动调用提供的 lambda 回调。Window 的事件循环会继续正常运行。
开发者也可自行实例化 Dialog
```
Dialog dlg(mainWindow, "标题", "内容……", StellarX::MessageBoxType::OKCancel, false);
dlg.setResultCallback([](MessageBoxResult res) { /* 处理结果 */ });
dlg.show();
```
以上将创建一个非模态对话框。一般推荐使用 `MessageBox` 封装函数更方便。
## MessageBox 工具类
**描述:** `StellarX::MessageBox` 提供静态方法方便地创建标准对话框,无需直接操作 Dialog 类。类似 Windows API 的 MessageBox但这里可以指定按钮组合类型并支持非模态调用。
- **静态函数:**
- `static MessageBoxResult showModal(Window& wnd, const std::string& text, const std::string& caption = "提示", MessageBoxType type = MessageBoxType::OK);`
**描述:** 弹出一个模态对话框,在窗口 `wnd` 上显示,标题为 `caption`,正文为 `text`,按钮组合由 `type` 指定。此函数会阻塞当前执行直到用户在对话框上点击按钮关闭对话框。返回值为 `MessageBoxResult`,表示用户点击了哪个选项(例如 `MessageBoxResult::OK` 或 `Cancel` 等)。开发者可根据返回值执行不同逻辑。
- `static void showAsync(Window& wnd, const std::string& text, const std::string& caption = "提示", MessageBoxType type = MessageBoxType::OK, std::function<void(MessageBoxResult)> onResult = nullptr);`
**描述:** 在窗口 `wnd` 弹出一个非模态对话框。参数类似 `showModal`,但 `onResult` 是可选的回调函数。如果提供了回调函数,当用户关闭对话框时将异步调用该函数并传入用户选择结果。该函数本身立即返回,不阻塞程序流程。如果在 `wnd` 中已经存在一个内容和标题都相同的非模态对话框未关闭,则此函数会发出警报音并不创建新的对话框,以防止重复(内部使用 `Window::hasNonModalDialogWithCaption(caption,text)` 检查)。
**注意:** 如果不提供 `onResult` 回调,那么需要其他方式得知对话框结果(例如通过 Dialog 的公有接口或者全局状态),但通常建议提供回调以处理结果。
- **实现细节:** `showModal` 和 `showAsync` 内部都会创建一个 `Dialog` 对象,并根据 `type` 设置相应按钮逻辑:
- `showModal` 创建 Dialog 后,会调用 `dlg.setInitialization(true)` 准备好对话框尺寸,然后调用 `dlg.show()` 以模态方式显示,并阻塞等待结果,最后返回 `dlg.getResult()`。
- `showAsync` 则创建 Dialog 后,同样初始化,若指定回调则通过 `dlgPtr->setResultCallback(onResult)` 注册,然后把对话框通过 `wnd.addDialog()` 加入主窗口的对话框列表,最后调用 `dlgPtr->show()` 非模态显示。非模态显示不会阻塞,因此 `showAsync` 很快返回;对话框的关闭与结果传递在回调中完成。
- **典型用途:** MessageBox 提供与系统 MessageBox 类似的简明接口:
- 显示一条提示信息并等待确认:
`MessageBox::showModal(mainWindow, "操作成功完成!", "提示", MessageBoxType::OK);`
- 提示加确认选择:
`auto res = MessageBox::showModal(mainWindow, "确定要删除记录吗?", "请确认", MessageBoxType::YesNo); if (res == MessageBoxResult::Yes) { ... }`
- 非模态通知:
`MessageBox::showAsync(mainWindow, "下载已在后台进行。", "通知", MessageBoxType::OK); // 用户点OK时对话框将自行关闭`
- 非模态询问并通过回调获取结果:
```
MessageBox::showAsync(mainWindow, "发现新版本,是否现在更新?", "更新提示", MessageBoxType::YesNo,
[](MessageBoxResult res) {
if (res == MessageBoxResult::Yes) startUpdate();
});
```
通过这些静态方法,可以很方便地融合对话框提示流程,无需手动管理 Dialog 对象的生命周期和事件处理逻辑。
## Table 类 (表格控件)
**描述:****Table** 类是一个高级表格控件,支持分页显示和大数据量展示[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/include/StellarX/table.h#L2-L10)。它提供完整的数据表格功能包括表头、数据行和分页导航等实现类似电子表格的呈现。Table 控件可自动计算列宽和行高支持自定义边框样式、填充模式以及文本字体样式从而适应不同的UI设计需求。通过内置的分页机制Table 能有效展示大量数据而不影响界面性能,并使用背景缓存来优化渲染,避免重绘闪烁。
**主要特性:**
- **自动分页:** 根据数据行数和每页行数自动计算总页数和当前页显示内容[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/include/StellarX/table.h#L8-L16)。提供 **上一页/下一页** 按钮和页码指示,方便用户浏览分页数据。
- **自定义配置:** 开发者可配置每页行数、是否显示翻页按钮,设置表格边框颜色、背景色,选择填充模式(实色填充或透明)和边框线型等,满足不同视觉风格要求。
- **高效渲染:** Table 通过缓存背景图像来提高渲染效率。在表格内容变化或首次绘制时缓存底图,绘制时仅更新差异部分,减少重复绘制带来的闪烁[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L210-L219)[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L241-L249)。
- **典型场景:** 适用于数据列表、报表、记录浏览等需要表格式展示的场景[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/include/StellarX/table.h#L14-L17)。例如显示数据库查询结果、日志记录、统计报表等,让用户以行列形式查看信息。
### 公共成员函数
#### Table(int x, int y)
**函数原型:** `Table(int x, int y)`
**参数:**
- `x`:表格左上角的 X 坐标位置。
- `y`:表格左上角的 Y 坐标位置。
**返回值:** 无(构造函数无返回值)。
**功能描述:** Table 类的构造函数。在指定的坐标位置创建一个新的表格控件实例[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L164-L171)。初始状态下表格没有设置表头和数据宽度和高度初始为0会在设置数据后根据内容自动计算调整。`x` 和 `y` 参数用于将表格定位在窗口中的起始位置。
**使用场景:** 当需要在界面上显示表格数据时,首先调用此构造函数实例化一个 Table 对象。例如,在创建主窗口后,可以创建 `Table table(50, 50);` 来放置一个表格控件,然后再设置表头和数据。
#### ~Table()
**函数原型:** `~Table()`
**参数:** 无。
**返回值:** 无。
**功能描述:** Table 类的析构函数。在对象销毁时自动调用,负责释放表格内部分配的资源。如表格内部创建的翻页按钮、页码标签以及背景缓存图像都将在析构函数中删除以防止内存泄漏[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L169-L178)。
**使用场景:** 一般不直接调用,由系统自动管理。当 Table 对象的生命周期结束(例如其所在窗口关闭或程序结束)时,析构函数确保清理内存。不需开发者手动调用,但了解其行为有助于避免重复释放资源。
#### draw()
**函数原型:** `void draw() override`
**参数:** 无。
**返回值:** 无。
**功能描述:** 重写自 `Control` 基类的绘制函数,用于将表格绘制到屏幕[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L185-L194)[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L252-L260)。调用 `draw()` 时Table 根据当前状态绘制表头、当前页的数据行、页码标签和翻页按钮如果启用。绘制过程中会应用先前设置的文本样式、边框颜色、背景色等属性。为避免频繁重绘Table 内部维护一个`dirty`标志,只有当表格内容或外观需更新时(例如调用了设置函数后)`draw()` 才会实际重绘内容[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L187-L196)[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L258-L266)。
**使用场景:** 通常在设置完表头和数据、或修改了样式后调用,令表格按新内容渲染。一般框架的窗口在刷新时会自动调用各控件的 `draw()` 方法,开发者也可在需要立即刷新表格时手动调用它。例如,当表格数据更新且需要即时反映在界面上时,可以设置相应属性并调用 `draw()` 触发重绘。
#### handleEvent(const ExMessage& msg)
**函数原型:** `bool handleEvent(const ExMessage& msg) override`
**参数:**
- `msg`事件消息对象EasyX库的 `ExMessage` 结构),包含了键盘或鼠标事件的信息。
**返回值:** 布尔值,表示事件是否被表格控件处理。返回 `true` 表示事件已被消耗,不再需要向其他控件传播;返回 `false` 则表示表格未处理该事件。
**功能描述:** 重写自 `Control` 基类的事件处理函数,用于处理与表格相关的用户交互事件。对于 Table 而言,`handleEvent` 主要用于处理翻页按钮的点击事件。当 `isShowPageButton` 为真(显示翻页按钮)时,该方法会将传入的事件传递给内部的 “上一页” 和 “下一页” 按钮进行处理[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L274-L283)。如果用户点击了上一页或下一页按钮,按钮的内部逻辑会更新当前页码并调用 `draw()` 重绘表格,`handleEvent` 随后返回 `true` 表示事件已处理。若翻页按钮未启用(例如只有一页数据或通过 `showPageButton(false)` 隐藏了按钮),此函数直接返回 `false`[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L276-L283),不对事件进行处理。
**使用场景:** 框架的主事件循环会调用控件的 `handleEvent` 方法来分发用户输入。当使用 Table 控件时无需手动调用此函数但了解其行为有助于处理自定义事件。例如可在Window的事件循环中将鼠标点击事件传给表格控件使其内部的翻页按钮响应点击从而实现分页浏览的数据切换。
#### setHeaders(std::initializer_liststd::string headers)
**函数原型:** `void setHeaders(std::initializer_list<std::string> headers)`
**参数:**
- `headers`:表头文本字符串的初始化列表,每个字符串对应表格一列的列名。
**返回值:** 无。
**功能描述:** 设置表格的列标题。调用此函数会清除当前已有的表头并使用提供的字符串列表作为新表头[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L288-L296)。在设置表头后Table 会将内部状态标记为需要重新计算单元格尺寸和重新绘制表头[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L291-L295)。列标题用于在表格顶部显示,表示每列数据的含义。表头数量确定了表格的列数,后续添加的数据应与列数匹配。
**使用场景:** 通常在创建 Table 实例后、添加数据之前调用,用来定义表格的列结构。例如:`table.setHeaders({"姓名", "年龄", "职业"});` 将表格设置为包含“姓名”、“年龄”、“职业”三列。设置表头后即可调用 `setData` 添加对应列的数据行。
#### setData(std::vectorstd::string data) / setData(std::initializer_list<std::vectorstd::string> data)
**函数原型1** `void setData(std::vector<std::string> data)`
**参数:**
- `data`:包含单行表格数据的字符串向量,每个元素对应一列的内容。
**返回值:** 无。
**功能描述:** 将一个数据行添加到表格中。若传入的这行数据列数少于当前表头列数,则函数会自动在行数据末尾补空字符串,使其列数与表头一致[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L300-L308)。然后将该行数据追加到内部数据集合 `data` 中,并更新表格的总页数 (`totalPages`) 属性[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L302-L308)。更新页数时根据当前总行数和每页行数计算页数确保至少为1页[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L303-L308)。函数调用后会标记表格需要重新计算单元格尺寸,并在下次绘制时包含新添加的数据。
**使用场景:** 可用于逐行构建表格数据,例如从数据源读取记录并一条条加入表格。适合数据逐步到达或者需要动态添加行的情形。每次调用都会在末页附加一行,并相应更新分页显示。
**函数原型2** `void setData(std::initializer_list<std::vector<std::string>> data)`
**参数:**
- `data`:一个由多行数据组成的初始化列表,每个内部的 `std::vector<std::string>` 代表一行的数据。
**返回值:** 无。
**功能描述:** 批量添加多行数据到表格中。对于提供的每一行数据,函数会检查其列数是否与表头列数匹配;若不足则在该行末尾补充空字符串以对齐列数[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L313-L321)。然后将处理后的整行数据依次追加到表格内部的数据集中。添加完所有行后,函数重新计算总数据行数下的总页数,并至少确保一页[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L322-L328)。与单行版本类似,此方法也会触发单元格尺寸重新计算和脏标记,使表格内容在下次绘制时更新。
**使用场景:** 适用于一次性导入整批数据的情形,例如应用初始化时加载整个数据表。调用此函数可以方便地用初始化列表直接提供多行初始数据,而无需多次重复调用单行的 `setData`。在需要清空已有数据并填入新数据时可以在调用此方法前手动清除旧数据例如重新创建Table或使用返回的data容器修改
#### setRowsPerPage(int rows)
**函数原型:** `void setRowsPerPage(int rows)`
**参数:**
- `rows`:每页显示的数据行数(正整数)。
**返回值:** 无。
**功能描述:** 设置表格分页时每页包含的数据行数。调用此函数会修改内部的 `rowsPerPage` 属性,并根据当前总数据行数重新计算总页数 `totalPages`[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L332-L338)。如果重新计算后总页数低于1则强制设为1页避免出现0页的非法状态。改变每页行数将影响表格的高度和分页布局因此会标记需要重新计算单元格大小以及重新绘制。在下次 `draw()` 时,表格将按照新的分页大小重新呈现内容。
**使用场景:** 当需要调整表格分页密度时使用。例如默认每页显示5行数据可以通过 `table.setRowsPerPage(10);` 改为每页10行以减少总页数。常用于根据用户偏好或窗口大小调整每页显示量。应注意如果当前页码在调整后超出新的总页数范围表格在下次翻页时会自动纠正到最后一页。
#### showPageButton(bool isShow)
**函数原型:** `void showPageButton(bool isShow)`
**参数:**
- `isShow`:布尔值,指定是否显示翻页按钮。`true` 表示显示 “上一页” 和 “下一页” 按钮,`false` 则隐藏它们。
**返回值:** 无。
**功能描述:** 控制表格分页导航按钮的可见性。调用此函数可以在界面上隐藏或显示表格底部的翻页按钮。如果设置为隐藏 (`false`),则用户将无法通过界面按钮切换页码(此时表格将始终停留在当前页);当再次显示按钮 (`true`) 时,用户可以使用按钮查看其他页的数据。该函数仅影响按钮的绘制和事件处理状态,不改变当前页码等分页数据。
**使用场景:** 适用于当数据量较少仅一页即可显示完毕,或开发者希望以其他方式控制分页时。例如,当表格内容只有一页数据时,可以隐藏翻页按钮以简化界面。需要再次启用分页功能时再调用 `showPageButton(true)` 显示按钮。
#### setTableBorder(COLORREF color)
**函数原型:** `void setTableBorder(COLORREF color)`
**参数:**
- `color`:表格边框颜色,类型为 `COLORREF`Windows RGB 颜色值,例如 `RGB(0,0,0)` 表示黑色)。
**返回值:** 无。
**功能描述:** 设置表格单元格边框线的颜色。调用该方法会将 Table 内部存储的边框颜色更新为指定的 `color`,并将表格标记为需要重绘。在随后的 `draw()` 调用中,表格的线条(包括单元格边框和外围边框)将使用新的颜色绘制[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L192-L199)。默认的边框颜色为黑色,如需与界面主题匹配可使用此函数调整。
**使用场景:** 当需要改变表格线条颜色以配合UI配色方案时使用。例如将表格边框设为浅灰色以获得柔和的视觉效果`table.setTableBorder(RGB(200,200,200));`。在深色模式下,可将边框设为亮色以提高对比度。调用后下一次重绘将体现颜色变化。
#### setTableBk(COLORREF color)
**函数原型:** `void setTableBk(COLORREF color)`
**参数:**
- `color`:表格背景填充颜色,类型为 `COLORREF`。
**返回值:** 无。
**功能描述:** 设置表格单元格的背景颜色。调用该函数会更新 Table 内部的背景色属性,并触发重绘标记。在绘制表格时,每个单元格将使用指定的背景颜色进行填充(当填充模式为实色时)[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L192-L199)。默认背景颜色为白色,如需改变单元格底色(例如间隔行着色等)可以使用不同的颜色多次调用或自定义绘制。
**使用场景:** 用于调整表格的底色风格。例如,将表格背景设为浅黄色以突出显示内容区域:`table.setTableBk(RGB(255, 255, 224));`。结合填充模式使用可以实现透明背景等效果。注意背景颜色变化在填充模式为Solid时可见若填充模式为Null则背景不绘制实色。
#### setTableFillMode(StellarX::FillMode mode)
**函数原型:** `void setTableFillMode(StellarX::FillMode mode)`
**参数:**
- `mode`:填充模式,枚举类型 `StellarX::FillMode`。可选值包括 Solid实色填充、Null不填充、Hatched/Pattern 等。
**返回值:** 无。
**功能描述:** 设置表格的填充模式,即单元格背景的绘制方式。常用的模式有 Solid纯色填充背景或 Null透明背景不填充。当前版本中仅对 Solid 和 Null 模式提供完全支持:如果传入其他模式值,内部会默认退回使用 Solid 模式[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L360-L369)。调用此方法会应用新的填充模式,并将此模式同步到表格的翻页按钮和页码标签控件上,使它们风格一致[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L365-L373)。例如,当设置为 Null 模式时,表格单元格背景将不被填充颜色,呈现透明效果,同时页码标签也设置为透明背景显示。改变填充模式会将表格及相关控件标记为需重绘。
**使用场景:** 根据界面设计需要调整表格背景绘制策略。例如,为实现镂空透明效果,可使用 `table.setTableFillMode(StellarX::FillMode::Null);` 使表格仅绘制文字和边框而无底色。默认使用 Solid 模式填充背景,实现传统表格外观。当需要图案填充等高级效果时,当前版本可能不支持,仍会以纯色替代。
#### setTableLineStyle(StellarX::LineStyle style)
**函数原型:** `void setTableLineStyle(StellarX::LineStyle style)`
**参数:**
- `style`:边框线型,枚举类型 `StellarX::LineStyle`,如 Solid实线、Dash虚线、Dot点线等。
**返回值:** 无。
**功能描述:** 设置表格网格线(单元格边框线条)的样式。传入不同的线型枚举值可以改变表格绘制时边框的样式,例如使用虚线或点线来绘制单元格边框。调用该方法将更新内部存储的线型并标记表格需要重绘,下次绘制时新的线型将生效。需注意实际效果取决于绘图库对线型的支持。
**使用场景:** 当希望改变表格外观以区分层次或提升美观时,可修改线型。例如,`table.setTableLineStyle(StellarX::LineStyle::Dash);` 将表格边框改为虚线风格,以在视觉上弱化表格线对界面的干扰。此功能在打印报表或特殊主题界面中会很有用。
#### setTableBorderWidth(int width)
**函数原型:** `void setTableBorderWidth(int width)`
**参数:**
- `width`:边框线宽度(像素值,正整数)。
**返回值:** 无。
**功能描述:** 设置表格边框线条的粗细线宽。默认情况下表格边框宽度为1像素调用此函数可使所有单元格的边框线加粗或变细。更新线宽后表格将在下次重绘时使用新的线宽绘制边框[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L198-L201)。如果设置的宽度大于1为避免粗线覆盖过多背景Table 在绘制时会调整背景区域的恢复以适应粗线描边[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L220-L228)[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L240-L248)。
**使用场景:** 在需要强调表格结构或适应高分辨率显示时,可增加线宽。例如,`table.setTableBorderWidth(2);` 将表格线条加粗为2像素使表格框架更加醒目。若要获得细网格效果可以将线宽设置为1以下不过线宽最小为1像素。改变线宽后请确保颜色、线型等设置搭配以获得预期视觉效果。
### 获取属性方法Getters
以下函数用于获取 Table 当前的状态或配置信息:
- **int getCurrentPage() const**获取当前显示的页码索引。第一页为1第二页为2以此类推[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L390-L398)。可用于在界面上显示当前页码或在程序逻辑中判断分页位置。
- **int getTotalPages() const**:获取根据当前数据行数和每页行数计算出的总页数[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L394-L402)。通常在设置完数据或调整分页参数后调用,以了解数据被分页成多少页。
- **int getRowsPerPage() const**:获取当前每页显示的数据行数设置[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L400-L408)。可用于在界面上提示用户分页大小,或在逻辑中根据该值调整分页处理。
- **bool getShowPageButton() const**获取当前翻页按钮的显示状态true 为显示false 为隐藏)[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L404-L412)。可根据此值判断界面上是否提供了翻页导航。
- **COLORREF getTableBorder() const**:获取当前表格边框颜色[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L410-L418)返回一个COLORREF值。可以用于在需要时与其他控件颜色保持一致或者保存当前配置。
- **COLORREF getTableBk() const**:获取当前表格背景填充颜色[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L414-L422)。
- **StellarX::FillMode getTableFillMode() const**:获取当前表格的填充模式设置[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L420-L428)。返回值为 `StellarX::FillMode` 枚举,可判断是实色填充、透明等模式。
- **StellarX::LineStyle getTableLineStyle() const**:获取当前表格边框线型设置[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L424-L432)。返回 `StellarX::LineStyle` 枚举值,如实线、虚线等。
- **std::vectorstd::string getHeaders() const**:获取当前表格的表头列表副本[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L430-L438)。返回一个字符串向量,其中包含每一列的标题。可以使用此函数查看或保存表格的列标题设置。
- **std::vector<std::vectorstd::string> getData() const**:获取当前表格的所有数据[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L434-L442)。返回一个二维字符串向量,其中每个内部向量代表表格中的一行数据(各元素对应该行每列的值)。这允许访问或遍历表格内容,例如用于导出数据。
- **int getTableBorderWidth() const**:获取当前设置的表格边框线宽度(像素)[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L440-L446)。
以上获取函数均为 `const` 成员函数,不会修改 Table 的状态,只返回相应属性的值。开发者可根据需要随时调用这些函数来查询表格状态,例如在界面其他区域显示统计信息或在调试时验证设置是否正确。
### 公共成员变量
- **StellarX::ControlText textStyle**:文本样式结构体,表示表格中文字内容的字体和样式设置[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/include/StellarX/CoreTypes.h#L132-L140)。通过修改该结构体的成员,可以自定义表格文本的外观,例如字体字号、高度、宽度、字体名称 (`lpszFace`),文字颜色 (`color`),以及是否加粗 (`nWeight`)、斜体 (`bItalic`)、下划线 (`bUnderline`) 等[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/include/StellarX/CoreTypes.h#L134-L142)。Table 默认使用框架的默认字体(例如微软雅黑)和颜色绘制文字。开发者可以直接访问并调整 `textStyle` 的属性,然后调用 `draw()` 重新绘制,使新的文本样式生效。例如,`table.textStyle.nHeight = 20; table.textStyle.color = RGB(0,0,128);` 将文字调大并改为深蓝色。值得注意的是Table 内部的翻页按钮和页码标签也会同步使用 Table 的 `textStyle` 设置,以保持表格组件整体风格的一致。
### 示例
下面是一个使用 Table 控件的示例代码[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/README.md#L268-L276)[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/README.md#L278-L286),演示如何创建表格、设置表头和数据、配置分页,并将其添加到窗口中显示:
```
// 创建一个表格控件,位置在 (50, 50)
auto myTable = std::make_unique<Table>(50, 50);
// 设置表头(列名称)
myTable->setHeaders({ "ID", "姓名", "年龄", "职业" });
// 添加数据行
myTable->setData({ "1", "张三", "25", "工程师" });
myTable->setData({ "2", "李四", "30", "设计师" });
myTable->setData({ "3", "王五", "28", "产品经理" });
// 设置每页显示2行数据
myTable->setRowsPerPage(2);
// 设置表格文本和样式
myTable->textStyle.nHeight = 16; // 文本字体高度
myTable->setTableBorder(RGB(50, 50, 50)); // 表格边框颜色(深灰)
myTable->setTableBk(RGB(240, 240, 240)); // 表格背景色(浅灰)
// 将表格控件添加到主窗口(假设 mainWindow 已创建)
mainWindow.addControl(std::move(myTable));
```
上述代码首先创建了一个 Table 实例并依次设置表头和三行示例数据。接着将每页行数设为2行这意味着数据将分页为两页3条数据在每页2行的设置下将分两页显示。然后调整了文本样式和颜色将字体高度设为16像素边框颜色设为灰色背景色设为浅灰以定制表格外观。最后将配置好的表格控件添加到主窗口中使其随着窗口一起绘制和响应用户操作。在实际应用中添加控件后通常需要调用主窗口的 `draw()` 来绘制界面,及调用 `runEventLoop()` 进入消息循环以响应用户交互。通过上述流程,开发者即可在应用程序窗口中成功集成并显示一个功能完善的分页表格控件。
以上即为 StellarX 框架各主要组件的接口说明和使用方法。通过这些类和函数,开发者可以组合出完整的 GUI 应用。请注意StellarX 基于 EasyX 实现,适用于 Windows 平台的教学和轻量工具开发场景。使用时需要确保 EasyX 图形库已初始化,并在应用结束前调用 `closegraph()` 释放图形资源。

207
CHANGELOG.en.md Normal file
View File

@@ -0,0 +1,207 @@
# Changelog
All notable changes to the StellarX project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
[中文文档](CHANGELOG.md)
## [v2.1.0] - 2025-10-27
**Focus**: Resizable/maximized window (EasyX reinforced by Win32), first-phase layout manager (HBox/VBox), early Tabs control. We also fixed black borders, maximize “ghosts”, flicker, and the issue where controls only appeared after interaction. Control-level **background snapshot/restore**, **single-line truncation**, and **tooltips** are now standardized.
### ✨ Added
- **Window resize/maximize reinforcement (EasyX + Win32)**
- `Window::enableResize(bool enable, int minW, int minH)`; toggle at runtime and set min track size.
- Subclassed WndProc for `WM_GETMINMAXINFO / WM_SIZE / WM_EXITSIZEMOVE / WM_ERASEBKGND / WM_PAINT` with `WS_THICKFRAME | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_CLIPCHILDREN | WS_CLIPSIBLINGS`.
- **Full-surface background paint** in `WM_PAINT` (solid/image), removing black borders and maximize ghosts.
- **One-shot redraw** on resize with `pendingW/H + needResizeDirty` coalescing.
- **Layout manager (phase 1)**
- On `Canvas`: `LayoutKind::{Absolute, HBox, VBox, Grid(placeholder), Flow(placeholder), Stack(placeholder)}` with `LayoutParams` (`margins`, `fixedW/fixedH(-1=same)`, `weight`, `Align{Start,Center,End,Stretch}`).
- Implemented **HBox/VBox** auto layout inside containers; containers stay absolutely positioned; nesting supported.
- **Tabs control (early)**
- Decoupled tab strip and page container; background snapshot for transparent themes.
- **Button single-line truncation (MBCS; CN/EN aware)**
- Pixel-width threshold with `...` avoiding half-glyph artifacts.
- **Hover tooltip**
- Implemented via `Label` with delay & auto-hide; per-control background snapshot/restore; customizable text with sensible fallback.
### 🔧 Changed
- **Window rendering path**
- Reduced reliance on “offscreen frame blit”; in `WM_PAINT` use **GDI full background + EasyX batch drawing** (`BeginBatchDraw/EndBatchDraw + FlushBatchDraw`) to suppress flicker.
- On resize, only rebuild the scaled background (`zoomBackground`); actual painting happens next frame.
- **Control base**
- Standardized **captureBackground/restoreBackground**; transparent/stacked visuals are stable.
- Unified `dirty`: containers paint their own background when dirty; **children still evaluate/draw** as needed each frame.
- **Table**
- In transparent mode, pagination widgets now inherit table text/fill style.
- Reworked **pagination math + block centering** (header included in available width).
- Fixed background snapshot sizing (header inclusion) that caused failed restores.
- **Event loop**
- Coalesced `WM_SIZE` to the loop tail to avoid redraw storms and pointer-hover lag.
### ✅ Fixed
- **Black borders / maximize ghost / flicker**: blocked system background erase; full-surface paint in `WM_PAINT`; cleared clipping to prevent stale fragments.
- **Containers not drawn; controls only after interaction**: first-frame & post-input full-tree dirty; children draw even if the container isnt dirty.
- **Table pagination overlap & transparent shadowing**: corrected snapshot area; recomputed coordinates; instant restore + redraw after paging.
### ⚠️ Breaking
- If external code accessed `Window` private members (e.g., `dialogs`), use `getControls()` / `getdialogs()`.
- Pagination math & header inclusion may shift hard-coded offsets in custom renderers.
- Custom controls that dont restore `SetWorkingImage(nullptr)` before drawing should be reviewed.
### 📌 Upgrade notes
1. Migrate manual `cleardevice()+putimage` paths to unified **full-surface background** in `WM_PAINT`.
2. For transparent controls, `captureBackground()` before first draw; `restoreBackground()` when hiding/overdrawing.
3. For layout, set container `layout.kind = HBox/VBox` and child `LayoutParams` (`margin/fixed/weight/align`).
4. Keep a **single** truncation pass for buttons to avoid duplicate `...`.
5. Prefer built-in table pagination.
## [v2.0.1] - 2025-10-03
### Added
- New example: 32-bit register viewer tool implemented based on StellarX (supports bit inversion, left shift, right shift, hexadecimal/decimal signed/unsigned toggle, grouped binary display).
- Example path: `examples/register-viewer/`
- `TextBox` added `setText` API, allowing external setting of text box content
- `TextBox::setText` API modified: immediately calls `draw` method to redraw after setting text
- `Button` added `setButtonClick` API, allowing external functions to modify button click state and execute corresponding callback functions
- ==All documents updated with corresponding English versions(.en)==
## [v2.0.0] - 2025-09-21
### Overview
v2.0.0 is a major release. This release introduces dialog and message box factory (Dialog / MessageBox), with several important fixes and improvements to event distribution, API semantics, and internal resource management.
Some APIs/behaviors have breaking changes that are not backward compatible.
### Added
- **Dialog System**:
- Added `Dialog` class, inheriting from `Canvas`, for building modal and non-modal dialogs
- Added `MessageBox` factory class, providing two convenient APIs: `ShowModal` (synchronous blocking) and `ShowAsync` (asynchronous callback)
- Supports six standard message box types: `OK`, `OKCancel`, `YesNo`, `YesNoCancel`, `RetryCancel`, `AbortRetryIgnore`
- Automatically handles dialog layout, background saving and restoration, event propagation, and lifecycle management
- **Enhanced Event System**:
- All controls' `handleEvent` methods now return `bool` type, indicating whether the event was consumed
- Introduced event consumption mechanism, supporting finer-grained event propagation control
- Window class event loop now prioritizes dialog event processing
- **Control State Management**:
- Control base class added `dirty` flag and `setDirty()` method, uniformly managing redraw state
- All controls now implement `IsVisible()` and `model()` methods
- **API Enhancements**:
- Button class added `setButtonFalseColor()` method
- TextBox class `setMaxCharLen()` now accepts `size_t` type parameter
- Window class added dialog management methods and duplicate detection mechanism
### Breaking Changes
- **API Signature Changes**:
- All controls' `handleEvent(const ExMessage& msg)` method changed from returning `void` to returning `bool`
- Control base class added pure virtual functions `IsVisible() const` and `model() const`, all derived classes must implement them
- **Resource Management Changes**:
- Control base class style saving changed from stack objects to heap objects, managed using pointers
- Enhanced resource release safety, all resources are properly released in destructors
- **Event Handling Logic**:
- Window's `runEventLoop()` method completely rewritten, now prioritizes dialog events
- Introduced event consumption mechanism, events do not continue propagating after being consumed
### Fixed
- **Memory Management**:
- Fixed memory leak issue in `Button::setFillIma()`
- Fixed resource release issues in Control base class destructor
- Fixed background image resource management issues in Dialog class
- **Layout and Rendering**:
- Fixed pagination calculation, column width, and row height boundary issues in `Table` component
- Fixed layout disorder caused by `pX` coordinate accumulation error in `Table`
- Fixed dirty determination issue in `Canvas::draw()` that prevented child controls from being drawn
- Fixed asymmetric call issues between `TextBox::draw()` and `restoreStyle()`
- **Event Handling**:
- Fixed window event distribution logic to ensure dialog/top-level controls prioritize event processing
- Fixed delayed state updates when mouse moves out of button area
- Fixed race conditions in non-modal dialog event handling
- **Other Issues**:
- Fixed potential errors in text measurement and rendering
- Fixed incomplete background restoration after dialog closure
- Fixed z-order management issues in multi-dialog scenarios
### Changed
- **Code Quality**:
- Refactored numerous internal APIs, enhancing exception safety and maintainability
- Used smart pointers and modern C++ features to replace raw new/delete
- Unified code style and naming conventions
- **Performance Optimization**:
- Optimized event processing flow, reducing unnecessary redraws
- Improved dialog background saving and restoration mechanism
- Reduced memory allocation and copy operations
- **Documentation and Examples**:
- Added detailed usage examples for all new features
- Improved code comments and API documentation
- Updated README and CHANGELOG to reflect latest changes
## [1.1.0] - 2025-09-08
### Added
- **Window Class API Enhancements**:
- Added complete getter method set, improving class encapsulation and usability
- `getHwnd()` - Get window handle for integration with native Windows API
- `getWidth()` - Get window width
- `getHeight()` - Get window height
- `getHeadline()` - Get window title
- `getBkcolor()` - Get window background color
- `getBkImage()` - Get window background image pointer
- `getControls()` - Get reference to control management container, allowing iteration and manipulation of added controls
### Improved
- **API Consistency**: Provided symmetric setter and getter methods for all important attributes
- **Code Documentation**: Further improved class comments, making them clearer and more professional
## [1.0.0] - 2025-09-08
### Release Summary
First stable release
### Added
- First stable version of StellarX framework
- Complete control library: Button, Label, TextBox, Canvas, Table, and Window
- CMake-based build system
- Detailed documentation and example code
- **Explicit declaration: This framework is specifically designed for Windows platform**
### Released
- **First release of pre-compiled binary library files**, facilitating quick integration without compiling from source
- Provided release packages include:
- `StellarX-v1.0.0-x64-Windows-msvc-x64.zip`
- **Build Environment**: Visual Studio 2022 (MSVC v143)
- **Architecture**: x64 (64-bit)
- **Runtime Library**: `/MD`
- **Build Modes**: `Release | Debug`
- **Contents**: Includes all necessary header files (`include/`) and static library files (`lib/StellarX-Debug.lib StellarX-Release.lib`)
### Core Features
- Modular design following SOLID principles
- Unified control interface (`draw()` and `handleEvent()`)
- Support for multiple control shapes and styles
- Custom event handling callbacks
- Lightweight design with no external dependencies
## [0.1.0] - 2025-08-15
### Added
- Initial project structure and core architecture
- Control base class and basic event handling system
- Basic examples and documentation setup

View File

@@ -5,7 +5,74 @@ StellarX 项目所有显著的变化都将被记录在这个文件中。
格式基于 [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
并且本项目遵循 [语义化版本](https://semver.org/lang/zh-CN/)。
[English document](CHANGELOG.en.md)
## [v2.1.0] - 2025-10-27
**重点**:窗口可拉伸/最大化补强EasyX + Win32、布局管理器HBox/VBox 第一阶段)、选项卡控件雏形;系统性修复黑边、最大化残影、频闪与“控件需交互才出现”等历史问题。并统一了**背景快照/恢复**、**按钮单行截断**、**Tooltip** 的机制。
### ✨ 新增
- **窗口拉伸 / 最大化补强(在 EasyX 基础上用 Win32 加固)**
- 新增 `Window::enableResize(bool enable, int minW, int minH)`;运行时开关可拉伸并设置最小跟踪尺寸。
- 子类化窗口过程:处理 `WM_GETMINMAXINFO / WM_SIZE / WM_EXITSIZEMOVE / WM_ERASEBKGND / WM_PAINT`,并启用 `WS_THICKFRAME | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_CLIPCHILDREN | WS_CLIPSIBLINGS`,解决 EasyX 窗口原生不可拉伸问题。
- **整窗背景绘制**:在 `WM_PAINT` 用 GDI 直写客户区(纯色/图片均支持),彻底消除黑边与最大化残影。
- **一次性重绘**:合并连续 `WM_SIZE`,使用 `pendingW/H + needResizeDirty` 在主循环尾部统一置脏重绘,避免死循环与抖动。
- **布局管理器(第一阶段)**
-`Canvas` 引入布局元数据:`LayoutKind::{Absolute, HBox, VBox, Grid(预留), Flow(预留), Stack(预留)}``LayoutParams``margin{L,R,T,B}``fixedW/fixedH(-1=沿用)``weight``Align{Start,Center,End,Stretch}`)。
- 实装 **HBox/VBox** 自动布局(容器内部自动排布;容器本身仍绝对定位,可嵌套)。
- **选项卡Tabs控件雏形**
- 页签条与页面容器解耦;多页签切换;为透明主题提供**背景快照**避免叠影。
- **按钮文本单行截断MBCS中/英分治)**
- 基于像素宽度阈值的 `...` 截断,避免半字节/半汉字。
- **悬停提示Tooltip**
-`Label` 为载体,支持延时出现、自动隐藏、自定义文案(默认回退到按钮文本);使用控件级**背景快照/恢复**。
### 🔧 变更
- **Window 渲染路径**
- 弱化“离屏 frame→整屏贴图”的依赖`WM_PAINT`**GDI 整窗背景 + EasyX 批量绘制**`BeginBatchDraw/EndBatchDraw + FlushBatchDraw`)以抑制频闪。
- 尺寸变化仅重建 `zoomBackground`(图片背景的缩放副本),显示延后到下一帧。
- **Control 基类**
- 标准化 **captureBackground/restoreBackground**;统一透明/叠放控件行为。
- 统一 `dirty` 语义:容器自身脏才重画背景,但**子控件每帧评估并按需绘制**,不再出现“容器不脏→子控件不画”的空窗。
- **Table**
- 透明模式下,分页按钮与页码标签跟随表格的文本/填充风格,避免风格漂移。
- **分页度量与整体居中**重做:可用宽度含表头;“上一页/下一页+页码”作为**一个块**水平居中。
- 修复**背景快照区域**未计入表头导致恢复失败的问题。
- **事件循环**
- 合并 `WM_SIZE` 到循环尾统一处理,降低输入延迟与重绘风暴。
### ✅ 修复
- **黑边 / 最大化残影 / 频闪**`WM_ERASEBKGND` 返回 1 禁止系统擦背景;`WM_PAINT` 全面覆盖;清空裁剪区防止旧帧残留。
- **容器不画、控件需交互后才出现**:首帧与输入后触发**全树置脏**;子控件在容器未脏时也能正常绘制。
- **Table 翻页重叠与透明叠影**:修正快照区域、重算坐标并即时恢复+重绘。
### ⚠️ 可能不兼容
- 若外部代码直接访问 `Window` 私有成员(如 `dialogs`),请改用 `getControls()` / `getdialogs()`
- `Table` 的分页与表头度量变化可能影响外部硬编码偏移;需要对齐新公式。
- 自定义控件若未遵循绘制前 `SetWorkingImage(nullptr)` 的约定,请自查。
### 📌 升级指引
1. **窗口背景**:将手工 `cleardevice()+putimage` 迁移到 `WM_PAINT` 的**整窗覆盖**流程。
2. **透明控件**:首绘前 `captureBackground()`,隐藏/覆盖时 `restoreBackground()`
3. **布局**:容器设 `layout.kind = HBox/VBox`;子项用 `LayoutParams``margin/fixed/weight/align`)。
4. **按钮截断**:保持单次截断,避免重复 `...`
5. **表格**:移除自绘分页,使用内置导航。
## [v2.0.1] - 2025 - 10 - 03
### 新增
- 新增示例:基于 StellarX 实现的 32 位寄存器查看工具(支持位取反、左移、右移,十六进制/十进制带符号/无符号切换,分组二进制显示)。
- 示例路径:`examples/register-viewer/`
- `TextBox`新增`setText`API可在外部设置文本框内容
- `TextBox::setText`API修改在设置文本后立即调用`draw`方法重绘
- `Button`新增`setButtonClick`API,允许通过外部函数修改按钮的点击状态,并执行相应的回调函数
- ==所有文档更新对应英文版本==
## [v2.0.0] - 2025-09-21

94
CONTRIBUTING.en.md Normal file
View File

@@ -0,0 +1,94 @@
# Contributing to StellarX
Thank you for your interest in contributing to StellarX! This document provides guidelines and steps for contributing.
StellarX is a C++ GUI framework built for the **Windows platform**, based on the EasyX graphics library.
## Development Environment Setup
1. Install Visual Studio 2019 or later
2. Install the corresponding version of EasyX graphics library
3. Install CMake 3.12 or later
4. Clone the project repository
5. Use CMake to generate the solution and compile
## How to Contribute
### Reporting Bugs
1. Check [Issues](../../issues) to see if the bug has already been reported.
2. If not, create a new Issue.
3. Use the "Bug Report" template.
4. Provide a **clear title and description**.
5. Include relevant code snippets, screenshots, or steps to reproduce the issue.
### Suggesting Enhancements
1. Check existing Issues to see if your idea has been suggested.
2. Create a new Issue using the "Feature Request" template.
3. Clearly describe the new feature and explain why it would be useful.
### Submitting Code Changes (Pull Requests)
1. **Fork** the repository on GitHub.
2. **Clone** your forked repository to your local machine.
3. Create a **new branch** for your feature or bug fix (`git checkout -b my-feature-branch`).
4. **Make your changes**. Ensure your code follows the project's style (see below).
5. **Commit your changes** with clear, descriptive commit messages.
6. **Push** your branch to your forked GitHub repository (`git push origin my-feature-branch`).
7. Open a **Pull Request** against the original StellarX repository's `main` branch.
## Code Style Guide
* Follow the existing code formatting and naming conventions in the project.
* Use meaningful names for variables, functions, and classes.
* Comment your code when necessary, especially for complex logic.
* Ensure your code compiles without warnings.
* Test your changes thoroughly.
* Use **4 spaces** for indentation (no tabs)
* Class names use **PascalCase** (e.g., `ClassName`)
* Functions and variables use **camelCase** (e.g., `functionName`, `variableName`)
* Constants use **UPPER_CASE** (e.g., `CONSTANT_VALUE`)
* Member variables use **m_** prefix (e.g., `m_memberVariable`)
* Use meaningful names for control properties, avoid abbreviations
* Add detailed comments for all public interfaces
* Follow RAII principles for resource management
## Example Code Style
```c++
// Good Example
class MyControl : public Control {
public:
MyControl(int x, int y, int width, int height)
: Control(x, y, width, height), m_isActive(false) {}
void draw() override {
// Drawing logic
}
private:
bool m_isActive;
};
// Bad Example
class my_control: public Control{
public:
my_control(int x,int y,int w,int h):Control(x,y,w,h),active(false){}
void Draw() override{
// Drawing logic
}
private:
bool active;
};
```
## Project Structure
Please follow the project's directory structure:
- Header files go in the `include/StellarX/` directory
- Implementation files go in the `src/` directory
- Example code goes in the `examples/` directory
## Questions?
If you have any questions, feel free to open an Issue or contact the maintainers.

View File

@@ -0,0 +1,872 @@
# English API Documentation
[API文档](API 文档.md)
Below is the API documentation for the **StellarX GUI framework** classes and functions, in English. It covers the usage, parameters, and behavior of each primary component:
### Control Class (Abstract Base Class)
**Description:** `Control` is the abstract base class for all GUI controls. It defines common properties and interfaces such as position, size, and dirty (redraw) flags. It also implements saving and restoring of drawing state to ensure that drawing a control does not affect global graphics state. `Control` itself is not instantiated directly.
- **Key Properties:**
- `x, y`: Coordinates of the control's top-left corner.
- `width, height`: Dimensions of the control.
- `dirty`: A flag indicating whether the control needs to be redrawn.
- `show`: Visibility flag (whether the control is visible).
- `rouRectangleSize`: A `StellarX::RouRectangle` struct that stores the ellipse width and height for rounded rectangle corners (used by controls with round-rect shape).
- **Note:** `Control` internally maintains pointers (`currentFont`, etc.) to backup current drawing state (font, colors, line style) so that it can restore the state after custom drawing.
- **Main Interface Methods:**
- `virtual void draw() = 0;`
**Description:** Pure virtual function to draw the control. Each derived control class must implement its own drawing logic.
- `virtual bool handleEvent(const ExMessage& msg) = 0;`
**Description:** Pure virtual function to handle an input event message (mouse or keyboard). Returns true if the event is consumed by this control (meaning it should not propagate further).
- `void saveBackground(int x, int y, int w, int h);`
**Description:** Saves a background snapshot of the specified area `(x, y, w, h)` (in screen coordinates) where the control is drawn. Used to restore background when needed (e.g., hiding a popup).
- `void restBackground();`
**Description:** Restores the last saved background image by putting it back to the saved position. Typically used when a control is hidden or needs to erase its drawing.
- `void discardBackground();`
**Description:** Discards the currently saved background snapshot and frees its resources. Should be called when the saved background is no longer valid (e.g., after window resize) to avoid using stale snapshots.
- *Property Accessors and Mutators:*
- `int getX() const, getY() const, getWidth() const, getHeight() const`: Get the control's position and size.
- `int getRight() const, getBottom() const`: Get the coordinate of the control's right boundary (`x + width`) and bottom boundary (`y + height`).
- `void setX(int nx), setY(int ny), setWidth(int w), setHeight(int h)`: Set the control's position or size. Setting these will mark the control as dirty (need redraw).
- `void setDirty(bool d)`: Manually mark the control as needing redraw or not.
- `void setShow(bool visible)`: Set the control's visibility (`show` flag). If set to false, the control will not draw itself.
- `bool isVisible() const`: Returns the current visibility state of the control (`show` flag). **Note:** If a control is hidden, its `draw()` is typically not called.
- *Other Methods:*
- `virtual bool model() const = 0;`
**Description:** Pure virtual function to check if the control is "modal". Only dialog controls need to implement this (modal dialogs return true), other controls can ignore (return false). The window event loop uses this to prioritize modal dialog events.
- `void saveStyle(); void restoreStyle();` *(protected)*
**Description:** Saves the current global drawing style (font, colors, line style, etc.) and restores it. Controls should call `saveStyle()` at the beginning of `draw()` to backup global settings, and `restoreStyle()` at the end of `draw()` to not pollute the global state for other drawings.
### Window Class (Application Main Window)
**Description:** The `Window` class represents the main application window. It manages the window creation, message loop, and acts as the root container for all controls and dialogs. Typically, an application creates one Window instance as the main GUI container.
- **Constructors:**
- `Window(int width, int height, int mode, COLORREF bkColor = ..., std::string headline = "窗口")`
**Description:** Creates a Window with specified width and height. `mode` is the graphics mode (such as double-buffering or manual flush flags in EasyX, use NULL for default). `bkColor` is the window background color, and `headline` is the window title text. Construction does not immediately display the window; you need to call `draw()` to open it.
- **Main Methods:**
- `void draw();`
**Description:** Initializes and opens the graphics window. It uses the specified mode to create the drawing window (calls EasyX `initgraph`), sets up the window's title and background color, and then draws all added controls. Usually called once right after creating the Window object.
- `void draw(std::string pImgFile);`
**Description:** Similar to the above, but uses an image file as the window background. This loads the image and draws it scaled to the window size, then draws all child controls on top.
- `void runEventLoop();`
**Description:** Enters the main event processing loop for the window. This loop continuously retrieves user input events (`peekmessage`) and dispatches them to child controls or dialogs:
- Dialogs (non-modal ones) get first priority: for each event, it iterates over `dialogs` and if a dialog is visible (`isVisible() == true`) and not modal (`model() == false`), it calls that dialog's `handleEvent`. If any dialog consumes the event (returns true), it stops further propagation.
- If no dialog consumed the event, it then iterates through `controls` (regular controls) in order and calls each control's `handleEvent` until one returns true (meaning the event was handled).
- After handling an event (or if none present), it checks if any dialog is open or was just closed (`dialogClose` flag). If so, it forces a redraw of the entire interface to keep things updated:
- It synthesizes a `WM_MOUSEMOVE` message to update hover states (ensuring controls properly update their highlight states), then calls `draw()` on all controls and dialogs, and flushes the drawing.
- It resets `dialogClose` to false after the redraw.
- Sleeps for 10 ms at each loop iteration to prevent high CPU usage.
- The loop continues until a `WM_CLOSE` message is received (e.g., user closes the window), at which point it sets `running` false and breaks out.
- `void setBkImage(IMAGE* img);` / `void setBkImage(std::string filePath);`
**Description:** Changes the window's background image. You can pass an already loaded `IMAGE*`, or specify a file path to load. After setting, it immediately repaints the window (drawing all controls and dialogs) with the new background.
- `void setBkcolor(COLORREF c);`
**Description:** Sets the window background color and immediately clears the screen with that color (this does not remove an existing background image if one was set; it just overlays the color).
- `void setHeadline(std::string title);`
**Description:** Sets the window title text. If the window is already open, it updates the title bar via `SetWindowText` immediately.
- `void addControl(std::unique_ptr<Control> control);`
**Description:** Adds a regular control to the window. The Window maintains a list of such controls; `draw()` and `runEventLoop()` use this list for rendering and event dispatch. Ownership is transferred (control is managed by Window after adding).
- `void addDialog(std::unique_ptr<Control> dialog);`
**Description:** Adds a dialog control to the window (usually an instance of `Dialog`). Dialogs are managed separately from normal controls, with their own event and drawing handling.
- `bool hasNonModalDialogWithCaption(const std::string& caption) const;`
**Description:** Checks if there is a currently open **non-modal** dialog with the given title caption. Returns true if found. Typically used to avoid opening duplicate dialogs with the same purpose.
- **Overloaded variant:** `bool hasNonModalDialogWithCaption(const std::string& caption, const std::string& text) const;`
Additionally compares the dialog's message text. This provides a stricter duplicate check (same caption **and** same content). It is used internally by `MessageBox::showAsync` to prevent popping up the same notification multiple times.
- *Information Getters:*
- `HWND getHwnd() const;` Returns the underlying window handle (HWND from EasyX).
- `int getWidth() const, getHeight() const;` Returns the window's dimensions.
- `std::string getHeadline() const;` Returns the current window title string.
- `COLORREF getBkcolor() const;` Returns the background color.
- `IMAGE* getBkImage() const;` Returns the current background image pointer (if any).
- `std::vector<std::unique_ptr<Control>>& getControls();` Returns a reference to the list of child controls (for iteration or debugging; generally you don't modify this directly).
### Canvas Class (Container Control)
**Description:** `Canvas` is a container control that can hold child controls, grouping them with a unified background and border style, and enabling composite layouts. Canvas itself is a control (derives from `Control`), and typically used for panels or dialog surfaces.
- **Features:**
- Supports four rectangular background shapes (normal rectangle or rounded rectangle, each with border or borderless version). Set via `setShape`.
- Customizable background color (`canvasBkColor`), border color (`canvasBorderColor`), border line style (`canvasLineStyle`), and fill mode (`canvasFillMode`, e.g., solid color, hatched pattern, no fill).
- Automatically manages the lifecycle of child controls (added via `addControl`, and destroyed when Canvas is destroyed).
- When drawing, Canvas will draw its background and then iterate through its child controls to draw them. For event handling, it propagates events to children (with last-added getting first chance).
- **Important Members:**
- `std::vector<std::unique_ptr<Control>> controls;` The list of child controls.
- `StellarX::ControlShape shape;` Background shape of the container (default is RECTANGLE).
- `StellarX::FillMode canvasFillMode;` Background fill mode (default Solid color).
- `StellarX::LineStyle canvasLineStyle;` Border line style (default Solid line).
- `int canvaslinewidth;` Border line width in pixels.
- `COLORREF canvasBorderColor, canvasBkColor;` Border and background colors of the container.
- `StellarX::LayoutKind Kind;` (Reserved) Layout management type (Absolute/HBox/VBox/Grid). Not fully implemented but can be used to indicate layout strategy.
- **Note:** Canvas overrides `isVisible()` to always return false because a Canvas itself is not considered a standalone visible entity for event loop prioritization (the event loop doesn't handle Canvas directly, only its children). This doesn't affect drawing or child event dispatch.
- **Main Methods:**
- `Canvas(); Canvas(int x, int y, int width, int height);`
**Description:** Constructors to create a Canvas container at position `(x,y)` with given size.
- `void addControl(std::unique_ptr<Control> control);`
**Description:** Adds a child control to the canvas. The child control's coordinates are relative to the canvas (Canvas does not reposition children automatically; you should set the child's position accordingly before adding). After adding, the Canvas is marked dirty so it will redraw with the new content.
- `void setShape(StellarX::ControlShape shape);`
**Description:** Sets the canvas background shape. Supports `RECTANGLE`, `B_RECTANGLE` (borderless rectangle), `ROUND_RECTANGLE` (rounded corners with border), `B_ROUND_RECTANGLE` (rounded without border). If a circular/ellipse shape is passed, Canvas does not support it (it will treat it as RECTANGLE internally).
- `void setCanvasFillMode(StellarX::FillMode mode);`
**Description:** Sets the background fill mode, e.g., Solid, Null (no fill), Hatched pattern, or pattern/bitmap fill. This influences how the background is drawn (using EasyX `setfillstyle`). Default is solid fill.
- `void setBorderColor(COLORREF color);` / `void setCanvasBkColor(COLORREF color);` / `void setCanvasLineStyle(StellarX::LineStyle style);` / `void setLinewidth(int width);`
**Description:** Set the container's border color, background color, border line style, and line width, respectively. Changing any of these will mark the Canvas dirty (to be redrawn).
- `void draw() override;`
**Description:** Draws the canvas background and all its child controls. Steps:
1. If not dirty, returns immediately (no redraw needed).
2. Saves current style and sets line color, fill color, fill style, and line style based on canvas properties.
3. Draws the background shape: e.g., calls `fillrectangle` or `fillroundrect` depending on `shape`. If an unsupported shape (Circle/Ellipse) was set, it defaults to rectangle.
4. Iterates over `controls` and calls each child's `setDirty(true)` then `draw()`, ensuring each child is redrawn on this canvas.
5. Restores the style and marks itself clean (`dirty = false`).
*Note:* Canvas uses `saveStyle()`/`restoreStyle()` to preserve global drawing state, and uses EasyX drawing functions to fill the background according to its shape.
- `bool handleEvent(const ExMessage& msg) override;`
**Description:** Propagates the event to its children in reverse order (so the last added child gets the event first). It iterates `controls` from end to start and calls each child's `handleEvent(msg)`. If any child returns true (meaning it consumed the event), Canvas stops and returns true. If none consumed it, returns false.
*Use case:* This ensures that in overlapping or layered controls, the topmost (last added) gets first crack at the event, implementing a basic Z-order.
- `void clearAllControls();` *(protected)*
**Description:** Clears all child controls from the canvas, deleting them. This releases all smart pointers in the `controls` list. Typically used in destructor or when resetting the UI. In normal use, you don't call this directly (the Canvas destructor will automatically free children).
### Label Class (Static Text Label)
**Description:** `Label` displays a static text string on the UI. It supports transparent background and custom text style, but does not handle user input (no interactive events). It is lightweight and intended for captions, status messages, etc.
- **Constructors:**
- `Label(); Label(int x, int y, std::string text = "标签", COLORREF textColor = BLACK, COLORREF bkColor = WHITE);`
**Description:** Creates a Label control at `(x,y)`. You can specify initial text, text color, and background color. The default text is "标签" (label in Chinese), default text color is black, background is white.
- **Key Properties:**
- `std::string text;` The text content displayed.
- `COLORREF textColor;` The text color.
- `COLORREF textBkColor;` The background color behind the text when not transparent.
- `bool textBkDisap;` Flag indicating if the background is transparent. If true, the label is drawn with a transparent background (so whatever is behind it shows through); if false, it draws an opaque rectangle behind the text using `textBkColor`.
- `StellarX::ControlText textStyle;` The text style struct (includes font face, size, weight, etc. as well as text color).
- **Note:** Label's width and height are initially 0; usually a Label's size is determined by its text content automatically when drawn. You normally don't need to set width/height for labels.
- **Main Methods:**
- `void setTextdisap(bool transparent);`
**Description:** Sets whether the label's background is transparent. If true, when drawing, the text is rendered with `TRANSPARENT` background mode (not overwriting the background behind it). If false, the text is drawn on an opaque background colored `textBkColor`.
- `void setTextColor(COLORREF color);` / `void setTextBkColor(COLORREF color);`
**Description:** Sets the text color and background color, respectively. After setting, the label is marked dirty for redraw.
- `void setText(std::string text);`
**Description:** Changes the label's displayed text content. Marks the label dirty (so it will redraw with the new text).
- `void draw() override;`
**Description:** Draws the label's text (and background if not transparent). It:
- Calls `saveStyle()`.
- If `textBkDisap` is true, calls `setbkmode(TRANSPARENT)`; otherwise `setbkmode(OPAQUE)` and sets `bkcolor` to `textBkColor`.
- Sets the text color via `settextcolor(textColor)` and font via `settextstyle` based on `textStyle`.
- Saves the area behind where the text will be drawn using `saveBackground` (so it can restore it later if needed).
- Writes the text at `(x,y)` using `outtextxy`.
- Restores the drawing style and marks `dirty = false`.
This approach ensures that if the label text is redrawn, the background behind it is handled properly.
- `bool handleEvent(...) override;`
**Description:** Label does not handle any events; it always returns false (meaning it never consumes events).
- `void hide();`
**Description:** Hides the label. Specifically, it uses `restBackground()` to put back the saved background (erasing the text from the screen) and then `discardBackground()` to free the snapshot, and sets `dirty = false`. This is typically used when a Label is serving as a transient tooltip or overlay and needs to be removed without causing a full screen redraw.
- **Usage Scenarios:**
Label is used for static text like descriptions, titles, or status information. You can adjust `textStyle` to change the font and size (by default, the font might be "微软雅黑" with height 0 meaning default height). For example:
```
Label *status = new Label(10, 10, "Ready", RGB(0,128,0));
status->textStyle.nHeight = 20; // set font size if needed
```
If you want the label to blend into a custom background, you can do:
```
status->setTextdisap(true);
```
to make the background transparent.
### Button Class (Button Control)
**Description:** `Button` provides a clickable button control, supporting both standard push-button behavior and toggle (on/off) behavior. It handles click and hover events, and allows setting various styles (shape, colors). Buttons are one of the primary interactive controls.
- **Operating Modes:** Determined by `StellarX::ButtonMode`:
- `NORMAL` Standard push-button. Each click triggers an action but does not maintain a pressed state.
- `TOGGLE` Toggle button. Each click changes the button's state (pressed vs not pressed) and triggers different callbacks for each state.
- `DISABLED` Disabled button. It does not respond to user clicks and typically displays in a grayed-out style with strikeout text.
- The mode can be changed via `setButtonMode(ButtonMode mode)`.
- **Appearance Shape:** Determined by `StellarX::ControlShape`:
- Supports rectangle (`RECTANGLE`/`B_RECTANGLE`), rounded rectangle (`ROUND_RECTANGLE`/`B_ROUND_RECTANGLE`), circle (`CIRCLE`/`B_CIRCLE`), and ellipse (`ELLIPSE`/`B_ELLIPSE`) eight shape options in total (`B_` prefix indicates borderless). Use `setButtonShape(ControlShape shape)` to set.
- Note: When switching shapes, ensure the button's width/height are appropriate (for circle/ellipse shapes, the drawing will use width and height differently; circle uses min(width,height)/2 as radius, ellipse uses width,height as bounding box).
- The button's border will be drawn for shapes without the `B_` prefix; borderless shapes omit the border.
- **Key Configurable Properties:**
- **Colors:**
- `buttonTrueColor` color when button is in pressed state (for toggle or momentary press in normal mode).
- `buttonFalseColor` color when button is not pressed (normal default state).
- `buttonHoverColor` color when mouse is hovering over the button.
- `buttonBorderColor` border outline color.
- **Fill:**
- `buttonFillMode` fill mode for the button background (solid, hatched pattern, custom pattern, custom image).
- `buttonFillIma` pattern style for hatched fills (if `FillMode == Hatched`).
- `buttonFileIMAGE` pointer to an `IMAGE` for custom image fill (if `FillMode == DibPattern`).
- **Text:**
- `text` the text label displayed on the button.
- `textStyle` text style (font face, size, weight, etc., including text color).
- Additionally, `cutText` is used internally if the text is too long to fit; the button can automatically truncate and add "..." or a Chinese ellipsis to fit.
- **Tooltip (hover hint):**
- `tipEnabled` whether a tooltip is enabled on hover.
- `tipTextClick` tooltip text for the NORMAL mode (single-state).
- `tipTextOn` / `tipTextOff` tooltip texts for toggle mode when the button is ON or OFF.
- `tipFollowCursor` if true, tooltip appears near the cursor; if false, tooltip appears at a fixed offset below the button.
- `tipDelayMs` delay in milliseconds before tooltip appears when hovering.
- `tipOffsetX, tipOffsetY` offset of tooltip position relative to cursor or button (depending on `tipFollowCursor`).
- `tipLabel` an internal `Label` object used to display the tooltip text.
- **Rounded Corner Size:** In round-rectangle shapes, the corner radii come from the inherited `rouRectangleSize` (`ROUND_RECTANGLEwidth` and `height`). Set via `setRoundRectangleWidth(int)` and `setRoundRectangleHeight(int)`.
- **Main Methods:**
- `void setOnClickListener(const std::function<void()>&& callback);`
**Description:** Sets the callback function to be invoked when the button is clicked in NORMAL mode. For a toggle button, this callback is called every time the button is clicked (but typically you might use toggle-specific callbacks instead). The callback is executed when the user releases the mouse button over the button.
- `void setOnToggleOnListener(const std::function<void()>&& callback);`
**Description:** Sets the callback for when a toggle button is switched to the "on/pressed" state.
- `void setOnToggleOffListener(const std::function<void()>&& callback);`
**Description:** Sets the callback for when a toggle button is switched to the "off/released" state.
- `void setButtonText(const char* text)` / `void setButtonText(std::string text);`
**Description:** Changes the button's label text. Accepts a C-string or an std::string. After changing, it recalculates the text width/height (for centering and truncation logic) and marks the button dirty to redraw.
- `void setButtonBorder(COLORREF border);` / `void setButtonFalseColor(COLORREF color);`
**Description:** Sets the border color and the default (false) state fill color, respectively.
- `void setFillMode(FillMode mode);` / `void setFillPattern(FillStyle pattern);` / `void setFillImage(const std::string& path);`
**Description:** Configures the background fill of the button:
- `setFillMode` changes the fill mode (solid, hatched pattern, custom bitmap, etc).
- If setting to a hatched pattern fill, call `setFillPattern` to choose the hatch style (`StellarX::FillStyle`).
- If setting to a custom image fill, call `setFillImage` with the image file path; it will load the image (resizing to button size) and store it in `buttonFileIMAGE`.
- `void setButtonClick(bool click);`
**Description:** Programmatically sets the button's "clicked" state.
- For NORMAL mode: setting true will simulate a click it triggers the onClick callback (if any) and then immediately resets to false (not pressed).
- For TOGGLE mode: setting true or false will force the button into that state and trigger the corresponding onToggle callback if provided.
- In any case, it marks the button dirty and redraws it (ensuring visual state update).
- **Note:** If the button is disabled, this method has no effect.
- `bool isClicked() const;`
**Description:** Returns whether the button is currently in the pressed state. This is primarily meaningful for toggle buttons (true if toggled on, false if off). For a normal button, this is typically false except during the brief moment of click handling.
- Other getters such as `getButtonText()`, `getButtonMode()`, `getButtonShape()`, `getFillMode()`, `getFillPattern()`, `getFillImage()`, `getButtonBorder()`, `getButtonTextColor()`, `getButtonTextStyle()`, `getButtonWidth()`, `getButtonHeight()` provide read access to the button's corresponding properties.
- `void draw() override;`
**Description:** Draws the button's background and text according to its current state and properties:
- Chooses the fill color based on state: if disabled, uses `DISABLEDCOLOUR` (gray) and applies a strike-out to the text; otherwise, if `click` is true (pressed or toggled on) uses `buttonTrueColor`, else if `hover` is true uses `buttonHoverColor`, otherwise `buttonFalseColor`.
- Sets transparent background mode for text, sets border color and text style (color, font).
- If `needCutText` is true, calls `cutButtonText()` to possibly truncate the text with ellipsis if it doesn't fit in the button width.
- Recalculates `text_width` and `text_height` if the text content or style changed (to ensure text is centered correctly).
- Sets the fill style for background (using `buttonFillMode`, pattern or image as needed).
- Draws the shape:
- For rectangle shapes: uses `fillrectangle` or `solidrectangle`.
- For round-rectangle: uses `fillroundrect` or `solidroundrect` with `rouRectangleSize` radii.
- For circle: uses `fillcircle` or `solidcircle` with radius = min(width,height)/2.
- For ellipse: uses `fillellipse` or `solidellipse` with bounding box corners.
- Draws the text:
- It calculates the position to center the text/cutText horizontally and vertically within the button.
- If `isUseCutText` is true (meaning the original text was truncated), it draws `cutText` (with "..." or Chinese ellipsis).
- Otherwise, draws the full `text`.
- Restores style and marks dirty false after drawing.
- `bool handleEvent(const ExMessage& msg) override;`
**Description:** Handles mouse events for the button:
- If the button is hidden (`show == false`), returns false immediately.
- Tracks the previous `hover` and `click` states to detect changes.
- If the message is `WM_MOUSEMOVE`, updates `lastMouseX/Y` for tooltip positioning.
- Determines `hover` state by checking if the mouse `(msg.x, msg.y)` is within the button's shape (calls `isMouseInCircle` or `isMouseInEllipse` for those shapes, otherwise simple rectangle bounds check).
- Handling `WM_LBUTTONDOWN`: if button is enabled and mouse is over it:
- In NORMAL mode: set `click = true` (button appears pressed), mark dirty, and mark event consumed.
- In TOGGLE mode: do nothing on down (toggle action is deferred to release).
- Handling `WM_LBUTTONUP`: if button is enabled and mouse is currently over it:
- In NORMAL mode: if it was pressed (`click` was true), trigger the onClick callback (if set), then set `click = false` (release it). Mark dirty, consume event, and hide tooltip (if any). Also flush the message queue of any pending mouse/keyboard events using `flushmessage` to prevent processing a duplicate click message.
- In TOGGLE mode: flip the `click` state (`click = !click`). If it becomes true, trigger onToggleOn callback; if false, trigger onToggleOff callback. Mark dirty, consume event, update tooltip text via `refreshTooltipTextForState()`, hide tooltip, and flush message queue similarly.
- Handling `WM_MOUSEMOVE`:
- If moving outside (not hover anymore) while in NORMAL mode and the button was pressed (`click == true`), then user is dragging out: set `click = false` (cancel the press) and mark dirty (so it returns to unpressed visual).
- If hover state changed (entered or exited), mark dirty.
- Tooltip management:
- If tooltip enabled (`tipEnabled`):
- If just hovered (hover true and oldHover false): record the timestamp via `tipHoverTick` and set `tipVisible = false` (starting hover timer).
- If just exited (hover false and oldHover true): immediately call `hideTooltip()` (which hides the tooltip label if it was visible).
- If still hovering and tooltip not yet visible:
- Check if current time minus `tipHoverTick` >= `tipDelayMs`; if so:
- Set `tipVisible = true`.
- Determine tooltip position: if `tipFollowCursor` is true, `tipX = lastMouseX + tipOffsetX`, `tipY = lastMouseY + tipOffsetY`. If false, perhaps `tipX = lastMouseX` (or button center) and `tipY = y + height` (so it appears below the button).
- Set the tooltip text: if `tipUserOverride` is true (meaning user explicitly set tooltip text via `setTooltipText` or `setTooltipTextsForToggle`), then:
- For NORMAL mode: always use `tipTextClick` (explicitly set text or maybe button text by default).
- For TOGGLE mode: use `click ? tipTextOn : tipTextOff`.
- If `tipUserOverride` is false (no explicit text provided):
- If mode is TOGGLE: default behavior might be to use `tipTextOn/Off` (which could be set to default values like "On"/"Off" or left empty).
- If mode is NORMAL: possibly do nothing (the code currently only sets text in else for toggle, leaving normal with no dynamic text unless overridden).
- Position the internal `tipLabel` at (tipX, tipY), mark it dirty.
- After updating tooltip state,
- If `hover` or `click` state changed compared to old states, mark the button dirty so it will redraw to reflect highlight or press/unpress.
- If the button is dirty, call `draw()` immediately for real-time feedback.
- If tooltip is enabled and now visible (`tipVisible`), call `tipLabel.draw()` to draw the tooltip label.
- Return true if the event was handled (for example, a click or certain modal conditions), else false.
- (In summary, the event is considered consumed if a click occurred or if the modal logic swallowed it. Hover alone doesnt consume the event.)
- **Usage Notes:**
Typically, you create a Button and set its callback like:
```
auto btn = std::make_unique<Button>(50, 50, 80, 30, "OK");
btn->setOnClickListener([](){ /* handle click */ });
```
For toggle functionality:
```
btn->setButtonMode(StellarX::ButtonMode::TOGGLE);
btn->setOnToggleOnListener([](){ /* handle toggle on */ });
btn->setOnToggleOffListener([](){ /* handle toggle off */ });
```
Adjusting appearance:
```
btn->textStyle.color = RGB(255,255,255); // white text
btn->setButtonFalseColor(RGB(100,150,200)); // default background
btn->setButtonBorder(RGB(80,80,80)); // dark gray border
btn->setButtonShape(StellarX::ControlShape::RECTANGLE);
```
To provide a tooltip on hover:
```
btn->tipEnabled = true;
btn->tipTextClick = "Click to confirm"; // for normal mode
// for toggle mode:
// btn->setTooltipTextsForToggle("On state hint", "Off state hint");
```
The Button control will handle highlighting itself when hovered, pressing down on click, toggling, etc. Ensure to keep the `runEventLoop` running so these UI feedbacks happen in real time.
### TextBox Class (Single-line Text Box)
**Description:** `TextBox` is a single-line text input box, which can either allow user input or be read-only. It uses an EasyX input box for user text entry (which is a modal popup), then displays the entered text in the control. It's a simple text field primarily for small inputs like numbers or short strings.
- **Modes:** Controlled by `StellarX::TextBoxmode`:
- `INPUT_MODE` the user can click and enter text. On click, a modal input dialog appears where the user can type.
- `READONLY_MODE` the text box is for display only. Clicking it will not change the text; it might just show an alert that input is not allowed (the current implementation pops an InputBox with a message).
- Set via `setMode(TextBoxmode mode)`.
- **Appearance:**
- Supports rectangular shapes (normal or rounded, with or without border). Set with `setTextBoxShape(ControlShape)`. Circular/ellipse shapes are not logically typical for text entry and are treated as rectangle in the implementation.
- By default, a TextBox has a black border and white background. Use `setTextBoxBorder` and `setTextBoxBk` to change those.
- Text style is adjustable via `textStyle` (including font face, size, color).
- **Key Properties:**
- `std::string text;` The content string displayed in the text box.
- `TextBoxmode mode;` Current mode (input or read-only).
- `ControlShape shape;` Current border shape (default RECTANGLE or B_RECTANGLE).
- `bool click;` (Internal flag) Indicates if the text box was clicked (used to trigger the InputBox; it's set true on LButtonUp and then immediately reset after processing).
- `size_t maxCharLen;` Maximum number of characters allowed. Default is 255.
- `COLORREF textBoxBorderColor, textBoxBkColor;` Border color and background color of the text box.
- `StellarX::ControlText textStyle;` Text style for the content (e.g., can use monospace font for numeric input if desired, or change the color).
- **Main Methods:**
- `void setMode(StellarX::TextBoxmode mode);`
**Description:** Switches the text box between input mode and read-only mode. Changing mode does not clear the current text content.
- `void setMaxCharLen(size_t len);`
**Description:** Sets the maximum number of characters that can be input. If len > 0 (non-zero positive), it will enforce that limit. Input beyond this length will be truncated.
- `void setTextBoxShape(StellarX::ControlShape shape);`
**Description:** Sets the shape of the text box. Supports rectangle or rounded rectangle (with or without border). If a shape like circle/ellipse is passed, it's internally treated as rectangle (the implementation falls back to rectangle for unsupported shapes). Changing shape marks the text box dirty for redraw.
- `void setTextBoxBorder(COLORREF color);` / `void setTextBoxBk(COLORREF color);`
**Description:** Sets the border color and background color of the text box.
- `void setText(std::string text);`
**Description:** Updates the displayed text content of the text box. If the new text exceeds `maxCharLen`, it is automatically truncated to that length. Marks the text box dirty and immediately calls `draw()` to update the displayed text.
- `std::string getText() const;`
**Description:** Returns the current text content of the text box.
- `void draw() override;`
**Description:** Draws the text box background and the text:
- Saves style and sets `fillcolor` to `textBoxBkColor`, `linecolor` to `textBoxBorderColor`.
- Ensures the font height/width do not exceed the control's dimensions (if `textStyle.nHeight` is larger than `height`, it sets it equal to height; similarly for width).
- Sets the font and text color via `settextstyle` and `settextcolor` according to `textStyle`.
- Transparent background mode (`setbkmode(TRANSPARENT)` is used to avoid drawing a separate background behind text, since the background is already filled by fillrectangle).
- Calculates the pixel width and height of the text (`textwidth` and `textheight`).
- Draws the background shape:
- If shape is RECTANGLE (with border): calls `fillrectangle` (the border is then drawn by outline because linecolor is set).
- If shape is B_RECTANGLE: calls `solidrectangle` (no border outline).
- If shape is ROUND_RECTANGLE: calls `fillroundrect` using `rouRectangleSize` for corners.
- If shape is B_ROUND_RECTANGLE: calls `solidroundrect`.
- (Other shapes default to rectangle).
- Draws the text: It positions the text 10 pixels from the left inside the box (`x + 10`) and vertically centers it (`y + (height - text_h) / 2`).
- Restores the style and sets dirty false.
- **Note:** If text content is larger than the control width, it will overflow/clipped; there's no automatic horizontal scrolling or ellipsis for text box implemented. The developer should ensure the width is sufficient or the `maxCharLen` is set appropriately.
- `bool handleEvent(const ExMessage& msg) override;`
**Description:** Handles the mouse events for the text box:
- Determines if the mouse is over the text box (`hover`) using the shape logic (similar to button, but generally rectangular).
- For `WM_LBUTTONUP` when the cursor is over the text box:
- If in `INPUT_MODE`: It sets `click = true` and calls `InputBox` (EasyX's modal input dialog). The `InputBox` parameters include:
- The output buffer (here passing `LPTSTR(text.c_str())` to supply initial text).
- Max characters (`maxCharLen`).
- A title like "输入框" (Input Box).
- It passes `text.c_str()` as default content, etc.
- The returned value (the function returns nonzero if text changed, 0 if canceled, but here they capture it as `dirty`).
- After `InputBox` returns, if any input was done, the `text` variable will contain the new text (because the first parameter was the buffer referencing `text` content).
- They mark `dirty` based on InputBox result (the code sets `dirty` to whatever InputBox returned, which in EasyX indicates whether text was changed).
- They mark `consume = true` to indicate the click was handled.
- If in `READONLY_MODE`: They do not change the text. Instead, they possibly call `InputBox(NULL, maxCharLen, "输出框(输入无效!)", ... text ...)` which essentially shows the text in an output box stating input is invalid (effectively a message box).
- They keep `dirty = false` (since no visual change), set `consume = true`.
- In both cases, after handling, they flush the input messages with `flushmessage(EX_MOUSE | EX_KEY)` to clear any leftover events (similar reason to button: to avoid re-triggering).
- For other messages (like `WM_LBUTTONDOWN` or `WM_MOUSEMOVE`):
- `WM_LBUTTONDOWN` is not explicitly handled (the code simply sets `hover` and sets `consume = false` in the switch).
- `WM_MOUSEMOVE`: The code sets `hover` accordingly (and uses `consume = false`).
- After event processing:
- If `dirty` is true (text changed), it calls `draw()` to refresh the display with new text.
- If `click` was set (meaning an input happened), it resets `click = false` after handling to ensure state is consistent.
- Returns `consume` (which would be true if a click caused an InputBox or a read-only alert, false otherwise).
- Essentially, clicking the text box in input mode opens an input dialog for user to type, and clicking it in read-only mode just shows a dummy output box (so user knows it's not editable).
- **Usage Considerations:**
- Because `InputBox` is modal, it will pause the entire program's event loop until the user closes the input dialog. This simplifies input handling but means the UI doesn't update during text entry (the `runEventLoop` is blocked). For small apps and short inputs, this is acceptable, but for more advanced usage you might want to implement a custom non-blocking input field.
- Always set an appropriate `maxCharLen` if expecting certain input sizes (like numeric values of certain length) to avoid overflow of the display region.
- Use `textStyle` to set a suitable font for the input. For instance, for numeric input, you might choose a monospace font for alignment.
- Example:
```
auto field = std::make_unique<TextBox>(100, 100, 200, 30, "0");
field->setMaxCharLen(10);
field->textStyle.color = RGB(255,69,0); // orange text
field->textStyle.nHeight = 18;
// field is input mode by default. Optionally:
// field->setMode(StellarX::TextBoxmode::READONLY_MODE);
```
- After user input (in input mode), you can retrieve the text via `field->getText()`. In read-only mode, you may set the text programmatically via `setText` and it will just display.
### Dialog Class (Dialog Control)
**Description:** `Dialog` implements modal and modeless dialog windows with rich content and multiple buttons (like message boxes). It inherits from `Canvas` (so it is a container), and includes a caption, message text, and standard buttons combination. It integrates with `Window` to manage blocking input for modal dialogs.
- **Creation & Modes:**
- Constructor `Dialog(Window& parent, std::string title, std::string message = "对话框", MessageBoxType type = OK, bool modal = true)`
**Description:** Creates a dialog associated with the `parent` window. `title` is the dialog title text (often shown at top of dialog), `message` is the body text content. `type` specifies which buttons are included (see `MessageBoxType` enum for combinations, e.g., OK, YesNo, etc.). `modal` determines if this is a modal dialog (true) or modeless (false). The dialog is initially not visible (you must call `show()` to display it).
- `MessageBoxType` enumeration defines common button sets:
- `OK` only an "OK" button.
- `OKCancel` "OK" and "Cancel" buttons.
- `YesNo` "Yes" and "No" buttons.
- `YesNoCancel` "Yes", "No", and "Cancel".
- `RetryCancel` "Retry" and "Cancel".
- `AbortRetryIgnore` "Abort", "Retry", "Ignore".
- Modal dialogs (`modal == true`): When `show()` is called, it will block the current thread until the dialog is closed (the internal `show()` implementation runs its own loop). This is useful for confirmations or critical inputs that need immediate resolution. After `show()` returns, you can get the result via `getResult()`.
- Modeless dialogs (`modal == false`): When `show()` is called, it returns immediately (the dialog remains open, but the program flow continues and the main event loop will handle the dialogs interactions concurrently). To get the result, you typically provide a callback or check `getResult()` after the dialog closes (e.g., via a callback or a flag).
- **Component Elements of Dialog:**
- **Title label:** A `Label` (stored in `title`) showing the dialog title at top.
- **Message text:** The main content text, which can be multi-line. It's internally split into `lines` and drawn in the dialog client area.
- **Buttons:** One to three `Button` controls at the bottom of the dialog, depending on `MessageBoxType`. They are usually centered or evenly spaced.
- **Close (X) button:** A small `Button` in the top-right corner to close the dialog (especially for modeless dialogs, acts like a Cancel).
- **Background style:** Typically a rounded rectangle background with a slight border. There is no actual transparency or blur (the background might just capture what's behind for restoration when closed).
- **Key Properties:**
- `bool modal;` Whether the dialog is modal.
- `titleText` The dialog's title text string.
- `message` The dialog's message content string.
- `MessageBoxType type;` The type of dialog (determines buttons).
- `std::vector<std::string> lines;` The message content split into lines (populated internally).
- `std::unique_ptr<Label> title;` Label control for title.
- `Button* closeButton;` Pointer to the "X" close button.
- `Window& hWnd;` Reference to the parent window (for sending refresh signals, etc.).
- `StellarX::MessageBoxResult result;` The result of the dialog (which button was clicked by the user). Common values: OK=1, Cancel=2, Yes=6, No=7, etc. (Matches typical message box results).
- `std::function<void(MessageBoxResult)> resultCallback;` If set (for modeless use), a callback function to be called when the dialog is closed with the final result.
- (Other internal flags track if initialization is needed, if a cleanup is pending, etc.)
- **Main Methods:**
- `void show();`
**Description:** Displays the dialog.
- If `modal == true`, this function enters a loop that blocks the rest of the application until the dialog is closed. During this period, it intercepts events in its own loop (preventing main window events).
- If `modal == false`, this function simply makes the dialog visible and returns immediately; the dialog will be handled by the main window's event loop.
- Note: The implementation sets `dirty` and possibly runs its own flush for modals, and uses `shouldClose` to break out of the modal loop if needed.
- `void closeDialog();`
**Description:** Closes the dialog.
- It hides the dialog (`show = false`), marks it for cleanup, and signals the parent window to refresh other controls (by marking them dirty).
- If a `resultCallback` is set and the dialog is modeless, it invokes the callback with the result.
- This is typically called internally when a button is clicked (like Cancel or the X button).
- `StellarX::MessageBoxResult getResult() const;`
**Description:** Returns the `MessageBoxResult` code indicating which button the user pressed. For modal dialogs, you call this after `show()` returns to get the user's choice. For modeless, you'll typically get the result from the callback rather than polling this function.
- `void setTitle(const std::string& title);` / `void setMessage(const std::string& message);`
**Description:** Changes the dialog's title or message text. Changing the message triggers re-splitting into lines and recalculating sizes (the code marks dirty and reinitializes layout).
- `void setType(StellarX::MessageBoxType newType);`
**Description:** Changes the dialog's button configuration. This will recreate the buttons layout (calls `initButtons()` internally) and mark the dialog dirty.
- `void setModal(bool modal);`
**Description:** Sets the modal property (should be done before showing). Typically you decide at creation whether a dialog is modal or not; toggling at runtime is rare.
- `void setResult(StellarX::MessageBoxResult res);`
**Description:** Sets the dialog's result code. This is called internally when a button is clicked (for example, clicking "Yes" calls `setResult(MessageBoxResult::Yes)`).
- `void setInitialization(bool init);`
**Description:** If `init` is true, it performs an initialization of dialog size and captures the background. This method is used before showing the dialog (particularly by `MessageBox::showModal` and `showAsync`) to prepare the dialog geometry off-screen. Essentially, it calls `initDialogSize()` and saves the background behind where the dialog will appear (so that it can restore it on close).
- `void setResultCallback(std::function<void(MessageBoxResult)> cb);`
**Description:** Sets a callback function to be called with the dialog result when a modeless dialog is closed. Use this for asynchronous dialog handling.
- *Private internal methods (for implementation):*
- `initButtons()` Creates the appropriate `Button` objects for the dialog's `type`, positions them, and sets their onClick listeners to call `setResult` and `closeDialog`. For example, in a YesNo dialog, it creates "Yes" and "No" buttons and sets their listeners to `SetResult(Yes)` / `...No` and `closeDialog()` accordingly.
- `initCloseButton()` Creates the "X" close button at top-right of dialog (small size). Its onClick calls `SetResult(Cancel)` (or similarly appropriate default) and triggers `hWnd.dialogClose = true` and `closeDialog()`.
- `initTitle()` Creates the title Label at the top inside the dialog.
- `splitMessageLines()` Splits the `message` string by newline characters into the `lines` vector for drawing.
- `getTextSize()` Calculates the maximum text width and single line height among the `lines`. It does this by setting the text style and measuring each line (`textwidth` & `textheight` calls). It stores results in `textWidth` and `textHeight`.
- `initDialogSize()` Computes the dialog's required width and height based on:
- The greater of text area width (textWidth + margins) and button area width (depending on number of buttons).
- For height: sums title bar height, spacing to text, total text block height (line count * textHeight + line spacing), and button area height.
- Enforces a minimum width (in code, 200 px).
- Centers the dialog on the parent window by setting `x,y` such that the dialog is at window center.
- Also sets a default text style height (e.g., 20 px).
- Then calls `initButtons()`, `initTitle()`, and `initCloseButton()` to create all child controls.
- `performDelayedCleanup()` Handles the delayed destruction of the dialog's content after its closed. It:
- Marks `isCleaning = true` to avoid reentry.
- Clears all child controls (`controls.clear()`).
- Resets pointers (closeButton, title).
- If a background image was saved and still present (`hasSnap` is true), it restores the screen background (`restBackground()`) and discards the snapshot (`discardBackground()`) to remove any ghost image of dialog.
- Resets flags (needsInitialization, pendingCleanup, isCleaning, shouldClose).
- This is called by the event loop after the dialog is hidden, to safely remove the dialog from UI and restore background.
- `void draw() override;`
**Description:** Draws the dialog. It combines container drawing with additional text drawing:
- If dialog is not visible (`show == false`), it does nothing (except if `pendingCleanup` and not cleaning, it calls `performDelayedCleanup()`, but that's in handleEvent rather than draw).
- If `needsInitialization` is true and dialog is now visible, it calls `initDialogSize()` to compute layout and then sets `needsInitialization = false`. This ensures the dialog's size and children are all properly set up on first draw.
- It then checks `dirty` and `show`, and if both are true:
- Saves drawing style.
- If it's the first draw and `saveBkImage` is null, it calls `saveBackground(...)` to capture what's behind the dialog.
- Sets the border color, border width, background color, and shape on the base Canvas part (calls `Canvas::setBorderColor`, etc.) to round rectangle and appropriate style.
- Marks all child controls dirty (so they will be redrawn).
- Calls `Canvas::draw()` to draw the rounded rectangle background and all the child controls (like buttons, title).
- Then sets text color and style (for the message text) and calculates a starting y coordinate `ty` just below the title area (using `closeButtonHeight` and a margin).
- For each line in `lines`, it computes an x coordinate `tx` to center that line in the dialog (x + (width - textwidth(line))/2), and then outputs the text with `outtextxy(tx, ty, line)`.
- Moves `ty` down by textheight + 5 for the next line (5 px line spacing).
- Restores style, sets `dirty = false`.
- The base Canvas::draw already drew the background and the title label and buttons. So these text drawing steps are to overlay the message text content (which is not a separate control, but drawn directly).
- `bool handleEvent(const ExMessage& msg) override;`
**Description:** Handles events for the dialog:
- If the dialog is not visible (`!show`):
- If there's a `pendingCleanup` and not already cleaning, it calls `performDelayedCleanup()`.
- Returns false (no event consumed).
- If a cleanup is pending or in progress (`pendingCleanup || isCleaning`), it immediately returns false (dialog isn't interactive at this moment).
- If modal:
- If the user clicked outside the dialog (LButtonUp at a point outside `[x, y, x+width, y+height]`):
- It prints a bell (`std::cout << "\a"`) to alert (like system beep).
- Returns true (consumes the event) to prevent underlying controls from receiving the click.
- Then it attempts to dispatch the event to dialog's child controls:
- `consume = Canvas::handleEvent(msg)` calls the base container logic to let any button inside the dialog handle the event (e.g., if user clicked "OK", the button's handleEvent will consume it).
- If a child consumed it, `consume` becomes true.
- After dispatching, if `pendingCleanup && !isCleaning`, it calls `performDelayedCleanup()` (maybe if a button triggered immediate closure).
- Returns `consume` (so if a dialog button or modal logic handled the event, it returns true, otherwise false).
- The effect is that normal controls behind won't receive events while a modal dialog is open (due to the modal check swallowing outside clicks and because the Window event loop prioritizes dialogs).
- For modeless dialogs, events outside are not swallowed by Dialog (so user can interact with background controls as well, which is typical for modeless).
- **Usage Examples:**
It's more common to use `MessageBox` static methods to show dialogs (documented next), but you can use `Dialog` directly:
- **Modal example:**
```
Dialog confirm(window, "Confirm Delete", "Are you sure you want to delete?", StellarX::MessageBoxType::YesNo, true);
confirm.show();
if (confirm.getResult() == StellarX::MessageBoxResult::Yes) {
// proceed with delete
}
```
- **Modeless example:**
```
Dialog *notify = new Dialog(window, "Notice", "Background task started.", StellarX::MessageBoxType::OK, false);
notify->setResultCallback([](StellarX::MessageBoxResult res) {
// maybe log that user acknowledged
});
notify->show();
```
Here we allocate on heap (since for modeless we need it to persist beyond function scope), set a callback to handle result asynchronously, and show it. The main loop will handle its events. The callback will be invoked on close (OK click).
- Typically, prefer using `MessageBox::showModal` and `showAsync` which handle these patterns for you, including cleanup of the `Dialog` object when done.
### MessageBox Utility Class
**Description:** `StellarX::MessageBox` provides convenient static methods to display standard dialogs (message boxes) without manually managing `Dialog` objects. It mimics the idea of a system message box where you specify a message and get a user response.
- **Static Functions:**
- `static MessageBoxResult showModal(Window& wnd, const std::string& text, const std::string& caption = "提示", MessageBoxType type = MessageBoxType::OK);`
**Description:** Displays a modal message box on window `wnd` with given content. `caption` is the title of the dialog (e.g., "Error", "Confirmation"), `text` is the message to display, and `type` is the combination of buttons to include. This function blocks until the user closes the dialog. It returns a `MessageBoxResult` indicating which button the user clicked (e.g., `MessageBoxResult::OK` or `Cancel`, etc.). The caller can then branch logic based on this result.
- `static void showAsync(Window& wnd, const std::string& text, const std::string& caption = "提示", MessageBoxType type = MessageBoxType::OK, std::function<void(MessageBoxResult)> onResult = nullptr);`
**Description:** Displays a modeless (asynchronous) message box. It returns immediately, leaving the dialog on screen. If `onResult` callback is provided, it will be called once the user closes the dialog, with the `MessageBoxResult` of what they clicked. If a dialog with the same caption **and text** is already open on `wnd`, this function will emit a beep (`\a`) and not open another (to prevent duplicates).
- This duplicate check uses `wnd.hasNonModalDialogWithCaption(caption, text)` internally.
- If no callback is provided, the function simply opens the dialog and returns; you'll have to find another way to know when it's closed (perhaps by checking the `Dialog` via some global or so, but ideally you provide a callback).
- **Implementation Details:**
- `showModal`:
- Creates a `Dialog` (with modal = true) on the stack.
- Calls `dlg.setInitialization(true)` to prepare its layout and capture background before showing (to minimize flicker).
- Calls `dlg.show()`, which will block until the dialog is closed.
- When `show()` returns, it retrieves the result with `dlg.getResult()` and returns that.
- All memory is cleaned up on function exit since `dlg` was on stack.
- `showAsync`:
- First checks for duplicates; if found, prints a bell and returns without doing anything.
- Otherwise, creates a `Dialog` on the heap (with modal = false) using a unique_ptr. It then:
- Calls `dlgPtr->setInitialization(true)` to layout and capture background.
- If an `onResult` callback is provided, calls `dlgPtr->setResultCallback(std::move(onResult))` to register it.
- Uses `wnd.addDialog(std::move(dlg))` to add the dialog to the window's management list (transferring ownership to the window).
- Calls `dlgPtr->show()` to display the dialog modelessly.
- Then returns immediately. The dialog will operate asynchronously; when closed, it triggers `resultCallback` if set.
- **Typical Uses:**
- To display an information message and wait for user acknowledgment:
```
StellarX::MessageBox::showModal(mainWindow, "Operation completed successfully.", "Info", MessageBoxType::OK);
```
This will show an OK dialog and block until user presses OK.
- To ask a yes/no question:
```
auto res = StellarX::MessageBox::showModal(mainWindow, "Delete this file?", "Confirm", MessageBoxType::YesNo);
if (res == MessageBoxResult::Yes) {
// delete the file
}
```
- To show a non-blocking notification:
```
StellarX::MessageBox::showAsync(mainWindow, "The update is downloading in the background.", "Update", MessageBoxType::OK);
// user can continue using app; the dialog will close when they press OK.
```
- To ask something in the background and get result via callback:
```
StellarX::MessageBox::showAsync(mainWindow, "New version available. Download now?", "Update Available", MessageBoxType::YesNo,
[](StellarX::MessageBoxResult res) {
if (res == StellarX::MessageBoxResult::Yes) {
startDownload();
}
});
```
Here, the lambda will execute once the user clicks Yes or No (Yes triggers `startDownload()` in this example).
Using these static methods is recommended because they handle the creation and cleanup of `Dialog` internally, and provide a simple synchronous interface for modal dialogs and an event-driven interface for modeless dialogs.
## Table Class
### Class Overview and Purpose
The **Table** class is an advanced table UI control that supports pagination and displaying large datasets[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/include/StellarX/table.h#L2-L10). It provides a comprehensive data grid functionality, including column headers, data rows, and pagination navigation, similar to a spreadsheet. The Table control automatically computes appropriate column widths and row heights, and it allows customization of border style, fill mode, and text font style to fit various UI design requirements. With its built-in paging mechanism, Table can efficiently handle and present large amounts of data without overwhelming the interface, utilizing background buffering to optimize rendering and prevent flicker during redraws.
**Key Features:**
- **Pagination Support:** Automatically handles pagination by computing total pages based on data size and configurable rows-per-page, and provides **Previous/Next** navigation buttons with a page indicator[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/README.md#L274-L282). This enables users to browse through data that spans multiple pages easily.
- **Customization:** Developers can configure the number of rows per page, toggle the visibility of page navigation buttons, and adjust visual aspects such as border color, background color, fill mode (solid fill or transparent), and line style to match the application's theme.
- **Efficient Rendering:** The Table control uses double-buffering techniques by capturing the background under the table and only redrawing changed areas[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L210-L219)[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L241-L249). This minimizes redraw artifacts and improves performance when updating the table content or appearance.
- **Typical Use Cases:** Ideal for displaying tabular data like lists, reports, and records in a GUI application[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/include/StellarX/table.h#L14-L17). For example, it can be used to show database query results, log entries, or statistical reports in a scrollable, paginated view, allowing users to easily read and navigate the information.
### Public Member Functions
#### Table(int x, int y)
**Prototype:** `Table(int x, int y)`
**Parameters:**
- `x` The X coordinate for the tables top-left corner position.
- `y` The Y coordinate for the tables top-left corner position.
**Return Value:** None (constructor).
**Description:** Constructs a new Table control at the specified position on the screen[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L164-L171). Initially, the table has no headers or data; its width and height are set to 0 and will be calculated once data is provided and headers are set. The `x` and `y` coordinates position the table within the parent window or container. Internally, Table inherits from the base `Control` class, so it is created with an initial area that will expand based on content.
**Usage Scenario:** Use this constructor when you need to create a table in your applications UI. For example, after creating a main window, you might call `auto table = std::make_unique<Table>(50, 50);` to place a Table at coordinates (50,50). You would then set up its headers and data before rendering it. The Table can be added to a Window or Canvas as a sub-control for display.
#### ~Table()
**Prototype:** `~Table()`
**Parameters:** None.
**Return Value:** None.
**Description:** Destructor for the Table class. It is called automatically when a Table object is destroyed, and it ensures that all internally allocated resources are freed. This includes deleting any internal controls such as the pagination buttons, page number label, and the background image buffer that were created during the Tables operation[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L169-L178). By cleaning up these resources, the destructor prevents memory leaks.
**Usage Scenario:** Generally, you do not need to call this explicitly; it will be invoked when the Table goes out of scope or the program terminates. Understanding the destructors behavior is useful for developers to ensure that if a Table is dynamically allocated (e.g., via `new` or `make_unique`), it should be deleted or allowed to go out of scope to trigger cleanup of internal components.
#### draw()
**Prototype:** `void draw() override`
**Parameters:** None.
**Return Value:** None.
**Description:** Overrides the `Control::draw()` method to render the table onto the screen[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L185-L194)[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L252-L260). When `draw()` is called, the Table will draw its column headers, the data rows for the current page, the page number label, and the navigation buttons (if they are enabled). The drawing routine uses the current text style, border color, background color, fill mode, and line style settings that have been configured. The Table employs a “dirty flag” mechanism to optimize rendering: it will only redraw its content if something has changed (such as data or style) that marks it as needing an update[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L187-L196)[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L258-L266). If `dirty` is false, calling `draw()` has no effect. Internally, the function also takes care of restoring any saved background and drawing with minimal flicker.
**Usage Scenario:** In most cases, you do not need to call `table.draw()` manually, as the frameworks window or event loop will handle drawing. Typically, after adding the Table to a Window and calling the windows `draw()`, the Table will be drawn. However, if you update the Tables content or appearance at runtime (for example, in response to an event) and want to refresh immediately, you may call `table.draw()` to force a redraw. This will ensure the tables display is updated with any new data or style changes.
#### handleEvent(const ExMessage& msg)
**Prototype:** `bool handleEvent(const ExMessage& msg) override`
**Parameters:**
- `msg` An event message (`ExMessage` from EasyX library) containing details of a user input event (mouse click, key press, etc.).
**Return Value:** Boolean, indicating whether the event was handled by the Table. Returns `true` if the event was consumed (handled) by this Table control, or `false` if the Table did not handle the event.
**Description:** Overrides the `Control::handleEvent()` method to process user input events relevant to the Table. For the Table control, `handleEvent` primarily deals with events on the pagination buttons. If page navigation buttons are enabled (`isShowPageButton` is true), this function will forward the incoming event to the internal "Previous Page" and "Next Page" Button controls for handling[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L274-L283). For example, if the user clicks one of these buttons, the respective internal Button will capture the event, update the Tables `currentPage` (decrement or increment it), and trigger a redraw of the table to show the new page. In such a case, `handleEvent` returns `true` to indicate the event was handled (and no further processing is needed). If the page buttons are hidden (`isShowPageButton == false`), the Table will ignore all events and always return `false`[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L276-L283), as there is no interactive element for the user to manipulate directly in the Table.
**Usage Scenario:** In normal usage, you do not call this method directly. The frameworks main loop will call `handleEvent` on each control (including Table) to propagate user inputs. However, understanding this function is useful if you plan to extend the Table or integrate it into a custom event handling flow. For instance, when integrating Table into a Window, the Windows `runEventLoop()` will pass events to `table.handleEvent(msg)` automatically. If you have disabled the default page buttons, you might implement a custom paging control elsewhere; in that case, Tables `handleEvent` would not consume events, and you could handle them in your custom controls.
#### setHeaders(std::initializer_liststd::string headers)
**Prototype:** `void setHeaders(std::initializer_list<std::string> headers)`
**Parameters:**
- `headers` An initializer list of strings, each representing a column header title.
**Return Value:** None.
**Description:** Defines the column headers of the table. This function clears any existing headers and sets up new headers as provided by the list[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L288-L296). Each string in the list becomes the title of a column in the table header. After calling this, the Table marks itself as needing to recalculate cell dimensions and to redraw the headers[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L291-L295) on the next draw. The number of headers determines the number of columns in the table; any subsequent data rows added should match this column count.
**Usage Scenario:** Call this after creating a Table and before adding data. For example: `table.setHeaders({"Name", "Age", "Occupation"});` will configure the table to have three columns titled "Name", "Age", and "Occupation". Defining headers is typically the first step in preparing the Table for use. Once headers are set, you can proceed to populate data rows with `setData`. Changing headers after data has been added is possible but not recommended, as the existing data might not align with the new columns (if the counts differ); if you must do so, you should reset or adjust the data accordingly.
#### setData(std::vectorstd::string data) / setData(std::initializer_list<std::vectorstd::string> data)
**Prototype 1:** `void setData(std::vector<std::string> data)`
**Parameters:**
- `data` A vector of strings representing a single row of data, where each element corresponds to a cell under the respective column.
**Return Value:** None.
**Description:** Adds one row of data to the table. If the number of elements in the provided vector is less than the number of headers (columns), the function will append empty strings to the vector until it matches the header count[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L300-L308). This ensures that every row has a value (or placeholder) for each column. The row is then appended to the Tables internal data storage, and the total page count (`totalPages`) is recalculated based on the new total number of rows and the current `rowsPerPage` setting[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L302-L308). The calculation rounds up to ensure at least one page[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L303-L308). After adding the row, the Table is marked as needing a layout recompute and redraw.
**Usage Scenario:** Use this function to build the table row by row. For example, in a loop reading records from a file or database, you might call `table.setData({val1, val2, val3});` for each record to append it as a new row. This is especially useful for dynamically updating the table with new entries (e.g., real-time log updates). Each call adds the row at the end of the data and the Tables display will include it on the appropriate page (immediately if it falls on the current page and you redraw, or after navigation).
**Prototype 2:** `void setData(std::initializer_list<std::vector<std::string>> data)`
**Parameters:**
- `data` An initializer list of rows, where each row is represented by a `std::vector<std::string>` (as described above).
**Return Value:** None.
**Description:** Adds multiple rows of data to the table in one call. For each row provided in the initializer list, the function checks if its number of elements matches the number of table headers; if a row is shorter, empty strings will be appended until its length equals the header count[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L313-L321). Each row (adjusted as needed) is then appended to the tables data. After inserting all the rows, the total page count is recalculated accordingly, ensuring it is at least 1[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L322-L328). Like the single-row version, this overload marks the table as needing re-layout and redraw. Note that this function does **not** clear existing data; new rows are appended to any data already present.
**Usage Scenario:** This is convenient for initializing the table with a batch of data. For example, if you already have a collection of rows ready (perhaps from a data structure or file), you can call `table.setData({row1, row2, row3});` to add them all at once instead of calling the single-row version repeatedly. It simplifies populating the table with initial data. If you intend to replace all existing data, you may want to clear the old data (by resetting the Table or using the returned data vector) before calling this overload. Use this when you have a static set of data to display, especially during initialization or when loading a new dataset into the table.
#### setRowsPerPage(int rows)
**Prototype:** `void setRowsPerPage(int rows)`
**Parameters:**
- `rows` The number of data rows to display per page.
**Return Value:** None.
**Description:** Sets the pagination size of the table by specifying how many rows are shown on each page. This function updates the internal `rowsPerPage` setting and recalculates the total number of pages (`totalPages`) based on the current total number of data rows[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L332-L338). If the recalculation results in less than 1 page, it defaults to 1 to maintain a valid state. Changing this value will affect the layout (particularly the vertical size of the table and the distribution of data across pages), so the Table marks itself for resizing of cells and redraw. The current page (`currentPage`) is not explicitly adjusted in this function, so if the new `rowsPerPage` significantly reduces the total pages, the table may internally handle out-of-range page indices on the next draw by capping the current page to the last page.
**Usage Scenario:** Use this to adjust how dense the table appears in terms of data per page. For instance, if a table initially shows 5 rows per page by default but you want to show more data on a larger screen, calling `table.setRowsPerPage(10);` will double the number of rows shown at once, reducing the total page count roughly by half. This is useful for user preferences (show more or fewer items per page) or responsive design considerations. It can be called before or after adding data. If called after data is loaded and the current page is beyond the new total pages, the next redraw will naturally show the last page of data.
#### showPageButton(bool isShow)
**Prototype:** `void showPageButton(bool isShow)`
**Parameters:**
- `isShow` A boolean flag indicating whether the page navigation buttons (Previous/Next) should be displayed. `true` to show the buttons; `false` to hide them.
**Return Value:** None.
**Description:** Toggles the visibility of the pagination control buttons of the table. When set to `false`, the “Previous Page” and “Next Page” buttons will not be drawn, and the user will not have a UI control to navigate between pages (the table will effectively remain on whatever the current page is, unless changed programmatically). When set to `true`, the buttons are visible, allowing the user to click them to change pages. Hiding the buttons does not alter the data or current page internally; it purely affects the UI and event handling (when hidden, `handleEvent` will ignore navigation clicks as described above). Any call to this function will mark the table as dirty so that on the next draw the buttons will be shown/hidden accordingly.
**Usage Scenario:** Useful when the dataset fits on one page or when you want to control pagination through a different means. For example, if your table only has a few rows that all fit on a single page, you might call `table.showPageButton(false);` to simplify the interface by removing unnecessary controls. Conversely, if you later load more data that requires paging, you can call `showPageButton(true)` to reveal the navigation. It can also be toggled in response to user actions (like a "show all data" toggle that disables manual paging).
#### setTableBorder(COLORREF color)
**Prototype:** `void setTableBorder(COLORREF color)`
**Parameters:**
- `color` A COLORREF value specifying the new color for the tables border lines (grid lines). This is typically created by the `RGB(r,g,b)` macro on Windows (e.g., `RGB(0,0,0)` for black).
**Return Value:** None.
**Description:** Sets the color used to draw the tables cell borders and outline. After calling this, the Table updates its internal border color and marks itself for redraw. The next time the table is drawn, all grid lines and borders will be rendered in the specified color[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L192-L199). By default, the tables border color is black. Changing the border color can be useful to match the overall UI theme or to highlight the table. This affects the lines around each cell and the table perimeter.
**Usage Scenario:** Use this to customize the appearance of the tables grid. For example, in a dark-themed application you might use a lighter border color for contrast, or in a light theme, a subtle gray to avoid a heavy look: `table.setTableBorder(RGB(200, 200, 200));`. This is purely a cosmetic setting and can be changed anytime (even at runtime) to alter the tables look. Remember to refresh the display (via `draw()` or window redraw) to see the effect immediately.
#### setTableBk(COLORREF color)
**Prototype:** `void setTableBk(COLORREF color)`
**Parameters:**
- `color` A COLORREF specifying the background fill color for the tables cells.
**Return Value:** None.
**Description:** Sets the background color used to fill each cell of the table. This updates the internal background color property and marks the Table as needing redraw. During the next `draw()`, each cells background will be filled with the given color (provided the fill mode is solid). By default, table cell backgrounds are white. Changing this can provide alternating row colors or highlight the table area against the window background. If the fill mode is set to Null (transparent), this color will not be visible since cells wont be filled with a solid color.
**Usage Scenario:** To change the background color of the table for styling or readability. For instance, `table.setTableBk(RGB(240, 240, 240));` would set a light gray background for all cells, which can reduce glare compared to bright white. This can be paired with custom text colors for better contrast. If you want to implement alternating row colors (like “zebra striping”), the current Table API does not support per-row color via this function directly you would need to draw custom backgrounds or modify the data drawing logic.
#### setTableFillMode(StellarX::FillMode mode)
**Prototype:** `void setTableFillMode(StellarX::FillMode mode)`
**Parameters:**
- `mode` A `StellarX::FillMode` value indicating the fill pattern mode for the cell backgrounds. Common values are `FillMode::Solid` (solid color fill) or `FillMode::Null` (no fill/transparent background). Other modes like `Hatched`, `Pattern`, etc., are also defined but may fall back to Solid if not supported.
**Return Value:** None.
**Description:** Configures how the tables cell background is filled when drawn. In practice, this toggles between solid fill and no fill for the Table (the current implementation treats any mode other than Solid or Null as Solid)[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L360-L369). Setting `FillMode::Solid` means cells will be drawn with the current background color (`tableBk`), whereas `FillMode::Null` means cell backgrounds will not be drawn (making them transparent, showing whatever was behind the table). This function also propagates the fill mode and text style to the internal navigation buttons and page number label to ensure visual consistency[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L365-L373). For example, if you choose Null mode (transparent), the page number labels background will be made transparent as well. Any change here marks the Table (and its sub-controls) for redraw.
**Usage Scenario:** Use this when you need to adjust whether the tables background is visible. A typical use is to set `table.setTableFillMode(StellarX::FillMode::Null);` if you want the table to overlay on a custom background (so only text and borders render, letting the underlying image or color show through). Otherwise, keep it as Solid to use a uniform background color for cells. The default is Solid fill. Be aware that patterns or hatched fills are not fully supported in this version of the framework; even if set, they will revert to a solid fill for simplicity[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L360-L368).
#### setTableLineStyle(StellarX::LineStyle style)
**Prototype:** `void setTableLineStyle(StellarX::LineStyle style)`
**Parameters:**
- `style` A `StellarX::LineStyle` value indicating the style of the grid lines (e.g., `LineStyle::Solid` for solid lines, `LineStyle::Dash` for dashed lines, `LineStyle::Dot` for dotted lines, etc.).
**Return Value:** None.
**Description:** Sets the style used for drawing the tables border and grid lines. Changing this will cause the table to use the specified line style (if supported by the underlying graphics library) for all cell borders and the outline on the next redraw. Examples include dashed or dotted lines for a lighter look compared to solid lines. After calling this, the line style property is updated and the table is marked dirty for redrawing. Note that the actual rendering of different line styles depends on the graphics library capabilities.
**Usage Scenario:** This is a visual customization for the tables grid. For instance, calling `table.setTableLineStyle(StellarX::LineStyle::Dash);` will make the table draw its cell borders with dashed lines, which might be desirable in a print preview or a specialized UI theme. Use it to differentiate sections or to achieve a stylistic effect. Combine it with appropriate border color and width for best results. If a particular line style is not obvious on screen, ensure that line width is 1 and the color contrasts with the background.
#### setTableBorderWidth(int width)
**Prototype:** `void setTableBorderWidth(int width)`
**Parameters:**
- `width` The thickness of the border lines in pixels. Must be a positive integer.
**Return Value:** None.
**Description:** Sets the width (thickness) of the lines used for the tables cell borders and outline. By default, grid lines are 1 pixel thick. Increasing this value will make the tables grid lines thicker/bolder, while a value of 1 keeps them thin. When this property is changed, the table marks itself for redraw and will use the new line width for drawing borders on the next `draw()`. If a thicker border is set (greater than 1), the Tables drawing logic takes into account the larger stroke when restoring the background to avoid artifacts (adjusting the area it refreshes)[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L220-L228)[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L240-L248).
**Usage Scenario:** Use a larger border width when you want the table grid to stand out more, such as in a presentation or when displaying on a high-DPI screen where a 1-pixel line may appear too thin. For example, `table.setTableBorderWidth(2);` will draw grid lines at 2 pixels thickness, making the table look more pronounced. Be cautious with very thick lines as they can clutter the appearance and might overlap cell content if too large relative to cell size. Always test the appearance after changing this setting.
#### Accessor Methods (Getters)
The following methods allow you to query the Tables current state or configuration:
- **int getCurrentPage() const** Returns the current page index (1-based) that the table is displaying[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L390-L398). For example, if the table is showing the first page of data, this returns 1; if the user has navigated to the second page, it returns 2, and so on. This can be used to display or log the current page number.
- **int getTotalPages() const** Returns the total number of pages of data in the table given the current dataset and rows-per-page setting[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L394-L402). This is calculated as ceil(total_rows / rowsPerPage). Its useful for informing the user (e.g., “Page 2 of 5”) or for logic that might depend on whether more pages are available.
- **int getRowsPerPage() const** Returns the number of data rows the table is set to display on each page[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L400-L408). This reflects the value last set by `setRowsPerPage`. It can be used to verify the current pagination setting or to adjust external controls accordingly.
- **bool getShowPageButton() const** Returns the current setting of whether the pagination buttons are visible[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L404-L412) (`true` if they are shown, `false` if hidden). This could be checked to decide if manual navigation UI should be enabled or if an alternate navigation method is needed.
- **COLORREF getTableBorder() const** Returns the current color (COLORREF) used for the tables border/grid lines[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L410-L418). For instance, `RGB(0,0,0)` for black. This allows you to retrieve the color if you need to apply the same color elsewhere or for saving the configuration.
- **COLORREF getTableBk() const** Returns the current background fill color (COLORREF) of the tables cells[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L414-L422).
- **StellarX::FillMode getTableFillMode() const** Returns the current fill mode for the tables cells[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L420-L428), as a `StellarX::FillMode` enum (e.g., Solid or Null). This lets you check whether the table is drawing solid backgrounds or is in transparent mode.
- **StellarX::LineStyle getTableLineStyle() const** Returns the current line style used for the tables grid lines[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L424-L432) (as a `StellarX::LineStyle` enum). You can use this to inspect the style (solid, dashed, etc.) programmatically.
- **std::vectorstd::string getHeaders() const** Returns a copy of the current list of header strings[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L430-L438). The returned vector contains each columns header text in order. This can be used to retrieve the headers for display elsewhere or for serialization.
- **std::vector<std::vectorstd::string> getData() const** Returns a copy of all the tables data[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L434-L442). The returned value is a 2D vector of strings, where each inner vector represents one row of the table (with elements corresponding to each columns value in that row). This is useful for exporting the table data or performing computations on the data outside of the table (like searching or sorting, which would typically be done on the data and then reapplied to the table).
- **int getTableBorderWidth() const** Returns the current width (in pixels) of the tables border lines[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/src/table.cpp#L440-L446).
All these getter functions are `const` and do not modify the Tables state. They are intended for retrieving the tables configuration and content at any moment. For example, after populating the table, you might call `getTotalPages()` to update a UI element that shows the total page count, or use `getData()` to verify the data loaded into the table.
### Public Member Variables
- **StellarX::ControlText textStyle** A struct that defines the font and style used for text rendered in the table (including header text, cell content, and the page number label). By modifying this public member, you can customize the appearance of the tables text. For instance, you can set `textStyle.nHeight` to change the font height (size), `textStyle.lpszFace` to change the font family name, `textStyle.color` to change the text color, and other attributes such as `nWeight` for boldness, `bItalic` for italics, `bUnderline` for underline, etc.[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/include/StellarX/CoreTypes.h#L134-L142). The Table uses these settings when drawing text via EasyX functions. The default `ControlText` values are initialized to a standard font (e.g., a default Chinese font "微软雅黑" as in the code, or "SimSun") at a default size and black color. Developers can adjust `textStyle` any time before calling `draw()` to apply new text styles. Notably, the Tables internal prev/next Buttons and page number Label also use the Tables `textStyle` to ensure consistency in typography across the entire control. For example, to make the table text larger and blue:
```
table.textStyle.nHeight = 20; // set font height
table.textStyle.color = RGB(0, 0, 128); // set text color to navy blue
```
After adjusting, calling `table.draw()` (or a window redraw) will render the table with the new font settings.
### Example
Below is an example demonstrating how to create and use a Table control in a StellarX application[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/README.en.md#L275-L283)[GitHub](https://github.com/Ysm-04/StellarX/blob/95149238e221b19a15bfa750bcf937620e23fd47/README.en.md#L284-L290):
```c++
// Create a table control at position (50, 50)
auto myTable = std::make_unique<Table>(50, 50);
// Set column headers
myTable->setHeaders({ "ID", "Name", "Age", "Occupation" });
// Add data rows
myTable->setData({ "1", "Zhang San", "25", "Engineer" });
myTable->setData({ "2", "Li Si", "30", "Designer" });
myTable->setData({ "3", "Wang Wu", "28", "Product Manager" });
// Set to display 2 rows per page
myTable->setRowsPerPage(2);
// Customize text style and table appearance
myTable->textStyle.nHeight = 16; // set font height
myTable->setTableBorder(RGB(50, 50, 50)); // set border color (dark gray)
myTable->setTableBk(RGB(240, 240, 240)); // set background color (light gray)
// Add the table to the main window (assume mainWindow is a Window instance)
mainWindow.addControl(std::move(myTable));
```
In this code, we create a `Table` at coordinates (50, 50) on the window. We then define four column headers: "ID", "Name", "Age", "Occupation". Next, three rows of sample data are added to the table. The `setRowsPerPage(2)` call configures the table to show only 2 rows per page; since we added 3 rows of data, the table will have 2 pages (the first page showing the first 2 rows, and the second page showing the remaining 1 row). We then adjust the tables appearance by setting the font size (making text slightly larger), changing the border color to a dark gray, and the background color of cells to a light gray for better contrast. Finally, we add the table to a `mainWindow` (which is an instance of `Window`) so that it becomes part of the GUI. Once added, the windows drawing routine will render the table, and the event loop will ensure the tables pagination buttons respond to user clicks. Typically, after adding controls to a window, you would call `mainWindow.draw()` to render the initial UI, and then `mainWindow.runEventLoop()` to start processing events, allowing interactions like clicking the "Next" button to flip pages. This example illustrates the basic steps to integrate the Table control into an application and configure its data and appearance.
The above documentation covers all key classes in the StellarX framework. With these, developers can construct fully-featured GUI applications with windows, interactive buttons, text fields, and pop-up dialogs. Keep in mind StellarX is built on EasyX and thus inherits its platform limitation (Windows only) and certain modal behaviors (like `InputBox` use). It is ideal for educational purposes and lightweight tools requiring a simple GUI.

451
README.en.md Normal file
View File

@@ -0,0 +1,451 @@
# StellarX GUI Framework
[中文文档](README.md)
------
![GitHub all releases](https://img.shields.io/github/downloads/Ysm-04/StellarX/total)
[![Star GitHub Repo](https://img.shields.io/github/stars/Ysm-04/StellarX.svg?style=social&label=Star%20This%20Repo)](https://github.com/Ysm-04/StellarX)
![Version](https://img.shields.io/badge/Version-2.0.0-brightgreen.svg)
![Download](https://img.shields.io/badge/Download-2.0.0_Release-blue.svg)
![C++](https://img.shields.io/badge/C++-17+-00599C?logo=cplusplus&logoColor=white)
![Windows](https://img.shields.io/badge/Platform-Windows-0078D6?logo=windows)
![EasyX](https://img.shields.io/badge/Based_on-EasyX-00A0EA)
![License](https://img.shields.io/badge/License-MIT-blue.svg)
![Architecture](https://img.shields.io/badge/Architecture-Modular%20OOP-brightgreen)
![CMake](https://img.shields.io/badge/Build-CMake-064F8C?logo=cmake)
> **"Bound by Stars, Light as Dust"** — A native C++ GUI framework for Windows platform, featuring extreme lightweight and high modularity.
`StellarX` was born from resistance against "overly bloated" modern GUI frameworks. It rejects dependencies that often reach hundreds of MB, long compilation times, and steep learning curves, choosing to return to the essence: solving core desktop application development needs with the most concise code, clearest architecture, and highest efficiency.
It is a **pure teaching-level, tool-level framework** designed to help developers deeply understand GUI principles and quickly build lightweight Windows tools.
---
## 📦 Project Structure & Design Philosophy
StellarX framework adopts classic **Object-Oriented** and **modular** design with a clear and standardized project structure:
```markdown
StellarX/
├── include/ # Header files directory
│ └── StellarX/ # Framework header files
│ ├── StellarX.h # Main include header - one-click import of entire framework
│ ├── CoreTypes.h # ★ Core ★ - Single source of truth for all enums and structs
│ ├── Control.h # Abstract base class - defines unified interface for all controls
│ ├── Button.h # Button control
│ ├── Window.h # Window management
│ ├── Label.h # Label control
│ ├── TextBox.h # Text box control
│ ├── Canvas.h # Canvas container
│ ├── Dialog.h # Dialog control (new in v2.0.0)
│ ├── MessageBox.h # Message box factory (new in v2.0.0)
│ └── Table.h # Table control
├── src/ # Source files directory
│ ├── Control.cpp
│ ├── Button.cpp
│ ├── Window.cpp
│ ├── Label.cpp
│ ├── TextBox.cpp
│ ├── Canvas.cpp
│ ├── Table.cpp
│ ├── Dialog.cpp # v2.0.0 new
│ └── MessageBox.cpp # v2.0.0 new
├── examples/ # Example code directory
│ └── demo.cpp # Basic demonstration
├── docs/ # Documentation directory
│ └── CODE_OF_CONDUCT.md # Code of Conduct
├── CMakeLists.txt # CMake build configuration
├── CONTRIBUTING.md # Contribution guide
├── CHANGELOG.md # Changelog
├── Doxyfile # Doxygen configuration
├── LICENSE # MIT License
└── README.md # Project description
```
### **Design Philosophy:**
1. **Single Responsibility Principle (SRP)**: Each class/file is responsible for one thing only.
2. **Dependency Inversion Principle (DIP)**: High-level modules (like `Window`) don't depend on low-level modules (like `Button`), both depend on abstractions (`Control`).
3. **Open/Closed Principle (OCP)**: New controls can be easily extended by inheriting the `Control` base class without modifying existing code.
4. **Consistency**: All controls share unified `draw()` and `handleEvent()` interfaces.
## 🚀 Core Features
- **Extreme Lightweight**: Core library compiles to only ~12MB, with zero external dependencies. Generated applications are compact.
- **Clear Modular Architecture**: Uses `CoreTypes.h` to uniformly manage all types, eliminating duplicate definitions and greatly improving maintainability.
- **Native C++ Performance**: Built directly on EasyX and Win32 API, providing near-native execution efficiency with very low memory footprint (typically <10MB).
- **Complete Control System**: Button, Label, TextBox, Canvas, Table, Dialog, and MessageBox factory.
- **Highly Customizable**: From control colors, shapes (rectangle, rounded, circle, ellipse) to fill modes and font styles, all have detailed enum support for easy customization.
- **Simple & Intuitive API**: Uses classic object-oriented design, code as documentation, low learning curve.
- **Standard Project Structure**: Adopts standard include/src separation structure, supports CMake build, easy to integrate and use.
- **Enhanced Event System**: v2.0.0 introduces event consumption mechanism, all `handleEvent` methods return `bool` indicating whether event was consumed, supporting finer-grained event propagation control.
- **Dialog System**: New complete dialog support, including modal and non-modal dialogs, automatically handling background saving and restoration.
## ⚡ Quick Start (5 Minutes to Get Started)
> **🎯 Latest Version Download**
> Download pre-compiled library files and header files from [GitHub Releases](https://github.com/Ysm-04/StellarX/releases/latest) for quick integration into your project.
### Environment Requirements
- **OS**: Windows 10 or higher
- **Compiler**: C++17 supported compiler (e.g., **Visual Studio 2019+**)
- **Graphics Library**: [EasyX](https://easyx.cn/) (2022 version or higher, please select the version matching your compiler during installation)
- **Build Tool**: CMake 3.12+ (optional, recommended)
### Install EasyX
1. Visit [EasyX official website](https://easyx.cn/) to download the latest version
2. Run the installer, select the version matching your Visual Studio version
3. After installation, no additional configuration is needed, StellarX framework will automatically link EasyX
### Method 1: Using CMake Build (Recommended)
1. **Clone the project**:
```bash
git clone https://github.com/Ysm-04/StellarX.git
cd StellarX
```
2. **Generate build system**:
```bash
mkdir build
cd build
cmake ..
```
3. **Compile the project**:
```bash
cmake --build .
```
4. **Run the example**:
```bash
./examples/Demo
```
### Method 2: Manual Integration into Existing Project
1. **Copy the include and src directories** to your project
2. **Configure include paths** to ensure the compiler can find the `include/StellarX/` directory
3. **Add all .cpp files** to your project for compilation
### Create Your First StellarX Application
```cpp
// Just include this one header to use all features
#include "StellarX.h"
// Program entry point (use WinMain for better compatibility)
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
{
// 1. Create a 640x480 window with white background, titled "My App"
Window mainWindow(640, 480, 0, RGB(255, 255, 255), "My First StellarX App");
// 2. Create a button (managed with smart pointer)
auto myButton = std::make_unique<Button>(
250, 200, 140, 40, // x, y, width, height
"Click Me", // button text
StellarX::ButtonMode::NORMAL,
StellarX::ControlShape::ROUND_RECTANGLE
);
// 3. Set click event for the button (using Lambda expression)
myButton->setOnClickListener([&mainWindow]() {
// Use message box factory to create modal dialog
auto result = StellarX::MessageBox::ShowModal(
mainWindow,
"Welcome to StellarX GUI\r\nAuthor: Ysm-04",
"Greeting",
StellarX::MessageBoxType::OKCancel
);
// Handle dialog result
if (result == StellarX::MessageBoxResult::OK) {
// User clicked OK button
}
});
// 4. (Optional) Set button style
myButton->textStyle.nHeight = 20;
myButton->textStyle.color = RGB(0, 0, 128); // Dark blue text
myButton->setButtonBorder(RGB(0, 128, 255)); // Blue border
// 5. Add button to window
mainWindow.addControl(std::move(myButton));
// 6. Draw the window
mainWindow.draw();
// 7. Enter message loop, wait for user interaction
mainWindow.runEventLoop();
return 0;
}
```
1. **Compile and run!** You'll see a window with a blue rounded button, clicking it will pop up a message box.
## 📚 Core Types Detailed Explanation (`CoreTypes.h`)
All visual and behavioral properties of the StellarX framework are controlled through the elegant enums and structs defined in `CoreTypes.h`.
### Enum Types (Enums)
| Enum Type | Description | Common Values |
| :--------------------- | :---------------------- | :----------------------------------------------------------- |
| **`ControlShape`** | Control geometric shape | `RECTANGLE`, `B_RECTANGLE`, `ROUND_RECTANGLE`, `CIRCLE`, `ELLIPSE`, etc. |
| **`ButtonMode`** | Button behavior mode | `NORMAL`, `TOGGLE`, `DISABLED` |
| **`TextBoxMode`** | Text box mode | `INPUT_MODE`, `READONLY_MODE` |
| **`FillMode`** | Graphics fill mode | `SOLID`, `NULL`, `HATCHED`, etc. |
| **`FillStyle`** | Pattern fill style | `HORIZONTAL`, `CROSS`, etc. |
| **`LineStyle`** | Border line style | `SOLID`, `DASH`, `DOT`, etc. |
| **`MessageBoxType`** | Message box type | `OK`, `OKCancel`, `YesNo`, `YesNoCancel`, `RetryCancel`, `AbortRetryIgnore` |
| **`MessageBoxResult`** | Message box result | `OK`, `Cancel`, `Yes`, `No`, `Abort`, `Retry`, `Ignore` |
### Structs (Structs)
| Struct | Description |
| :----------------- | :----------------------------------------------------------- |
| **`ControlText`** | Encapsulates all text style attributes, including font, size, color, bold, italic, underline, strikethrough, etc. |
| **`RouRectangle`** | Defines rounded rectangle corner ellipse dimensions, contains width and height properties. |
**Usage Example:**
```c++
// Create a complex text style
StellarX::ControlText myStyle;
myStyle.nHeight = 25; // Font height
myStyle.lpszFace = _T("Microsoft YaHei"); // Font
myStyle.color = RGB(255, 0, 0); // Red color
myStyle.nWeight = FW_BOLD; // Bold
myStyle.bUnderline = true; // Underline
// Apply to controls
myLabel->textStyle = myStyle;
myButton->textStyle = myStyle;
```
## 🧩 Complete Control Library
### 1. Basic Controls
| Control | Header File | Description | Key Features |
| :---------- | :---------- | :-------------------- | :----------------------------------------------------------- |
| **Button** | `Button.h` | Multi-function button | Supports multiple modes/shapes/states, hover/click colors, custom callbacks |
| **Label** | `Label.h` | Text label | Supports transparent/opaque background, custom font styles |
| **TextBox** | `TextBox.h` | Input box/Display box | Supports input and read-only modes, integrates EasyX's `InputBox` |
### 2. Container Controls
| Control | Header File | Description |
| :--------- | :---------- | :----------------------------------------------------------- |
| **Canvas** | `Canvas.h` | Container control, can serve as parent container for other controls, supports custom borders and background. |
| **Window** | `Window.h` | Top-level window, ultimate container for all controls, responsible for message loop and dispatching. |
### 3. Advanced Controls
| Control | Header File | Description | Key Features |
| :--------- | :---------- | :----------- | :----------------------------------------------------------- |
| **Table** | `Table.h` | Data table | **Framework highlight feature**, supports paginated display, custom headers and data, automatic column width calculation, page turn buttons. |
| **Dialog** | `Dialog.h` | Dialog class | Implements complete dialog functionality, supports multiple button combinations and asynchronous result callbacks. Automatically handles layout, background save/restore and lifecycle management. |
**Table Control Example:**
```c++
// Create a table
auto myTable = std::make_unique<Table>(50, 50);
// Set headers
myTable->setHeaders({ "ID", "Name", "Age", "Occupation" });
// Add data rows
myTable->setData({ "1", "Zhang San", "25", "Engineer" });
myTable->setData({ "2", "Li Si", "30", "Designer" });
myTable->setData({ "3", "Wang Wu", "28", "Product Manager" });
// Set rows per page
myTable->setRowsPerPage(2);
// Set table style
myTable->textStyle.nHeight = 16;
myTable->setTableBorder(RGB(50, 50, 50));
myTable->setTableBk(RGB(240, 240, 240));
// Add to window
mainWindow.addControl(std::move(myTable));
```
### 4. Static Factory Class
| Control | Header File | Description | Key Features |
| :------------- | :------------- | :------------------- | :----------------------------------------------------------- |
| **MessageBox** | `MessageBox.h` | Dialog factory calls | Static method calls, no instantiation needed, automatically handles modal and non-modal logic differences, integrated into window's dialog management system, provides deduplication mechanism to prevent duplicate dialogs |
**MessageBox Usage Example:**
```c++
// Modal message box (blocks until closed)
auto result = StellarX::MessageBox::ShowModal(
mainWindow,
"Confirm to perform this operation?",
"Confirmation",
StellarX::MessageBoxType::YesNo
);
if (result == StellarX::MessageBoxResult::Yes)
{
// User selected "Yes"
}
// Non-modal message box (asynchronous callback)
StellarX::MessageBox::ShowAsync(
mainWindow,
"Operation completed",
"Notification",
StellarX::MessageBoxType::OK,
[](StellarX::MessageBoxResult result) {
// Asynchronously handle result
}
);
```
## Examples
- **Register Viewer (≈450 lines)** — An interactive 32-bit register visualization tool implemented based on StellarX (supports bit inversion, left/right shift, hex/decimal conversion, signed/unsigned toggle, binary grouping display).
Path: `examples/register-viewer/`
## 🔧 Advanced Topics & Best Practices
### 1. Custom Controls
You can create custom controls by inheriting from the `Control` base class. Just implement the two pure virtual functions `draw()` and `handleEvent()`.
```c++
class MyCustomControl : public Control {
public:
MyCustomControl(int x, int y) : Control(x, y, 100, 100) {}
void draw() override {
saveStyle();
// Your custom drawing logic
setfillcolor(RGB(255, 100, 100));
fillrectangle(x, y, x + width, y + height);
restoreStyle();
}
bool handleEvent(const ExMessage& msg) override {
// Your custom event handling logic
if (msg.message == WM_LBUTTONDOWN &&
msg.x > x && msg.x < x + width &&
msg.y > y && msg.y < y + height) {
// Handle click
return true; // Event consumed
}
return false; // Event not consumed
}
bool IsVisible() const override { return true; }
bool model() const override { return false; }
};
```
### 2. Layout Management
Current version of StellarX primarily uses **absolute positioning**. For simple layouts, you can achieve this by calculating coordinates. For complex layouts, consider:
- Nesting controls in `Canvas` to achieve relative positioning.
- Implementing simple flow layout or grid layout managers yourself.
### 3. Performance Optimization
- **Dirty Rectangle Rendering**: Implemented internally in the framework, controls set `dirty=true` when state changes, only redraw when necessary.
- **Image Resources**: Use `IMAGE` objects to load images, then reuse them to avoid multiple loads.
- **Reduce Operations in Loops**: Avoid heavy calculations in `draw()` and `handleEvent()`.
- **Event Consumption Mechanism**: Use event consumption return values appropriately to avoid unnecessary event propagation.
### 4. Dialog Usage Tips
- **Modal Dialogs**: Use `ShowModal` method, blocks current thread until dialog closes, suitable for important operations requiring user confirmation.
- **Non-modal Dialogs**: Use `ShowAsync` method, doesn't block main thread, suitable for informational prompts or background tasks.
- **Dialog Deduplication**: Framework has built-in non-modal dialog deduplication mechanism to prevent multiple dialogs for the same message from appearing simultaneously.
## ⚠️ Important Limitations & Suitable Scenarios
**StellarX framework's design goals are lightness, clarity, and teaching value, therefore it explicitly is NOT suitable for the following scenarios:**
- **High-performance games or complex animations**: Rendering is based on EasyX's CPU software rendering, performance is limited.
- **Applications requiring high DPI scaling**: Limited support for high DPI displays, interface may not display correctly.
- **Applications requiring accessibility features**: No support for screen readers and other assistive technologies.
- **Cross-platform applications**: Deeply dependent on Windows API and EasyX, cannot run directly on Linux/macOS.
- **Complex commercial software frontends**: Lacks advanced controls (like tree views, rich text boxes, tabs, advanced lists) and mature automatic layout managers.
**If you need to develop the aforementioned types of applications, please consider the following mature solutions:**
- **Qt**: Extremely powerful, cross-platform, suitable for large commercial applications.
- **wxWidgets**: Native appearance, cross-platform.
- **Dear ImGui**: Immediate mode GUI, very suitable for tools and debug interfaces.
- **Web Technology Stack (Electron/CEF)**: Suitable for scenarios requiring web technologies.
## 📜 License
This project uses the **MIT License**.
You are free to:
- Use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the framework.
- Use it for private or commercial projects.
The only requirement is:
- Please retain the original copyright notice in your projects.
See the [LICENSE](https://license/) file in the project root directory for details.
## 👥 Contribution Guide
We welcome all forms of contributions! If you want to contribute to the StellarX framework, please read the following guidelines:
1. **Code Style**: Please follow the existing Google C++ style guide (use spaces for indentation, braces on new lines, etc.).
2. **New Features**: Must provide **example code** and update relevant parts of this README document.
3. **Submitting PRs**: Please ensure your code is tested before submission and clearly describe your changes and motivation.
4. **Issue Reporting**: If you find a bug or have new ideas, welcome to submit Issues on GitHub.
Detailed contribution guide please refer to [CONTRIBUTING.md](CONTRIBUTING.en.md).
## 🙏 Acknowledgements
- Thanks to [EasyX Graphics Library](https://easyx.cn/) for providing the simple and easy-to-use graphics foundation for this project, making C++ GUI programming education possible.
- Thanks to all developers pursuing the **concise, efficient, clear** coding philosophy, you are the inspiration for StellarX's birth.
------
**Star Ocean Vast, Code as Boat.**
May `StellarX` become a reliable cornerstone for your exploration of the GUI world, whether for learning, teaching, or creating practical tools.
## 📞 Support & Feedback
If you encounter problems during use or have any suggestions:
1. Check [Example Code](examples/) for usage reference
2. Check [Changelog](CHANGELOG.en.md) to learn about latest changes
3. Submit Issues on GitHub repository to report problems
------
*StellarX Framework - Light as Dust, Bound by Stars*

View File

@@ -1,7 +1,15 @@
# 星垣 (StellarX) GUI Framework
[English document](README.en.md)
------
![GitHub all releases](https://img.shields.io/github/downloads/Ysm-04/StellarX/total)
[![Star GitHub Repo](https://img.shields.io/github/stars/Ysm-04/StellarX.svg?style=social&label=Star%20This%20Repo)](https://github.com/Ysm-04/StellarX)
![Version](https://img.shields.io/badge/Version-2.0.0-brightgreen.svg)
![Download](https://img.shields.io/badge/Download-1.0.0_Release-blue.svg)
![Download](https://img.shields.io/badge/Download-2.0.0_Release-blue.svg)
![C++](https://img.shields.io/badge/C++-17+-00599C?logo=cplusplus&logoColor=white)
![Windows](https://img.shields.io/badge/Platform-Windows-0078D6?logo=windows)
@@ -68,7 +76,7 @@ StellarX/
## 🚀 核心特性
- **极致的轻量级**: 核心库编译后仅 ~1.2MB无任何外部依赖。生成的应用程序小巧玲珑。
- **极致的轻量级**: 核心库编译后仅 ~12MB无任何外部依赖。生成的应用程序小巧玲珑。
- **清晰的模块化架构**: 使用`CoreTypes.h`统一管理所有类型,消除重复定义,极大提升可维护性。
- **原生C++性能**: 直接基于EasyX和Win32 API提供接近原生的执行效率内存占用极低通常<10MB
- **完整的控件体系**: 按钮Button、标签Label、文本框TextBox、画布Canvas、表格Table、对话框Dialog与消息框工厂MessageBox
@@ -317,7 +325,9 @@ StellarX::MessageBox::ShowAsync(
);
```
## 示例
- **寄存器查看器 (≈450 行)** — 一个基于 StellarX 实现的交互式 32 位寄存器可视化工具(支持位取反、左/右移、十六进制/十进制转换、带有符号/无符号切换、二进制分组显示)。
路径:`examples/register-viewer/`
## 🔧 高级主题与最佳实践

View File

@@ -0,0 +1,25 @@
# Contributor Covenant Code of Conduct
## Our Pledge
We as members, contributors, and maintainers pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
## Our Standards
Examples of behavior that contributes to creating a positive environment include:
* Being considerate and respectful of others
* Respecting different viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
...
## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the project maintainers. All complaints will be reviewed and investigated promptly and fairly, and will result in a response that is deemed necessary and appropriate to the circumstances.
...
For answers to common questions about this code of conduct, see https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.

View File

@@ -0,0 +1,17 @@
# Register Viewer (StellarX example)
**A 32-bit register visualizer built with StellarX** (Windows + EasyX).
Features:
- 32 toggle bits (MSB→LSB = 31..0)
- Range invert, logical left/right shift
- Hex/Decimal display with signed/unsigned toggle
- Grouped binary view with "last/current" snapshots
- One-click set all 0 / all 1
## Build & Run
1. Ensure StellarX and EasyX are in your include/lib paths.
2. Build as a normal C++ Win32 desktop app (e.g., VS 2019+).
3. Run the executable.
This example demonstrates StellarX APIs with a single `main.cpp` (~450 LOC):
`Window / Canvas / Button / Label / TextBox` + simple event callbacks.

View File

@@ -0,0 +1,462 @@
// 本工具基于 StellarX 构建,轻量级的 Windows GUI 框架。
#include"StellarX.h"
#include <sstream>
#include<iomanip>
#include<array>
auto blackColor = RGB(202, 255, 255);
char initData[33] = "00000000000000000000000000000000";//初始数据
bool gSigned = false; //是否为有符号数
void main()
{
Window mainWindow(700,500,NULL,RGB(255,255,255), "寄存器查看工具 V1.0——我在人间做废物 (同类工具定制3150131407(Q / V))");
//选择区控件
auto selectionAreaLabel = std::make_unique<Label>(18, 0,"32位选择区");
selectionAreaLabel->setTextdisap(true);
std::vector<std::unique_ptr<Label>>selectionAreaButtonLabel;
std::vector<std::unique_ptr<Button>>selectionAreaButton;
std::vector<Button*>selectionAreaButton_ptr;
auto selectionArea = std::make_unique <Canvas>(10, 10, 680, 150);
selectionArea->setCanvasBkColor(blackColor);
selectionArea->setShape(StellarX::ControlShape::B_ROUND_RECTANGLE);
for (int y = 0; y < 2; y ++)
{
std::ostringstream os;
for (int x = 0; x <16; x++)
{
if (0 == y)
{
selectionAreaButtonLabel.push_back(std::make_unique<Label>(x * 35 + 40 + 28 * (x / 4), 26, "", RGB(208, 208, 208)));
os << std::setw(2) << std::setfill('0') << 31 - x;
selectionAreaButtonLabel.back()->setText(os.str());
selectionAreaButtonLabel.back()->setTextdisap(true);
selectionAreaButton.push_back(
std::make_unique<Button>(x * 35 + 42 + 28 * (x / 4), 58,20,32,"0",
blackColor,RGB(171, 196, 220),StellarX::ButtonMode::TOGGLE));
selectionAreaButton.back()->textStyle.color = RGB(226, 116, 152);
selectionAreaButton.back()->setButtonShape(StellarX::ControlShape::B_RECTANGLE);
selectionAreaButton_ptr.push_back(selectionAreaButton.back().get());
int k = 32 - x - 1;
//选择区按钮被点击后在二进制0和1之间切换并更新initData
selectionAreaButton_ptr.back()->setOnToggleOnListener([k, btn = selectionAreaButton_ptr.back()]()
{
btn->setButtonText("1");
initData[k] = '1';
});
selectionAreaButton_ptr.back()->setOnToggleOffListener([k, btn = selectionAreaButton_ptr.back()]()
{
btn->setButtonText("0");
initData[k] = '0';
});
}
else
{
selectionAreaButtonLabel.push_back(std::make_unique<Label>(x * 35 + 40 + 28 * (x / 4), 90, "", RGB(208, 208, 208)));
os << std::setw(2) << std::setfill('0') << 15-x;
selectionAreaButtonLabel.back()->setText(os.str());
selectionAreaButtonLabel.back()->setTextdisap(true);
selectionAreaButton.push_back(
std::make_unique<Button>(x * 35 + 42 + 28 * (x / 4), 120, 20, 32, "0",
blackColor, RGB(171, 196, 220), StellarX::ButtonMode::TOGGLE));
selectionAreaButton.back()->textStyle.color = RGB(226, 116, 152);
selectionAreaButton.back()->setButtonShape(StellarX::ControlShape::B_RECTANGLE);
selectionAreaButton_ptr.push_back(selectionAreaButton.back().get());
int k =15 - x;
selectionAreaButton.back()->setOnToggleOnListener([k,btn = selectionAreaButton_ptr.back()]()
{
btn->setButtonText("1");
initData[k] = '1';
});
selectionAreaButton.back()->setOnToggleOffListener([k, btn = selectionAreaButton_ptr.back()]()
{
btn->setButtonText("0");
initData[k] = '0';
});
}
os.str("");
os.clear();
}
}
selectionArea->addControl(std::move(selectionAreaLabel));
for (auto& s : selectionAreaButton)
selectionArea->addControl(std::move(s));
for (auto& s : selectionAreaButtonLabel)
selectionArea->addControl(std::move(s));
//功能区控件
//功能区总容器
auto function = std::make_unique<Canvas>(0, 0, 0, 0);
function->setCanvasfillMode(StellarX::FillMode::Null);
auto bitInvert = std::make_unique<Canvas>(10,170,220,70);
auto leftShift = std::make_unique<Canvas>(240, 170, 220, 70);
auto rightShift = std::make_unique<Canvas>(470, 170, 220, 70);
bitInvert->setCanvasBkColor(blackColor);
bitInvert->setShape(StellarX::ControlShape::B_ROUND_RECTANGLE);
leftShift->setCanvasBkColor(blackColor);
leftShift->setShape(StellarX::ControlShape::B_ROUND_RECTANGLE);
rightShift->setCanvasBkColor(blackColor);
rightShift->setShape(StellarX::ControlShape::B_ROUND_RECTANGLE);
auto bitInvertLabel = std::make_unique<Label>(18,160,"位取反");
bitInvertLabel->setTextdisap(true);
auto leftShiftLabel = std::make_unique<Label>(248, 160, "左移位");
leftShiftLabel->setTextdisap(true);
auto rightShiftLabel = std::make_unique<Label>(478, 160, "右移位");
rightShiftLabel->setTextdisap(true);
// ====== 公用小工具======
auto clamp = [](int v, int lo, int hi) { return v < lo ? lo : (v > hi ? hi : v); };
auto toInt = [](const std::string& s, int def = 0) {
try { return std::stoi(s); }
catch (...) { return def; }
};
// bit号(31..0) -> selectionAreaButton下标(0..31)
auto vecIndexFromBit = [](int bit) { return 31 - bit; };
// 读取当前32位点击态
auto snapshotBits = [&]() {
std::array<bool, 32> a{};
for (int b = 0; b < 32; ++b)
a[b] = selectionAreaButton_ptr[vecIndexFromBit(b)]->isClicked();
return a;
};
// 应用目标态:仅当不同才 setButtonClick
auto applyBits = [&](const std::array<bool, 32>& a) {
for (int b = 0; b < 32; ++b) {
auto btn = selectionAreaButton_ptr[vecIndexFromBit(b)];
if (btn->isClicked() != a[b]) btn->setButtonClick(a[b]);
}
};
//取反区控件
std::array<std::unique_ptr<Label>, 4> bitInvertFunctionLabel;
bitInvertFunctionLabel[0] = std::make_unique<Label>(35, 180, "低位");
bitInvertFunctionLabel[1] = std::make_unique<Label>(90, 180, "高位");
bitInvertFunctionLabel[2] = std::make_unique<Label>(15, 198, "");
bitInvertFunctionLabel[3] = std::make_unique<Label>(75, 198, "");
std::array<std::unique_ptr<TextBox>, 2> bitInvertFunctionTextBox;
bitInvertFunctionTextBox[0] = std::make_unique<TextBox>(35, 203, 35, 30, "0");
bitInvertFunctionTextBox[1] = std::make_unique<TextBox>(95, 203, 35, 30, "0");
auto invL = bitInvertFunctionTextBox[0].get();
auto invH = bitInvertFunctionTextBox[1].get();
auto bitInvertFunctionButton = std::make_unique<Button>(150,195, 70, 35, "位取反",
blackColor, RGB(171, 196, 220));
bitInvertFunctionButton->textStyle.color = RGB(226, 116, 152);
bitInvertFunctionButton->setButtonShape(StellarX::ControlShape::B_RECTANGLE);
auto bitInvertFunctionButton_ptr = bitInvertFunctionButton.get();
bitInvert->addControl(std::move(bitInvertFunctionButton));
bitInvert->addControl(std::move(bitInvertLabel));
for (auto& b : bitInvertFunctionTextBox)
{
b->setMaxCharLen(3);
b->textStyle.color = RGB(226, 116, 152);
b->setTextBoxBk(RGB(244, 234, 142));
b->setTextBoxshape(StellarX::ControlShape::B_RECTANGLE);
bitInvert->addControl(std::move(b));
}
for (auto& b : bitInvertFunctionLabel)
{
b->setTextdisap(true);
bitInvert->addControl(std::move(b));
}
//左移控件
auto leftShiftFunctionLabel = std::make_unique<Label>(435, 198, "");
leftShiftFunctionLabel->setTextdisap(true);
auto leftShiftFunctionTextBox = std::make_unique<TextBox>(325, 195, 100, 30, "0");
leftShiftFunctionTextBox->setMaxCharLen(3);
leftShiftFunctionTextBox->textStyle.color = RGB(226, 116, 152);
leftShiftFunctionTextBox->setTextBoxBk(RGB(244, 234, 142));
leftShiftFunctionTextBox->setTextBoxshape(StellarX::ControlShape::B_RECTANGLE);
auto shlBox = leftShiftFunctionTextBox.get();
auto leftShiftFunctionButton = std::make_unique<Button>(250, 195, 60, 30, "左移",
blackColor, RGB(171, 196, 220));
leftShiftFunctionButton->textStyle.color = RGB(226, 116, 152);
leftShiftFunctionButton->setButtonShape(StellarX::ControlShape::B_RECTANGLE);
auto leftShiftFunctionButton_ptr = leftShiftFunctionButton.get();
leftShift->addControl(std::move(leftShiftFunctionButton));
leftShift->addControl(std::move(leftShiftFunctionTextBox));
leftShift->addControl(std::move(leftShiftLabel));
leftShift->addControl(std::move(leftShiftFunctionLabel));
//右移控件
auto rightShiftFunctionLabel = std::make_unique<Label>(665, 198, "");
rightShiftFunctionLabel->setTextdisap(true);
auto rightShiftFunctionTextBox = std::make_unique<TextBox>(555, 195, 100, 30, "0");
rightShiftFunctionTextBox->setMaxCharLen(3);
rightShiftFunctionTextBox->textStyle.color = RGB(226, 116, 152);
rightShiftFunctionTextBox->setTextBoxBk(RGB(244, 234, 142));
rightShiftFunctionTextBox->setTextBoxshape(StellarX::ControlShape::B_RECTANGLE);
auto shrBox = rightShiftFunctionTextBox.get();
auto rightShiftFunctionButton = std::make_unique<Button>(480, 195, 60, 30, "右移",
blackColor, RGB(171, 196, 220));
rightShiftFunctionButton->textStyle.color = RGB(226, 116, 152);
rightShiftFunctionButton->setButtonShape(StellarX::ControlShape::B_RECTANGLE);
auto rightShiftFunctionButton_ptr = rightShiftFunctionButton.get();
rightShift->addControl(std::move(rightShiftFunctionButton));
rightShift->addControl(std::move(rightShiftFunctionTextBox));
rightShift->addControl(std::move(rightShiftLabel));
rightShift->addControl(std::move(rightShiftFunctionLabel));
function->addControl(std::move(bitInvert));
function->addControl(std::move(leftShift));
function->addControl(std::move(rightShift));
//显示区控件
//数值显示
auto NumericalDisplayArea = std::make_unique<Canvas>(10, 255, 680, 70);
NumericalDisplayArea->setCanvasBkColor(blackColor);
NumericalDisplayArea->setShape(StellarX::ControlShape::B_ROUND_RECTANGLE);
std::array<std::unique_ptr<Label>, 3> NumericalDisplayAreaLabel;
NumericalDisplayAreaLabel[0] = std::make_unique<Label>(18, 245, "数值显示区");
NumericalDisplayAreaLabel[1] = std::make_unique<Label>(20, 278, "十六进制");
NumericalDisplayAreaLabel[2] = std::make_unique<Label>(330, 278, "十进制");
std::array<std::unique_ptr<TextBox>, 2> NumericalDisplayAreaTextBox;
NumericalDisplayAreaTextBox[0] = std::make_unique<TextBox>(110, 275, 200, 30, "0");
NumericalDisplayAreaTextBox[1] = std::make_unique<TextBox>(400, 275, 200, 30, "0");
auto hex = NumericalDisplayAreaTextBox[0].get();
auto dec = NumericalDisplayAreaTextBox[1].get();
for (auto& b : NumericalDisplayAreaLabel)
{
b->setTextdisap(true);
NumericalDisplayArea->addControl(std::move(b));
}
for (auto& b : NumericalDisplayAreaTextBox)
{
b->setMaxCharLen(11);
b->textStyle.color = RGB(255, 69, 0);
b->setTextBoxBk(RGB(141, 141, 141));
b->setTextBoxshape(StellarX::ControlShape::B_RECTANGLE);
b->setMode(StellarX::TextBoxmode::READONLY_MODE);
NumericalDisplayArea->addControl(std::move(b));
}
//二进制显示
auto BinaryDisplayArea = std::make_unique<Canvas>(10, 335, 680, 110);
BinaryDisplayArea->setCanvasBkColor(blackColor);
BinaryDisplayArea->setShape(StellarX::ControlShape::B_ROUND_RECTANGLE);
std::array<std::unique_ptr<Label>, 3> BinaryDisplayAreaLabel;
BinaryDisplayAreaLabel[0] = std::make_unique<Label>(18, 325, "二进制显示区");
BinaryDisplayAreaLabel[1] = std::make_unique<Label>(35, 353, "上次值");
BinaryDisplayAreaLabel[2] = std::make_unique<Label>(35, 400, "本次值");
std::array<std::unique_ptr<TextBox>, 2> BinaryDisplayAreaTextBox;
BinaryDisplayAreaTextBox[0] = std::make_unique<TextBox>(110, 350, 520, 30, "0000_0000_0000_0000_0000_0000_0000_0000");
BinaryDisplayAreaTextBox[1] = std::make_unique<TextBox>(110, 400, 520, 30, "0000_0000_0000_0000_0000_0000_0000_0000");
auto Last = BinaryDisplayAreaTextBox[0].get();
auto This = BinaryDisplayAreaTextBox[1].get();
for (auto& b : BinaryDisplayAreaLabel)
{
b->setTextdisap(true);
BinaryDisplayArea->addControl(std::move(b));
}
for (auto& b : BinaryDisplayAreaTextBox)
{
b->setMaxCharLen(40);
b->textStyle.color = RGB(255, 69, 0);
b->setTextBoxBk(RGB(141, 141, 141));
b->setTextBoxshape(StellarX::ControlShape::B_RECTANGLE);
b->setMode(StellarX::TextBoxmode::READONLY_MODE);
BinaryDisplayArea->addControl(std::move(b));
}
// 由位图 bits 生成数值
auto valueFromBits = [](const std::array<bool, 32>& bits) -> uint32_t {
uint32_t v = 0;
for (int b = 0; b < 32; ++b) if (bits[b]) v |= (1u << b);
return v;
};
// 由位图 bits 生成 "0000_0000_..._0000"MSB→LSB31..0
auto binaryGroupedFromBits = [](const std::array<bool, 32>& bits) -> std::string {
std::string s; s.reserve(39);
for (int b = 31; b >= 0; --b) {
s.push_back(bits[b] ? '1' : '0');
if (b % 4 == 0 && b != 0) s.push_back('_');
}
return s;
};
// 用“目标位图 bits”刷新显示区
auto refreshDisplaysWithBits = [&](const std::string& prevThis,
const std::array<bool, 32>& bits,
TextBox* hex, TextBox* dec, TextBox* Last, TextBox* This)
{
const uint32_t val = valueFromBits(bits);
const int32_t s = static_cast<int32_t>(val);
char hexbuf[16];
std::snprintf(hexbuf, sizeof(hexbuf), "%08X", val);
hex->setText(hexbuf); // HEX大写8位
dec->setText(gSigned ? std::to_string(s) : std::to_string(val)); // DEC
Last->setText(prevThis); // 上次值 ← 刷新前的本次值
This->setText(binaryGroupedFromBits(bits)); // 本次值 ← 由“目标位图”生成
};
bitInvertFunctionButton_ptr->setOnClickListener([=, &snapshotBits, &applyBits, &refreshDisplaysWithBits]() {
const std::string prevThis = This->getText();
int L = clamp(toInt(invL->getText(), 0), 0, 31);
int H = clamp(toInt(invH->getText(), 0), 0, 31);
if (L > H) std::swap(L, H);
auto cur = snapshotBits();
for (int b = L; b <= H; ++b) cur[b] = !cur[b];
applyBits(cur); // 只改按钮点击态(触发位按钮自回调)
refreshDisplaysWithBits(prevThis, cur, hex, dec, Last, This);
});
leftShiftFunctionButton_ptr->setOnClickListener([=, &snapshotBits, &applyBits, &refreshDisplaysWithBits]() {
const std::string prevThis = This->getText();
int n = clamp(toInt(shlBox->getText(), 0), 0, 31);
auto cur = snapshotBits();
std::array<bool, 32> nxt{}; // 默认全 0
// 逻辑左移:高位丢弃、低位补 0
for (int b = 31; b >= 0; --b) nxt[b] = (b >= n) ? cur[b - n] : false;
applyBits(nxt);
refreshDisplaysWithBits(prevThis, nxt, hex, dec, Last, This);
});
rightShiftFunctionButton_ptr->setOnClickListener([=, &snapshotBits, &applyBits, &refreshDisplaysWithBits]() {
const std::string prevThis = This->getText();
int n = clamp(toInt(shrBox->getText(), 0), 0, 31);
auto cur = snapshotBits();
std::array<bool, 32> nxt{};
// 逻辑右移:低位丢弃、高位补 0
for (int b = 0; b < 32; ++b) nxt[b] = (b + n <= 31) ? cur[b + n] : false;
applyBits(nxt);
refreshDisplaysWithBits(prevThis, nxt, hex, dec, Last, This);
});
//配置区控件clearrectangle(10, 440, 690, 490);
auto configuration = std::make_unique<Canvas>(10, 455, 680, 40);
configuration->setCanvasBkColor(blackColor);
configuration->setShape(StellarX::ControlShape::B_ROUND_RECTANGLE);
auto configurationLabel = std::make_unique<Label>(20, 445, "配置区");
configurationLabel->setTextdisap(true);
std::array<std::unique_ptr<Button>,2> configurationButton;
configurationButton[0] = std::make_unique<Button>(450, 465, 80, 20, "一键置0",
blackColor, RGB(171, 196, 220));
configurationButton[0]->textStyle.color = RGB(226, 116, 152);
configurationButton[0]->setButtonShape(StellarX::ControlShape::B_RECTANGLE);
configurationButton[1] = std::make_unique<Button>(550, 465, 80, 20, "一键置1",
blackColor, RGB(171, 196, 220));
configurationButton[1]->textStyle.color = RGB(226, 116, 152);
configurationButton[1]->setButtonShape(StellarX::ControlShape::B_RECTANGLE);
configurationButton[0]->setOnClickListener(
[&]() {
for (auto& s : selectionAreaButton_ptr)
if (s->isClicked()) s->setButtonClick(false);
// 刷新显示prevThis 用当前 This 文本
const std::string prevThis = This->getText();
auto cur = snapshotBits();
{
char hexbuf[16];
uint32_t u = 0; for (int b = 0; b < 32; ++b) if (cur[b]) u |= (1u << b);
int32_t s = static_cast<int32_t>(u);
std::snprintf(hexbuf, sizeof(hexbuf), "%08X", u);
hex->setText(hexbuf);
dec->setText(gSigned ? std::to_string(s) : std::to_string(u));
Last->setText(prevThis);
This->setText(binaryGroupedFromBits(cur));
}
});
configurationButton[1]->setOnClickListener(
[&]() {
for (auto& s : selectionAreaButton_ptr)
if (!s->isClicked()) s->setButtonClick(true);
const std::string prevThis = This->getText();
auto cur = snapshotBits();
char hexbuf[16];
uint32_t u = 0; for (int b = 0; b < 32; ++b) if (cur[b]) u |= (1u << b);
int32_t s = static_cast<int32_t>(u);
std::snprintf(hexbuf, sizeof(hexbuf), "%08X", u);
hex->setText(hexbuf);
dec->setText(gSigned ? std::to_string(s) : std::to_string(u));
Last->setText(prevThis);
This->setText(binaryGroupedFromBits(cur));
});
auto signedToggle = std::make_unique<Button>(
350, 465, 80, 20, "无符号",
blackColor, RGB(171, 196, 220), StellarX::ButtonMode::TOGGLE);
signedToggle->textStyle.color = RGB(226, 116, 152);
signedToggle->setButtonShape(StellarX::ControlShape::B_RECTANGLE);
auto* signedTogglePtr = signedToggle.get();
signedTogglePtr->setOnToggleOnListener([&]() {
gSigned = true;
signedTogglePtr->setButtonText("有符号");
// 立即刷新十进制显示:用当前位图算出新值,仅改 dec
auto cur = snapshotBits();
const uint32_t u = [&] { uint32_t v = 0; for (int b = 0; b < 32; ++b) if (cur[b]) v |= (1u << b); return v; }();
const int32_t s = static_cast<int32_t>(u);
dec->setText(std::to_string(s));
});
signedTogglePtr->setOnToggleOffListener([&]() {
gSigned = false;
signedTogglePtr->setButtonText("无符号");
auto cur = snapshotBits();
const uint32_t u = [&] { uint32_t v = 0; for (int b = 0; b < 32; ++b) if (cur[b]) v |= (1u << b); return v; }();
dec->setText(std::to_string(u));
});
configuration->addControl(std::move(configurationButton[0]));
configuration->addControl(std::move(configurationButton[1]));
configuration->addControl(std::move(signedToggle));
configuration->addControl(std::move(configurationLabel));
mainWindow.addControl(std::move(selectionArea));
mainWindow.addControl(std::move(function));
mainWindow.addControl(std::move(NumericalDisplayArea));
mainWindow.addControl(std::move(BinaryDisplayArea));
mainWindow.addControl(std::move(configuration));
mainWindow.draw();
return mainWindow.runEventLoop();
}

View File

@@ -19,6 +19,12 @@
******************************************************************************/
#pragma once
#include "Control.h"
#include"label.h"
#define DISABLEDCOLOUR RGB(96, 96, 96) //禁用状态颜色
#define TEXTMARGINS_X 6
#define TEXTMARGINS_Y 4
class Button : public Control
{
@@ -27,6 +33,12 @@ class Button : public Control
bool click; // 是否被点击
bool hover; // 是否被悬停
std::string cutText; // 切割后的文本
bool needCutText = true; // 是否需要切割文本
bool isUseCutText = false; // 是否使用切割文本
int padX = TEXTMARGINS_X; // 文本最小左右内边距
int padY = TEXTMARGINS_Y; // 文本最小上下内边距
COLORREF buttonTrueColor; // 按钮被点击后的颜色
COLORREF buttonFalseColor; // 按钮未被点击的颜色
COLORREF buttonHoverColor; // 按钮被鼠标悬停的颜色
@@ -50,6 +62,24 @@ class Button : public Control
int oldtext_height = -1;
int text_width = 0;
int text_height = 0;
// === Tooltip ===
bool tipEnabled = false; // 是否启用
bool tipVisible = false; // 当前是否显示
bool tipFollowCursor = false; // 是否跟随鼠标
bool tipUserOverride = false; // 是否用户自定义了tip文本
int tipDelayMs = 1000; // 延时(毫秒)
int tipOffsetX = 12; // 相对鼠标偏移
int tipOffsetY = 18;
ULONGLONG tipHoverTick = 0; // 开始悬停的时间戳
int lastMouseX = 0; // 最新鼠标位置(用于定位)
int lastMouseY = 0;
std::string tipTextClick; //NORMAL 模式下用
std::string tipTextOn; // click==true 时用
std::string tipTextOff; // click==false 时用
Label tipLabel; // 直接复用Label作为提示
public:
StellarX::ControlText textStyle; // 按钮文字样式
@@ -72,7 +102,6 @@ public:
//绘制按钮
void draw() override;
//按钮事件处理
bool handleEvent(const ExMessage& msg) override;
//设置回调函数
@@ -102,6 +131,8 @@ public:
void setButtonText(std::string text);
//设置按钮形状
void setButtonShape(StellarX::ControlShape shape);
//设置按钮点击状态
void setButtonClick(BOOL click);
//判断按钮是否被点击
bool isClicked() const;
@@ -129,11 +160,31 @@ public:
int getButtonWidth() const;
//获取按钮高度
int getButtonHeight() const;
//获取按钮横坐标
int getButtonX() const;
//获取按钮纵坐标
int getButtonY() const;
public:
// === Tooltip API===
//设置是否启用提示框
void enableTooltip(bool on) { tipEnabled = on; if (!on) tipVisible = false; }
//设置提示框延时
void setTooltipDelay(int ms) { tipDelayMs = (ms < 0 ? 0 : ms); }
//设置提示框是否跟随鼠标
void setTooltipFollowCursor(bool on) { tipFollowCursor = on; }
//设置提示框位置偏移
void setTooltipOffset(int dx, int dy) { tipOffsetX = dx; tipOffsetY = dy; }
//设置提示框样式
void setTooltipStyle(COLORREF text, COLORREF bk, bool transparent)
{
tipLabel.setTextColor(text);
tipLabel.setTextBkColor(bk);
tipLabel.setTextdisap(transparent);
}
//设置提示框文本
void setTooltipText(const std::string& s){ tipTextClick = s; tipUserOverride = true; }
void setTooltipTextsForToggle(const std::string& onText, const std::string& offText)
{
tipTextOn = onText;
tipTextOff = offText;
tipUserOverride = true;
}
private:
//初始化按钮
void initButton(const std::string text, StellarX::ButtonMode mode, StellarX::ControlShape shape, COLORREF ct, COLORREF cf, COLORREF ch);
@@ -141,10 +192,13 @@ private:
bool isMouseInCircle(int mouseX, int mouseY, int x, int y, int radius);
//判断鼠标是否在椭圆按钮内
bool isMouseInEllipse(int mouseX, int mouseY, int x, int y, int width, int height);
//检查是否对话框是否可见
bool IsVisible() const override { return false; }
//获取对话框类型
bool model() const override { return false; }
void cutButtonText();
// 统一隐藏&恢复背景
void hideTooltip();
// 根据当前 click 状态选择文案
void refreshTooltipTextForState();
};

View File

@@ -2,8 +2,8 @@
* @类: Canvas
* @摘要: 画布容器控件,用于分组和管理子控件
* @描述:
* 作为其他控件的父容器,提供统一的背景和边框样式。
* 负责将事件传递给子控件并管理它们的绘制顺序。
* 作为其他控件的父容器,提供统一的背景和边框样式。
* 负责将事件传递给子控件并管理它们的绘制顺序。
*
* @特性:
* - 支持四种矩形形状(普通、圆角,各有边框和无边框版本)
@@ -14,7 +14,7 @@
* @使用场景: 用于分组相关控件、实现复杂布局或作为对话框基础
* @所属框架: 星垣(StellarX) GUI框架
* @作者: 我在人间做废物
******************************************************************************/
*******************************************************************************/
#pragma once
#include "Control.h"
@@ -24,22 +24,17 @@ class Canvas : public Control
protected:
std::vector<std::unique_ptr<Control>> controls;
StellarX::ControlShape shape = StellarX::ControlShape::RECTANGLE; //容器形状
StellarX::FillMode canvasFillMode = StellarX::FillMode::Solid; //容器填充模式
StellarX::LineStyle canvasLineStyle = StellarX::LineStyle::Solid; //线
int canvaslinewidth = 1; //线宽
StellarX::ControlShape shape = StellarX::ControlShape::RECTANGLE; //容器形状
StellarX::FillMode canvasFillMode = StellarX::FillMode::Solid; //容器填充模式
StellarX::LineStyle canvasLineStyle = StellarX::LineStyle::Solid; //线型
int canvaslinewidth = 1; //线
COLORREF canvasBorderClor = RGB(0, 0, 0);//边框颜色
COLORREF canvasBorderClor = RGB(0, 0, 0); //边框颜色
COLORREF canvasBkClor = RGB(255,255,255); //背景颜色
void clearAllControls(); // 清除所有子控件
//检查是否对话框是否可见
bool IsVisible() const override { return false; }
//获取对话框类型
bool model() const override { return false; }
// 清除所有子控件
void clearAllControls();
public:
Canvas();
Canvas(int x, int y, int width, int height);
@@ -49,7 +44,6 @@ public:
bool handleEvent(const ExMessage& msg) override;
//添加控件
void addControl(std::unique_ptr<Control> control);
//设置容器样式
void setShape(StellarX::ControlShape shape);
//设置容器填充模式
@@ -62,6 +56,9 @@ public:
void setCanvasLineStyle(StellarX::LineStyle style);
//设置线段宽度
void setLinewidth(int width);
private:
//用来检查对话框是否模态,此控件不做实现
bool model() const override { return false; };
};

View File

@@ -16,6 +16,16 @@
* @作者: 我在人间做废物
******************************************************************************/
#pragma once
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0600
#endif
#ifndef WINVER
#define WINVER _WIN32_WINNT
#endif
#include <windows.h>
#include <vector>
#include <memory>
#include <easyx.h>
@@ -24,13 +34,19 @@
#include <string>
#include <functional>
#include "CoreTypes.h"
class Control
{
protected:
int x, y; // 左上角坐标
int width, height; // 控件尺寸
bool dirty = true; // 是否重绘
bool show = true; // 是否显示
/* == 背景快照 == */
IMAGE* saveBkImage = nullptr;
int saveBkX = 0, saveBkY = 0; // 快照保存起始坐标
int saveWidth = 0, saveHeight = 0; // 快照保存尺寸
bool hasSnap = false; // 当前是否持有有效快照
StellarX::RouRectangle rouRectangleSize; // 圆角矩形椭圆宽度和高度
@@ -40,7 +56,6 @@ protected:
COLORREF* currentBorderColor = new COLORREF(); // 边框颜色
LINESTYLE* currentLineStyle = new LINESTYLE(); // 保存当前线型
public:
Control(const Control&) = delete;
Control& operator=(const Control&) = delete;
Control(Control&&) = default;
@@ -48,25 +63,31 @@ public:
Control() : x(0), y(0), width(100), height(100) {}
Control(int x, int y, int width, int height)
: x(x), y(y), width(width), height(height) {
}
: x(x), y(y), width(width), height(height) {}
public:
virtual ~Control() {
virtual ~Control()
{
delete currentFont;
delete currentColor;
delete currentBkColor;
delete currentBorderColor;
delete currentLineStyle;
currentFont = nullptr;
currentFont = nullptr;
currentColor = nullptr;
currentBkColor = nullptr;
currentBorderColor = nullptr;
currentLineStyle = nullptr;
}
protected:
void saveBackground(int x, int y, int w, int h);
void restBackground(); // putimage 回屏
void discardBackground(); // 释放快照(窗口重绘/尺寸变化后必须作废)
public:
//释放快照重新保存,在尺寸变化时更新背景快照避免尺寸变化导致显示错位
void updateBackground();
// 获取位置和尺寸
int getX() const { return x; }
int getY() const { return y; }
@@ -74,16 +95,25 @@ protected:
int getHeight() const { return height; }
int getRight() const { return x + width; }
int getBottom() const { return y + height; }
void setX(int x) { this->x = x; dirty = true; }
void setY(int y) { this->y = y; dirty = true; }
void setWidth(int width) { this->width = width; dirty = true; }
void setHeight(int height) { this->height = height; dirty = true; }
public:
//设置是否重绘
void setDirty(bool dirty) { this->dirty = dirty; }
virtual void draw() = 0;
virtual bool handleEvent(const ExMessage& msg) = 0;//返回true代表事件已消费
//用来检查非模态对话框是否可见,其他控件不用实现
virtual bool IsVisible() const = 0;
//设置是否显示
void setShow(bool show) { this->show = show; }
//检查控件是否可见
bool IsVisible() const { return show; };
//用来检查对话框是否模态,其他控件不用实现
virtual bool model()const = 0;
protected:
void saveStyle();
void restoreStyle();

View File

@@ -111,7 +111,7 @@ namespace StellarX
* @使用示例:
* LineStyle var = LineStyle::Solid;
*/
enum class LineStyle
enum class LineStyle
{
Solid = PS_SOLID, // 实线
Dash = PS_DASH, // 虚线
@@ -251,7 +251,7 @@ namespace StellarX
};
// 消息框类型
enum class MessageBoxType
enum class MessageBoxType
{
OK, // 只有确定按钮
OKCancel, // 确定和取消按钮
@@ -262,7 +262,7 @@ namespace StellarX
};
// 消息框返回值
enum class MessageBoxResult
enum class MessageBoxResult
{
OK = 1, // 确定按钮
Cancel = 2, // 取消按钮
@@ -272,4 +272,51 @@ namespace StellarX
Retry = 4, // 重试按钮
Ignore = 5 // 忽略按钮
};
#if 0 //布局管理器相关 —待实现—
/*
*
*@枚举名称: LayoutKind
* @功能描述 : 定义布局管理类型
*
*@详细说明 :
* 根据布局管理类型,控件可以有不同的布局方式。
* 用户可以在具体情况下设置布局管理类型。
*
* @取值说明 :
* Absolute不管保持子控件自己的坐标向后兼容
* HBox 水平方向排队;支持固定宽、权重分配、对齐、拉伸。
* VBox 竖直方向排队;同上。
* Grid网格按行列摆支持固定/自适应/权重行列;支持跨行/跨列;单元内对齐/拉伸。
*
*/
// 布局类型
enum class LayoutKind
{
Absolute = 1,
HBox,
VBox,
Grid,
Flow,
Stack
};
// 布局参数
struct LayoutParams
{
// 边距左、右、上、下
int marginL = 0, marginR = 0, marginT = 0, marginB = 0;
// 固定尺寸(>=0 强制;-1 用控件当前尺寸)
int fixedW = -1, fixedH = -1;
// 主轴权重HBox=宽度、VBox=高度、Grid见下
float weight = 0.f;
// 对齐(非拉伸时生效)
enum Align { Start = 0, Center = 1, End = 2, Stretch = 3 };
int alignX = Start; // HBox: 次轴=YVBox: 次轴=XGrid: 单元内
int alignY = Start; // Grid 控制单元内垂直HBox / VBox通常只用 alignX
// Grid 专用(可先不做)
int gridRow = 0, gridCol = 0, rowSpan = 1, colSpan = 1;
// Flow 专用(可先不做)
int flowBreak = 0; // 1=强制换行
};
#endif
}

View File

@@ -20,7 +20,7 @@
#pragma once
#include"StellarX.h"
#define closeButtonWidth 20 //关闭按钮宽度
#define closeButtonWidth 23 //关闭按钮宽度
#define closeButtonHeight 20 //关闭按钮高度 同时作为对话框标题栏高度
#define functionButtonWidth 70 //按钮宽度
#define functionButtonHeight 30 //按钮高度
@@ -39,10 +39,6 @@ class Dialog : public Canvas
int buttonNum = 0; // 按钮数量
int BorderWidth = 2; //边框宽度
IMAGE* saveBkImage = nullptr; // 用于保存背景图像
int saveBkX = 0, saveBkY = 0; // 保存背景的位置
int saveBkWidth = 0, saveBkHeight = 0; // 保存背景的尺寸
StellarX::MessageBoxType type = StellarX::MessageBoxType::OK; //对话框类型
std::string titleText = "提示"; //标题文本
std::unique_ptr<Label> title = nullptr; //标题标签
@@ -54,7 +50,6 @@ class Dialog : public Canvas
bool needsInitialization = true; //是否需要初始化
bool close = false; //是否关闭
bool modal = true; //是否模态
bool isVisible = false; //是否可见
COLORREF backgroundColor = RGB(240, 240, 240); //背景颜色
COLORREF borderColor = RGB(100, 100, 100); //边框颜色
@@ -68,10 +63,10 @@ class Dialog : public Canvas
StellarX::MessageBoxResult result = StellarX::MessageBoxResult::Cancel; // 对话框结果
public:
bool shouldClose = false; //是否应该关闭
bool isCleaning = false; //是否正在清理
bool pendingCleanup = false; //延迟清理
public:
StellarX::ControlText textStyle; // 字体样式
// 清理方法声明
void performDelayedCleanup();
@@ -81,6 +76,8 @@ public:
void SetResultCallback(std::function<void(StellarX::MessageBoxResult)> cb);
//获取对话框消息,用以去重
std::string GetCaption() const;
//获取对话框消息,用以去重
std::string GetText() const;
public:
@@ -100,13 +97,9 @@ public:
void SetModal(bool modal);
// 设置对话框结果
void SetResult(StellarX::MessageBoxResult result);
// 获取对话框结果
StellarX::MessageBoxResult GetResult() const;
// 获取模态属性
bool getModal() const;
// 检查是否可见
bool IsVisible() const override;
//获取对话框类型
bool model() const override;
@@ -114,6 +107,9 @@ public:
void Show();
// 关闭对话框
void Close();
//初始化
void setInitialization(bool init);
private:
// 初始化按钮
@@ -128,8 +124,7 @@ private:
void getTextSize();
//初始化对话框尺寸
void initDialogSize();
// 初始化对话框
void initializeDialog();
// 清除所有控件

View File

@@ -28,13 +28,13 @@ namespace StellarX
{
public:
// 模态:阻塞直到关闭,返回结果
static MessageBoxResult ShowModal(Window& wnd,
static MessageBoxResult showModal(Window& wnd,
const std::string& text,
const std::string& caption = "提示",
MessageBoxType type = MessageBoxType::OK);
// 非模态:立即返回,通过回调异步获取结果
static void ShowAsync(Window& wnd,
static void showAsync(Window& wnd,
const std::string& text,
const std::string& caption = "提示",
MessageBoxType type = MessageBoxType::OK,

View File

@@ -22,20 +22,20 @@
* @包含顺序:
* 1. CoreTypes.h - 基础类型定义
* 2. Control.h - 控件基类
* 3. 其他具体控件头文件
* 3. ...其他具体控件头文件
* 4. Dialog继承自 CanvasDialog 为可包含子控件的对话框容器)
* 5.MessageBox对话框工厂提供便捷的模态/非模态调用方式
* 5. MessageBox对话框工厂提供便捷的模态/非模态调用方式
******************************************************************************/
#pragma once
#include "CoreTypes.h"
#include "Control.h"
#include"Button.h"
#include"Canvas.h"
#include"Window.h"
#include"Button.h"
#include"Label.h"
#include"TextBox.h"
#include"Canvas.h"
#include"Table.h"
#include"Dialog.h"
#include"MessageBox.h"

View File

@@ -30,6 +30,8 @@ class Label : public Control
//标签事件处理(标签无事件)不实现具体代码
bool handleEvent(const ExMessage& msg) override { return false; }
//用来检查对话框是否模态,此控件不做实现
bool model() const override { return false; };
public:
StellarX::ControlText textStyle; //标签文本样式
public:
@@ -37,6 +39,7 @@ public:
Label(int x, int y, std::string text = "标签",COLORREF textcolor = BLACK, COLORREF bkColor= RGB(255,255,255));
void draw() override;
void hide();
//设置标签背景是否透明
void setTextdisap(bool key);
//设置标签文本颜色
@@ -45,13 +48,6 @@ public:
void setTextBkColor(COLORREF color);
//设置标签文本
void setText(std::string text);
private:
//检查是否对话框是否可见
bool IsVisible() const override { return false; }
//获取对话框类型
bool model() const override { return false; }
};

View File

@@ -34,8 +34,6 @@ private:
std::vector<int> colWidths; // 每列的宽度
std::vector<int> lineHeights; // 每行的高度
IMAGE* saveBkImage = nullptr;
int rowsPerPage = 5; // 每页显示的行数
int currentPage = 1; // 当前页码
int totalPages = 1; // 总页数
@@ -67,11 +65,9 @@ private:
void drawHeader(); //绘制表头
void drawPageNum(); //绘制页码信息
void drawButton(); //绘制翻页按钮
//检查是否对话框是否可见
bool IsVisible() const override { return false; }
//获取对话框类型
bool model() const override { return false; }
private:
//用来检查对话框是否模态,此控件不做实现
bool model() const override { return false; };
public:
StellarX::ControlText textStyle; // 文本样式

View File

@@ -29,11 +29,6 @@ class TextBox : public Control
COLORREF textBoxBkClor = RGB(255, 255, 255); //背景颜色
COLORREF textBoxBorderClor = RGB(0,0,0); //边框颜色
//检查是否对话框是否可见
bool IsVisible() const override { return false; }
//获取对话框类型
bool model() const override { return false; }
public:
StellarX::ControlText textStyle; //文本样式
@@ -49,11 +44,16 @@ public:
//设置边框颜色
void setTextBoxBorder(COLORREF color);
//设置背景颜色
void setTextBoxBk(COLORREF color);
void setTextBoxBk(COLORREF color);
//设置文本
void setText(std::string text);
//获取文本
std::string getText() const;
private:
//用来检查对话框是否模态,此控件不做实现
bool model() const override { return false; };
};

View File

@@ -24,10 +24,17 @@ class Window
int width; //窗口宽度
int height; //窗口高度
int windowMode = NULL; //窗口模式
// --- 尺寸变化去抖用 ---
int pendingW;
int pendingH;
bool needResizeDirty = false;
HWND hWnd = NULL; //窗口句柄
std::string headline; //窗口标题
COLORREF wBkcolor = BLACK; //窗口背景
IMAGE* background = nullptr; //窗口背景图片
std::string bkImageFile; //窗口背景图片文件名
std::vector<std::unique_ptr<Control>> controls; //控件管理
std::vector<std::unique_ptr<Control>> dialogs; //对话框管理
@@ -43,9 +50,8 @@ public:
void draw();
void draw(std::string pImgFile);
//事件循环
void runEventLoop();
int runEventLoop();
//设置窗口背景图片
void setBkImage(std::string pImgFile);
//设置窗口背景颜色
void setBkcolor(COLORREF c);
@@ -56,7 +62,7 @@ public:
//添加对话框
void addDialog(std::unique_ptr<Control> dialogs);
//检查是否已有对话框显示用于去重,防止工厂模式调用非模态对话框,多次打开污染对话框背景快照
bool hasNonModalDialogWithCaption(const std::string& caption) const;
bool hasNonModalDialogWithCaption(const std::string& caption, const std::string& message) const;
//获取窗口句柄
HWND getHwnd() const;

View File

@@ -17,6 +17,116 @@ Button::Button(int x, int y, int width, int height, const std::string text, COLO
{
initButton(text, mode, shape, ct, cf, ch);
}
// ====== GBK/MBCS 安全:字符边界与省略号裁切 ======
static inline int gbk_char_len(const std::string& s, size_t i)
{
unsigned char b = (unsigned char)s[i];
if (b <= 0x7F) return 1; // ASCII
if (b >= 0x81 && b <= 0xFE && i + 1 < s.size())
{
unsigned char b2 = (unsigned char)s[i + 1];
if (b2 >= 0x40 && b2 <= 0xFE && b2 != 0x7F) return 2; // 合法双字节
}
return 1; // 容错
}
static inline void rtrim_spaces_gbk(std::string& s)
{
while (!s.empty() && s.back() == ' ') s.pop_back(); // ASCII 空格
while (s.size() >= 2)
{ // 全角空格 A1 A1
unsigned char a = (unsigned char)s[s.size() - 2];
unsigned char b = (unsigned char)s[s.size() - 1];
if (a == 0xA1 && b == 0xA1) s.resize(s.size() - 2);
else break;
}
}
static inline bool is_ascii_only(const std::string& s)
{
for (unsigned char c : s) if (c > 0x7F) return false;
return true;
}
static inline bool is_word_boundary_char(unsigned char c)
{
return c == ' ' || c == '-' || c == '_' || c == '/' || c == '\\' || c == '.' || c == ':';
}
// 英文优先策略:优先在“词边界”回退,再退化到逐字符;省略号为 "..."
static std::string ellipsize_ascii_pref(const std::string& text, int maxW)
{
if (maxW <= 0) return "";
if (textwidth(LPCTSTR(text.c_str())) <= maxW) return text;
const std::string ell = "...";
int ellW = textwidth(LPCTSTR(ell.c_str()));
if (ellW > maxW)
{ // 连 ... 都放不下
std::string e = ell;
while (!e.empty() && textwidth(LPCTSTR(e.c_str())) > maxW) e.pop_back();
return e; // 可能是 ".."、"." 或 ""
}
const int limit = maxW - ellW;
// 先找到能放下的最长前缀
size_t i = 0, lastFit = 0;
while (i < text.size())
{
int clen = gbk_char_len(text, i);
size_t j = text.size() < i + (size_t)clen ? text.size() : i + (size_t)clen;
int w = textwidth(LPCTSTR(text.substr(0, j).c_str()));
if (w <= limit) { lastFit = j; i = j; }
else break;
}
if (lastFit == 0) return ell;
// 在已适配前缀范围内,向左找最近的词边界
size_t cutPos = lastFit;
for (size_t k = lastFit; k > 0; --k)
{
unsigned char c = (unsigned char)text[k - 1];
if (c <= 0x7F && is_word_boundary_char(c)) { cutPos = k - 1; break; }
}
std::string head = text.substr(0, cutPos);
rtrim_spaces_gbk(head);
head += ell;
return head;
}
// 中文优先策略:严格逐“字符”(1/2字节)回退;省略号用全角 "…"
static std::string ellipsize_cjk_pref(const std::string& text, int maxW, const char* ellipsis = "")
{
if (maxW <= 0) return "";
if (textwidth(LPCTSTR(text.c_str())) <= maxW) return text;
std::string ell = ellipsis ? ellipsis : "";
int ellW = textwidth(LPCTSTR(ell.c_str()));
if (ellW > maxW)
{ // 连省略号都放不下
std::string e = ell;
while (!e.empty() && textwidth(LPCTSTR(e.c_str())) > maxW) e.pop_back();
return e;
}
const int limit = maxW - ellW;
size_t i = 0, lastFit = 0;
while (i < text.size())
{
int clen = gbk_char_len(text, i);
size_t j = text.size() < i + (size_t)clen ? text.size() : i + (size_t)clen;
int w = textwidth(LPCTSTR(text.substr(0, j).c_str()));
if (w <= limit) { lastFit = j; i = j; }
else break;
}
if (lastFit == 0) return ell;
std::string head = text.substr(0, lastFit);
rtrim_spaces_gbk(head);
head += ell;
return head;
}
void Button::initButton(const std::string text, StellarX::ButtonMode mode, StellarX::ControlShape shape, COLORREF ct, COLORREF cf, COLORREF ch)
{
@@ -28,6 +138,14 @@ void Button::initButton(const std::string text, StellarX::ButtonMode mode, Stell
this->buttonHoverColor = ch;
this->click = false;
this->hover = false;
// === Tooltip 默认:文本=按钮文本;白底黑字;不透明;用当前按钮字体样式 ===
tipTextClick = tipTextOn = tipTextOff = this->text;
tipLabel.setText(tipTextClick);
tipLabel.setTextColor(RGB(167, 170, 172));
tipLabel.setTextBkColor(RGB(255, 255, 255));
tipLabel.setTextdisap(false);
tipLabel.textStyle = this->textStyle; // 复用按钮字体样式
}
@@ -40,103 +158,122 @@ Button::~Button()
void Button::draw()
{
if (dirty)
if (dirty && show)
{
//保存当前样式和颜色
saveStyle();
if (StellarX::ButtonMode::DISABLED == mode) //设置禁用按钮色
{
setfillcolor(RGB(96, 96, 96));
textStyle.bStrikeOut = 1;
setfillcolor(DISABLEDCOLOUR);
textStyle.bStrikeOut = true;
}
else
{
// 点击状态优先级最高,然后是悬停状态,最后是默认状态
if (click)
setfillcolor(buttonTrueColor);
else if (hover)
setfillcolor(buttonHoverColor);
else
setfillcolor(buttonFalseColor);
COLORREF col = click ? buttonTrueColor : (hover ? buttonHoverColor : buttonFalseColor);
setfillcolor(col);
}
//
//设置字体背景色透明
setbkmode(TRANSPARENT);
//边框颜色
setlinecolor(buttonBorderColor);
if (this->textStyle != oldStyle)
{
//设置字体颜色
settextcolor(textStyle.color);
//设置字体样式
settextstyle(textStyle.nHeight, textStyle.nWidth, textStyle.lpszFace,
textStyle.nEscapement, textStyle.nOrientation, textStyle.nWeight,
textStyle.bItalic, textStyle.bUnderline, textStyle.bStrikeOut); //设置字体样式
}
//设置按钮填充模式
setfillstyle((int)buttonFillMode, (int)buttonFillIma, buttonFileIMAGE);
//设置字体颜色
settextcolor(textStyle.color);
//设置字体样式
settextstyle(textStyle.nHeight, textStyle.nWidth, textStyle.lpszFace,
textStyle.nEscapement, textStyle.nOrientation, textStyle.nWeight,
textStyle.bItalic, textStyle.bUnderline, textStyle.bStrikeOut);
if (needCutText)
cutButtonText();
//获取字符串像素高度和宽度
if ((this->oldtext_width != this->text_width || this->oldtext_height != this->text_height)
|| (-1 == oldtext_width && oldtext_height == -1))
{
this->oldtext_width = this->text_width = textwidth(LPCTSTR(this->text.c_str()));
this->oldtext_height = this->text_height = textheight(LPCTSTR(this->text.c_str()));
if(isUseCutText)
{
this->oldtext_width = this->text_width = textwidth(LPCTSTR(this->cutText.c_str()));
this->oldtext_height = this->text_height = textheight(LPCTSTR(this->cutText.c_str()));
}
else
{
this->oldtext_width = this->text_width = textwidth(LPCTSTR(this->text.c_str()));
this->oldtext_height = this->text_height = textheight(LPCTSTR(this->text.c_str()));
}
}
//设置按钮填充模式
setfillstyle((int)buttonFillMode, (int)buttonFillIma, buttonFileIMAGE);
//根据按钮形状绘制
switch (shape)
{
case StellarX::ControlShape::RECTANGLE:
fillrectangle(x, y, x + width, y + height);//有边框填充矩形
outtextxy((x + (width - text_width) / 2), (y + (height - text_height) / 2), LPCTSTR(text.c_str()));
case StellarX::ControlShape::RECTANGLE://有边框填充矩形
fillrectangle(x, y, x + width, y + height);
isUseCutText ? outtextxy((x + (width - text_width) / 2), (y + (height - text_height) / 2), LPCTSTR(cutText.c_str()))
:outtextxy((x + (width - text_width) / 2), (y + (height - text_height) / 2), LPCTSTR(text.c_str()));
break;
case StellarX::ControlShape::B_RECTANGLE:
solidrectangle(x, y, x + width, y + height);//无边框填充矩形
outtextxy((x + (width - text_width) / 2), (y + (height - text_height) / 2), LPCTSTR(text.c_str()));
case StellarX::ControlShape::B_RECTANGLE://无边框填充矩形
solidrectangle(x, y, x + width, y + height);
isUseCutText ? outtextxy((x + (width - text_width) / 2), (y + (height - text_height) / 2), LPCTSTR(cutText.c_str()))
:outtextxy((x + (width - text_width) / 2), (y + (height - text_height) / 2), LPCTSTR(text.c_str()));
break;
case StellarX::ControlShape::ROUND_RECTANGLE:
fillroundrect(x, y, x + width, y + height, rouRectangleSize.ROUND_RECTANGLEwidth, rouRectangleSize.ROUND_RECTANGLEheight);//有边框填充圆角矩形
outtextxy((x + (width - text_width) / 2), (y + (height - text_height) / 2), LPCTSTR(text.c_str()));
case StellarX::ControlShape::ROUND_RECTANGLE://有边框填充圆角矩形
fillroundrect(x, y, x + width, y + height, rouRectangleSize.ROUND_RECTANGLEwidth, rouRectangleSize.ROUND_RECTANGLEheight);
isUseCutText? outtextxy((x + (width - text_width) / 2), (y + (height - text_height) / 2), LPCTSTR(cutText.c_str()))
:outtextxy((x + (width - text_width) / 2), (y + (height - text_height) / 2), LPCTSTR(text.c_str()));
break;
case StellarX::ControlShape::B_ROUND_RECTANGLE:
solidroundrect(x, y, x + width, y + height, rouRectangleSize.ROUND_RECTANGLEwidth, rouRectangleSize.ROUND_RECTANGLEheight);//无边框填充圆角矩形
outtextxy((x + (width - text_width) / 2), (y + (height - text_height) / 2), LPCTSTR(text.c_str()));
case StellarX::ControlShape::B_ROUND_RECTANGLE://无边框填充圆角矩形
solidroundrect(x, y, x + width, y + height, rouRectangleSize.ROUND_RECTANGLEwidth, rouRectangleSize.ROUND_RECTANGLEheight);
isUseCutText? outtextxy((x + (width - text_width) / 2), (y + (height - text_height) / 2), LPCTSTR(cutText.c_str()))
:outtextxy((x + (width - text_width) / 2), (y + (height - text_height) / 2), LPCTSTR(text.c_str()));
break;
case StellarX::ControlShape::CIRCLE:
fillcircle(x + width / 2, y + height / 2, min(width, height) / 2);//有边框填充圆形
outtextxy(x + width / 2 - text_width / 2, y + height / 2 - text_height / 2, LPCTSTR(text.c_str()));
case StellarX::ControlShape::CIRCLE://有边框填充圆形
fillcircle(x + width / 2, y + height / 2, min(width, height) / 2);
isUseCutText? outtextxy(x + width / 2 - text_width / 2, y + height / 2 - text_height / 2, LPCTSTR(cutText.c_str()))
:outtextxy(x + width / 2 - text_width / 2, y + height / 2 - text_height / 2, LPCTSTR(text.c_str()));
break;
case StellarX::ControlShape::B_CIRCLE:
solidcircle(x + width / 2, y + height / 2, min(width, height) / 2);//无边框填充圆形
outtextxy(x + width / 2 - text_width / 2, y + height / 2 - text_height / 2, LPCTSTR(text.c_str()));
case StellarX::ControlShape::B_CIRCLE://无边框填充圆形
solidcircle(x + width / 2, y + height / 2, min(width, height) / 2);
isUseCutText ? outtextxy(x + width / 2 - text_width / 2, y + height / 2 - text_height / 2, LPCTSTR(cutText.c_str()))
:outtextxy(x + width / 2 - text_width / 2, y + height / 2 - text_height / 2, LPCTSTR(text.c_str()));
break;
case StellarX::ControlShape::ELLIPSE:
fillellipse(x, y, x + width, y + height);//有边框填充椭圆
outtextxy((x + (width - text_width) / 2), (y + (height - text_height) / 2), LPCTSTR(text.c_str()));
case StellarX::ControlShape::ELLIPSE://有边框填充椭圆
fillellipse(x, y, x + width, y + height);
isUseCutText ? outtextxy((x + (width - text_width) / 2), (y + (height - text_height) / 2), LPCTSTR(cutText.c_str()))
:outtextxy((x + (width - text_width) / 2), (y + (height - text_height) / 2), LPCTSTR(text.c_str()));
break;
case StellarX::ControlShape::B_ELLIPSE:
solidellipse(x, y, x + width, y + height);//无边框填充椭圆
outtextxy((x + (width - text_width) / 2), (y + (height - text_height) / 2), LPCTSTR(text.c_str()));
case StellarX::ControlShape::B_ELLIPSE://无边框填充椭圆
solidellipse(x, y, x + width, y + height);
isUseCutText ? outtextxy((x + (width - text_width) / 2), (y + (height - text_height) / 2), LPCTSTR(cutText.c_str()))
:outtextxy((x + (width - text_width) / 2), (y + (height - text_height) / 2), LPCTSTR(text.c_str()));
break;
}
restoreStyle();//恢复默认字体样式和颜色
dirty = false; //标记按钮不需要重绘
}
}
// 处理鼠标事件,检测点击和悬停状态
// 根据按钮模式和形状进行不同的处理
bool Button::handleEvent(const ExMessage& msg)
{
if (!show)
return false;
bool oldHover = hover;
bool oldClick = click;
bool consume = false;//是否消耗事件
// 记录鼠标位置用于tip定位
if (msg.message == WM_MOUSEMOVE)
{
lastMouseX = msg.x;
lastMouseY = msg.y;
}
// 检测悬停状态(根据不同形状)
switch (shape)
{
@@ -159,6 +296,7 @@ bool Button::handleEvent(const ExMessage& msg)
// 处理鼠标点击事件
if (msg.message == WM_LBUTTONDOWN && hover && mode != StellarX::ButtonMode::DISABLED)
{
if (mode == StellarX::ButtonMode::NORMAL)
{
click = true;
@@ -174,12 +312,14 @@ bool Button::handleEvent(const ExMessage& msg)
// TOGGLE 模式:在释放时切换状态,并触发相应的开/关回调。
else if (msg.message == WM_LBUTTONUP && hover && mode != StellarX::ButtonMode::DISABLED)
{
hideTooltip(); // 隐藏悬停提示
if (mode == StellarX::ButtonMode::NORMAL && click)
{
if (onClickCallback) onClickCallback();
click = false;
dirty = true;
consume = true;
hideTooltip();
// 清除消息队列中积压的鼠标和键盘消息,防止本次点击事件被重复处理
flushmessage(EX_MOUSE | EX_KEY);
}
@@ -190,6 +330,8 @@ bool Button::handleEvent(const ExMessage& msg)
else if (!click && onToggleOffCallback) onToggleOffCallback();
dirty = true;
consume = true;
refreshTooltipTextForState();
hideTooltip();
// 清除消息队列中积压的鼠标和键盘消息,防止本次点击事件被重复处理
flushmessage(EX_MOUSE | EX_KEY);
}
@@ -204,22 +346,61 @@ bool Button::handleEvent(const ExMessage& msg)
dirty = true;
}
else if (hover != oldHover)
{
dirty = true;
}
if (tipEnabled)
{
if (hover && !oldHover)
{
// 刚刚进入悬停:开计时,暂不显示
tipHoverTick = GetTickCount64();
tipVisible = false;
}
if (!hover && oldHover)
{
// 刚移出:立即隐藏
hideTooltip();
}
if (hover && !tipVisible)
{
// 到点就显示
if (GetTickCount64() - tipHoverTick >= (ULONGLONG)tipDelayMs)
{
tipVisible = true;
// 定位(跟随鼠标 or 相对按钮)
int tipX = tipFollowCursor ? (lastMouseX + tipOffsetX) : lastMouseX;
int tipY = tipFollowCursor ? (lastMouseY + tipOffsetY) : y + height;
// 设置文本(用户可能动态改了提示文本
if (tipUserOverride)
{
if (mode == StellarX::ButtonMode::NORMAL)
tipLabel.setText(tipTextClick);
else if (mode == StellarX::ButtonMode::TOGGLE)
tipLabel.setText(click ? tipTextOn : tipTextOff);
}
else
if (mode == StellarX::ButtonMode::TOGGLE)
tipLabel.setText(click ? tipTextOn : tipTextOff);
// 设置位置
tipLabel.setX(tipX);
tipLabel.setY(tipY);
// 标记需要绘制
tipLabel.setDirty(true);
}
}
}
// 如果状态发生变化,标记需要重绘
if (hover != oldHover || click != oldClick)
{
dirty = true;
}
// 如果需要重绘,立即执行
if (dirty)
{
draw();
}
if(tipEnabled && tipVisible)
tipLabel.draw();
return consume;
}
@@ -263,7 +444,7 @@ bool Button::isClicked() const
void Button::setFillMode(StellarX::FillMode mode)
{
buttonFillMode = mode;
this->buttonFillMode = mode;
this->dirty = true; // 标记需要重绘
}
@@ -304,6 +485,9 @@ void Button::setButtonText(const char* text)
this->text_width = textwidth(LPCTSTR(this->text.c_str()));
this->text_height = textheight(LPCTSTR(this->text.c_str()));
this->dirty = true;
this->needCutText = true;
if (!tipUserOverride)
tipTextClick = tipTextOn = tipTextOff = text;
}
void Button::setButtonText(std::string text)
@@ -312,12 +496,43 @@ void Button::setButtonText(std::string text)
this->text_width = textwidth(LPCTSTR(this->text.c_str()));
this->text_height = textheight(LPCTSTR(this->text.c_str()));
this->dirty = true; // 标记需要重绘
this->needCutText = true;
if (!tipUserOverride)
tipTextClick = tipTextOn = tipTextOff = text;
}
void Button::setButtonShape(StellarX::ControlShape shape)
{
this->shape = shape;
this->dirty = true;
this->needCutText = true;
}
//允许通过外部函数修改按钮的点击状态,并执行相应的回调函数
void Button::setButtonClick(BOOL click)
{
this->click = click;
if (mode == StellarX::ButtonMode::NORMAL && click)
{
if (onClickCallback) onClickCallback();
dirty = true;
hideTooltip();
// 清除消息队列中积压的鼠标和键盘消息,防止本次点击事件被重复处理
flushmessage(EX_MOUSE | EX_KEY);
}
else if (mode == StellarX::ButtonMode::TOGGLE)
{
if (click && onToggleOnCallback) onToggleOnCallback();
else if (!click && onToggleOffCallback) onToggleOffCallback();
dirty = true;
refreshTooltipTextForState();
hideTooltip();
// 清除消息队列中积压的鼠标和键盘消息,防止本次点击事件被重复处理
flushmessage(EX_MOUSE | EX_KEY);
}
if (dirty)
draw();
}
@@ -381,15 +596,6 @@ int Button::getButtonHeight() const
return this->height;
}
int Button::getButtonX() const
{
return this->x;
}
int Button::getButtonY() const
{
return this->y;
}
bool Button::isMouseInCircle(int mouseX, int mouseY, int x, int y, int radius)
@@ -418,4 +624,51 @@ bool Button::isMouseInEllipse(int mouseX, int mouseY, int x, int y, int width, i
return false;
}
void Button::cutButtonText()
{
const int contentW = 1 > this->width - 2 * padX ? 1 : this->width - 2 * padX;
// 放得下:不截断,直接用原文
if (textwidth(LPCTSTR(this->text.c_str())) <= contentW) {
isUseCutText = false;
needCutText = false;
cutText.clear();
return;
}
// 放不下按语言偏好裁切ASCII→词边界CJK→逐字符不撕裂双字节
if (is_ascii_only(this->text))
{
cutText = ellipsize_ascii_pref(this->text, contentW); // "..."
}
else
{
cutText = ellipsize_cjk_pref(this->text, contentW, ""); // 全角省略号
}
isUseCutText = true;
needCutText = false;
}
void Button::hideTooltip()
{
if (tipVisible)
{
tipVisible = false;
tipLabel.hide(); // 还原快照+作废,防止残影
tipHoverTick = GetTickCount64(); // 重置计时基线
}
}
void Button::refreshTooltipTextForState()
{
if (!tipUserOverride) return;
if (tipUserOverride) return; // 用户显式设置过 tipText保持不变
if(mode==StellarX::ButtonMode::NORMAL)
tipLabel.setText(tipTextClick);
else if(mode==StellarX::ButtonMode::TOGGLE)
tipLabel.setText(click ? tipTextOn : tipTextOff);
}

View File

@@ -1,21 +1,20 @@
#include "Canvas.h"
Canvas::Canvas()
:Control(0, 0, 100, 100) {}
Canvas::Canvas(int x, int y, int width, int height)
:Control(x, y, width, height) {}
void Canvas::clearAllControls()
{
controls.clear();
}
Canvas::Canvas()
:Control(0,0,100,100)
{
}
Canvas::Canvas(int x, int y, int width, int height)
:Control(x, y, width, height) {}
void Canvas::draw()
{
if (!dirty)return;
if (!dirty && !show)return;
saveStyle();
setlinecolor(canvasBorderClor);//设置线色
@@ -41,8 +40,10 @@ void Canvas::draw()
}
// 绘制所有子控件
for (auto& control : controls)
{
control->setDirty(true);
control->draw();
}
restoreStyle();
dirty = false; //标记画布不需要重绘
@@ -50,10 +51,12 @@ void Canvas::draw()
bool Canvas::handleEvent(const ExMessage& msg)
{
for (auto& control : controls)
if (control->handleEvent(msg))
return true;//事件被消费短路传递立即返回true 否则返回false
return false;
if(!show)return false;
for (auto it = controls.rbegin(); it != controls.rend(); ++it)
if ((*it)->handleEvent(msg))
return true; // 事件被消费短路传递立即返回true 否则返回false
return false;
}
void Canvas::addControl(std::unique_ptr<Control> control)
@@ -114,3 +117,5 @@ void Canvas::setLinewidth(int width)
dirty = true;
}

View File

@@ -64,3 +64,51 @@ void Control::restoreStyle()
setlinecolor(*currentBorderColor);
setfillstyle(BS_SOLID);//恢复填充
}
void Control::saveBackground(int x, int y, int w, int h)
{
if (w <= 0 || h <= 0) return;
saveBkX = x; saveBkY = y; saveWidth = w; saveHeight = h;
if (saveBkImage)
{
//尺寸变了才重建,避免反复 new/delete
if (saveBkImage->getwidth() != w || saveBkImage->getheight() != h)
{
delete saveBkImage; saveBkImage = nullptr;
}
}
if (!saveBkImage) saveBkImage = new IMAGE(w, h);
SetWorkingImage(nullptr); // ★抓屏幕
getimage(saveBkImage, x, y, w, h);
hasSnap = true;
}
void Control::restBackground()
{
if (!hasSnap || !saveBkImage) return;
// 直接回贴屏幕(与抓取一致)
SetWorkingImage(nullptr);
putimage(saveBkX, saveBkY, saveBkImage);
}
void Control::discardBackground()
{
if (saveBkImage)
{
delete saveBkImage;
saveBkImage = nullptr;
}
hasSnap = false; saveWidth = saveHeight = 0;
}
void Control::updateBackground()
{
if (saveBkImage)
{
delete saveBkImage;
saveBkImage = nullptr;
}
hasSnap = false; saveWidth = saveHeight = 0;
}

View File

@@ -3,7 +3,7 @@
Dialog::Dialog(Window& h,std::string text,std::string message, StellarX::MessageBoxType type, bool modal)
: Canvas(),message(message), type(type), modal(modal), hWnd(h), titleText(text)
{
initializeDialog();
show = false;
}
@@ -14,7 +14,7 @@ Dialog::~Dialog()
void Dialog::draw()
{
if(!isVisible)
if(!show)
{
// 如果对话框不可见且需要清理,执行清理
if (pendingCleanup && !isCleaning)
@@ -24,28 +24,22 @@ void Dialog::draw()
return;
}
// 如果需要初始化,则执行初始化
if (needsInitialization && isVisible)
if (needsInitialization && show)
{
initDialogSize();
needsInitialization = false;
}
if (dirty && isVisible)
if (dirty && show)
{
// 保存当前绘图状态
saveStyle();
// 保存背景(仅在第一次绘制时)
if (saveBkImage == nullptr) {
saveBkX = x - BorderWidth;
saveBkY = y - BorderWidth;
saveBkWidth = width + 2 * BorderWidth;
saveBkHeight = height + 2 * BorderWidth;
saveBkImage = new IMAGE(saveBkWidth, saveBkHeight);
getimage(saveBkImage, saveBkX, saveBkY, saveBkWidth, saveBkHeight);
}
if (saveBkImage == nullptr)
saveBackground((x - BorderWidth), (y - BorderWidth), (width + 2 * BorderWidth), (height + 2 * BorderWidth));
Canvas::setBorderColor(this->borderColor);
Canvas::setLinewidth(this->BorderWidth);
Canvas::setCanvasBkColor(this->backgroundColor);
@@ -82,10 +76,11 @@ void Dialog::draw()
}
bool Dialog::handleEvent(const ExMessage& msg)
{
bool consume = false;
if (!isVisible)
if (!show)
{
if (pendingCleanup && !isCleaning)
{
@@ -158,16 +153,6 @@ StellarX::MessageBoxResult Dialog::GetResult() const
return this->result;
}
bool Dialog::getModal() const
{
return modal;
}
bool Dialog::IsVisible() const
{
return isVisible;
}
bool Dialog::model() const
{
return modal;
@@ -178,16 +163,16 @@ void Dialog::Show()
if (pendingCleanup)
performDelayedCleanup();
isVisible = true;
show = true;
dirty = true;
needsInitialization = true;
close = false;
shouldClose = false;
shouldClose = false;
if (modal)
{
// 模态对话框需要阻塞当前线程直到对话框关闭
while (isVisible && !close)
while (show && !close)
{
// 处理消息
ExMessage msg;
@@ -216,25 +201,20 @@ void Dialog::Show()
// 模态对话框关闭后执行清理
if (pendingCleanup && !isCleaning)
{
performDelayedCleanup();
}
}
else
{
// 非模态对话框只需标记为可见,由主循环处理
dirty = true;
}
}
void Dialog::Close()
{
if (!isVisible) return;
if (!show) return;
isVisible = false;
show = false;
close = true;
dirty = true;
pendingCleanup = true; // 只标记需要清理,不立即执行
@@ -249,6 +229,15 @@ void Dialog::Close()
}
void Dialog::setInitialization(bool init)
{
if (init)
{
initDialogSize();
saveBackground((x - BorderWidth), (y - BorderWidth), (width + 2 * BorderWidth), (height + 2 * BorderWidth));
}
}
void Dialog::initButtons()
{
@@ -286,8 +275,8 @@ void Dialog::initButtons()
this->Close(); });
auto cancelButton = createDialogButton(
(okButton.get()->getButtonX() + okButton.get()->getButtonWidth() + buttonMargin),
okButton.get()->getButtonY(),
(okButton.get()->getX() + okButton.get()->getButtonWidth() + buttonMargin),
okButton.get()->getY(),
"取消"
);
cancelButton->setOnClickListener([this]()
@@ -317,8 +306,8 @@ void Dialog::initButtons()
this->Close(); });
auto noButton = createDialogButton(
(yesButton.get()->getButtonX() + yesButton.get()->getButtonWidth() + buttonMargin),
yesButton.get()->getButtonY(),
(yesButton.get()->getX() + yesButton.get()->getButtonWidth() + buttonMargin),
yesButton.get()->getY(),
""
);
noButton->setOnClickListener([this]()
@@ -348,8 +337,8 @@ void Dialog::initButtons()
this->Close(); });
auto noButton = createDialogButton(
yesButton.get()->getButtonX() + yesButton.get()->getButtonWidth() + buttonMargin,
yesButton.get()->getButtonY(),
yesButton.get()->getX() + yesButton.get()->getButtonWidth() + buttonMargin,
yesButton.get()->getY(),
""
);
noButton->setOnClickListener([this]()
@@ -359,8 +348,8 @@ void Dialog::initButtons()
this->Close(); });
auto cancelButton = createDialogButton(
noButton.get()->getButtonX() + noButton.get()->getButtonWidth() + buttonMargin,
noButton.get()->getButtonY(),
noButton.get()->getX() + noButton.get()->getButtonWidth() + buttonMargin,
noButton.get()->getY(),
"取消"
);
cancelButton->setOnClickListener([this]()
@@ -393,8 +382,8 @@ void Dialog::initButtons()
this->Close(); });
auto cancelButton = createDialogButton(
retryButton.get()->getButtonX() + retryButton.get()->getButtonWidth() + buttonMargin,
retryButton.get()->getButtonY(),
retryButton.get()->getX() + retryButton.get()->getButtonWidth() + buttonMargin,
retryButton.get()->getY(),
"取消"
);
cancelButton->setOnClickListener([this]()
@@ -424,8 +413,8 @@ void Dialog::initButtons()
this->Close();
});
auto retryButton = createDialogButton(
abortButton.get()->getButtonX() + abortButton.get()->getButtonWidth() + buttonMargin,
abortButton.get()->getButtonY(),
abortButton.get()->getX() + abortButton.get()->getButtonWidth() + buttonMargin,
abortButton.get()->getY(),
"重试"
);
retryButton->setOnClickListener([this]()
@@ -435,8 +424,8 @@ void Dialog::initButtons()
this->Close();
});
auto ignoreButton = createDialogButton(
retryButton.get()->getButtonX() + retryButton.get()->getButtonWidth() + buttonMargin,
retryButton.get()->getButtonY(),
retryButton.get()->getX() + retryButton.get()->getButtonWidth() + buttonMargin,
retryButton.get()->getY(),
"忽略"
);
ignoreButton.get()->setOnClickListener([this]()
@@ -464,7 +453,7 @@ void Dialog::initCloseButton()
auto but = std::make_unique<Button>
(
(this->x + this->width - closeButtonWidth) - 3, (this->y+3), closeButtonWidth-1, closeButtonHeight,
"×", // 按钮文本
"X", // 按钮文本
RGB(255, 0, 0), // 按钮被点击颜色
this->canvasBkClor, // 按钮背景颜色
RGB(255, 0, 0), // 按钮被悬停颜色
@@ -513,12 +502,14 @@ void Dialog::splitMessageLines()
}
// 添加最后一行(如果有内容)
if (!currentLine.empty()) {
if (!currentLine.empty())
{
lines.push_back(currentLine);
}
// 如果消息为空,至少添加一个空行
if (lines.empty()) {
if (lines.empty())
{
lines.push_back("");
}
}
@@ -595,15 +586,6 @@ void Dialog::initDialogSize()
initCloseButton(); // 初始化关闭按钮
}
void Dialog::initializeDialog()
{
needsInitialization = true;
pendingCleanup = false;
isCleaning = false;
close = false;
isVisible = false;
}
// 延迟清理策略:由于对话框绘制时保存了背景快照,必须在对话框隐藏后、
// 所有控件析构前恢复背景,否则会导致背景图像被错误覆盖。
@@ -622,12 +604,10 @@ void Dialog::performDelayedCleanup()
title.reset();
// 释放背景图像资源
if (saveBkImage)
if (saveBkImage && hasSnap)
{
// 恢复背景
putimage(saveBkX, saveBkY, saveBkImage);
delete saveBkImage;
saveBkImage = nullptr;
restBackground();
discardBackground();
}
// 重置状态
@@ -647,6 +627,11 @@ std::string Dialog::GetCaption() const
return titleText;
}
std::string Dialog::GetText() const
{
return message;
}
void Dialog::clearControls()
{
controls.clear();

View File

@@ -2,26 +2,28 @@
namespace StellarX
{
MessageBoxResult MessageBox::ShowModal(Window& wnd,const std::string& text,const std::string& caption,
MessageBoxResult MessageBox::showModal(Window& wnd,const std::string& text,const std::string& caption,
MessageBoxType type)
{
Dialog dlg(wnd, caption, text, type, true); // 模态
dlg.setInitialization(true);
dlg.Show();
return dlg.GetResult();
}
void MessageBox::ShowAsync(Window& wnd,const std::string& text,const std::string& caption,MessageBoxType type,
void MessageBox::showAsync(Window& wnd,const std::string& text,const std::string& caption,MessageBoxType type,
std::function<void(MessageBoxResult)> onResult)
{
//去重,如果窗口内已有相同的对话框被触发,则不再创建
if (wnd.hasNonModalDialogWithCaption(caption))
if (wnd.hasNonModalDialogWithCaption(caption, text))
{
std::cout << "\a" << std::endl;
return;
}
auto dlg = std::make_unique<Dialog>(wnd, caption, text, type, false); // 非模态
auto dlg = std::make_unique<Dialog>(wnd, caption, text,
type, false); // 非模态
Dialog* dlgPtr = dlg.get();
dlgPtr->setInitialization(true);
// 设置回调
if (onResult)
dlgPtr->SetResultCallback(std::move(onResult));

View File

@@ -18,7 +18,7 @@ Label::Label(int x, int y, std::string text, COLORREF textcolor, COLORREF bkColo
void Label::draw()
{
if(dirty)
if (dirty && show)
{
saveStyle();
if (textBkDisap)
@@ -32,12 +32,20 @@ void Label::draw()
settextstyle(textStyle.nHeight, textStyle.nWidth, textStyle.lpszFace,
textStyle.nEscapement, textStyle.nOrientation, textStyle.nWeight,
textStyle.bItalic, textStyle.bUnderline, textStyle.bStrikeOut); //设置字体样式
this->saveBackground(x, y,textwidth(text.c_str()),textheight(text.c_str()));
this-> restBackground();
outtextxy(x, y, LPCTSTR(text.c_str()));
restoreStyle();
this->restoreStyle();
dirty = false;
}
}
//用于“隐藏提示框”时调用(还原并释放快照)
void Label::hide()
{
restBackground(); // 还原屏幕像素
discardBackground(); // 作废快照,防止错贴旧图
dirty = false;
}
void Label::setTextdisap(bool key)
{
textBkDisap = key;

View File

@@ -3,151 +3,222 @@
// 使用双循环绘制行和列,考虑分页偏移
void Table::drawTable()
{
dX = x;
dY = uY;
const int border = tableBorderWidth > 0 ? tableBorderWidth : 0;
// 表体从“表头之下”开始
dX = x + border;
dY = y + border + lineHeights.at(0) + 10; // 表头高度
uY = dY + lineHeights.at(0) + 10;
for (size_t i = (currentPage * rowsPerPage - rowsPerPage); i < (currentPage*rowsPerPage) && i < data.size(); i++)
size_t startRow = (currentPage - 1) * rowsPerPage;
size_t endRow = startRow + (size_t)rowsPerPage < data.size() ? startRow + (size_t)rowsPerPage : data.size();
for (size_t i = startRow; i < endRow; ++i)
{
for (size_t j = 0; j < data[i].size(); j++)
for (size_t j = 0; j < data[i].size(); ++j)
{
uX = dX + colWidths.at(j) + 20;
uX = dX + colWidths.at(j) + 20; // 列宽 + 20
fillrectangle(dX, dY, uX, uY);
outtextxy(dX + 10, dY + 5, LPCTSTR(data[i][j].c_str()));
dX += this->colWidths.at(j) + 20;
dX += colWidths.at(j) + 20;
}
dX = x;
dX = x + border;
dY = uY;
uY = dY + lineHeights.at(0) + 10;
}
uY = y + lineHeights.at(0) + 10;
}
void Table::drawHeader()
{
const int border = tableBorderWidth > 0 ? tableBorderWidth : 0;
// 内容区原点 = x+border, y+border
dX = x + border;
dY = y + border;
uY = dY + lineHeights.at(0) + 10;
for(size_t i = 0; i < headers.size(); i++)
for (size_t i = 0; i < headers.size(); i++)
{
uX = dX + colWidths.at(i) + 20;
uX = dX + colWidths.at(i) + 20; // 注意这里是 +20和表体一致
fillrectangle(dX, dY, uX, uY);
outtextxy(dX + 10, dY + 5, LPCTSTR(headers[i].c_str()));
dX += this->colWidths.at(i) + 20;
dX += colWidths.at(i) + 20; // 列间距 20
}
}
// 遍历所有数据单元和表头,计算每列的最大宽度和每行的最大高度,
// 为后续绘制表格单元格提供尺寸依据。此计算在数据变更时自动触发。
void Table::initTextWaH()
{
this->colWidths.resize(this->headers.size());
this->lineHeights.resize(this->headers.size());
int width = 0;
int height = 0;
//计算数据尺寸
for (size_t i = 0; i < data.size(); i++)
// 和绘制一致的单元内边距
const int padX = 10; // 左右 padding
const int padY = 5; // 上下 padding
const int colGap = 20; // 列间距
const int border = tableBorderWidth > 0 ? tableBorderWidth : 0;
// 统计每列最大文本宽 & 每列最大行高(包含数据 + 表头)
colWidths.assign(headers.size(), 0);
lineHeights.assign(headers.size(), 0);
// 先看数据
for (size_t i = 0; i < data.size(); ++i)
{
for (size_t j = 0; j < data[i].size(); j++)
for (size_t j = 0; j < data[i].size(); ++j)
{
width = textwidth(LPCTSTR(data[i][j].c_str()));
height = textheight(LPCTSTR(data[i][j].c_str()));
if (width > this->colWidths.at(j))
this->colWidths.at(j) = width;
if (height > this->lineHeights[j])
this->lineHeights.at(j) = height;
const int w = textwidth(LPCTSTR(data[i][j].c_str()));
const int h = textheight(LPCTSTR(data[i][j].c_str()));
if (w > colWidths[j]) colWidths[j] = w;
if (h > lineHeights[j]) lineHeights[j] = h;
}
}
for (size_t i = 0; i < this->headers.size(); i++)
// 再用表头更新(谁大取谁)
for (size_t j = 0; j < headers.size(); ++j)
{
width = textwidth(LPCTSTR(headers[i].c_str()));
height = textheight(LPCTSTR(headers[i].c_str()));
if (width > this->colWidths.at(i))
this->colWidths.at(i) = width;
if (height > this->lineHeights[i])
this->lineHeights.at(i) = height;
const int w = textwidth(LPCTSTR(headers[j].c_str()));
const int h = textheight(LPCTSTR(headers[j].c_str()));
if (w > colWidths[j]) colWidths[j] = w;
if (h > lineHeights[j]) lineHeights[j] = h;
}
// 计算表格总宽度和高度
this->width = 0;
for (size_t i = 0; i < colWidths.size(); i++)
this->width += colWidths.at(i) + 20;
LINESTYLE currentStyle;
// 用“所有列的最大行高”作为一行的基准高度
int maxLineH = 0;
this->width += tableBorderWidth;
for (int h : lineHeights)
if (h > maxLineH)
maxLineH = h;
this->height = lineHeights.at(0) * (rowsPerPage + 1) + rowsPerPage * 10+20 ; // 表头+数据行+页码区域
// 列的像素宽 = 内容宽 + 左右 padding
// 表内容总宽 = Σ(列宽 + 列间距)
int contentW = 0;
for (size_t j = 0; j < colWidths.size(); ++j)
contentW += (colWidths[j] + 2 * padX) + colGap;
// 表头高 & 行高(与 drawHeader/drawTable 内部一致:+上下 padding
const int headerH = maxLineH + 2 * padY;
const int rowH = maxLineH + 2 * padY;
const int rowsH = rowH * rowsPerPage;
// 由于表格绘制会覆盖原有区域,需要先保存背景以便在下次绘制前恢复,
// 避免重叠绘制造成的残影问题。
// 如果背景图像不存在或尺寸不匹配,创建或重新创建
if (saveBkImage == nullptr) {
saveBkImage = new IMAGE(width, height);
}
else if (saveBkImage->getwidth() != width || saveBkImage->getheight() != height) {
delete saveBkImage;
saveBkImage = new IMAGE(width, height);
}
// 页脚:
const int pageTextH = textheight(LPCTSTR(pageNumtext.c_str()));
const int btnTextH = textheight(LPCTSTR("上一页"));
const int btnPadV = 8;
const int btnH = btnTextH + 2 * btnPadV;
const int footerPad = 16;
const int footerH = (pageTextH > btnH ? pageTextH : btnH) + footerPad;
// 最终表宽/高:内容 + 对称边框
this->width = contentW + (border << 1);
this->height = headerH + rowsH + footerH + (border << 1);
}
void Table::initButton()
{
int x1, x2;
int y1, y2;
x1 = pX - 72;
x2 = pX + textwidth(LPCTSTR(pageNumtext.c_str())) + 10;
y1 = y2 = pY;
this->prevButton = new Button(x1, y1, 62, textheight(LPCTSTR(pageNumtext.c_str())), "上一页", RGB(0, 0, 0), RGB(255, 255, 255));
this->nextButton = new Button(x2, y2, 62, textheight(LPCTSTR(pageNumtext.c_str())), "下一页", RGB(0, 0, 0), RGB(255, 255, 255));
this->prevButton->textStyle = this->textStyle;
this->nextButton->textStyle = this->textStyle;
this->prevButton->setFillMode(tableFillMode);
this->nextButton->setFillMode(tableFillMode);
prevButton->setOnClickListener([this]()
{if (this->currentPage > 1)
{
this->currentPage--;
this->dirty = true;
this->draw();
} });
const int gap = 12; // 页码与按钮之间的固定间距
const int padH = 12; // 按钮水平内边距
const int padV = 0; // 按钮垂直内边距
int pageW = textwidth(LPCTSTR(pageNumtext.c_str()));
int lblH = textheight(LPCTSTR(pageNumtext.c_str()));
// 统一按钮尺寸(用按钮文字自身宽高 + padding
int prevW = textwidth(LPCTSTR("上一页")) + padH * 2;
int nextW = textwidth(LPCTSTR("下一页")) + padH * 2;
int btnH = lblH + padV * 2;
nextButton->setOnClickListener([this]()
{if (this->currentPage < (this->totalPages))
// 基于“页码标签”的矩形来摆放:
// prev 在页码左侧 gap 处next 在右侧 gap 处Y 对齐 pY
int prevX = pX - gap - prevW;
int nextX = pX + pageW + gap;
int btnY = pY; // 和页码同一基线
if (!prevButton)
prevButton = new Button(prevX, btnY, prevW, btnH, "上一页", RGB(0, 0, 0), RGB(255, 255, 255));
else
{
this->currentPage++;
this->dirty = true;
this->draw();
}});
prevButton->setX(prevX);
prevButton->setY(btnY);
}
if (!nextButton)
nextButton = new Button(nextX, btnY, nextW, btnH, "下一页", RGB(0, 0, 0), RGB(255, 255, 255));
else
{
nextButton->setX(nextX);
nextButton->setY(btnY);
}
prevButton->textStyle = this->textStyle;
nextButton->textStyle = this->textStyle;
prevButton->setFillMode(tableFillMode);
nextButton->setFillMode(tableFillMode);
prevButton->setOnClickListener([this]()
{
if (currentPage > 1)
{
--currentPage;
dirty = true;
if (pageNum) pageNum->setDirty(true);
}
});
nextButton->setOnClickListener([this]()
{
if (currentPage < totalPages)
{
++currentPage;
dirty = true;
if (pageNum) pageNum->setDirty(true);
}
});
}
void Table::initPageNum()
{
if (0 == pY)
pY = uY + lineHeights.at(0) * rowsPerPage + rowsPerPage * 10+20;
for (size_t i = 0; i < colWidths.size(); i++)
this->pX += colWidths.at(i) + 20;
this->pX -= textwidth(LPCTSTR(pageNumtext.c_str()));
this->pX /= 2;
this->pX += x;
this->pageNum = new Label(this->pX, pY, pageNumtext);
// 统一坐标系
const int border = tableBorderWidth > 0 ? tableBorderWidth : 0;
const int baseH = lineHeights.empty() ? 0 : lineHeights.at(0);
const int headerH = baseH + 10;
const int rowsH = baseH * rowsPerPage + rowsPerPage * 10;
// 内容宽度 = sum(colWidths + 20)initTextWaH() 已把 this->width += 2*border
// 因此 contentW = this->width - 2*border 更稳妥
const int contentW = this->width - (border << 1);
// 页脚顶部位置(表头 + 可视数据区 之后)
pY = y + border + headerH + rowsH + 8; // +8 顶部留白
// 按理来说 x + (this->width - textW) / 2;就可以
// 但是在绘制时发现控件偏右因此减去40
int textW = textwidth(LPCTSTR(pageNumtext.c_str()));
pX = x - 40 +(this->width - textW) / 2;
if (!pageNum)
pageNum = new Label(pX, pY, pageNumtext);
else
{
pageNum->setX(pX);
pageNum->setY(pY);
}
pageNum->textStyle = this->textStyle;
if (StellarX::FillMode::Null == tableFillMode)
pageNum->setTextdisap(true);
pageNum->setTextdisap(true); // 透明文本
}
void Table::drawPageNum()
{
if (nullptr == pageNum)
initPageNum();
pageNumtext = "";
pageNumtext+= std::to_string(currentPage);
pageNumtext += "页/共";
pageNumtext += std::to_string(totalPages);
pageNumtext += "";
if (nullptr == pageNum)
initPageNum();
pageNum->setText(pageNumtext);
if (StellarX::FillMode::Null == tableFillMode)
pageNum->setTextdisap(true);
pageNum->draw();
}
@@ -156,6 +227,15 @@ void Table::drawButton()
{
if (nullptr == prevButton || nullptr == nextButton)
initButton();
this->prevButton->textStyle = this->textStyle;
this->nextButton->textStyle = this->textStyle;
this->prevButton->setFillMode(tableFillMode);
this->nextButton->setFillMode(tableFillMode);
this->prevButton->setButtonShape(StellarX::ControlShape::B_RECTANGLE);
this->nextButton->setButtonShape(StellarX::ControlShape::B_RECTANGLE);
this->prevButton->setDirty(true);
this->nextButton->setDirty(true);
prevButton->draw();
nextButton->draw();
@@ -184,7 +264,7 @@ Table::~Table()
void Table::draw()
{
if (this->dirty)
if (this->dirty && this->show)
{
// 先保存当前绘图状态
saveStyle();
@@ -206,47 +286,26 @@ void Table::draw()
initTextWaH();
isNeedCellSize = false;
}
// 优化性能:仅在首次绘制或表格尺寸变化时捕获背景图像。
// 由于表格绘制会覆盖原有区域,需要先保存背景以便在下次绘制前恢复,
// 避免重叠绘制造成的残影问题。
static bool firstDraw = true;
if (firstDraw || isNeedDrawHeaders)
if (isNeedDrawHeaders)
{
// 确保在绘制任何表格内容之前捕获背景
if (saveBkImage)
{
// 临时恢复样式,确保捕获正确的背景
restoreStyle();
if(tableBorderWidth>1)
getimage(saveBkImage, this->x- tableBorderWidth, this->y- tableBorderWidth, this->width+ tableBorderWidth, this->height+ tableBorderWidth);
else
getimage(saveBkImage, this->x, this->y, this->width, this->height);
saveStyle(); // 重新保存样式
// 重新设置表格样式
setfillcolor(tableBkClor);
setlinecolor(tableBorderClor);
settextstyle(textStyle.nHeight, textStyle.nWidth, textStyle.lpszFace,
textStyle.nEscapement, textStyle.nOrientation, textStyle.nWeight,
textStyle.bItalic, textStyle.bUnderline, textStyle.bStrikeOut);
settextcolor(textStyle.color);
setlinestyle((int)tableLineStyle, tableBorderWidth);
setfillstyle((int)tableFillMode);
setbkmode(TRANSPARENT);
}
firstDraw = false;
// 重新设置表格样式
setfillcolor(tableBkClor);
setlinecolor(tableBorderClor);
settextstyle(textStyle.nHeight, textStyle.nWidth, textStyle.lpszFace,
textStyle.nEscapement, textStyle.nOrientation, textStyle.nWeight,
textStyle.bItalic, textStyle.bUnderline, textStyle.bStrikeOut);
settextcolor(textStyle.color);
setlinestyle((int)tableLineStyle, tableBorderWidth);
setfillstyle((int)tableFillMode);
setbkmode(TRANSPARENT);
}
//确保在绘制任何表格内容之前捕获背景
// 临时恢复样式,确保捕获正确的背景
if (!saveBkImage)
saveBackground(this->x, this->y, this->width, this->height);
// 恢复背景(清除旧内容)
if (saveBkImage)
{
if (tableBorderWidth > 1)
putimage(this->x - tableBorderWidth, this->y - tableBorderWidth, saveBkImage);
else
putimage(this->x,this->y,this->saveBkImage);
}
restBackground();
// 绘制表头
dX = x;
@@ -273,6 +332,7 @@ void Table::draw()
bool Table::handleEvent(const ExMessage& msg)
{
if(!show)return false;
bool consume = false;
if(!this->isShowPageButton)
return consume;
@@ -282,6 +342,8 @@ bool Table::handleEvent(const ExMessage& msg)
if (!consume)
consume = nextButton->handleEvent(msg);
}
if (dirty)
draw();
return consume;
}
@@ -361,17 +423,17 @@ void Table::setTableFillMode(StellarX::FillMode mode)
this->tableFillMode = mode;
else
this->tableFillMode = StellarX::FillMode::Solid;
this->prevButton->textStyle = this->textStyle;
this->nextButton->textStyle = this->textStyle;
this->prevButton->setFillMode(tableFillMode);
this->nextButton->setFillMode(tableFillMode);
if (StellarX::FillMode::Null == tableFillMode)
pageNum->setTextdisap(true);
this->prevButton->setDirty(true);
this->nextButton->setDirty(true);
this->prevButton->setDirty(true);
this->nextButton->setDirty(true);
if (this->prevButton && this->nextButton && this->pageNum)
{
this->prevButton->textStyle = this->textStyle;
this->nextButton->textStyle = this->textStyle;
this->prevButton->setFillMode(tableFillMode);
this->nextButton->setFillMode(tableFillMode);
if (StellarX::FillMode::Null == tableFillMode)
pageNum->setTextdisap(true);
this->prevButton->setDirty(true);
this->nextButton->setDirty(true);
}
this->dirty = true;
}

View File

@@ -9,7 +9,7 @@ TextBox::TextBox(int x, int y, int width, int height, std::string text, StellarX
void TextBox::draw()
{
if(dirty)
if (dirty && show)
{
saveStyle();
setfillcolor(textBoxBkClor);
@@ -140,6 +140,15 @@ void TextBox::setTextBoxBk(COLORREF color)
this->dirty = true;
}
void TextBox::setText(std::string text)
{
if(text.size() > maxCharLen)
text = text.substr(0, maxCharLen);
this->text = text;
this->dirty = true;
draw();
}
std::string TextBox::getText() const
{
return this->text;

View File

@@ -1,111 +1,147 @@
#include "Window.h"
#include"Dialog.h"
#include "Dialog.h"
#include <windows.h> // 确保包含 Windows API 头文件
Window::Window(int width, int height, int mode)
{
this->width = width;
this->height = height;
this->windowMode = mode;
this->pendingW = this->width = width;
this->pendingH = this->height = height;
this->windowMode = mode;
}
Window::Window(int width, int height, int mode, COLORREF bkcloc)
{
this->width = width;
this->height = height;
this->windowMode = mode;
this->wBkcolor = bkcloc;
this->pendingW = this->width = width;
this->pendingH = this->height = height;
this->windowMode = mode;
this->wBkcolor = bkcloc;
}
Window::Window(int width, int height, int mode, COLORREF bkcloc, std::string headline)
{
this->width = width;
this->height = height;
this->windowMode = mode;
this->wBkcolor = bkcloc;
this->pendingW = this->width = width;
this->pendingH = this->height = height;
this->windowMode = mode;
this->wBkcolor = bkcloc;
this->headline = headline;
}
Window::~Window()
{
if (background)
delete background;
background = nullptr;
closegraph(); // 确保关闭图形上下文
if (background)
delete background;
background = nullptr;
closegraph(); // 确保关闭图形上下文
}
void Window::draw()
void Window::draw() {
// 使用 EasyX 创建基本窗口
hWnd = initgraph(width, height, windowMode);
SetWindowText(hWnd, headline.c_str());
// **启用窗口拉伸支持**:添加厚边框和最大化按钮样式
LONG style = GetWindowLong(hWnd, GWL_STYLE);
style |= WS_THICKFRAME | WS_MAXIMIZEBOX | WS_MINIMIZEBOX; // 可调整边框,启用最大化/最小化按钮
SetWindowLong(hWnd, GWL_STYLE, style);
// 通知窗口样式变化生效
SetWindowPos(hWnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
// 设置背景色并清屏
setbkcolor(wBkcolor);
cleardevice();
// 初次绘制所有控件(双缓冲)
BeginBatchDraw();
for (auto& control : controls)
{
control->draw();
}
// (如果有初始对话框,也可绘制 dialogs
EndBatchDraw();
}
void Window::draw(std::string imagePath)
{
hWnd = initgraph(width, height, windowMode);
SetWindowText(hWnd,headline.c_str());
setbkcolor(wBkcolor);
cleardevice();
BeginBatchDraw(); // 开始批量绘制
// 绘制所有子控件
for (auto& control : controls)
control->draw();
EndBatchDraw(); // 结束批量绘制
}
// 使用背景图片绘制窗口
// @参数 pImgFile: 图片文件路径,支持常见图片格式
// @备注: 会拉伸图片以适应窗口尺寸
void Window::draw(std::string pImgFile)
{
this->background = new IMAGE(width, height);
hWnd = initgraph(width, height, windowMode);
SetWindowText(hWnd, headline.c_str());
loadimage(background, pImgFile.c_str(), width, height, true);
putimage(0,0, background);
// 绘制所有子控件
BeginBatchDraw(); // 开始批量绘制
for (auto& control : controls)
control->draw();
for(auto& d: dialogs)
d->draw();
EndBatchDraw(); // 结束批量绘制
// 使用指定图片绘制窗口背景(铺满窗口)
this->background = new IMAGE(width, height);
bkImageFile = imagePath;
hWnd = initgraph(width, height, windowMode);
SetWindowText(hWnd, headline.c_str());
loadimage(background, imagePath.c_str(), width, height, true);
putimage(0, 0, background);
// 同样应用可拉伸样式
LONG style = GetWindowLong(hWnd, GWL_STYLE);
style |= WS_THICKFRAME | WS_MAXIMIZEBOX | WS_MINIMIZEBOX;
SetWindowLong(hWnd, GWL_STYLE, style);
SetWindowPos(hWnd, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
// 绘制控件(含对话框)到窗口
BeginBatchDraw();
for (auto& control : controls) control->draw();
for (auto& dlg : dialogs) dlg->draw();
EndBatchDraw();
}
// 运行主事件循环,处理用户输入和窗口消息
// 此方法会阻塞直到窗口关闭
// 主消息循环优先级:对话框 > 普通控件。
// 重绘策略:为保证视觉一致性,每次有对话框状态变化(打开/关闭)时,
// 会强制重绘所有控件。先绘制普通控件,再绘制对话框(确保对话框在最上层)。
void Window::runEventLoop()
int Window::runEventLoop()
{
ExMessage msg;
bool running = true;
while (running)
while (running)
{
bool consume = false;// 是否处理了消息
// 处理所有消息
if (peekmessage(&msg, EX_MOUSE | EX_KEY | EX_WINDOW, true))
{
if (msg.message == WM_CLOSE)
{
running = false;
break;
return 0;
}
// 优先处理对话框事件
for (auto& d : dialogs)
if (msg.message == WM_SIZE)
{
if (msg.wParam != SIZE_MINIMIZED)
{
const int nw = LOWORD(msg.lParam);
const int nh = HIWORD(msg.lParam);
// 仅在尺寸真的变化时标脏
if (nw > 0 && nh > 0 || (nw != width || nh != height))
{
pendingW = nw;
pendingH = nh;
needResizeDirty = true;
}
}
continue;//在末尾重绘制窗口
}
// 优先处理对话框事件
for (auto it = dialogs.rbegin(); it != dialogs.rend(); ++it)
{
auto& d = *it;
if (d->IsVisible() && !d->model())
consume = d->handleEvent(msg);
if (consume)
break;
}
if(!consume)
for (auto& c : controls)
//普通控件
if (!consume)
for (auto it = controls.rbegin(); it != controls.rend(); ++it)
{
consume = c->handleEvent(msg);
consume = (*it)->handleEvent(msg);
if (consume)
break;
}
}
//如果有对话框打开或者关闭强制重绘
//如果有对话框打开或者关闭强制重绘
bool needredraw = false;
for (auto& d : dialogs)
{
@@ -115,7 +151,7 @@ void Window::runEventLoop()
if (needredraw || dialogClose)
{
// 对话框关闭后,需要手动合成一个鼠标移动消息并分发给所有普通控件,
// 以便它们能及时更新悬停状态hover否则悬停状态可能保持错误状态。
// 以便它们能及时更新悬停状态hover否则悬停状态可能保持错误状态。
// 先把当前鼠标位置转换为客户区坐标,并合成一次 WM_MOUSEMOVE先分发给控件更新 hover 状态
POINT pt;
if (GetCursorPos(&pt))
@@ -123,8 +159,8 @@ void Window::runEventLoop()
ScreenToClient(this->hWnd, &pt);
ExMessage mm;
mm.message = WM_MOUSEMOVE;
mm.x =(short) pt.x;
mm.y =(short) pt.y;
mm.x = (short)pt.x;
mm.y = (short)pt.y;
// 只分发给 window 层控件(因为 dialog 已经关闭或即将关闭)
for (auto& c : controls)
c->handleEvent(mm);
@@ -145,10 +181,43 @@ void Window::runEventLoop()
needredraw = false;
}
//—— 统一“收口”:尺寸变化后的** 一次性** 重绘 ——
if (needResizeDirty)
{
//确保窗口不会小于初始尺寸
if (pendingW >= width && pendingH >= height)
Resize(nullptr, pendingW, pendingH);
else
Resize(nullptr, width, height);
if (background)
{
delete background;
background = new IMAGE;
loadimage(background, bkImageFile.c_str(), pendingW, pendingH);
putimage(0, 0, background);
}
// 标记所有控件/对话框为脏,确保都补一次背景/外观
for (auto& c : controls)
{
c->setDirty(true);
c->updateBackground();
c->draw();
}
for (auto& d : dialogs)
{
auto dd = dynamic_cast<Dialog*>(d.get());
dd->setDirty(true);
dd->setInitialization(true);
dd->draw();
}
needResizeDirty = false;
}
// 避免CPU占用过高
// 降低占用
Sleep(10);
}
return 1;
}
void Window::setBkImage(std::string pImgFile)
@@ -158,13 +227,44 @@ void Window::setBkImage(std::string pImgFile)
else
delete background;
this->background = new IMAGE;
this->bkImageFile = pImgFile;
loadimage(background, pImgFile.c_str(), width, height, true);
putimage(0, 0, background);
BeginBatchDraw();
for (auto& c : controls)
{
c->setDirty(true);
c->draw();
}
for (auto& c : dialogs)
{
c->setDirty(true);
c->draw();
}
EndBatchDraw();
}
void Window::setBkcolor(COLORREF c)
{
wBkcolor = c;
setbkcolor(wBkcolor);
cleardevice();
// 初次绘制所有控件(双缓冲)
BeginBatchDraw();
for (auto& c : controls)
{
c->setDirty(true);
c->draw();
}
for (auto& c : dialogs)
{
c->setDirty(true);
c->draw();
}
EndBatchDraw();
}
void Window::setHeadline(std::string headline)
@@ -183,13 +283,15 @@ void Window::addDialog(std::unique_ptr<Control> dialogs)
this->dialogs.push_back(std::move(dialogs));
}
bool Window::hasNonModalDialogWithCaption(const std::string& caption) const {
for (const auto& dptr : dialogs) {
bool Window::hasNonModalDialogWithCaption(const std::string& caption, const std::string& message) const
{
for (const auto& dptr : dialogs)
{
if (!dptr) continue;
// 只检查 Dialog 类型的控件
Dialog* d = dynamic_cast<Dialog*>(dptr.get());
//检查是否有非模态对话框可见,并且消息内容一致
if (d && d->IsVisible() && !d->model() && d->GetCaption() == caption)
if (d && d->IsVisible() && !d->model() && d->GetCaption() == caption && d->GetText() == message)
return true;
}
return false;
@@ -203,12 +305,12 @@ HWND Window::getHwnd() const
int Window::getWidth() const
{
return this->width;
return this->pendingW;
}
int Window::getHeight() const
{
return this->height;
return this->pendingH;
}
std::string Window::getHeadline() const