feat: add a new awesome feature

This commit is contained in:
2025-11-08 01:06:37 +08:00
parent cc08187ced
commit 5420bfd644
16 changed files with 918 additions and 421 deletions

View File

@@ -7,6 +7,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[中文文档](CHANGELOG.md)
## [v2.2.2] - 2025 - 11- 08
### ⚙️ Changes
- Modified the coordinate transfer method for the Canvas container. Child control coordinates are now passed as relative coordinates (with the origin at the top-left corner of the container, obtainable via the `getX`/`getY` interface), instead of the original global coordinates. Child control coordinates can now be set to negative values.
- The example under `examples\register-viewer` has been updated to the latest version, aligning container child controls to use relative coordinates.
### ✅ Fixes
- Fixed jittering/bouncing and flickering when resizing the window (left/top edges):
- `WM_SIZING` only clamps the minimum size; `WM_GETMINMAXINFO` sets the window-level minimum tracking size.
- Freezes redrawing during dragging and handles resizing uniformly upon release; `WM_SIZE` only records the new size without interfering with drawing.
- Disabled `WM_ERASEBKGND` background erasing and removed `CS_HREDRAW`/`CS_VREDRAW` to reduce flickering.
- Fixed issues related to dialog boxes:
- Resolved occasional residual functional buttons after closing a dialog.
- Fixed issues where window resizing failed to redraw or displayed a corrupted background when a modal dialog was active.
- Fixed delayed updates of background snapshots for the table control's pagination buttons and page number labels during window changes.
## [v2.2.1] - 2025-11-04
==This release is a hotfix for v2.2.0==

View File

@@ -7,6 +7,24 @@ StellarX 项目所有显著的变化都将被记录在这个文件中。
[English document](CHANGELOG.en.md)
## [v2.2.2] - 2025 - 11- 08
### ⚙️ 变更
- Canvas容器坐标传递方式改变子控件坐标由原来的传递全局坐标改为传递相对坐标坐标原点为容器的左上角坐标可通过getX/Y接口获得可以设置子控件坐标为负值
- examples\register-viewer下的案例已同步修改为最新同步容器子控件为相对坐标
### ✅ 修复
- 修复窗口拉伸(左/上边)时的抖动/弹回与闪烁
- `WM_SIZING` 仅做最小尺寸夹紧;`WM_GETMINMAXINFO` 设置窗口级最小轨迹
- 拖拽期冻结重绘,松手统一收口;`WM_SIZE` 只记录尺寸不抢绘制
- 禁用 `WM_ERASEBKGND` 擦背景并移除 `CS_HREDRAW/CS_VREDRAW`,减少闪烁
- 对话框的相关问题
- 对话框关闭后概率出现功能按钮残留
- 模态对话框触发时,窗口拉伸无法重绘或背景错乱
- 表格控件在窗口变化时其翻页按钮和页码标签背景快照更新不及时的问题
## [v2.2.1] - 2025 - 11 - 4
==此版本为v2.2.0的修复版本==

View File

@@ -25,22 +25,15 @@ This is a **teaching-grade and tooling-grade** framework that helps developers u
------
## **🆕 v2.2.1 (Hotfix for v2.2.0)**
## 🆕 v2.2.2 — Stable Release
- Addressed a flickering issue that occurred when using the Canvas and TabControl containers.
- Fixed issues where border remnants and functional buttons could persist after closing a Dialog.
- In the next planned update, control states will synchronize in real-time during window resizing operations.
- Modified the coordinate transfer method for Canvas containers. Child control coordinates are now passed as relative coordinates (with the origin at the top-left corner of the container, obtainable via the `getX`/`getY` interface) instead of global coordinates. Child control coordinates can now be set to negative values.
- The example under `examples\register-viewer` has been updated to the latest version, aligning container child controls to use relative coordinates.
- Addressed issues related to window resizing and dialog boxes as mentioned above.
For details, please refer to the [CHANGELOG.en](CHANGELOG.en.md).
## Whats new in v2.2.0
- **New TabControl for multi-page tabbed UIs:** With `TabControl`, its easy to create a tabbed layout. Tabs can be arranged on the top, bottom, left, or right, and clicking switches the displayed page. Suitable for settings panels and multi-view switching.
- **Enhanced control show/hide and resize responsiveness:** All controls now share a unified interface (`setIsVisible`) to toggle visibility. When a container control is hidden, its child controls automatically hide/show with it. Meanwhile, we introduce `onWindowResize` for controls to respond to window size changes so elements update in sync after resizing, eliminating artifacts or misalignment.
- **Refined text-style mechanism:** The Label control now uses a unified `ControlText` style structure. Developers can easily customize font, color, size, etc. (replacing older interfaces, and more flexible). Button Tooltips also support richer customization and different texts for toggle states.
- **Other improvements:** Dialog management gains de-duplication to prevent identical prompts from popping up repeatedly. Several bug fixes and refresh optimizations further improve stability.
See `CHANGELOG.md / CHANGELOG.en.md` for the full list.
------
## 📦 Project Structure & Design Philosophy

View File

@@ -30,24 +30,14 @@
---
## 🆕v2.2.1v2.2.0修复版)
## 🆕v2.2.2 ——稳定版本
- 解决了使用Canvas和TabControl容器时出现频闪问题
- 预计下次版本更新,将同步窗口拉伸时,控件同步更新状态
- 修复了Dialog对话框关闭时概率出边边框残留和功能按钮残留问题
详情参考[更新日志](CHANGELOG.md)
## V2.2.0 有何变化
- **新增 TabControl 控件,实现多页面选项卡界面:** 通过 `TabControl` 可以轻松创建选项卡式布局,支持页签在上下左右排列、点击切换显示不同内容页面。适用于设置面板、多视图切换等场景。
- **控件显隐与布局响应能力增强:** 现在所有控件都可以使用统一接口动态隐藏或显示(`setIsVisible`),容器控件隐藏时其内部子控件会自动随之隐藏/显示。与此同时,引入控件对窗口尺寸变化的响应机制(`onWindowResize`),窗口拉伸后界面各元素可协调更新,杜绝拉伸过程中出现残影或错位。
- **文本样式机制完善:** Label 控件改用统一的文本样式结构 `ControlText`,开发者可方便地设置字体、颜色、大小等属性来定制 Label 的外观替代旧接口更加灵活。Button 的 Tooltip 提示也支持更丰富的定制和针对切换状态的不同提示文本。
- **其他改进:** 框架底层的对话框管理增加了防重复弹出相同提示的机制,修复了一些细节 Bug 并优化了刷新效率,进一步提升了稳定性。
详见 `CHANGELOG.md / CHANGELOG.en.md` 获取完整更新列表。
- Canvas容器坐标传递方式改变子控件坐标由原来的传递全局坐标改为传递相对坐标坐标原点为容器的左上角坐标可通过getX/Y接口获得可以设置子控件坐标为负值
- examples\register-viewer下的案例已同步修改为最新同步容器子控件为相对坐标
- 对于窗口拉伸和对话框问题进行了修复
- 详情参考[更新日志](CHANGELOG.md)
---

View File

@@ -7,9 +7,9 @@
auto blackColor = RGB(202, 255, 255);
char initData[33] = "00000000000000000000000000000000";//初始数据
bool gSigned = false; //是否为有符号数
void main()
int main()
{
Window mainWindow(700,500,NULL,RGB(255,255,255), "寄存器查看工具 V1.0——我在人间做废物 (同类工具定制3150131407(Q / V))");
Window mainWindow(700, 510, NULL, RGB(255, 255, 255), "寄存器查看工具 V1.0——我在人间做废物 (同类工具定制3150131407(Q / V))");
//选择区控件
auto selectionAreaLabel = std::make_unique<Label>(18, 0, "32位选择区");
@@ -29,13 +29,13 @@ void main()
{
if (0 == y)
{
selectionAreaButtonLabel.push_back(std::make_unique<Label>(x * 35 + 40 + 28 * (x / 4), 26, "", RGB(208, 208, 208)));
selectionAreaButtonLabel.push_back(std::make_unique<Label>(x * 35 + 25 + 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",
std::make_unique<Button>(x * 35 + 27 + 28 * (x / 4), 58, 25, 30, "0",
blackColor, RGB(171, 196, 220), StellarX::ButtonMode::TOGGLE));
selectionAreaButton.back()->textStyle.color = RGB(226, 116, 152);
selectionAreaButton.back()->setButtonShape(StellarX::ControlShape::B_RECTANGLE);
@@ -55,13 +55,13 @@ void main()
}
else
{
selectionAreaButtonLabel.push_back(std::make_unique<Label>(x * 35 + 40 + 28 * (x / 4), 90, "", RGB(208, 208, 208)));
selectionAreaButtonLabel.push_back(std::make_unique<Label>(x * 35 + 25 + 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",
std::make_unique<Button>(x * 35 + 27 + 28 * (x / 4), 120, 25, 30, "0",
blackColor, RGB(171, 196, 220), StellarX::ButtonMode::TOGGLE));
selectionAreaButton.back()->textStyle.color = RGB(226, 116, 152);
selectionAreaButton.back()->setButtonShape(StellarX::ControlShape::B_RECTANGLE);
@@ -104,11 +104,11 @@ void main()
rightShift->setShape(StellarX::ControlShape::B_ROUND_RECTANGLE);
auto bitInvertLabel = std::make_unique<Label>(18,160,"位取反");
auto bitInvertLabel = std::make_unique<Label>(13, -10, "位取反");
bitInvertLabel->setTextdisap(true);
auto leftShiftLabel = std::make_unique<Label>(248, 160, "左移位");
auto leftShiftLabel = std::make_unique<Label>(13, -10, "左移位");
leftShiftLabel->setTextdisap(true);
auto rightShiftLabel = std::make_unique<Label>(478, 160, "右移位");
auto rightShiftLabel = std::make_unique<Label>(13, -10, "右移位");
rightShiftLabel->setTextdisap(true);
// ====== 公用小工具======
@@ -139,17 +139,17 @@ void main()
//取反区控件
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, "");
bitInvertFunctionLabel[0] = std::make_unique<Label>(35, 10, "低位");
bitInvertFunctionLabel[1] = std::make_unique<Label>(90, 10, "高位");
bitInvertFunctionLabel[2] = std::make_unique<Label>(15, 38, "");
bitInvertFunctionLabel[3] = std::make_unique<Label>(75, 38, "");
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");
bitInvertFunctionTextBox[0] = std::make_unique<TextBox>(35, 35, 35, 30, "0");
bitInvertFunctionTextBox[1] = std::make_unique<TextBox>(95, 35, 35, 30, "0");
auto invL = bitInvertFunctionTextBox[0].get();
auto invH = bitInvertFunctionTextBox[1].get();
auto bitInvertFunctionButton = std::make_unique<Button>(150,195, 70, 35, "位取反",
auto bitInvertFunctionButton = std::make_unique<Button>(135, 35, 80, 30, "位取反",
blackColor, RGB(171, 196, 220));
bitInvertFunctionButton->textStyle.color = RGB(226, 116, 152);
bitInvertFunctionButton->setButtonShape(StellarX::ControlShape::B_RECTANGLE);
@@ -171,16 +171,16 @@ void main()
bitInvert->addControl(std::move(b));
}
//左移控件
auto leftShiftFunctionLabel = std::make_unique<Label>(435, 198, "");
auto leftShiftFunctionLabel = std::make_unique<Label>(198, 30, "");
leftShiftFunctionLabel->setTextdisap(true);
auto leftShiftFunctionTextBox = std::make_unique<TextBox>(325, 195, 100, 30, "0");
auto leftShiftFunctionTextBox = std::make_unique<TextBox>(90, 30, 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, "左移",
auto leftShiftFunctionButton = std::make_unique<Button>(15, 30, 60, 30, "左移",
blackColor, RGB(171, 196, 220));
leftShiftFunctionButton->textStyle.color = RGB(226, 116, 152);
leftShiftFunctionButton->setButtonShape(StellarX::ControlShape::B_RECTANGLE);
@@ -194,16 +194,16 @@ void main()
leftShift->addControl(std::move(leftShiftFunctionLabel));
//右移控件
auto rightShiftFunctionLabel = std::make_unique<Label>(665, 198, "");
auto rightShiftFunctionLabel = std::make_unique<Label>(198, 30, "");
rightShiftFunctionLabel->setTextdisap(true);
auto rightShiftFunctionTextBox = std::make_unique<TextBox>(555, 195, 100, 30, "0");
auto rightShiftFunctionTextBox = std::make_unique<TextBox>(90, 30, 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, "右移",
auto rightShiftFunctionButton = std::make_unique<Button>(15, 30, 60, 30, "右移",
blackColor, RGB(171, 196, 220));
rightShiftFunctionButton->textStyle.color = RGB(226, 116, 152);
rightShiftFunctionButton->setButtonShape(StellarX::ControlShape::B_RECTANGLE);
@@ -228,13 +228,13 @@ void main()
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, "十进制");
NumericalDisplayAreaLabel[0] = std::make_unique<Label>(18, -10, "数值显示区");
NumericalDisplayAreaLabel[1] = std::make_unique<Label>(20, 25, "十六进制");
NumericalDisplayAreaLabel[2] = std::make_unique<Label>(330, 25, "十进制");
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");
NumericalDisplayAreaTextBox[0] = std::make_unique<TextBox>(110, 25, 200, 30, "0");
NumericalDisplayAreaTextBox[1] = std::make_unique<TextBox>(400, 25, 200, 30, "0");
auto hex = NumericalDisplayAreaTextBox[0].get();
auto dec = NumericalDisplayAreaTextBox[1].get();
@@ -259,13 +259,13 @@ void main()
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, "本次值");
BinaryDisplayAreaLabel[0] = std::make_unique<Label>(18, -10, "二进制显示区");
BinaryDisplayAreaLabel[1] = std::make_unique<Label>(35, 20, "上次值");
BinaryDisplayAreaLabel[2] = std::make_unique<Label>(35, 67, "本次值");
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");
BinaryDisplayAreaTextBox[0] = std::make_unique<TextBox>(110, 20, 520, 30, "0000_0000_0000_0000_0000_0000_0000_0000");
BinaryDisplayAreaTextBox[1] = std::make_unique<TextBox>(110, 67, 520, 30, "0000_0000_0000_0000_0000_0000_0000_0000");
auto Last = BinaryDisplayAreaTextBox[0].get();
auto This = BinaryDisplayAreaTextBox[1].get();
@@ -367,16 +367,16 @@ void main()
configuration->setCanvasBkColor(blackColor);
configuration->setShape(StellarX::ControlShape::B_ROUND_RECTANGLE);
auto configurationLabel = std::make_unique<Label>(20, 445, "配置区");
auto configurationLabel = std::make_unique<Label>(20, -10, "配置区");
configurationLabel->setTextdisap(true);
std::array<std::unique_ptr<Button>, 2> configurationButton;
configurationButton[0] = std::make_unique<Button>(450, 465, 80, 20, "一键置0",
configurationButton[0] = std::make_unique<Button>(420, 10, 90, 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",
configurationButton[1] = std::make_unique<Button>(530, 10, 90, 20, "一键置1",
blackColor, RGB(171, 196, 220));
configurationButton[1]->textStyle.color = RGB(226, 116, 152);
configurationButton[1]->setButtonShape(StellarX::ControlShape::B_RECTANGLE);
@@ -419,7 +419,7 @@ void main()
auto signedToggle = std::make_unique<Button>(
350, 465, 80, 20, "无符号",
330, 10, 80, 20, "无符号",
blackColor, RGB(171, 196, 220), StellarX::ButtonMode::TOGGLE);
signedToggle->textStyle.color = RGB(226, 116, 152);
signedToggle->setButtonShape(StellarX::ControlShape::B_RECTANGLE);
@@ -428,7 +428,7 @@ void main()
signedTogglePtr->setOnToggleOnListener([&]() {
gSigned = true;
signedTogglePtr->setButtonText("有符号");
StellarX::MessageBox::showAsync(mainWindow, "有符号模式下,\n最高位为符号位,\n其余位为数值位。", "有符号模式");
// 立即刷新十进制显示:用当前位图算出新值,仅改 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; }();
@@ -438,12 +438,13 @@ void main()
signedTogglePtr->setOnToggleOffListener([&]() {
gSigned = false;
signedTogglePtr->setButtonText("无符号");
StellarX::MessageBox::showAsync(mainWindow, "无符号模式下,\n所有位均为数值位。", "无符号模式");
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));
});
signedTogglePtr->enableTooltip(true);
signedTogglePtr->setTooltipTextsForToggle("切换无符号模式", "切换有符号模式");
configuration->addControl(std::move(configurationButton[0]));
configuration->addControl(std::move(configurationButton[1]));

View File

@@ -9,23 +9,20 @@
* - 定义控件基本属性(坐标、尺寸、脏标记)
* - 提供绘图状态管理saveStyle/restoreStyle
* - 声明纯虚接口draw、handleEvent等
* - 支持移动语义,禁止拷贝语义
* - 禁止移动语义,禁止拷贝语义
*
* @使用场景: 作为所有具体控件类的基类,不直接实例化
* @所属框架: 星垣(StellarX) GUI框架
* @作者: 我在人间做废物
******************************************************************************/
#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>

View File

@@ -126,7 +126,7 @@ private:
void saveBackground(int x, int y, int w, int h)override;
void restBackground()override;
void addControl(std::unique_ptr<Control> control);
// 清除所有控件
void clearControls();

View File

@@ -1,7 +1,7 @@
/*******************************************************************************
* @文件: StellarX.h
* @摘要: 星垣(StellarX) GUI框架 - 主包含头文件
* @版本: v2.2.1
* @版本: v2.2.2
* @描述:
* 一个为Windows平台打造的轻量级、模块化C++ GUI框架。
* 基于EasyX图形库提供简洁易用的API和丰富的控件。

View File

@@ -64,5 +64,6 @@ public:
int indexOf(const std::string& tabText) const;
void setDirty(bool dirty) override;
void requestRepaint(Control* parent)override;
};

View File

@@ -127,6 +127,8 @@ public:
void setTableLineStyle(StellarX::LineStyle style);
//设置边框宽度
void setTableBorderWidth(int width);
//窗口变化丢快照+标脏
void onWindowResize() override;
//************************** 获取属性 *****************************/

View File

@@ -1,86 +1,105 @@
/**
* Window头文件
*
* 设计目标:
* - 提供一个基于 Win32 + EasyX 的“可拉伸且稳定不抖”的窗口容器。
* - 通过消息过程子类化WndProcThunk接管关键消息WM_SIZING/WM_SIZE/...)。
* - 将“几何变化记录pendingW/H”与“统一收口重绘needResizeDirty”解耦。
*
* 关键点(与 .cpp 中实现对应):
* - isSizing处于交互拉伸阶段时冻结重绘松手后统一重绘防止抖动。
* - WM_SIZING只做“最小尺寸夹紧”不回滚矩形、不做对齐把其余交给系统。
* - WM_GETMINMAXINFO按最小“客户区”换算到“窗口矩形”提供系统层最小轨迹值。
* - runEventLoop只记录 WM_SIZE 的新尺寸;真正绘制放在 needResizeDirty 时集中处理。
*/
#pragma once
#include "Control.h"
/*******************************************************************************
* @类: Window
* @摘要: 应用程序主窗口类,管理窗口生命周期和消息循环
* @描述:
* 创建和管理应用程序主窗口,作为所有控件的根容器。
* 处理消息分发、事件循环和渲染调度。
*
* @特性:
* - 多种窗口模式配置(双缓冲、控制台等)
* - 背景图片和颜色支持
* - 集成的对话框管理系统
* - 完整的消息处理循环
* - 控件和对话框的生命周期管理
*
* @使用场景: 应用程序主窗口GUI程序的入口和核心
* @所属框架: 星垣(StellarX) GUI框架
* @作者: 我在人间做废物
******************************************************************************/
#include <string>
#include <vector>
#include <memory>
#include <windows.h>
class Window
{
int width; //窗口宽度
int height; //窗口高度
int windowMode = NULL; //窗口模式
// —— 尺寸状态 ——(绘制尺寸与待应用尺寸分离;收口时一次性更新)
int width; // 当前有效宽(已应用到画布/控件的客户区宽)
int height; // 当前有效高(已应用到画布/控件的客户区高)
int pendingW; // 待应用宽WM_SIZE/拉伸中记录用)
int pendingH; // 待应用高
int minClientW; // 业务设定的最小客户区宽(用于 GETMINMAXINFO 与 SIZING 夹紧)
int minClientH; // 业务设定的最小客户区高
int windowMode = NULL; // EasyX 初始化模式EX_SHOWCONSOLE/EX_TOPMOST/...
bool needResizeDirty = false; // 统一收口重绘标志(置位后在事件环末尾处理)
bool isSizing = false; // 是否处于拖拽阶段ENTER/EXIT SIZEMOVE 切换)
// --- 尺寸变化去抖用 ---
int pendingW;
int pendingH;
bool needResizeDirty = false;
// —— 原生窗口句柄与子类化钩子 ——(子类化 EasyX 的窗口过程以拦截关键消息)
HWND hWnd = NULL; // EasyX 初始化后的窗口句柄
WNDPROC oldWndProc = nullptr; // 保存旧过程CallWindowProc 回落)
bool procHooked = false; // 避免重复子类化
static LRESULT CALLBACK WndProcThunk(HWND h, UINT m, WPARAM w, LPARAM l); // 静态过程分发到 this
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; //对话框管理
// —— 绘制相关 ——(是否使用合成双缓冲、窗口标题、背景等)
bool useComposited = true; // 是否启用 WS_EX_COMPOSITED部分机器可能增加一帧观感延迟
std::string headline; // 窗口标题文本
COLORREF wBkcolor = BLACK; // 纯色背景(无背景图时使用)
IMAGE* background = nullptr; // 背景图对象指针(存在时优先绘制)
std::string bkImageFile; // 背景图文件路径loadimage 用)
// —— 控件/对话框 ——(容器内的普通控件与非模态对话框)
std::vector<std::unique_ptr<Control>> controls;
std::vector<std::unique_ptr<Control>> dialogs;
public:
bool dialogClose = false; //是否有对话框关闭
bool dialogClose = false; // 项目内使用的状态位
// —— 构造/析构 ——(仅初始化成员;实际样式与子类化在 draw() 中完成)
Window(int width, int height, int mode);
Window(int width, int height, int mode, COLORREF bkcloc);
Window(int width, int height, int mode , COLORREF bkcloc, std::string headline = "窗口");
Window(int width, int height, int mode, COLORREF bkcloc, std::string headline);
~Window();
//绘制窗口
void draw();
void draw(std::string pImgFile);
//事件循环
int runEventLoop();
//设置窗口背景图片
// —— 绘制与事件循环 ——draw* 完成一次全量绘制runEventLoop 驱动事件与统一收口)
void draw(); // 纯色背景版本
void draw(std::string pImgFile); // 背景图版本
int runEventLoop(); // 主事件循环PeekMessage + 统一收口重绘)
// —— 背景/标题设置 ——(更换背景、背景色与标题;立即触发一次批量绘制)
void setBkImage(std::string pImgFile);
//设置窗口背景颜色
void setBkcolor(COLORREF c);
//设置窗口标题
void setHeadline(std::string headline);
//添加控件
// —— 控件/对话框管理 ——(添加到容器,或做存在性判断)
void addControl(std::unique_ptr<Control> control);
//添加对话框
void addDialog(std::unique_ptr<Control> dialogs);
//检查是否已有对话框显示用于去重,防止工厂模式调用非模态对话框,多次打开污染对话框背景快照
bool hasNonModalDialogWithCaption(const std::string& caption, const std::string& message) const;
//获取窗口句柄
// —— 访问器 ——(只读接口,供外部查询当前窗口/标题/背景等)
HWND getHwnd() const;
//获取窗口宽度
int getWidth() const;
//获取窗口高度
int getHeight() const;
//获取窗口标题
std::string getHeadline() const;
//获取窗口背景颜色
COLORREF getBkcolor() const;
//获取窗口背景图片
IMAGE* getBkImage() const;
//获取窗口背景图片文件名
std::string getBkImageFile() const;
//获取控件管理
std::vector<std::unique_ptr<Control>>& getControls();
// —— 配置开关 ——(动态调整最小客户区、合成双缓冲)
inline void setMinClientSize(int w, int h)
{
// 仅更新阈值;实际约束在 WM_GETMINMAXINFO/WM_SIZING 中生效
minClientW = w;
minClientH = h;
}
inline void setComposited(bool on)
{
// 更新标志;真正应用在 draw()/样式 SetWindowLongEx + SWP_FRAMECHANGED
useComposited = on;
}
void processWindowMessage(const ExMessage & msg); // 处理 EX_WINDOW 中的 WM_SIZE 等
void pumpResizeIfNeeded(); // 执行一次统一收口重绘
void scheduleResizeFromModal(int w, int h);
};

View File

@@ -76,6 +76,9 @@ bool Canvas::handleEvent(const ExMessage& msg)
void Canvas::addControl(std::unique_ptr<Control> control)
{
//坐标转化
control->setX(control->getLocalX() + this->x);
control->setY(control->getLocalY() + this->y);
control->setParent(this);
controls.push_back(std::move(control));
dirty = true;
@@ -166,10 +169,7 @@ void Canvas::requestRepaint(Control* parent)
{
for (auto& control : controls)
if (control->isDirty() && control->IsVisible())
{
control->draw();
break;
}
}
else
onRequestRepaintAsRoot();

View File

@@ -170,16 +170,43 @@ void Dialog::Show()
if (modal)
{
// 模态对话框需要阻塞当前线程直到对话框关闭
if (modal)
{
// 记录当前窗口客户区尺寸,供轮询对比
RECT rc0;
GetClientRect(hWnd.getHwnd(), &rc0);
int lastW = rc0.right - rc0.left;
int lastH = rc0.bottom - rc0.top;
while (show && !close)
{
// ① 轮询窗口尺寸(不依赖 WM_SIZE
RECT rc;
GetClientRect(hWnd.getHwnd(), &rc);
const int cw = rc.right - rc.left;
const int ch = rc.bottom - rc.top;
// 处理消息
if (cw != lastW || ch != lastH)
{
lastW = cw;
lastH = ch;
// 通知父窗口:有新尺寸 → 标记 needResizeDirty
hWnd.scheduleResizeFromModal(cw, ch);
// 立即统一收口:父窗重绘 背景+普通控件(不会画到这只模态)
hWnd.pumpResizeIfNeeded();
// 这只模态在新尺寸下重建布局 / 重抓背景 → 本帧要画自己
setInitialization(true);
setDirty(true);
}
// ② 处理这只对话框的鼠标/键盘(沿用你原来 EX_MOUSE | EX_KEY
ExMessage msg;
if (peekmessage(&msg, EX_MOUSE | EX_KEY))
{
handleEvent(msg);
// 检查是否需要关闭
if (shouldClose)
{
Close();
@@ -187,16 +214,27 @@ void Dialog::Show()
}
}
// 重绘
// ③ 最后一笔:只画这只模态,保证永远在最上层
if (dirty)
{
requestRepaint(parent);
FlushBatchDraw();
BeginBatchDraw();
this->draw(); // 注意:不要 requestRepaint(parent),只画自己
EndBatchDraw();
dirty = false;
}
// 避免CPU占用过高
Sleep(10);
}
if (pendingCleanup && !isCleaning)
performDelayedCleanup();
}
else
{
// 非模态仍由主循环托管
dirty = true;
}
// 模态对话框关闭后执行清理
if (pendingCleanup && !isCleaning)
performDelayedCleanup();
@@ -609,6 +647,13 @@ void Dialog::restBackground()
putimage(saveBkX - BorderWidth, saveBkY - BorderWidth,saveBkImage);
}
void Dialog::addControl(std::unique_ptr<Control> control)
{
control->setParent(this);
controls.push_back(std::move(control));
dirty = true;
}
// 延迟清理策略:由于对话框绘制时保存了背景快照,必须在对话框隐藏后、
// 所有控件析构前恢复背景,否则会导致背景图像被错误覆盖。
// 此方法在对话框不可见且被标记为待清理时由 draw() 或 handleEvent() 调用。
@@ -634,6 +679,23 @@ void Dialog::performDelayedCleanup()
FlushBatchDraw();
discardBackground();
}
if (!(saveBkImage && hasSnap))
{
// 没有背景快照:强制一次完整重绘,立即擦掉残影
hWnd.pumpResizeIfNeeded(); // 如果正好有尺寸标志,顺便统一收口
// 即使没有尺寸变化,也重绘一帧
BeginBatchDraw();
// 背景
if (hWnd.getBkImage() && !hWnd.getBkImageFile().empty())
putimage(0, 0, hWnd.getBkImage());
else { setbkcolor(hWnd.getBkcolor()); cleardevice(); }
// 所有普通控件
for (auto& c : hWnd.getControls()) c->draw();
// 其他对话框this 已经 show=false会早退不绘
// 注意:此处若有容器管理,需要按你的现状遍历 dialogs 再 draw
EndBatchDraw();
FlushBatchDraw();
}
// 重置状态
needsInitialization = true;
pendingCleanup = false;
@@ -685,10 +747,7 @@ void Dialog::requestRepaint(Control* parent)
{
for (auto& control : controls)
if (control->isDirty() && control->IsVisible())
{
control->draw();
break;
}
}
else

View File

@@ -364,15 +364,9 @@ void TabControl::requestRepaint(Control* parent)
for (auto& control : controls)
{
if (control.first->isDirty() && control.first->IsVisible())
{
control.first->draw();
break;
}
else if (control.second->isDirty()&&control.second->IsVisible())
{
control.second->draw();
break;
}
}
}

View File

@@ -464,6 +464,14 @@ void Table::setTableBorderWidth(int width)
this->dirty = true;
}
void Table::onWindowResize()
{
Control::onWindowResize(); // 先处理自己
prevButton->onWindowResize();
nextButton->onWindowResize();
pageNum->onWindowResize();
}
int Table::getCurrentPage() const
{
return this->currentPage;

View File

@@ -1,114 +1,353 @@
#include "Window.h"
#include "Dialog.h"
#include <windows.h> // 确保包含 Windows API 头文件
Window::Window(int width, int height, int mode)
#include <graphics.h>
#include <algorithm>
/**
* ApplyResizableStyle
* 作用:统一设置可拉伸/裁剪样式,并按开关使用 WS_EX_COMPOSITED合成双缓冲
* 关键点:
* - WS_THICKFRAME允许从四边/四角拖动改变尺寸。
* - WS_CLIPCHILDREN / WS_CLIPSIBLINGS避免子控件互相覆盖时闪烁。
* - WS_EX_COMPOSITED在一些环境更平滑但个别显卡/驱动可能带来一帧延迟感。
* - SWP_FRAMECHANGED通知窗口样式已变更强制系统重算非客户区标题栏/边框)。
*/
static void ApplyResizableStyle(HWND h, bool useComposited)
{
LONG style = GetWindowLong(h, GWL_STYLE);
style |= WS_THICKFRAME | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
SetWindowLong(h, GWL_STYLE, style);
this->pendingW = this->width = width;
this->pendingH = this->height = height;
this->windowMode = mode;
LONG ex = GetWindowLong(h, GWL_EXSTYLE);
if (useComposited)
{
ex |= WS_EX_COMPOSITED;
}
else
{
ex &= ~WS_EX_COMPOSITED;
}
SetWindowLong(h, GWL_EXSTYLE, ex);
SetWindowPos(h, NULL, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
}
Window::Window(int width, int height, int mode, COLORREF bkcloc)
/**
* ApplyMinSizeOnSizing
* 作用:在 WM_SIZING 阶段执行“最小尺寸夹紧”。
* 规则:只回推“被拖动的那一侧”,另一侧当锚点(避免几何回弹/位置漂移)。
* 步骤:
* 1将“最小客户区尺寸”通过 AdjustWindowRectEx 换算为“最小窗口矩形”(含非客户区)。
* 2若当前矩形比最小还小则根据 edge哪条边/角在被拖)调整对应边,另一侧保持不动。
* 说明:仅保证不小于最小值;不做对齐/回滚等操作,把其余交给系统尺寸逻辑。
*/
static void ApplyMinSizeOnSizing(RECT* prc, WPARAM edge, HWND hWnd, int minClientW, int minClientH)
{
this->pendingW = this->width = width;
this->pendingH = this->height = height;
this->windowMode = mode;
this->wBkcolor = bkcloc;
RECT rcFrame{ 0, 0, minClientW, minClientH };
DWORD style = GetWindowLong(hWnd, GWL_STYLE);
DWORD ex = GetWindowLong(hWnd, GWL_EXSTYLE);
AdjustWindowRectEx(&rcFrame, style, FALSE, ex);
const int minW = rcFrame.right - rcFrame.left;
const int minH = rcFrame.bottom - rcFrame.top;
const int curW = prc->right - prc->left;
const int curH = prc->bottom - prc->top;
if (curW < minW)
{
switch (edge)
{
case WMSZ_LEFT:
case WMSZ_TOPLEFT:
case WMSZ_BOTTOMLEFT:
prc->left = prc->right - minW; // 锚定右侧,回推左侧(左边被拖)
break;
default:
prc->right = prc->left + minW; // 锚定左侧,回推右侧(右边被拖)
break;
}
}
Window::Window(int width, int height, int mode, COLORREF bkcloc, std::string headline)
if (curH < minH)
{
this->pendingW = this->width = width;
this->pendingH = this->height = height;
this->windowMode = mode;
this->wBkcolor = bkcloc;
this->headline = headline;
switch (edge)
{
case WMSZ_TOP:
case WMSZ_TOPLEFT:
case WMSZ_TOPRIGHT:
prc->top = prc->bottom - minH; // 锚定下侧,回推上侧(上边被拖)
break;
default:
prc->bottom = prc->top + minH; // 锚定上侧,回推下侧(下边被拖)
break;
}
}
}
// ---------------- 构造 / 析构 ----------------
/**
* 构造:初始化当前尺寸、待应用尺寸、最小客户区尺寸与 EasyX 模式。
* 注意:样式设置与子类化放在 draw() 内第一次绘制时完成。
*/
Window::Window(int w, int h, int mode)
{
minClientW = pendingW = width = w;
minClientH = pendingH = height = h;
windowMode = mode;
}
Window::Window(int w, int h, int mode, COLORREF bk)
{
minClientW = pendingW = width = w;
minClientH = pendingH = height = h;
windowMode = mode;
wBkcolor = bk;
}
Window::Window(int w, int h, int mode, COLORREF bk, std::string title)
{
minClientW = pendingW = width = w;
minClientH = pendingH = height = h;
windowMode = mode;
wBkcolor = bk;
headline = std::move(title);
}
Window::~Window()
{
if (background)
delete background;
// 析构:释放背景图对象并关闭 EasyX 图形环境
if (background) delete background;
background = nullptr;
closegraph(); // 确保关闭图形上下文
closegraph();
}
// ---------------- 原生消息钩子----------------
void Window::draw() {
// 使用 EasyX 创建基本窗口
/**
* WndProcThunk
* 作用:替换 EasyX 的窗口过程,接管关键消息。
* 关键处理:
* - WM_ERASEBKGND返回 1交由自绘清屏避免系统擦背景造成闪烁。
* - WM_ENTERSIZEMOVE开始拉伸 → isSizing=true 且 WM_SETREDRAW(FALSE) 冻结重绘。
* - WM_SIZING拉伸中 → 仅做“最小尺寸夹紧”(按被拖边回推),不回滚、不绘制。
* - WM_EXITSIZEMOVE结束拉伸 → 读取最终客户区尺寸 → 标记 needResizeDirty解冻并刷新。
* - WM_GETMINMAXINFO提供系统最小轨迹限制四边一致
*/
LRESULT CALLBACK Window::WndProcThunk(HWND h, UINT m, WPARAM w, LPARAM l)
{
auto* self = reinterpret_cast<Window*>(GetWindowLongPtr(h, GWLP_USERDATA));
if (!self)
{
return DefWindowProc(h, m, w, l);
}
// 关键点①:禁止系统擦背景,避免和我们自己的清屏/双缓冲打架造成闪烁
if (m == WM_ERASEBKGND)
{
return 1;
}
// 关键点②:拉伸开始 → 冻结重绘(系统调整窗口矩形时不触发即时重绘,防止抖)
if (m == WM_ENTERSIZEMOVE)
{
self->isSizing = true;
SendMessage(h, WM_SETREDRAW, FALSE, 0);
return 0;
}
// 关键点③:拉伸中 → 仅执行“最小尺寸夹紧”,不做对齐/节流/回滚,保持系统自然流畅
if (m == WM_SIZING)
{
RECT* prc = reinterpret_cast<RECT*>(l);
// “尺寸异常值”快速过滤:仅保护极端值,不影响正常拖动
int currentWidth = prc->right - prc->left;
int currentHeight = prc->bottom - prc->top;
if (currentWidth < 0 || currentHeight < 0 || currentWidth > 10000 || currentHeight > 10000)
{
return TRUE;
}
ApplyMinSizeOnSizing(prc, w, h, self->minClientW, self->minClientH);
return TRUE;
}
// 关键点④:拉伸结束 → 解冻重绘 + 统一收口(记录最终尺寸 -> 标记 needResizeDirty
if (m == WM_EXITSIZEMOVE)
{
self->isSizing = false;
RECT rc; GetClientRect(h, &rc);
const int aw = rc.right - rc.left;
const int ah = rc.bottom - rc.top;
if (aw >= self->minClientW && ah >= self->minClientH && aw <= 10000 && ah <= 10000)
{
self->pendingW = aw;
self->pendingH = ah;
self->needResizeDirty = true;
}
// 关键:立刻做统一收口,不用等下一条消息
self->pumpResizeIfNeeded();
// 不擦背景、不触发立即 WM_PAINT
// InvalidateRect(h, nullptr, TRUE);
// UpdateWindow(h);
InvalidateRect(h, nullptr, FALSE);
// 解冻 + 触发一次刷新InvalidateRect TRUE 会擦背景;如需无擦背景可传 FALSE
SendMessage(h, WM_SETREDRAW, TRUE, 0);
InvalidateRect(h, nullptr, TRUE);
UpdateWindow(h);
return 0;
}
// 关键点⑤:系统级最小轨迹限制(与 WM_SIZING 的夹紧互相配合)
if (m == WM_GETMINMAXINFO)
{
auto* mmi = reinterpret_cast<MINMAXINFO*>(l);
RECT rc{ 0, 0, self->minClientW, self->minClientH };
DWORD style = GetWindowLong(h, GWL_STYLE);
DWORD ex = GetWindowLong(h, GWL_EXSTYLE);
// 若后续添加菜单,请把第三个参数改为 HasMenu(h)
AdjustWindowRectEx(&rc, style, FALSE, ex);
mmi->ptMinTrackSize.x = rc.right - rc.left;
mmi->ptMinTrackSize.y = rc.bottom - rc.top;
return 0;
}
// 其它消息:回落到旧过程
return self->oldWndProc ? CallWindowProc(self->oldWndProc, h, m, w, l)
: DefWindowProc(h, m, w, l);
}
// ---------------- 绘制 ----------------
/**
* draw()
* 作用:首次初始化 EasyX 窗口与子类化过程;应用可拉伸样式;清屏并批量绘制。
* 关键步骤:
* 1initgraph 拿到 hWnd
* 2SetWindowLongPtr 子类化到 WndProcThunk只做一次
* 3ApplyResizableStyle 设置 WS_THICKFRAME/裁剪/(可选)合成双缓冲;
* 4去掉类样式 CS_HREDRAW/CS_VREDRAW避免全窗无效化引发闪屏
* 5清屏 + Begin/EndBatchDraw 批量绘制控件&对话框。
*/
void Window::draw()
{
if (!hWnd)
{
hWnd = initgraph(width, height, windowMode);
// **启用窗口拉伸支持**:添加厚边框和最大化按钮样式
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);
}
// 子类化:让我们的 WndProcThunk 接管窗口消息(仅执行一次)
if (!procHooked)
{
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)this);
oldWndProc = (WNDPROC)SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)&Window::WndProcThunk);
procHooked = (oldWndProc != nullptr);
}
ApplyResizableStyle(hWnd, useComposited);
// 关闭类样式的整窗重绘标志(减少尺寸变化时的整窗 redraw
LONG_PTR cls = GetClassLongPtr(hWnd, GCL_STYLE);
cls &= ~(CS_HREDRAW | CS_VREDRAW);
SetClassLongPtr(hWnd, GCL_STYLE, cls);
// 设置背景色并清屏
setbkcolor(wBkcolor);
cleardevice();
// 初次绘制所有控件(双缓冲)
BeginBatchDraw();
for (auto& control : controls)
for (auto& c : controls)
{
control->draw();
c->draw();
}
for (auto& d : dialogs)
{
d->draw();
}
// (如果有初始对话框,也可绘制 dialogs
EndBatchDraw();
}
/**
* draw(imagePath)
* 作用:在 draw() 的基础上加载并绘制背景图;其它流程完全一致。
* 注意这里按当前窗口客户区大小加载背景图loadimage 的 w/h保证铺满。
*/
void Window::draw(std::string imagePath)
{
// 使用指定图片绘制窗口背景(铺满窗口)
this->background = new IMAGE(width, height);
bkImageFile = imagePath;
if (!hWnd)
{
hWnd = initgraph(width, height, windowMode);
SetWindowText(hWnd, headline.c_str());
loadimage(background, imagePath.c_str(), width, height, true);
if(background)
putimage(0, 0, background);
else
{
// 设置背景色并清屏
setbkcolor(wBkcolor);
cleardevice();
}
// 同样应用可拉伸样式
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)
if (!procHooked)
{
control->setDirty(true);
control->draw();
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)this);
oldWndProc = (WNDPROC)SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)&Window::WndProcThunk);
procHooked = (oldWndProc != nullptr);
}
bkImageFile = std::move(imagePath);
if (!headline.empty())
{
SetWindowText(hWnd, headline.c_str());
}
ApplyResizableStyle(hWnd, useComposited);
LONG_PTR cls = GetClassLongPtr(hWnd, GCL_STYLE);
cls &= ~(CS_HREDRAW | CS_VREDRAW);
SetClassLongPtr(hWnd, GCL_STYLE, cls);
if (background)
{
delete background;
background = nullptr;
}
background = new IMAGE;
loadimage(background, bkImageFile.c_str(), width, height, true);
putimage(0, 0, background);
BeginBatchDraw();
for (auto& c : controls)
{
c->setDirty(true);
c->draw();
}
for (auto& d : dialogs)
{
d->draw();
}
for (auto& dlg : dialogs) dlg->draw();
EndBatchDraw();
}
// 运行主事件循环,处理用户输入和窗口消息
// 此方法会阻塞直到窗口关闭
// 主消息循环优先级:对话框 > 普通控件。
// 重绘策略:为保证视觉一致性,每次有对话框状态变化(打开/关闭)时,
// 会强制重绘所有控件。先绘制普通控件,再绘制对话框(确保对话框在最上层)
// ---------------- 事件循环 ----------------
/**
* runEventLoop()
* 作用:驱动输入/窗口消息;集中处理“统一收口重绘”
* 关键策略:
* - WM_SIZE始终更新 pendingW/H即使在拉伸中也只记录不立即绘制
* - needResizeDirty当尺寸确实变化时置位随后在循环尾进行一次性重绘
* - 非模态对话框优先消费事件(顶层从后往前);再交给普通控件。
*/
int Window::runEventLoop()
{
ExMessage msg;
bool running = true;
// 说明:统一使用 needResizeDirty 作为“收口重绘”的唯一标志位
// 不再引入额外 pendingResize 等状态,避免分叉导致状态不一致。
while (running)
{
bool consume = false;// 是否处理了消息
bool consume = false;
// 处理所有消息
if (peekmessage(&msg, EX_MOUSE | EX_KEY | EX_WINDOW, true))
{
if (msg.message == WM_CLOSE)
@@ -116,42 +355,58 @@ int Window::runEventLoop()
running = false;
return 0;
}
if (msg.message == WM_SIZE)
// 保险:如果 EX_WINDOW 转译了 GETMINMAXINFO同样按最小客户区折算处理
if (msg.message == WM_GETMINMAXINFO)
{
if (msg.wParam != SIZE_MINIMIZED)
auto* mmi = reinterpret_cast<MINMAXINFO*>(msg.lParam);
RECT rc{ 0, 0, minClientW, minClientH };
DWORD style = GetWindowLong(hWnd, GWL_STYLE);
DWORD ex = GetWindowLong(hWnd, GWL_EXSTYLE);
AdjustWindowRectEx(&rc, style, FALSE, ex);
mmi->ptMinTrackSize.x = rc.right - rc.left;
mmi->ptMinTrackSize.y = rc.bottom - rc.top;
continue;
}
// 关键点⑥WM_SIZE 只记录新尺寸;若非拉伸阶段则立即置位 needResizeDirty
if (msg.message == WM_SIZE && 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))
// 基本合法性校验(不小于最小值、不过大)
if (nw >= minClientW && nh >= minClientH && nw <= 10000 && nh <= 10000)
{
if (nw != width || nh != height)
{
pendingW = nw;
pendingH = nh;
// 在“非拉伸阶段”的 WM_SIZE例如最大化/还原/程序化调整)直接触发收口
needResizeDirty = true;
}
}
continue;//在末尾重绘制窗口
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 it = controls.rbegin(); it != controls.rend(); ++it)
{
consume = (*it)->handleEvent(msg);
if (consume)
break;
consume = d->handleEvent(msg);
}
if (consume) break;
}
if (!consume)
{
for (auto& c : controls)
{
consume = c->handleEvent(msg);
if (consume) break;
}
}
}
//如果有对话框打开或者关闭强制重绘
bool needredraw = false;
@@ -193,122 +448,187 @@ int Window::runEventLoop()
needredraw = false;
}
//—— 统一收口”:尺寸变化后的** 一次性** 重绘 ——
// —— 统一收口needResizeDirty 为真时执行一次性重绘——
if (needResizeDirty)
{
//确保窗口不会小于初始尺寸
if (pendingW >= width && pendingH >= height)
Resize(nullptr, pendingW, pendingH);
else
Resize(nullptr, width, height);
if (background)
// 以“实际客户区尺寸”为准,防止 pending 与真实尺寸出现偏差
RECT clientRect;
GetClientRect(hWnd, &clientRect);
int actualWidth = clientRect.right - clientRect.left;
int actualHeight = clientRect.bottom - clientRect.top;
const int finalW = (std::max)(minClientW, actualWidth);
const int finalH = (std::max)(minClientH, actualHeight);
// 变化过大/异常场景保护
if (finalW != width || finalH != height)
{
if (abs(finalW - width) > 1000 || abs(finalH - height) > 1000)
{
// 认为是异常帧,跳过本次(不改变任何状态)
needResizeDirty = false;
continue;
}
// 再次冻结窗口更新,保证批量绘制的原子性
SendMessage(hWnd, WM_SETREDRAW, FALSE, 0);
BeginBatchDraw();
// 调整底层画布尺寸
if (finalW != width || finalH != height)
{
Resize(nullptr, finalW, finalH);
// 重取一次实际客户区尺寸做确认
GetClientRect(hWnd, &clientRect);
int confirmedWidth = clientRect.right - clientRect.left;
int confirmedHeight = clientRect.bottom - clientRect.top;
int renderWidth = confirmedWidth;
int renderHeight = confirmedHeight;
// 背景:若设置了背景图则重载并铺满;否则清屏为纯色
if (background && !bkImageFile.empty())
{
delete background;
background = new IMAGE;
loadimage(background, bkImageFile.c_str(), pendingW, pendingH);
loadimage(background, bkImageFile.c_str(), renderWidth, renderHeight, true);
putimage(0, 0, background);
}
else
{
setbkcolor(wBkcolor);
cleardevice();
}
// 标记所有控件/对话框为脏,确保都补一次背景/外观
// 最终提交“当前已应用尺寸”(用于外部查询/下次比较)
width = renderWidth;
height = renderHeight;
}
// 批量通知控件“窗口尺寸变化”,并标记重绘
for (auto& c : controls)
{
c->onWindowResize();
c->setDirty(true);
}
for (auto& d : dialogs)
{
if (auto dd = dynamic_cast<Dialog*>(d.get()))
{
dd->setDirty(true);
dd->setInitialization(true);
}
}
// 统一批量绘制
for (auto& c : controls) c->draw();
for (auto& d : dialogs) d->draw();
EndBatchDraw();
// 解冻并触发一次无效化(这里 TRUE 表示会擦背景;如要避免闪白可改 FALSE
SendMessage(hWnd, WM_SETREDRAW, TRUE, 0);
InvalidateRect(hWnd, nullptr, TRUE);
}
needResizeDirty = false; // 收口完成,清标志
}
// 轻微睡眠,削峰填谷(不阻塞拖拽体验)
Sleep(10);
}
return 1;
}
// ---------------- 其余接口 ----------------
void Window::setBkImage(std::string pImgFile)
{
// 更换背景图:立即加载并绘制一次;同时将所有控件标 dirty 并重绘
if (background) delete background;
background = new IMAGE;
bkImageFile = std::move(pImgFile);
loadimage(background, bkImageFile.c_str(), width, height, true);
putimage(0, 0, background);
BeginBatchDraw();
for (auto& c : controls)
{
c->setDirty(true);
c->draw();
}
for (auto& d : dialogs)
{
auto dd = dynamic_cast<Dialog*>(d.get());
dd->setDirty(true);
dd->setInitialization(true);
d->setDirty(true);
d->draw();
}
needResizeDirty = false;
}
// 降低占用
Sleep(10);
}
return 1;
}
void Window::setBkImage(std::string pImgFile)
{
if(nullptr == background)
this->background = new IMAGE;
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)
for (auto& d : dialogs)
{
c->setDirty(true);
c->draw();
d->setDirty(true);
d->draw();
}
EndBatchDraw();
}
void Window::setHeadline(std::string headline)
void Window::setHeadline(std::string title)
{
this->headline = headline;
SetWindowText(this->hWnd, headline.c_str());
// 设置窗口标题(仅改文本,不触发重绘)
headline = std::move(title);
if (hWnd)
{
SetWindowText(hWnd, headline.c_str());
}
}
void Window::addControl(std::unique_ptr<Control> control)
{
this->controls.push_back(std::move(control));
// 新增控件:仅加入管理容器,具体绘制在 draw()/收口时统一进行
controls.push_back(std::move(control));
}
void Window::addDialog(std::unique_ptr<Control> dialogs)
void Window::addDialog(std::unique_ptr<Control> dlg)
{
this->dialogs.push_back(std::move(dialogs));
// 新增非模态对话框:管理顺序决定事件优先级(顶层从后往前)
dialogs.push_back(std::move(dlg));
}
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 && d->GetText() == message)
if (auto* d = dynamic_cast<Dialog*>(dptr.get()))
{
if (d->IsVisible() && !d->model() && d->GetCaption() == caption && d->GetText() == message)
{
return true;
}
}
}
return false;
}
HWND Window::getHwnd() const
{
return hWnd;
@@ -316,40 +636,117 @@ HWND Window::getHwnd() const
int Window::getWidth() const
{
return this->pendingW;
// 注意:这里返回 pendingW
// 表示“最近一次收到的尺寸”(可能尚未应用到画布,最终以收口时的 width 为准)
return pendingW;
}
int Window::getHeight() const
{
return this->pendingH;
// 同上,返回 pendingH与 getWidth 对应)
return pendingH;
}
std::string Window::getHeadline() const
{
return this->headline;
return headline;
}
COLORREF Window::getBkcolor() const
{
return this->wBkcolor;
return wBkcolor;
}
IMAGE* Window::getBkImage() const
{
return this->background;
return background;
}
std::string Window::getBkImageFile() const
{
return this->bkImageFile;
return bkImageFile;
}
std::vector<std::unique_ptr<Control>>& Window::getControls()
{
return this->controls;
return controls;
}
void Window::processWindowMessage(const ExMessage& msg)
{
if (msg.message == WM_SIZE && msg.wParam != SIZE_MINIMIZED)
{
const int nw = LOWORD(msg.lParam);
const int nh = HIWORD(msg.lParam);
if (nw >= minClientW && nh >= minClientH && nw <= 10000 && nh <= 10000)
{
if (nw != width || nh != height)
{
pendingW = nw;
pendingH = nh;
needResizeDirty = true; // 统一由 pumpResizeIfNeeded 来收口
}
}
}
}
void Window::pumpResizeIfNeeded()
{
if (!needResizeDirty) return;
RECT rc; GetClientRect(hWnd, &rc);
const int finalW = max(minClientW, rc.right - rc.left);
const int finalH = max(minClientH, rc.bottom - rc.top);
if (finalW == width && finalH == height) { needResizeDirty = false; return; }
SendMessage(hWnd, WM_SETREDRAW, FALSE, 0);
BeginBatchDraw();
// Resize + 背景
Resize(nullptr, finalW, finalH);
GetClientRect(hWnd, &rc);
if (background && !bkImageFile.empty())
{
delete background; background = new IMAGE;
loadimage(background, bkImageFile.c_str(), rc.right - rc.left, rc.bottom - rc.top, true);
putimage(0, 0, background);
}
else { setbkcolor(wBkcolor); cleardevice(); }
width = rc.right - rc.left; height = rc.bottom - rc.top;
// 通知控件/对话框
for (auto& c : controls)
c->onWindowResize();
for (auto& d : dialogs)
if (auto* dd = dynamic_cast<Dialog*>(d.get()))
dd->setInitialization(true); // 强制对话框在新尺寸下重建布局/快照
// 重绘
for (auto& c : controls) c->draw();
for (auto& d : dialogs) d->draw();
EndBatchDraw();
SendMessage(hWnd, WM_SETREDRAW, TRUE, 0);
// 原来是 TRUE会擦背景再触发 WM_PAINT容易把刚画好的层“盖掉”
// InvalidateRect(hWnd, nullptr, TRUE);
InvalidateRect(hWnd, nullptr, FALSE);
needResizeDirty = false;
}
void Window::scheduleResizeFromModal(int w, int h)
{
if (w < minClientW) w = minClientW;
if (h < minClientH) h = minClientH;
if (w > 10000) w = 10000;
if (h > 10000) h = 10000;
if (w != width || h != height)
{
pendingW = w;
pendingH = h;
needResizeDirty = true; // 交给 pumpResizeIfNeeded 做统一收口+重绘
}
}