Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7b087e296a | |||
| 8dee285de8 | |||
| d16b9bc226 | |||
| 270c6f5293 | |||
| 4bb0352088 | |||
|
|
c4852d080f | ||
|
|
dcf13895da | ||
|
|
95149238e2 | ||
|
|
3509b2bc39 | ||
|
|
26f30cee39 | ||
|
|
741987e48b | ||
|
|
9226deaf85 | ||
|
|
9f2b175b17 |
84
.gitignore
vendored
84
.gitignore
vendored
@@ -1,25 +1,13 @@
|
||||
# 编译生成文件
|
||||
*.exe
|
||||
*.ilk
|
||||
*.obj
|
||||
*.pdb
|
||||
*.o
|
||||
*.a
|
||||
*.lib
|
||||
*.dll
|
||||
*.exp
|
||||
*.so
|
||||
*.dylib
|
||||
# 编译输出目录
|
||||
/build/
|
||||
/bin/
|
||||
/lib/
|
||||
|
||||
# 构建目录
|
||||
build/
|
||||
bin/
|
||||
obj/
|
||||
Debug/
|
||||
Release/
|
||||
x64/
|
||||
x86/
|
||||
Win32/
|
||||
# CMake 生成文件
|
||||
/CMakeFiles/
|
||||
/CMakeCache.txt
|
||||
/cmake_install.cmake
|
||||
/Makefile
|
||||
|
||||
# Visual Studio 文件
|
||||
.vs/
|
||||
@@ -27,51 +15,15 @@ Win32/
|
||||
*.vcxproj
|
||||
*.vcxproj.filters
|
||||
*.vcxproj.user
|
||||
*.suo
|
||||
*.ncb
|
||||
*.user
|
||||
*.sdf
|
||||
ipch/
|
||||
*.aps
|
||||
*.res
|
||||
*.tlog
|
||||
*.lastbuildstate
|
||||
*.idb
|
||||
*.pch
|
||||
|
||||
# CMake 生成文件
|
||||
CMakeFiles/
|
||||
CMakeCache.txt
|
||||
cmake_install.cmake
|
||||
Makefile
|
||||
*.cmake
|
||||
*.cmake.*
|
||||
CTestTestfile.cmake
|
||||
Testing/
|
||||
_deps/
|
||||
|
||||
# 文档生成
|
||||
docs/html/
|
||||
docs/latex/
|
||||
docs/man/
|
||||
docs/rtf/
|
||||
docs/xml/
|
||||
# 编译产物
|
||||
*.obj
|
||||
*.pdb
|
||||
*.log
|
||||
*.exe
|
||||
*.dll
|
||||
*.lib
|
||||
|
||||
# 临时文件
|
||||
*.tmp
|
||||
*.temp
|
||||
*.log
|
||||
*.swp
|
||||
*.swo
|
||||
|
||||
# EasyX 临时文件
|
||||
*.jpg
|
||||
*.bmp
|
||||
*.png
|
||||
*.tga
|
||||
|
||||
# 其他
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
ehthumbs.db
|
||||
Desktop.ini
|
||||
*.cache
|
||||
*.zip
|
||||
|
||||
2028
API Documentation.en.md
Normal file
2028
API Documentation.en.md
Normal file
File diff suppressed because it is too large
Load Diff
255
CHANGELOG.en.md
Normal file
255
CHANGELOG.en.md
Normal file
@@ -0,0 +1,255 @@
|
||||
# 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.2.1] - 2025-11-04
|
||||
|
||||
==This release is a hotfix for v2.2.0==
|
||||
|
||||
### ✅ Fixed
|
||||
|
||||
- The `TabControl` class overrode the base class's `setDirty` method to ensure synchronized update status between the tab and its page list.
|
||||
- The `Canvas` container, special container `TabControl`, and dialog `Dialog` overrode the `requestRepaint` method. When control bubbling propagates upward, the parent pointer is passed. Repaint requests now only bubble up one level to the parent and no longer propagate to the root. Furthermore, the entire parent container is no longer repainted; instead, the parent container repaints only the dirtied child controls, avoiding flickering caused by frequent repaints of the entire container.
|
||||
- The `saveBackground` and `restoreBackground` methods were overridden in `Dialog` to ensure no border remnants remain after the dialog is closed.
|
||||
|
||||
### ⚙️ Changed
|
||||
|
||||
- Completely disabled copy and move semantics for the `Control` class:
|
||||
`Control(const Control&) = delete;`
|
||||
`Control& operator=(const Control&) = delete;`
|
||||
`Control(Control&&) = delete;`
|
||||
`Control& operator=(Control&&) = delete;`
|
||||
|
||||
## [v2.2.0] - 2025-11-02
|
||||
|
||||
**Highlights**: Officially introduces the TabControl, enhances control show/hide and layout responsiveness, and refines the text styling mechanism; fixes several UI details to improve stability.
|
||||
|
||||
### ✨ Added
|
||||
|
||||
- **TabControl (tabbed container control)**: Added the `TabControl` class to implement a multi-page tabbed UI. The tab bar supports **top/bottom/left/right** positions via `TabControl::setTabPlacement(...)`. Provides `TabControl::add(std::pair<std::unique_ptr<Button>, std::unique_ptr<Canvas>>&&)` to add a “tab button + page content” pair in one go and automatically manage switching. Each page’s content area is hosted by a `Canvas`; clicking different tabs (Button, TOGGLE mode) switches the visible page **(see API)**. TabControl can automatically adjust tab layout when the window size changes and uses background snapshots to avoid ghosting under transparent themes.
|
||||
- **Control visibility**: All controls now support runtime **show/hide** toggling. The base class adds `Control::setIsVisible(bool)` to control a control’s own visibility; hidden controls no longer participate in drawing and events. Container controls (`Canvas`) override this to implement **cascading hide**: hiding a container automatically hides all its child controls, and showing it restores them. This makes it more convenient to toggle the visibility of a group of UI elements.
|
||||
- **Window resize handling**: Introduces the virtual function `Control::onWindowResize()`, called when the parent window size changes. Controls can implement this to respond to window resizing (e.g., reset background caches, adjust layout). `Canvas` implements this and **recursively notifies child controls** to call `onWindowResize()`, ensuring nested layouts update correctly. This improvement fixes possible misalignment or background issues that occurred after resizing.
|
||||
- **Label text style structure**: The `Label` control now uses a unified `ControlText` style structure to manage fonts and colors. By accessing the public member `Label::textStyle`, you can set font name, size, color, underline, etc., achieving **full text style customization** (replacing the previous `setTextColor` interface). This enables richer formatting when displaying text with Label.
|
||||
- **Dialog de-duplication mechanism**: `Window` adds an internal check to **prevent popping up duplicate dialogs with the same content**. When using `MessageBox::showAsync` for a non-modal popup, the framework checks if a dialog with the same title and message is already open; if so, it avoids creating another one. This prevents multiple identical prompts from appearing in quick succession.
|
||||
|
||||
### ⚙️ Changed
|
||||
|
||||
- **Text color interface adjustment**: `Label` removes the outdated `setTextColor` method; use the public `textStyle.color` to set text color instead. To change a Label’s text color, modify `label.textStyle.color` and redraw. This improves consistency in text property management but may be incompatible with older code and require replacement.
|
||||
- **Tooltip styling and toggle**: The `Button` Tooltip interface is adjusted to support more customization. `Button::setTooltipStyle` can now flexibly set tooltip text color, background color, and transparency; `setTooltipTextsForToggle(onText, offText)` is added so toggle buttons can display different tooltip texts in **ON/OFF** states. The original tooltip-text setting interface remains compatible, but the internal implementation is optimized, fixing the previous issue where toggle-button tooltip text didn’t update.
|
||||
- **Control coordinate system and layout**: Controls now maintain both **global coordinates** and **local coordinates**. `Control` adds members (such as `getLocalX()/getLocalY()`) to get positions relative to the parent container, and a `parent` pointer to the parent container. This makes layout calculations in nested containers more convenient and accurate. In absolute-layout scenarios, a control’s global coordinates are automatically converted and stored as local coordinates when added to a container. Note: in the new version, when moving controls or changing sizes, prefer using the control’s own setters to keep internal coordinates in sync.
|
||||
- **Window resizing defaults**: Resizing is changed from experimental to **enabled by default**. The framework always enables resizable styles for the main window (`WS_THICKFRAME | WS_MAXIMIZEBOX | ...`), so there’s no need to call a separate method to enable it. This simplifies usage and means created windows can be resized by users by default. For scenarios where resizing is not desired, pass specific mode flags at creation time to disable it.
|
||||
|
||||
### ✅ Fixed
|
||||
|
||||
- **Toggle-button tooltip updates**: Fixed an issue where tooltips for toggle-mode buttons did not update promptly when the state changed. With `setTooltipTextsForToggle`, the tooltip now correctly shows the corresponding text when switching between **ON/OFF**.
|
||||
- **Background ghosting and coordinate sync for controls**: Fixed defects where, in some cases, control backgrounds were not refreshed in time and position calculations deviated after window/container resizing. By using `onWindowResize` to uniformly discard and update background snapshots, background ghosting and misalignment during window resizing are avoided, improving overall stability.
|
||||
- **`Control` class background-snapshot memory leak**: The destructor now calls `discardBackground` to free and restore the background snapshot, preventing the memory leak caused by not releasing `*saveBkImage` in the previous version.
|
||||
- **Duplicate dialog pop-ups**: Fixed an issue where repeatedly calling a non-modal message box in quick succession could create multiple identical dialogs. The new de-duplication check ensures that only one non-modal dialog with the same content exists at a time, avoiding UI disruption.
|
||||
- **Other**: Optimized the control drawing/refresh strategy to reduce unnecessary repaints in certain scenarios and improve performance; corrected minor memory-management details to eliminate potential leaks. These enhancements further improve the framework’s performance and reliability.
|
||||
|
||||
## [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
|
||||
|
||||
- **Bilingual API Documentation (Chinese and English)**
|
||||
- The documentation provides a detailed introduction of each class, including API descriptions, functionalities, and points to note, with a comprehensive explanation of each control.
|
||||
|
||||
- **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 isn’t 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 don’t 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
|
||||
216
CHANGELOG.md
216
CHANGELOG.md
@@ -5,6 +5,222 @@ StellarX 项目所有显著的变化都将被记录在这个文件中。
|
||||
格式基于 [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
并且本项目遵循 [语义化版本](https://semver.org/lang/zh-CN/)。
|
||||
|
||||
[English document](CHANGELOG.en.md)
|
||||
|
||||
## [v2.2.1] - 2025 - 11 - 4
|
||||
|
||||
==此版本为v2.2.0的修复版本==
|
||||
|
||||
### ✅ 修复
|
||||
|
||||
- `TabControl`类重写了基类的`setDirty`方法保证页签+页列表同步更新
|
||||
|
||||
状态
|
||||
|
||||
- `Canvas`容器和特殊容器`TabControl`以及对话框`Dialog`重写`requestRepaint`方法,控件向上冒泡时传递父指针,请求重绘时只向上到父一级,不再传递到根。并且不再重绘整个父容器,而是由父容器重绘标脏的子控件,避免了频繁真个容器重绘导致的频闪
|
||||
|
||||
- `Dialog`中重写了`saveBackground`和`restBackground`方法,保证对话框关闭后不会有边框残留
|
||||
|
||||
### ⚙️ 变更
|
||||
|
||||
- 彻底禁用`Control`的移动构造`Control(const Control&) = delete;`
|
||||
`Control& operator=(const Control&) = delete;`
|
||||
`Control(Control&&) = delete;`
|
||||
`Control& operator=(Control&&) = delete;`
|
||||
|
||||
## [v2.2.0] - 2025-11-02
|
||||
|
||||
**重点**:正式引入选项卡控件(TabControl),增强控件显隐与布局响应能力,并完善文本样式机制;修复若干UI细节问题以提升稳定性。
|
||||
|
||||
### ✨ 新增
|
||||
|
||||
- **选项卡容器控件 TabControl**:新增 `TabControl` 类,实现多页面选项卡界面。支持页签栏在 **上/下/左/右** 四种位置排列,可通过 `TabControl::setTabPlacement(...)` 设置。提供 `TabControl::add(std::pair<std::unique_ptr<Button>, std::unique_ptr<Canvas>>&&)` 接口,一次性添加“页签按钮+页面内容”对,并自动管理切换显示。各页内容区域由 `Canvas` 承载,点击不同页签(Button,TOGGLE模式)将切换显示对应页面**【见 API】**。TabControl 在窗口大小变化时可自动调整页签布局,并使用背景快照避免透明主题下的叠影问题。
|
||||
- **控件显隐控制**:所有控件现支持运行时**显示/隐藏**切换。基类新增方法 `Control::setIsVisible(bool)` 控制自身可见性,隐藏后控件不参与绘制和事件。容器控件(`Canvas`)重写该方法,实现**级联隐藏**:隐藏容器将自动隐藏其中所有子控件,再次显示时子控件也随之恢复。这使一组界面元素的显隐切换更加方便。
|
||||
- **窗口尺寸变化响应**:引入 `Control::onWindowResize()` 虚函数,当父窗口尺寸改变时调用。各控件可通过实现此函数响应窗口大小调整,例如重置背景缓存、调整布局等。`Canvas` 已实现该函数,会在窗口变化时**递归通知子控件**调用 `onWindowResize()`,确保嵌套布局能正确更新。此改进解决了之前窗口拉伸后子控件可能位置错位或背景异常的问题。
|
||||
- **Label 文本样式结构**:`Label` 控件现在使用统一的 `ControlText` 样式结构管理字体和颜色。可通过访问公有成员 `Label::textStyle` 设置字体名称、大小、颜色、下划线等属性,实现**完整文本样式定制**(替代原先的 setTextColor 接口)。这使 Label 在展示文本时支持更丰富的格式。
|
||||
- **Dialog 防重复机制**:`Window` 新增内部检查函数,**防止重复弹出相同内容的对话框**。在使用 `MessageBox::showAsync` 非模态弹窗时,框架会判断是否已有相同标题和消息的对话框未关闭,若是则避免再次创建。此机制杜绝了短时间内弹出多个相同提示的情况。
|
||||
|
||||
### ⚙️ 变更
|
||||
|
||||
- **文本颜色接口调整**:`Label` 移除了过时的 `setTextColor` 方法,改为通过公有的 `textStyle.color` 设置文字颜色。如需改变 Label 文本颜色,请直接修改 `label.textStyle.color` 并重绘。此改动提升了文本属性管理的一致性,但可能对旧版代码不兼容,需要做相应替换。
|
||||
- **Tooltip 样式与切换**:`Button` 的悬停提示 (Tooltip) 接口调整为支持更多自定义。`Button::setTooltipStyle` 现在可灵活设置提示文字颜色、背景色及透明模式;新增 `setTooltipTextsForToggle(onText, offText)` 用于切换按钮在 **ON/OFF** 两种状态下显示不同的提示文字。原有 Tooltip 文案设置接口仍兼容,但内部实现优化,修正了先前切换按钮提示文字不更新的问题。
|
||||
- **控件坐标系与布局**:控件现在同时维护**全局坐标**和**局部坐标**。`Control` 新增成员(如 `getLocalX()/getLocalY()` 等)用于获取控件相对父容器的位置,以及 `parent` 指针指向父容器。这一改进使得嵌套容器布局计算更加便捷和准确。在绝对布局场景下,控件的全局坐标会在添加进容器时自动转换为本地坐标保存。开发者需要注意,新版中移动控件或调整尺寸应优先使用控件自带的 setter,以确保内部坐标同步更新。
|
||||
- **默认窗口调整**:窗口拉伸功能从实验转为默认支持。框架始终为主窗口启用了可调整大小的样式(`WS_THICKFRAME|WS_MAXIMIZEBOX|...`),不再需要调用独立的方法启用。这一变更简化了使用,同时意味着创建的窗口默认可以被用户拖拽拉伸。对于不希望窗口缩放的场景,可在创建窗口时通过传递特定模式标志来禁止。
|
||||
|
||||
### ✅ 修复
|
||||
|
||||
- **切换按钮 Tooltip 更新**:修正了切换模式按钮在状态改变时悬停提示未及时更新的问题。现在使用 `setTooltipTextsForToggle` 设置不同提示后,按钮在 **ON/OFF** 状态切换时悬停提示文字会正确显示对应内容。
|
||||
- **控件背景残影与坐标同步**:解决了某些情况下窗口或容器尺寸变化后控件背景未及时刷新、位置计算偏差的缺陷。利用 `onWindowResize` 统一丢弃并更新背景快照,避免了拉伸窗口时可能出现的控件背景残影和错位现象,界面稳定性提升。
|
||||
- **`Control`类背景快照内存泄漏**:在析构函数里调用了`discardBackground`释放并恢复背景快照,避免了上一版本未释放`*saveBkImage`造成的内存泄漏
|
||||
- **重复对话框弹出**:修复了快速重复调用非模态消息框可能出现多个相同对话框的问题。新增的去重判断保证相同内容的非模态对话框同一时间只会存在一个,避免用户界面受到干扰。
|
||||
- **其他**:优化了控件绘制刷新策略,减少某些场景下的不必要重绘,提升运行效率;修正少量内存管理细节以消除潜在泄漏。上述改进进一步提高了框架的性能与可靠性。
|
||||
|
||||
## [v2.1.0] - 2025-10-27
|
||||
|
||||
**重点**:窗口可拉伸/最大化补强(EasyX + Win32)、布局管理器(HBox/VBox 第一阶段)、选项卡控件雏形;系统性修复黑边、最大化残影、频闪与“控件需交互才出现”等历史问题。并统一了**背景快照/恢复**、**按钮单行截断**、**Tooltip** 的机制。
|
||||
|
||||
### ✨ 新增
|
||||
|
||||
- **中英文双语 API文档**
|
||||
- 文档详细介绍了每个类,以及API描述、功能和需要注意的地方,详细介绍了每个控件
|
||||
|
||||
- **窗口拉伸 / 最大化补强(在 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
|
||||
|
||||
### 概述
|
||||
v2.0.0 为一次重大升级(major release)。本次发布新增对话框与消息框工厂(Dialog / MessageBox),并对事件分发、API语义与内部资源管理做了若干重要修复和改进。
|
||||
|
||||
部分 API/行为发生不向后兼容的变化(breaking changes)。
|
||||
|
||||
### 新增
|
||||
- **对话框系统**:
|
||||
- 新增 `Dialog` 类,继承自 `Canvas`,用于构建模态与非模态对话框
|
||||
- 新增 `MessageBox` 工厂类,提供 `ShowModal`(同步阻塞)与 `ShowAsync`(异步回调)两种便捷API
|
||||
- 支持六种标准消息框类型:`OK`, `OKCancel`, `YesNo`, `YesNoCancel`, `RetryCancel`, `AbortRetryIgnore`
|
||||
- 自动处理对话框布局、背景保存与恢复、事件传播和生命周期管理
|
||||
|
||||
- **事件系统增强**:
|
||||
- 所有控件的 `handleEvent` 方法现在返回 `bool` 类型,表示是否消费了事件
|
||||
- 引入事件消费机制,支持更精细的事件传播控制
|
||||
- Window 类的事件循环现在优先处理对话框事件
|
||||
|
||||
- **控件状态管理**:
|
||||
- Control 基类新增 `dirty` 标志和 `setDirty()` 方法,统一管理重绘状态
|
||||
- 所有控件现在都实现了 `IsVisible()` 和 `model()` 方法
|
||||
|
||||
- **API 增强**:
|
||||
- Button 类新增 `setButtonFalseColor()` 方法
|
||||
- TextBox 类的 `setMaxCharLen()` 现在接受 `size_t` 类型参数
|
||||
- Window 类新增对话框管理方法和去重检测机制
|
||||
|
||||
### 重大变更(Breaking Changes)
|
||||
- **API 签名变更**:
|
||||
- 所有控件的 `handleEvent(const ExMessage& msg)` 方法从返回 `void` 改为返回 `bool`
|
||||
- Control 基类新增纯虚函数 `IsVisible() const` 和 `model() const`,所有派生类必须实现
|
||||
|
||||
- **资源管理变更**:
|
||||
- Control 基类的样式保存从栈对象改为堆对象,使用指针管理
|
||||
- 增强了资源释放的安全性,所有资源都在析构函数中正确释放
|
||||
|
||||
- **事件处理逻辑**:
|
||||
- Window 的 `runEventLoop()` 方法完全重写,现在优先处理对话框事件
|
||||
- 引入了事件消费机制,事件被消费后不会继续传播
|
||||
|
||||
### 修复(Fixed)
|
||||
- **内存管理**:
|
||||
- 修复 `Button::setFillIma()` 的内存泄漏问题
|
||||
- 修复 Control 基类析构函数中的资源释放问题
|
||||
- 修复 Dialog 类背景图像资源管理问题
|
||||
|
||||
- **布局与渲染**:
|
||||
- 修复 `Table` 组件的分页计算、列宽和行高越界问题
|
||||
- 修复 `Table` 中 `pX` 坐标累加错误导致的布局错乱
|
||||
- 修复 `Canvas::draw()` 中导致子控件不被绘制的 dirty 判定问题
|
||||
- 修复 `TextBox::draw()` 和 `restoreStyle()` 的不对称调用问题
|
||||
|
||||
- **事件处理**:
|
||||
- 修复窗口事件分发逻辑,确保对话框/顶层控件优先处理事件
|
||||
- 修复鼠标移出按钮区域时状态更新不及时的问题
|
||||
- 修复非模态对话框事件处理中的竞争条件
|
||||
|
||||
- **其他问题**:
|
||||
- 修复文本测量和渲染中的潜在错误
|
||||
- 修复对话框关闭后背景恢复不完全的问题
|
||||
- 修复多对话框场景下的 z-order 管理问题
|
||||
|
||||
### 改进(Changed)
|
||||
- **代码质量**:
|
||||
- 重构了大量内部 API,增强异常安全性与可维护性
|
||||
- 使用智能指针和现代 C++ 特性替代裸 new/delete
|
||||
- 统一了代码风格和命名约定
|
||||
|
||||
- **性能优化**:
|
||||
- 优化了事件处理流程,减少不必要的重绘
|
||||
- 改进了对话框背景保存和恢复机制
|
||||
- 减少了内存分配和拷贝操作
|
||||
|
||||
- **文档与示例**:
|
||||
- 为所有新增功能添加了详细的使用示例
|
||||
- 完善了代码注释和 API 文档
|
||||
- 更新了 README 和 CHANGELOG 反映最新变化
|
||||
|
||||
## [1.1.0] - 2025-09-08
|
||||
|
||||
### 新增
|
||||
- **Window 类 API 增强**:
|
||||
- 添加了完整的获取器(getter)方法集,提高类的封装性和可用性
|
||||
- `getHwnd()` - 获取窗口句柄,便于与原生 Windows API 集成
|
||||
- `getWidth()` - 获取窗口宽度
|
||||
- `getHeight()` - 获取窗口高度
|
||||
- `getHeadline()` - 获取窗口标题
|
||||
- `getBkcolor()` - 获取窗口背景颜色
|
||||
- `getBkImage()` - 获取窗口背景图片指针
|
||||
- `getControls()` - 获取控件管理容器的引用,允许迭代和操作已添加的控件
|
||||
|
||||
### 改进
|
||||
- **API 一致性**: 为所有重要属性提供了对称的设置器(setter)和获取器(getter)方法
|
||||
- **代码文档**: 进一步完善了类注释,使其更加清晰和专业
|
||||
|
||||
## [1.0.0] - 2025-09-08
|
||||
|
||||
### 发布摘要
|
||||
|
||||
135
CMakeLists.txt
135
CMakeLists.txt
@@ -1,130 +1,19 @@
|
||||
cmake_minimum_required(VERSION 3.15)
|
||||
project(StellarX VERSION 1.0.0 LANGUAGES CXX)
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
# 项目定义
|
||||
project(StellarX VERSION 2.0.0 LANGUAGES CXX)
|
||||
|
||||
# 设置 C++ 标准
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||
|
||||
# Windows特定设置
|
||||
if(WIN32)
|
||||
add_definitions(-DWIN32 -D_WINDOWS)
|
||||
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
|
||||
# 包含头文件目录(目前头文件都在根目录)
|
||||
include_directories(${CMAKE_SOURCE_DIR})
|
||||
|
||||
# 设置Windows子系统
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SUBSYSTEM:WINDOWS")
|
||||
endif()
|
||||
|
||||
# 设置输出目录
|
||||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
|
||||
# 包含目录
|
||||
include_directories(include)
|
||||
|
||||
# Windows API库
|
||||
if(WIN32)
|
||||
find_library(GDI32_LIBRARY gdi32)
|
||||
find_library(USER32_LIBRARY user32)
|
||||
find_library(KERNEL32_LIBRARY kernel32)
|
||||
set(WIN32_LIBS ${GDI32_LIBRARY} ${USER32_LIBRARY} ${KERNEL32_LIBRARY})
|
||||
endif()
|
||||
|
||||
# 创建库
|
||||
file(GLOB_RECURSE SOURCES "src/*.cpp")
|
||||
add_library(StellarX STATIC ${SOURCES})
|
||||
|
||||
# 链接Windows库
|
||||
if(WIN32)
|
||||
target_link_libraries(StellarX ${WIN32_LIBS})
|
||||
endif()
|
||||
|
||||
# 设置库属性
|
||||
set_target_properties(StellarX PROPERTIES
|
||||
VERSION ${PROJECT_VERSION}
|
||||
SOVERSION 1
|
||||
PUBLIC_HEADER "include/StellarX/StellarX.h"
|
||||
OUTPUT_NAME "StellarX"
|
||||
# 源文件收集
|
||||
file(GLOB_RECURSE SOURCES
|
||||
"${CMAKE_SOURCE_DIR}/*.cpp"
|
||||
)
|
||||
|
||||
# 安装规则
|
||||
install(DIRECTORY include/StellarX DESTINATION include)
|
||||
install(TARGETS StellarX
|
||||
ARCHIVE DESTINATION lib
|
||||
LIBRARY DESTINATION lib
|
||||
RUNTIME DESTINATION bin
|
||||
PUBLIC_HEADER DESTINATION include/StellarX
|
||||
)
|
||||
|
||||
# 示例程序
|
||||
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/examples)
|
||||
file(GLOB EXAMPLE_SOURCES "examples/*.cpp")
|
||||
foreach(example_source ${EXAMPLE_SOURCES})
|
||||
get_filename_component(example_name ${example_source} NAME_WE)
|
||||
add_executable(${example_name} ${example_source})
|
||||
target_link_libraries(${example_name} StellarX ${WIN32_LIBS})
|
||||
|
||||
# 设置Windows子系统
|
||||
if(WIN32)
|
||||
set_target_properties(${example_name} PROPERTIES
|
||||
LINK_FLAGS "/SUBSYSTEM:WINDOWS"
|
||||
)
|
||||
endif()
|
||||
|
||||
# 安装示例
|
||||
install(TARGETS ${example_name} DESTINATION bin)
|
||||
endforeach()
|
||||
endif()
|
||||
|
||||
# 文档生成选项
|
||||
option(BUILD_DOCS "Build documentation" OFF)
|
||||
if(BUILD_DOCS)
|
||||
find_package(Doxygen)
|
||||
if(Doxygen_FOUND)
|
||||
set(DOXYGEN_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/docs)
|
||||
set(DOXYGEN_USE_MDFILE_AS_MAINPAGE ${CMAKE_CURRENT_SOURCE_DIR}/README.md)
|
||||
set(DOXYGEN_PROJECT_NAME "StellarX GUI Framework")
|
||||
set(DOXYGEN_PROJECT_NUMBER ${PROJECT_VERSION})
|
||||
set(DOXYGEN_PROJECT_BRIEF "A lightweight, modular C++ GUI framework for Windows")
|
||||
set(DOXYGEN_INPUT ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/src)
|
||||
set(DOXYGEN_RECURSIVE YES)
|
||||
set(DOXYGEN_EXTRACT_ALL YES)
|
||||
set(DOXYGEN_EXTRACT_PRIVATE YES)
|
||||
set(DOXYGEN_EXTRACT_STATIC YES)
|
||||
set(DOXYGEN_SOURCE_BROWSER YES)
|
||||
set(DOXYGEN_GENERATE_TREEVIEW YES)
|
||||
set(DOXYGEN_HAVE_DOT YES)
|
||||
set(DOXYGEN_CALL_GRAPH YES)
|
||||
set(DOXYGEN_CALLER_GRAPH YES)
|
||||
|
||||
doxygen_add_docs(
|
||||
docs
|
||||
${DOXYGEN_INPUT}
|
||||
COMMENT "Generate HTML documentation"
|
||||
)
|
||||
|
||||
install(DIRECTORY ${DOXYGEN_OUTPUT_DIRECTORY}/html DESTINATION docs)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# 打包配置
|
||||
include(InstallRequiredSystemLibraries)
|
||||
set(CPACK_PACKAGE_NAME ${PROJECT_NAME})
|
||||
set(CPACK_PACKAGE_VERSION ${PROJECT_VERSION})
|
||||
set(CPACK_PACKAGE_DESCRIPTION "A lightweight, modular C++ GUI framework for Windows")
|
||||
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "星垣 (StellarX) - A lightweight Windows GUI framework")
|
||||
set(CPACK_PACKAGE_VENDOR "StellarX Development Team")
|
||||
set(CPACK_PACKAGE_CONTACT "contact@stellarx.dev")
|
||||
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
|
||||
set(CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
|
||||
set(CPACK_PACKAGE_INSTALL_DIRECTORY "StellarX")
|
||||
set(CPACK_NSIS_MODIFY_PATH ON)
|
||||
|
||||
if(WIN32)
|
||||
set(CPACK_GENERATOR "ZIP;NSIS")
|
||||
else()
|
||||
set(CPACK_GENERATOR "ZIP")
|
||||
endif()
|
||||
|
||||
include(CPack)
|
||||
# 生成可执行文件
|
||||
add_executable(StellarX ${SOURCES})
|
||||
|
||||
94
CONTRIBUTING.en.md
Normal file
94
CONTRIBUTING.en.md
Normal 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.
|
||||
|
||||
298
README.en.md
Normal file
298
README.en.md
Normal file
@@ -0,0 +1,298 @@
|
||||
# StellarX GUI Framework README
|
||||
|
||||
[中文README](README.md)
|
||||
|
||||
------
|
||||
|
||||

|
||||
[](https://github.com/Ysm-04/StellarX)
|
||||
|
||||

|
||||

|
||||
|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||
|
||||
> **“Bounded by the stars, light as dust.”** — An ultra-lightweight, highly modular, native C++ GUI framework for Windows.
|
||||
|
||||
`StellarX` rejects bloat: no hundreds-of-MB dependencies, no marathon builds, and no steep learning curve. Back to the essence—clean code, clear architecture, and high efficiency to solve the core needs of desktop app development.
|
||||
|
||||
This is a **teaching-grade and tooling-grade** framework that helps developers understand GUI fundamentals and quickly build lightweight utilities.
|
||||
|
||||
------
|
||||
|
||||
## **🆕 v2.2.1 (Hotfix for v2.2.0)**
|
||||
|
||||
- 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.
|
||||
|
||||
For details, please refer to the [CHANGELOG.en](CHANGELOG.en.md).
|
||||
|
||||
## What’s new in v2.2.0
|
||||
|
||||
- **New TabControl for multi-page tabbed UIs:** With `TabControl`, it’s 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
|
||||
|
||||
StellarX adopts classic **OOP** and **modular** design with a clear structure:
|
||||
|
||||
```
|
||||
StellarX/
|
||||
├── include/
|
||||
│ └── StellarX/
|
||||
│ ├── StellarX.h
|
||||
│ ├── CoreTypes.h # single source of truth (enums/structs)
|
||||
│ ├── Control.h
|
||||
│ ├── Button.h
|
||||
│ ├── Window.h
|
||||
│ ├── Label.h
|
||||
│ ├── TextBox.h
|
||||
│ ├── TabControl.h #v2.2.0
|
||||
│ ├── Canvas.h
|
||||
│ ├── Dialog.h
|
||||
│ ├── MessageBox.h
|
||||
│ └── Table.h
|
||||
├── src/
|
||||
│ ├── Control.cpp
|
||||
│ ├── Button.cpp
|
||||
│ ├── Window.cpp
|
||||
│ ├── Label.cpp
|
||||
│ ├── TextBox.cpp
|
||||
│ ├── Canvas.cpp
|
||||
│ ├── TabControl.cpp #v2.2.0
|
||||
│ ├── Table.cpp
|
||||
│ ├── Dialog.cpp
|
||||
│ └── MessageBox.cpp
|
||||
├── examples/
|
||||
│ └── demo.cpp
|
||||
├── docs/
|
||||
│ └── CODE_OF_CONDUCT.md
|
||||
├── CMakeLists.txt
|
||||
├── CONTRIBUTING.md
|
||||
├── CHANGELOG.md
|
||||
├── CHANGELOG.en.md
|
||||
├── Doxyfile
|
||||
├── LICENSE
|
||||
├──API 文档.md
|
||||
├──API Documentation.en.md
|
||||
└── README.md
|
||||
```
|
||||
|
||||
**Design Philosophy:**
|
||||
|
||||
1. **Single Responsibility (SRP):** each class/file does exactly one thing.
|
||||
2. **Dependency Inversion (DIP):** high-level modules depend on abstractions (`Control`), not concrete controls.
|
||||
3. **Open/Closed (OCP):** extend by inheriting from `Control` without modifying existing code.
|
||||
4. **Consistency:** unified `draw()` / `handleEvent()` across all controls.
|
||||
|
||||
## 🚀 Core Features
|
||||
|
||||
- **Ultra-lightweight:** no heavyweight external dependencies besides EasyX.
|
||||
- **Clear modules:** `CoreTypes.h` unifies types and enums.
|
||||
- **Native performance:** EasyX + Win32 for efficient execution and low memory (often <10 MB).
|
||||
- **Complete control set:** Button, Label, TextBox, Canvas, Table, Dialog, MessageBox, **TabControl**.
|
||||
- **Highly customizable:** colors; shapes (rectangle/rounded/circle/ellipse); fills; fonts—switchable via enums.
|
||||
- **Simple, intuitive API:** OOP design with clear semantics—code as documentation.
|
||||
- **Standard project layout:** split `include/src`, CMake-friendly, easy to integrate or use out of the box.
|
||||
|
||||
------
|
||||
|
||||
## ⚡ Quick Start (5 minutes)
|
||||
|
||||
> Get the prebuilt package from [Releases](https://github.com/Ysm-04/StellarX/releases/latest).
|
||||
|
||||
### Requirements
|
||||
|
||||
- **OS:** Windows 10+
|
||||
- **Compiler:** C++17 (e.g., VS 2019+)
|
||||
- **Graphics:** [EasyX](https://easyx.cn/) 2022+ (matching your compiler)
|
||||
- **Build:** CMake 3.12+ (optional)
|
||||
|
||||
### Install EasyX
|
||||
|
||||
1. Download the latest EasyX
|
||||
2. Install components matching your Visual Studio version
|
||||
3. The framework links automatically—no extra config needed
|
||||
|
||||
### Build with CMake (recommended)
|
||||
|
||||
```
|
||||
git clone https://github.com/Ysm-04/StellarX.git
|
||||
cd StellarX
|
||||
mkdir build && cd build
|
||||
cmake ..
|
||||
cmake --build .
|
||||
./examples/Demo
|
||||
```
|
||||
|
||||
### Manual Integration
|
||||
|
||||
- Copy `include` and `src`
|
||||
- Add header search path: `include/StellarX/`
|
||||
- Add all `.cpp` files to your project
|
||||
|
||||
### First Resizable Window
|
||||
|
||||
```
|
||||
#include "StellarX.h"
|
||||
|
||||
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
|
||||
{
|
||||
// Resizing enabled by default; current size is the minimum size
|
||||
Window mainWindow(800, 600, 0, RGB(255,255,255), "My StellarX App");
|
||||
mainWindow.draw();
|
||||
|
||||
// Add your controls...
|
||||
// mainWindow.addControl(std::move(btn));
|
||||
|
||||
mainWindow.runEventLoop();
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
> Implementation note: perform **full-window background drawing** (solid/image) during `WM_PAINT`, and combine with EasyX batch drawing to suppress flicker and black edges.
|
||||
|
||||
------
|
||||
|
||||
## 📚 Core Types (excerpt from `CoreTypes.h`)
|
||||
|
||||
### Enums
|
||||
|
||||
| Enum | Description | Common values |
|
||||
| ------------------ | ---------------- | ------------------------------------------------------------ |
|
||||
| `ControlShape` | Geometric shape | `RECTANGLE`, `B_RECTANGLE`, `ROUND_RECTANGLE`, `CIRCLE`, `ELLIPSE` |
|
||||
| `ButtonMode` | Button behavior | `NORMAL`, `TOGGLE`, `DISABLED` |
|
||||
| `TextBoxMode` | TextBox mode | `INPUT_MODE`, `READONLY_MODE` |
|
||||
| `FillMode` | Fill mode | `SOLID`, `NULL`, `HATCHED` |
|
||||
| `FillStyle` | Pattern style | `HORIZONTAL`, `CROSS` |
|
||||
| `LineStyle` | Line style | `SOLID`, `DASH`, `DOT` |
|
||||
| `MessageBoxType` | Message box type | `OK`, `OKCancel`, `YesNo`, ... |
|
||||
| `MessageBoxResult` | Result | `OK`, `Cancel`, `Yes`, `No`, `Abort`, `Retry`, `Ignore` |
|
||||
| `TabPlacement` | Tab position | `Top`, `Bottom`, `Left`, `Right` |
|
||||
|
||||
### Structs
|
||||
|
||||
| Struct | Description |
|
||||
| -------------- | ---------------------------------------------------- |
|
||||
| `ControlText` | Font/size/color/bold/italic/underline/strike-through |
|
||||
| `RouRectangle` | Corner ellipse size for rounded rectangles |
|
||||
|
||||
------
|
||||
|
||||
## 🧩 Controls Library
|
||||
|
||||
### 1) Basic Controls
|
||||
|
||||
| Control | Header | Description | Key Points |
|
||||
| ------- | ----------- | ----------------- | ------------------------------------------------------------ |
|
||||
| Button | `Button.h` | Versatile button | Shapes/modes; hover/pressed colors; callbacks; **single-line truncation** + **Tooltip** (v2.1.0) |
|
||||
| Label | `Label.h` | Text label | Transparent/opaque background; custom fonts |
|
||||
| TextBox | `TextBox.h` | Input/display box | Input/readonly; integrates EasyX `InputBox` |
|
||||
|
||||
### 2) Container Controls
|
||||
|
||||
| Control | Header | Description |
|
||||
| ------- | ---------- | ------------------------------------------------------------ |
|
||||
| Canvas | `Canvas.h` | Parent container with custom border/background; **built-in HBox/VBox auto layout** (v2.1.0) |
|
||||
| Window | `Window.h` | Top-level container with message loop and dispatch; **resizable** (v2.1.0) |
|
||||
|
||||
### 3) Advanced Controls
|
||||
|
||||
| Control | Header | Description | Key Points |
|
||||
| ---------- | -------------- | ----------- | ------------------------------------------------------------ |
|
||||
| Table | `Table.h` | Data grid | Paging/header/auto column width; fixed page-control overlap/ghosting (v2.1.0) |
|
||||
| Dialog | `Dialog.h` | Dialog | Modal/non-modal; auto layout; background save/restore |
|
||||
| TabControl | `TabControl.h` | Tabs | One-click add of “tab + page” pair (pair), or add child controls to a page; uses relative coordinates |
|
||||
|
||||
### 4) Static Factory
|
||||
|
||||
| Control | Header | Description | Key Points |
|
||||
| ---------- | -------------- | ------------------- | -------------------------------------------- |
|
||||
| MessageBox | `MessageBox.h` | Message-box factory | Static API; modal/non-modal; de-dup built in |
|
||||
|
||||
------
|
||||
|
||||
## 📐 Layout Management (HBox/VBox)
|
||||
|
||||
==Reserved, to be implemented==
|
||||
|
||||
------
|
||||
|
||||
## 🗂 Tabs (TabControl)
|
||||
|
||||
- Tab strip (button group) + page container (`Canvas`)
|
||||
- For transparent themes: **background snapshot** switching in the page area to avoid ghosting
|
||||
- API: **see the API documentation**
|
||||
|
||||
------
|
||||
|
||||
## ✂️ Single-line Text Truncation & Button Tooltip
|
||||
|
||||
- **Button truncation:** separate handling for CJK/Latin under MBCS; append `...` based on pixel-width threshold
|
||||
- **Tooltip:** delayed show and auto-hide; default text = button text; customizable; uses control-level **background snapshot/restore**
|
||||
|
||||
------
|
||||
|
||||
## 🧊 Transparent Background & Background Snapshots
|
||||
|
||||
- **General convention:** `captureBackground(rect)` before the first draw; `restoreBackground()` before hiding/covering
|
||||
- **Table:** snapshot region **includes the header**; after page switch, restore immediately + redraw; paging controls centered
|
||||
|
||||
------
|
||||
|
||||
## 🔧 Advanced Topics & Best Practices
|
||||
|
||||
- Custom controls: inherit from `Control`, implement `draw()` / `handleEvent()`
|
||||
- Performance:
|
||||
- **Dirty rectangles:** set `dirty=true` on state changes for on-demand redraw
|
||||
- **Avoid extra `cleardevice()`**: background is centrally handled in `WM_PAINT`
|
||||
- Ensure `SetWorkingImage(nullptr)` before drawing so output goes to the screen
|
||||
- Event consumption: return `true` after handling to stop propagation
|
||||
|
||||
------
|
||||
|
||||
## ⚠️ Applicability & Limits
|
||||
|
||||
- Not suitable for high-performance games or complex animation; re-verify metrics under extreme DPI
|
||||
- No accessibility support yet
|
||||
- Windows-only, not cross-platform
|
||||
- For complex commercial front-ends, consider Qt / wxWidgets / ImGui / Electron
|
||||
|
||||
------
|
||||
|
||||
## 📜 License
|
||||
|
||||
MIT (see `LICENSE`).
|
||||
|
||||
## 👥 Contributing Guidelines
|
||||
|
||||
- Follow the existing C++ style
|
||||
- New features should include examples and README updates
|
||||
- Self-test before submitting and explain the motivation for changes
|
||||
- For bugs/ideas, please open an Issue
|
||||
|
||||
## 🙏 Acknowledgements
|
||||
|
||||
- Thanks to [EasyX](https://easyx.cn/)
|
||||
- Thanks to developers who value **simplicity/efficiency/clarity**
|
||||
|
||||
------
|
||||
|
||||
**Stars and seas, code as the vessel.**
|
||||
|
||||
## 📞 Support & Feedback
|
||||
|
||||
- See [examples/](examples/)
|
||||
- Read the [CHANGELOG](CHANGELOG.md / CHANGELOG.en.md)
|
||||
- Submit an Issue on GitHub
|
||||
440
README.md
440
README.md
@@ -1,7 +1,14 @@
|
||||
# 星垣 (StellarX) GUI Framework
|
||||
|
||||
 <!-- 新增版本徽章 -->
|
||||
 <!-- 新增下载徽章 -->
|
||||
[English document](README.en.md)
|
||||
|
||||
------
|
||||
|
||||

|
||||
[](https://github.com/Ysm-04/StellarX)
|
||||
|
||||

|
||||

|
||||
|
||||

|
||||

|
||||
@@ -12,332 +19,285 @@
|
||||
|
||||
> **「繁星为界,轻若尘埃」** —— 一个为 Windows 平台打造的、极致轻量级、高度模块化的 C++ 原生 GUI 框架。
|
||||
|
||||
`星垣 (StellarX)` 诞生于对现代GUI框架"过度臃肿"的反抗。它拒绝动辄数百MB的依赖、漫长的编译时间和复杂的学习曲线,选择回归本质:用最精简的代码、最清晰的架构和最高的效率,解决桌面应用开发的核心需求。
|
||||
`星垣 (StellarX)` 反对臃肿,拒绝动辄数百 MB 的依赖、漫长编译与高门槛学习曲线,回归本质:以精简代码、清晰架构与高效率,解决桌面应用开发的核心需求。
|
||||
|
||||
它是一个**纯粹的教学级、工具级框架**,旨在让开发者深入理解GUI原理,并快速构建轻量级Windows工具。
|
||||
这是一个**教学级、工具级**框架,帮助开发者深入理解 GUI 原理,并快速构建轻量工具。
|
||||
|
||||
---
|
||||
|
||||
## 🆕v2.2.1(v2.2.0修复版)
|
||||
|
||||
- 解决了使用Canvas和TabControl容器时,出现频闪问题
|
||||
|
||||
- 修复了Dialog对话框关闭时概率出边边框残留和功能按钮残留问题
|
||||
|
||||
详情参考[更新日志](CHANGELOG.md)
|
||||
|
||||
|
||||
|
||||
## V2.2.0 有何变化
|
||||
|
||||
- **新增 TabControl 控件,实现多页面选项卡界面:** 通过 `TabControl` 可以轻松创建选项卡式布局,支持页签在上下左右排列、点击切换显示不同内容页面。适用于设置面板、多视图切换等场景。
|
||||
- **控件显隐与布局响应能力增强:** 现在所有控件都可以使用统一接口动态隐藏或显示(`setIsVisible`),容器控件隐藏时其内部子控件会自动随之隐藏/显示。与此同时,引入控件对窗口尺寸变化的响应机制(`onWindowResize`),窗口拉伸后界面各元素可协调更新,杜绝拉伸过程中出现残影或错位。
|
||||
- **文本样式机制完善:** Label 控件改用统一的文本样式结构 `ControlText`,开发者可方便地设置字体、颜色、大小等属性来定制 Label 的外观(替代旧接口,更加灵活)。Button 的 Tooltip 提示也支持更丰富的定制和针对切换状态的不同提示文本。
|
||||
- **其他改进:** 框架底层的对话框管理增加了防重复弹出相同提示的机制,修复了一些细节 Bug 并优化了刷新效率,进一步提升了稳定性。
|
||||
|
||||
详见 `CHANGELOG.md / CHANGELOG.en.md` 获取完整更新列表。
|
||||
|
||||
---
|
||||
|
||||
## 📦 项目结构与设计哲学
|
||||
|
||||
星垣框架采用经典的**面向对象**和**模块化**设计,项目结构清晰规范:
|
||||
星垣采用经典 **OOP** 与 **模块化** 设计,结构清晰:
|
||||
|
||||
```
|
||||
```markdown
|
||||
StellarX/
|
||||
├── include/ # 头文件目录
|
||||
│ └── StellarX/ # 框架头文件
|
||||
│ ├── StellarX.h # 主包含头文件 - 一键引入整个框架
|
||||
│ ├── CoreTypes.h # ★ 核心 ★ - 所有枚举、结构体的唯一定义源
|
||||
│ ├── Control.h # 抽象基类 - 定义所有控件的统一接口
|
||||
│ ├── Button.h # 按钮控件
|
||||
│ ├── Window.h # 窗口管理
|
||||
│ ├── Label.h # 标签控件
|
||||
│ ├── TextBox.h # 文本框控件
|
||||
│ ├── Canvas.h # 画布容器
|
||||
│ └── Table.h # 表格控件
|
||||
├── src/ # 源文件目录
|
||||
├── include/
|
||||
│ └── StellarX/
|
||||
│ ├── StellarX.h
|
||||
│ ├── CoreTypes.h # 唯一定义源(枚举/结构体)
|
||||
│ ├── Control.h
|
||||
│ ├── Button.h
|
||||
│ ├── Window.h
|
||||
│ ├── Label.h
|
||||
│ ├── TextBox.h
|
||||
│ ├── TabControl.h #v2.2.0
|
||||
│ ├── Canvas.h
|
||||
│ ├── Dialog.h
|
||||
│ ├── MessageBox.h
|
||||
│ └── Table.h
|
||||
├── src/
|
||||
│ ├── Control.cpp
|
||||
│ ├── Button.cpp
|
||||
│ ├── Window.cpp
|
||||
│ ├── Label.cpp
|
||||
│ ├── TextBox.cpp
|
||||
│ ├── Canvas.cpp
|
||||
│ └── Table.cpp
|
||||
├── examples/ # 示例代码目录
|
||||
│ └── demo.cpp # 基础演示
|
||||
├── docs/ # 文档目录
|
||||
│ └── CODE_OF_CONDUCT.md # 行为准则
|
||||
├── CMakeLists.txt # CMake 构建配置
|
||||
├── CONTRIBUTING.md # 贡献指南
|
||||
├── CHANGELOG.md # 更新日志
|
||||
├── Doxyfile # Doxygen 配置
|
||||
├── LICENSE # MIT 许可证
|
||||
└── README.md # 项目说明
|
||||
│ ├── TabControl.cpp #v2.2.0
|
||||
│ ├── Table.cpp
|
||||
│ ├── Dialog.cpp
|
||||
│ └── MessageBox.cpp
|
||||
├── examples/
|
||||
│ └── demo.cpp
|
||||
├── docs/
|
||||
│ └── CODE_OF_CONDUCT.md
|
||||
├── CMakeLists.txt
|
||||
├── CONTRIBUTING.md
|
||||
├── CHANGELOG.md
|
||||
├── CHANGELOG.en.md
|
||||
├── Doxyfile
|
||||
├── LICENSE
|
||||
├──API 文档.md
|
||||
├──API Documentation.en.md
|
||||
└── README.md
|
||||
```
|
||||
|
||||
**设计理念:**
|
||||
1. **单一职责原则 (SRP)**: 每个类/文件只负责一件事。
|
||||
2. **依赖倒置原则 (DIP)**: 高层模块(如`Window`)不依赖低层模块(如`Button`),二者都依赖其抽象(`Control`)。
|
||||
3. **开闭原则 (OCP)**: 通过继承`Control`基类,可以轻松扩展新的控件,而无需修改现有代码。
|
||||
4. **一致性**: 所有控件共享统一的`draw()`和`handleEvent()`接口。
|
||||
|
||||
1. **单一职责(SRP)**:每个类/文件只做一件事。
|
||||
2. **依赖倒置(DIP)**:高层模块依赖抽象(`Control`),而非具体控件。
|
||||
3. **开闭原则(OCP)**:继承 `Control` 可扩展新控件,无需修改既有代码。
|
||||
4. **一致性**:所有控件统一 `draw()` / `handleEvent()` 接口。
|
||||
|
||||
## 🚀 核心特性
|
||||
|
||||
- **极致的轻量级**: 核心库编译后仅 ~1.2MB,无任何外部依赖。生成的应用程序小巧玲珑。
|
||||
- **清晰的模块化架构**: 使用`CoreTypes.h`统一管理所有类型,消除重复定义,极大提升可维护性。
|
||||
- **原生C++性能**: 直接基于EasyX和Win32 API,提供接近原生的执行效率,内存占用极低(通常<10MB)。
|
||||
- **丰富的控件库**: 提供按钮、标签、文本框、表格、画布等常用控件,满足基本桌面应用需求。
|
||||
- **高度可定制化**: 从控件颜色、形状(矩形、圆角、圆形、椭圆)到填充模式、字体样式,均有详尽枚举支持,可轻松定制。
|
||||
- **简洁直观的API**: 采用经典的面向对象设计,代码即文档,学习成本极低。
|
||||
- **标准项目结构**: 采用标准的include/src分离结构,支持CMake构建,易于集成和使用。
|
||||
- **极致轻量**:除 EasyX 外无外部重量级依赖。
|
||||
- **模块清晰**:`CoreTypes.h` 统一类型与枚举。
|
||||
- **原生性能**:EasyX + Win32,执行高效、内存低占用(常见 <10MB)。
|
||||
- **控件齐全**:Button、Label、TextBox、Canvas、Table、Dialog、MessageBox、**TabControl**。
|
||||
- **高度自定义**:颜色、形状(矩形/圆角/圆/椭圆)、填充、字体等皆有枚举配置,易于切换。
|
||||
- **简单直观 API**:OOP 设计,接口语义明确、调用友好,代码即文档。
|
||||
- **标准工程结构**:include/src 分离,支持 CMake 构建,方便集成到现有项目或开箱即用。
|
||||
|
||||
------
|
||||
|
||||
## ⚡ 快速开始(5 分钟上手)
|
||||
|
||||
> **🎯 最新版本下载**
|
||||
> 从 [GitHub Releases](https://github.com/Ysm-04/StellarX/releases/latest) 下载预编译的库文件和头文件,即可快速集成到你的项目中。
|
||||
> 从 [Releases](https://github.com/Ysm-04/StellarX/releases/latest) 获取预编译包。
|
||||
|
||||
### 环境要求
|
||||
|
||||
- **操作系统**: Windows 10 或更高版本
|
||||
- **编译器**: 支持C++17的编译器 (如: **Visual Studio 2019+**)
|
||||
- **图形库**: [EasyX](https://easyx.cn/) (2022版本或更高,安装时请选择与您编译器匹配的版本)
|
||||
- **构建工具**: CMake 3.12+ (可选,推荐使用)
|
||||
- **系统**:Windows 10+
|
||||
- **编译器**:C++17(如 VS 2019+)
|
||||
- **图形库**: [EasyX](https://easyx.cn/) 2022+(与编译器匹配)
|
||||
- **构建**:CMake 3.12+(可选)
|
||||
|
||||
### 安装 EasyX
|
||||
1. 访问 [EasyX 官网](https://easyx.cn/) 下载最新版本
|
||||
2. 运行安装程序,选择与您的 Visual Studio 版本匹配的版本
|
||||
3. 安装完成后,无需额外配置,星垣框架会自动链接 EasyX
|
||||
|
||||
### 方法一:使用CMake构建(推荐)
|
||||
1. 下载 EasyX 最新版
|
||||
2. 按 VS 版本安装匹配组件
|
||||
3. 框架会自动链接,无需额外配置
|
||||
|
||||
### CMake 构建(推荐)
|
||||
|
||||
1. **克隆项目**:
|
||||
```bash
|
||||
git clone https://github.com/Ysm-04/StellarX.git
|
||||
cd StellarX
|
||||
```
|
||||
|
||||
2. **生成构建系统**:
|
||||
```bash
|
||||
mkdir build
|
||||
cd build
|
||||
mkdir build && cd build
|
||||
cmake ..
|
||||
```
|
||||
|
||||
3. **编译项目**:
|
||||
```bash
|
||||
cmake --build .
|
||||
```
|
||||
|
||||
4. **运行示例**:
|
||||
```bash
|
||||
./examples/Demo
|
||||
```
|
||||
|
||||
### 方法二:手动集成到现有项目
|
||||
### 手动集成
|
||||
|
||||
1. **将include和src目录复制**到您的项目中
|
||||
2. **配置包含路径**,确保编译器可以找到`include/StellarX/`目录
|
||||
3. **将所有.cpp文件**添加到您的项目中编译
|
||||
- 拷贝 `include` 与 `src`
|
||||
- 配置头文件搜索路径:`include/StellarX/`
|
||||
- 将全部 `.cpp` 加入工程
|
||||
|
||||
### 创建你的第一个星垣应用
|
||||
### 第一个可拉伸窗口
|
||||
|
||||
```cpp
|
||||
// 只需包含这一个头文件即可使用所有功能
|
||||
```c++
|
||||
#include "StellarX.h"
|
||||
|
||||
// 程序入口点(请使用WinMain以获得更好的兼容性)
|
||||
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd) {
|
||||
|
||||
// 1. 创建一个640x480的窗口,背景为白色,标题为"我的应用"
|
||||
Window mainWindow(640, 480, 0, RGB(255, 255, 255), "我的第一个星垣应用");
|
||||
|
||||
// 2. 创建一个按钮 (使用智能指针管理)
|
||||
auto myButton = std::make_unique<Button>(
|
||||
250, 200, 140, 40, // x, y, 宽度, 高度
|
||||
"点击我", // 按钮文本
|
||||
StellarX::ButtonMode::NORMAL,
|
||||
StellarX::ControlShape::ROUND_RECTANGLE
|
||||
);
|
||||
|
||||
// 3. 为按钮设置点击事件(使用Lambda表达式)
|
||||
myButton->setOnClickListener([]() {
|
||||
MessageBox(nullptr, "Hello, 星垣!", "问候", MB_OK | MB_ICONINFORMATION);
|
||||
});
|
||||
|
||||
// 4. (可选)设置按钮样式
|
||||
myButton->textStyle.nHeight = 20;
|
||||
myButton->textStyle.color = RGB(0, 0, 128); // 深蓝色文字
|
||||
myButton->setButtonBorder(RGB(0, 128, 255)); // 蓝色边框
|
||||
|
||||
// 5. 将按钮添加到窗口
|
||||
mainWindow.addControl(std::move(myButton));
|
||||
|
||||
// 6. 绘制窗口
|
||||
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
|
||||
{
|
||||
//默认启动拉伸,当前尺寸为最小尺寸
|
||||
Window mainWindow(800, 600, 0, RGB(255,255,255), "我的星垣应用");
|
||||
mainWindow.draw();
|
||||
|
||||
// 7. 进入消息循环,等待用户交互
|
||||
mainWindow.runEventLoop();
|
||||
// 添加你的控件...
|
||||
// mainWindow.addControl(std::move(btn));
|
||||
|
||||
mainWindow.runEventLoop();
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
5. **编译并运行!** 您将看到一个带有蓝色圆角按钮的窗口,点击它将会弹出消息框。
|
||||
> 实现要点:在 `WM_PAINT` 期进行**整窗背景绘制**(纯色/图片),并配合 EasyX 批量绘制抑制频闪与黑边。
|
||||
|
||||
## 📚 核心类型详解 (`CoreTypes.h`)
|
||||
------
|
||||
|
||||
星垣框架的所有视觉和行为属性都通过`CoreTypes.h`中定义的精美枚举和结构体来控制。
|
||||
## 📚 核心类型(`CoreTypes.h` 摘要)
|
||||
|
||||
### 枚举类型 (Enums)
|
||||
### 枚举
|
||||
|
||||
| 枚举类型 | 描述 | 常用值 |
|
||||
| :----------------- | :----------- | :----------------------------------------------------------- |
|
||||
| **`ControlShape`** | 控件几何形状 | `RECTANGLE`, `B_RECTANGLE`, `ROUND_RECTANGLE`, `CIRCLE`, `ELLIPSE`等 |
|
||||
| **`ButtonMode`** | 按钮行为模式 | `NORMAL`(普通), `TOGGLE`(切换), `DISABLED`(禁用) |
|
||||
| **`TextBoxMode`** | 文本框模式 | `INPUT_MODE`(输入), `READONLY_MODE`(只读) |
|
||||
| **`FillMode`** | 图形填充模式 | `SOLID`(实心), `NULL`(空心), `HATCHED`(图案)等 |
|
||||
| **`FillStyle`** | 图案填充样式 | `HORIZONTAL`(水平线), `CROSS`(十字线)等 |
|
||||
| **`LineStyle`** | 边框线型 | `SOLID`(实线), `DASH`(虚线), `DOT`(点线)等 |
|
||||
| 枚举 | 描述 | 常用值 |
|
||||
| ------------------ | ---------- | ------------------------------------------------------------ |
|
||||
| `ControlShape` | 几何形状 | `RECTANGLE`, `B_RECTANGLE`, `ROUND_RECTANGLE`, `CIRCLE`, `ELLIPSE` |
|
||||
| `ButtonMode` | 按钮行为 | `NORMAL`, `TOGGLE`, `DISABLED` |
|
||||
| `TextBoxMode` | 文本框模式 | `INPUT_MODE`, `READONLY_MODE` |
|
||||
| `FillMode` | 填充模式 | `SOLID`, `NULL`, `HATCHED` |
|
||||
| `FillStyle` | 图案样式 | `HORIZONTAL`, `CROSS` |
|
||||
| `LineStyle` | 线型 | `SOLID`, `DASH`, `DOT` |
|
||||
| `MessageBoxType` | 消息框类型 | `OK`, `OKCancel`, `YesNo`, ... |
|
||||
| `MessageBoxResult` | 结果 | `OK`, `Cancel`, `Yes`, `No`, `Abort`, `Retry`, `Ignore` |
|
||||
| `TabPlacement` | 页签位置 | `Top`,`Bottom`,`Left`,`Right` |
|
||||
|
||||
### 结构体 (Structs)
|
||||
### 结构体
|
||||
|
||||
| 结构体 | 描述 |
|
||||
| :---------------- | :----------------------------------------------------------- |
|
||||
| **`ControlText`** | 封装了所有文本样式属性,包括字体、大小、颜色、粗体、斜体、下划线、删除线等。 |
|
||||
| -------------- | -------------------------------------- |
|
||||
| `ControlText` | 字体/字号/颜色/粗体/斜体/下划线/删除线 |
|
||||
| `RouRectangle` | 圆角矩形的角部椭圆尺寸 |
|
||||
|
||||
**使用示例:**
|
||||
```cpp
|
||||
// 创建一个复杂的文本样式
|
||||
StellarX::ControlText myStyle;
|
||||
myStyle.nHeight = 25; // 字体高度
|
||||
myStyle.lpszFace = _T("微软雅黑"); // 字体
|
||||
myStyle.color = RGB(255, 0, 0); // 红色
|
||||
myStyle.nWeight = FW_BOLD; // 粗体
|
||||
myStyle.bUnderline = true; // 下划线
|
||||
------
|
||||
|
||||
// 应用于控件
|
||||
myLabel->textStyle = myStyle;
|
||||
myButton->textStyle = myStyle;
|
||||
```
|
||||
## 🧩 控件库
|
||||
|
||||
## 🧩 控件库大全
|
||||
### 1) 基础控件
|
||||
|
||||
### 1. 基础控件
|
||||
| 控件 | 头文件 | 描述 | 关键点 |
|
||||
| ------- | ----------- | ----------- | ------------------------------------------------------------ |
|
||||
| Button | `Button.h` | 多功能按钮 | 形状/模式、悬停/点击色、回调,**单行截断** + **Tooltip**(v2.1.0) |
|
||||
| Label | `Label.h` | 文本标签 | 透明/不透明背景,自定义字体 |
|
||||
| TextBox | `TextBox.h` | 输入/显示框 | 输入/只读,整合 EasyX `InputBox` |
|
||||
|
||||
| 控件 | 头文件 | 描述 | 关键特性 |
|
||||
| :---------- | :---------- | :------------ | :------------------------------------------------------ |
|
||||
| **Button** | `Button.h` | 多功能按钮 | 支持多种模式/形状/状态,可设置悬停/点击颜色,自定义回调 |
|
||||
| **Label** | `Label.h` | 文本标签 | 支持背景透明/不透明,自定义字体样式 |
|
||||
| **TextBox** | `TextBox.h` | 输入框/显示框 | 支持输入和只读模式,集成EasyX的`InputBox` |
|
||||
|
||||
### 2. 容器控件
|
||||
### 2) 容器控件
|
||||
|
||||
| 控件 | 头文件 | 描述 |
|
||||
| :--------- | :--------- | :------------------------------------------------------- |
|
||||
| **Canvas** | `Canvas.h` | 容器控件,可作为其他控件的父容器,支持自定义边框和背景。 |
|
||||
| **Window** | `Window.h` | 顶级窗口,所有控件的最终容器,负责消息循环和调度。 |
|
||||
| ------ | ---------- | ------------------------------------------------------------ |
|
||||
| Canvas | `Canvas.h` | 父容器,自定义边框/背景,**内置 HBox/VBox 自动布局**(v2.1.0) |
|
||||
| Window | `Window.h` | 顶层容器,消息循环与分发,**可拉伸**(v2.1.0) |
|
||||
|
||||
### 3. 高级控件
|
||||
### 3) 高级控件
|
||||
|
||||
| 控件 | 头文件 | 描述 | 关键特性 |
|
||||
| :-------- | :-------- | :------- | :----------------------------------------------------------- |
|
||||
| **Table** | `Table.h` | 数据表格 | **框架功能亮点**,支持分页显示、自定义表头和数据、自动计算列宽、翻页按钮。 |
|
||||
| 控件 | 头文件 | 描述 | 关键点 |
|
||||
| ---------- | -------------- | -------- | ------------------------------------------------------------ |
|
||||
| Table | `Table.h` | 数据表格 | 分页/表头/列宽自动、翻页控件重叠/透明叠影已修复(v2.1.0) |
|
||||
| Dialog | `Dialog.h` | 对话框 | 支持模态/非模态,自动布局与背景保存/恢复 |
|
||||
| TabControl | `TabControl.h` | 选项卡 | 支持一键添加“页签+页”对(pair)也可以单独对某页添加子控件,采用相对坐标 |
|
||||
|
||||
**表格控件示例:**
|
||||
```cpp
|
||||
// 创建一个表格
|
||||
auto myTable = std::make_unique<StellarX::Table>(50, 50);
|
||||
### 4) 静态工厂
|
||||
|
||||
// 设置表头
|
||||
myTable->setHeaders({ "ID", "姓名", "年龄", "职业" });
|
||||
| 控件 | 头文件 | 描述 | 关键点 |
|
||||
| ---------- | -------------- | ---------- | ----------------------------------- |
|
||||
| MessageBox | `MessageBox.h` | 消息框工厂 | 静态 API;支持模态/非模态;内置去重 |
|
||||
|
||||
// 添加数据行
|
||||
myTable->addDataRow({ "1", "张三", "25", "工程师" });
|
||||
myTable->addDataRow({ "2", "李四", "30", "设计师" });
|
||||
myTable->addDataRow({ "3", "王五", "28", "产品经理" });
|
||||
------
|
||||
|
||||
// 设置每页显示2行
|
||||
myTable->setRowsPerPage(2);
|
||||
## 📐 布局管理(HBox/VBox)
|
||||
|
||||
// 设置表格样式
|
||||
myTable->textStyle.nHeight = 16;
|
||||
myTable->setTableBorder(RGB(50, 50, 50));
|
||||
myTable->setTableBackground(RGB(240, 240, 240));
|
||||
==预留,待实现==
|
||||
|
||||
// 添加到窗口
|
||||
mainWindow.addControl(std::move(myTable));
|
||||
```
|
||||
|
||||
|
||||
------
|
||||
|
||||
## 🗂 选项卡(TabControl)
|
||||
|
||||
- 页签条(按钮组) + 页面容器(`Canvas`)
|
||||
- 透明主题:页面区域**背景快照**切换,避免叠影
|
||||
- API :**查看API文档**
|
||||
|
||||
------
|
||||
|
||||
## ✂️ 文本单行截断 & Button Tooltip
|
||||
|
||||
- **按钮截断**:多字节字符集下**中/英分治**,基于像素宽度阈值追加 `...`
|
||||
- **Tooltip**:延时出现、自动隐藏;默认文字=按钮文本,可自定义;使用控件级**背景快照/恢复**
|
||||
|
||||
------
|
||||
|
||||
## 🧊 透明背景与背景快照
|
||||
|
||||
- **通用约定**:首绘前 `captureBackground(rect)`,隐藏/覆盖前 `restoreBackground()`
|
||||
- **Table**:快照区域**包含表头**;翻页后立即恢复 + 重绘,分页控件整体居中
|
||||
|
||||
------
|
||||
|
||||
## 🔧 高级主题与最佳实践
|
||||
|
||||
### 1. 自定义控件
|
||||
您可以通过继承`Control`基类来创建自定义控件。只需实现`draw()`和`handleEvent()`两个纯虚函数即可。
|
||||
- 自定义控件:继承 `Control`,实现 `draw()` / `handleEvent()`
|
||||
- 性能:
|
||||
- **脏矩形**:状态改变时置 `dirty=true`,按需重绘
|
||||
- **避免额外 `cleardevice()`**:背景已由 `WM_PAINT` 统一处理
|
||||
- 绘制前确保 `SetWorkingImage(nullptr)` 将输出落到屏幕
|
||||
- 事件消费:处理后返回 `true` 终止传播
|
||||
|
||||
```cpp
|
||||
class MyCustomControl : public StellarX::Control {
|
||||
public:
|
||||
MyCustomControl(int x, int y) : Control(x, y, 100, 100) {}
|
||||
void draw() override {
|
||||
// 您的自定义绘制逻辑
|
||||
setfillcolor(RGB(255, 100, 100));
|
||||
fillrectangle(x, y, x + width, y + height);
|
||||
}
|
||||
void handleEvent(const ExMessage& msg) override {
|
||||
// 您的自定义事件处理逻辑
|
||||
if (msg.message == WM_LBUTTONDOWN && isInControl(msg.x, msg.y)) {
|
||||
// 处理点击
|
||||
}
|
||||
}
|
||||
};
|
||||
```
|
||||
------
|
||||
|
||||
### 2. 布局管理
|
||||
当前版本星垣主要采用**绝对定位**。对于简单布局,您可以通过计算坐标来实现。对于复杂布局,可以考虑:
|
||||
- 在`Canvas`中嵌套控件,实现相对定位。
|
||||
- 自行实现简单的流式布局或网格布局管理器。
|
||||
## ⚠️ 适用与限制
|
||||
|
||||
### 3. 性能优化
|
||||
- **脏矩形渲染**: 框架内部已实现,控件状态改变时`dirty=true`,仅在需要时重绘。
|
||||
- **图像资源**: 使用`IMAGE`对象加载图片后,可重复使用,避免多次加载。
|
||||
- **减少循环内操作**: 在`draw()`和`handleEvent()`中避免进行重型计算。
|
||||
- 不适合高性能游戏/复杂动画;极端 DPI 需复核度量
|
||||
- 暂无无障碍能力
|
||||
- Windows 专用,不跨平台
|
||||
- 复杂商业前端建议用 Qt / wxWidgets / ImGui / Electron
|
||||
|
||||
## ⚠️ 重要限制与适用场景
|
||||
------
|
||||
|
||||
**星垣框架的设计目标是轻便、清晰和教学价值,因此它明确** **不适用于** **以下场景:**
|
||||
## 📜 许可证
|
||||
|
||||
- **高性能游戏或复杂动画**: 渲染基于EasyX的CPU软件渲染,性能有限。
|
||||
- **需要高DPI缩放的应用**: 对高DPI显示器的支持有限,界面可能显示不正确。
|
||||
- **需要无障碍功能的应用**: 未提供对屏幕阅读器等辅助技术的支持。
|
||||
- **跨平台应用**: 深度依赖Windows API和EasyX,无法直接在Linux/macOS上运行。
|
||||
- **复杂的商业软件前端**: 缺乏高级控件(如树形图、富文本框、选项卡、高级列表等)和成熟的自动布局管理器。
|
||||
MIT(见 `LICENSE`)。
|
||||
|
||||
**如果您需要开发上述类型的应用,请考虑使用以下成熟方案:**
|
||||
- **Qt**: 功能极其强大,跨平台,适合大型商业应用。
|
||||
- **wxWidgets**: 原生外观,跨平台。
|
||||
- **Dear ImGui**: 即时模式GUI,非常适合工具和调试界面。
|
||||
- **Web技术栈 (Electron/CEF)**: 适合需要Web技术的场景。
|
||||
## 👥 贡献指南
|
||||
|
||||
## 📜 许可证 (License)
|
||||
- 遵循现有 C++ 风格
|
||||
- 新特性需附示例与 README 更新
|
||||
- 提交前请自测,并说明变更动机
|
||||
- Bug/想法请提 Issue
|
||||
|
||||
本项目采用 **MIT 许可证**。
|
||||
## 🙏 致谢
|
||||
|
||||
您可以自由地:
|
||||
- 使用、复制、修改、合并、出版发行、散布、再授权及销售本框架的副本。
|
||||
- 将其用于私人或商业项目。
|
||||
- 感谢 [EasyX](https://easyx.cn/)
|
||||
- 感谢推崇**简洁/高效/清晰**的开发者
|
||||
|
||||
唯一要求是:
|
||||
- 请在您的项目中保留原始的版权声明。
|
||||
|
||||
详见项目根目录的 [LICENSE](LICENSE) 文件。
|
||||
|
||||
## 👥 贡献指南 (Contributing)
|
||||
|
||||
我们欢迎任何形式的贡献!如果您想为星垣框架添砖加瓦,请阅读以下指南:
|
||||
|
||||
1. **代码风格**: 请遵循现有的Google C++规范风格(使用空格缩进,大括号换行等)。
|
||||
2. **新增功能**: 必须提供**示例代码**,并更新本README文档的相关部分。
|
||||
3. **提交PR**: 请确保您的代码在提交前已经过测试,并描述清楚您的更改内容和动机。
|
||||
4. **问题反馈**: 如果您发现了Bug或有新的想法,欢迎在GitHub提交Issue。
|
||||
|
||||
详细贡献指南请参阅 [CONTRIBUTING.md](CONTRIBUTING.md)。
|
||||
|
||||
## 🙏 致谢 (Acknowledgements)
|
||||
|
||||
- 感谢 [EasyX Graphics Library](https://easyx.cn/) 为这个项目提供了简单易用的图形基础,使得用C++教学GUI编程成为可能。
|
||||
- 感谢所有追求**简洁、高效、清晰**编码理念的开发者,你们是"星垣"诞生的灵感源泉。
|
||||
|
||||
---
|
||||
------
|
||||
|
||||
**星辰大海,代码为舟。**
|
||||
|
||||
愿 `星垣 (StellarX)` 能成为您探索GUI世界的一颗可靠基石,无论是用于学习、教学还是创造实用的工具。
|
||||
|
||||
## 📞 支持与反馈
|
||||
|
||||
如果您在使用过程中遇到问题或有任何建议:
|
||||
1. 查看 [示例代码](examples/) 获取使用参考
|
||||
2. 查阅 [更新日志](CHANGELOG.md) 了解最新变化
|
||||
3. 在GitHub仓库提交Issue反馈问题
|
||||
|
||||
---
|
||||
*星垣框架 - 轻若尘埃,繁星为界*
|
||||
- 查看 [examples/](examples/)
|
||||
- 查阅 [更新日志](CHANGELOG.md)[CHANGELOG](CHANGELOG.en.md)
|
||||
- 在 GitHub 提交 Issue
|
||||
25
docs/CODE_OF_CONDUCT.en.md
Normal file
25
docs/CODE_OF_CONDUCT.en.md
Normal 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.
|
||||
@@ -1,39 +1,55 @@
|
||||
/**
|
||||
* @file demo.cpp
|
||||
* @brief 一个简单的演示程序,展示 StellarX GUI 框架的基本用法。
|
||||
* @description 创建一个带有按钮的窗口,点击按钮会改变其文本。
|
||||
* @description 创建一个带有按钮的窗口,点击按钮会弹出对话框
|
||||
*/
|
||||
|
||||
#include <StellarX/StellarX.h>
|
||||
#include <iostream>
|
||||
// 只需包含这一个头文件即可使用所有功能
|
||||
#include "StellarX.h"
|
||||
|
||||
int main()
|
||||
// 程序入口点(请使用WinMain以获得更好的兼容性)
|
||||
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
|
||||
{
|
||||
// 创建一个窗口 (Windows平台)
|
||||
Window mainWindow(800, 600, NULL, RGB(240, 240, 240), "StellarX 演示程序");
|
||||
// 1. 创建一个640x480的窗口,背景为白色,标题为"我的应用"
|
||||
Window mainWindow(640, 480, 0, RGB(255, 255, 255), "我的第一个星垣应用");
|
||||
|
||||
// 创建一个按钮
|
||||
auto myButton = std::make_unique<Button>(300, 250, 200, 80, "点击我!", StellarX::ButtonMode::NORMAL, StellarX::ControlShape::ROUND_RECTANGLE);
|
||||
// 2. 创建一个按钮 (使用智能指针管理)
|
||||
auto myButton = std::make_unique<Button>(
|
||||
250, 200, 140, 40, // x, y, 宽度, 高度
|
||||
"点击我", // 按钮文本
|
||||
StellarX::ButtonMode::NORMAL,
|
||||
StellarX::ControlShape::ROUND_RECTANGLE
|
||||
);
|
||||
|
||||
// 为按钮点击事件设置一个回调函数
|
||||
myButton->setOnClickListener([&myButton]() {
|
||||
std::cout << "按钮被点击了!" << std::endl;
|
||||
// 点击后改变按钮文本作为视觉反馈
|
||||
static bool 已切换 = false;
|
||||
if (已切换) {
|
||||
myButton->setButtonText("点击我!");
|
||||
// 3. 为按钮设置点击事件(使用Lambda表达式)
|
||||
myButton->setOnClickListener([&mainWindow]() {
|
||||
// 使用消息框工厂创建模态对话框
|
||||
auto result = StellarX::MessageBox::ShowModal(
|
||||
mainWindow,
|
||||
"欢迎使用星垣GUI\r\n作者:我在人间做废物",
|
||||
"问候",
|
||||
StellarX::MessageBoxType::OKCancel
|
||||
);
|
||||
// 处理对话框结果
|
||||
if (result == StellarX::MessageBoxResult::OK) {
|
||||
// 用户点击了确定按钮
|
||||
}
|
||||
else {
|
||||
myButton->setButtonText("被点过了!");
|
||||
}
|
||||
已切换 = !已切换;
|
||||
});
|
||||
|
||||
// 将按钮添加到窗口
|
||||
|
||||
|
||||
// 4. (可选)设置按钮样式
|
||||
myButton->textStyle.nHeight = 20;
|
||||
myButton->textStyle.color = RGB(0, 0, 128); // 深蓝色文字
|
||||
myButton->setButtonBorder(RGB(0, 128, 255)); // 蓝色边框
|
||||
|
||||
// 5. 将按钮添加到窗口
|
||||
mainWindow.addControl(std::move(myButton));
|
||||
|
||||
// 绘制窗口并运行事件循环 (Windows消息循环)
|
||||
// 6. 绘制窗口
|
||||
mainWindow.draw();
|
||||
|
||||
// 7. 进入消息循环,等待用户交互
|
||||
mainWindow.runEventLoop();
|
||||
|
||||
return 0;
|
||||
|
||||
17
examples/register-viewer/README.md
Normal file
17
examples/register-viewer/README.md
Normal 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.
|
||||
462
examples/register-viewer/main.cpp
Normal file
462
examples/register-viewer/main.cpp
Normal 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→LSB:31..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();
|
||||
}
|
||||
@@ -1,16 +1,30 @@
|
||||
/*******************************************************************************
|
||||
* @文件: Button.h
|
||||
* @摘要: 按钮控件类
|
||||
* @类: Button
|
||||
* @摘要: 多功能按钮控件,支持多种状态和样式
|
||||
* @描述:
|
||||
* 提供多种样式和行为的按钮控件,支持点击、悬停、切换等状态。
|
||||
* 继承自Control基类,是框架的核心交互组件之一。
|
||||
* 提供完整的按钮功能,包括普通点击、切换模式、禁用状态。
|
||||
* 支持多种形状(矩形、圆形、椭圆等)和丰富的视觉样式。
|
||||
* 通过回调函数机制实现灵活的交互逻辑。
|
||||
*
|
||||
* @特性:
|
||||
* - 支持三种工作模式:普通、切换、禁用
|
||||
* - 八种几何形状,各有边框和无边框版本
|
||||
* - 自定义颜色(默认、悬停、点击状态)
|
||||
* - 多种填充模式(纯色、图案、图像)
|
||||
* - 完整的鼠标事件处理(点击、悬停、移出)
|
||||
*
|
||||
* @使用场景: 作为主要交互控件,用于触发动作或表示状态
|
||||
* @所属框架: 星垣(StellarX) GUI框架
|
||||
* @作者: 我在人间做废物
|
||||
* @日期: September 2025
|
||||
******************************************************************************/
|
||||
#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
|
||||
{
|
||||
@@ -18,7 +32,12 @@ class Button : public Control
|
||||
std::string text; // 按钮上的文字
|
||||
bool click; // 是否被点击
|
||||
bool hover; // 是否被悬停
|
||||
bool dirty = true; // 是否重绘
|
||||
|
||||
std::string cutText; // 切割后的文本
|
||||
bool needCutText = true; // 是否需要切割文本
|
||||
bool isUseCutText = false; // 是否使用切割文本
|
||||
int padX = TEXTMARGINS_X; // 文本最小左右内边距
|
||||
int padY = TEXTMARGINS_Y; // 文本最小上下内边距
|
||||
|
||||
COLORREF buttonTrueColor; // 按钮被点击后的颜色
|
||||
COLORREF buttonFalseColor; // 按钮未被点击的颜色
|
||||
@@ -28,7 +47,7 @@ class Button : public Control
|
||||
StellarX::ButtonMode mode; // 按钮模式
|
||||
StellarX::ControlShape shape; // 按钮形状
|
||||
|
||||
int buttonFillMode = BS_SOLID; //按钮填充模式
|
||||
StellarX::FillMode buttonFillMode = StellarX::FillMode::Solid; //按钮填充模式
|
||||
StellarX::FillStyle buttonFillIma = StellarX::FillStyle::BDiagonal; //按钮填充图案
|
||||
IMAGE* buttonFileIMAGE = nullptr; //按钮填充图像
|
||||
|
||||
@@ -43,13 +62,28 @@ 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; // 按钮文字样式
|
||||
|
||||
public:
|
||||
/*************************************************************************/
|
||||
/********************************构造函数*********************************/
|
||||
/*************************************************************************/
|
||||
|
||||
//默认按钮颜色
|
||||
Button(int x, int y, int width, int height, const std::string text,
|
||||
@@ -65,24 +99,12 @@ public:
|
||||
//析构函数 释放图形指针内存
|
||||
~Button();
|
||||
|
||||
/*************************************************************************/
|
||||
/********************************Set方法**********************************/
|
||||
/*************************************************************************/
|
||||
//绘制按钮
|
||||
void draw() override;
|
||||
//按钮事件处理
|
||||
|
||||
void handleEvent(const ExMessage& msg) override;
|
||||
bool handleEvent(const ExMessage& msg) override;
|
||||
|
||||
//设置回调函数
|
||||
//************************************
|
||||
// 名称: setOnClickListener | setOnToggleOnListener | setOnToggleOffListener
|
||||
// 全名: Button::setOnClickListener
|
||||
// 访问: public
|
||||
// 返回类型: void
|
||||
// Parameter: const std::function<> & & callback 设置回调函数 传入回调函数名即可,不需要传入(),不需要传入参数,不需要传入返回值
|
||||
// 如果要传入参数,可以使用lambda表达式
|
||||
//************************************
|
||||
void setOnClickListener(const std::function<void()>&& callback);
|
||||
//设置TOGGLE模式下被点击的回调函数
|
||||
void setOnToggleOnListener(const std::function<void()>&& callback);
|
||||
@@ -91,30 +113,30 @@ public:
|
||||
//设置按钮模式
|
||||
void setbuttonMode(StellarX::ButtonMode mode);
|
||||
//设置圆角矩形椭圆宽度
|
||||
int setROUND_RECTANGLEwidth(int width);
|
||||
void setROUND_RECTANGLEwidth(int width);
|
||||
//设置圆角矩形椭圆高度
|
||||
int setROUND_RECTANGLEheight(int height);
|
||||
void setROUND_RECTANGLEheight(int height);
|
||||
//设置按钮填充模式
|
||||
void setFillMode(int mode);
|
||||
void setFillMode(StellarX::FillMode mode);
|
||||
//设置按钮填充图案
|
||||
void setFillIma(StellarX::FillStyle ima);
|
||||
//设置按钮填充图像
|
||||
void setFillIma(std::string imaName);
|
||||
//设置按钮边框颜色
|
||||
void setButtonBorder(COLORREF Border);
|
||||
//设置按钮未被点击颜色
|
||||
void setButtonFalseColor(COLORREF color);
|
||||
//设置按钮文本
|
||||
void setButtonText(const char* text);
|
||||
void setButtonText(std::string text);
|
||||
//设置按钮形状
|
||||
void setButtonShape(StellarX::ControlShape shape);
|
||||
//设置按钮点击状态
|
||||
void setButtonClick(BOOL click);
|
||||
|
||||
//判断按钮是否被点击
|
||||
bool isClicked() const;
|
||||
|
||||
/*************************************************************************/
|
||||
/********************************Get方法**********************************/
|
||||
/*************************************************************************/
|
||||
|
||||
//获取按钮文字
|
||||
std::string getButtonText() const;
|
||||
const char* getButtonText_c() const;
|
||||
@@ -123,7 +145,7 @@ public:
|
||||
//获取按钮形状
|
||||
StellarX::ControlShape getButtonShape() const;
|
||||
//获取按钮填充模式
|
||||
int getFillMode() const;
|
||||
StellarX::FillMode getFillMode() const;
|
||||
//获取按钮填充图案
|
||||
StellarX::FillStyle getFillIma() const;
|
||||
//获取按钮填充图像
|
||||
@@ -134,7 +156,25 @@ public:
|
||||
COLORREF getButtonTextColor() const;
|
||||
//获取按钮文字样式
|
||||
StellarX::ControlText getButtonTextStyle() const;
|
||||
|
||||
//获取按钮宽度
|
||||
int getButtonWidth() const;
|
||||
//获取按钮高度
|
||||
int getButtonHeight() 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);
|
||||
//设置提示框文本
|
||||
void setTooltipText(const std::string& s){ tipTextClick = s; tipUserOverride = true; }
|
||||
void setTooltipTextsForToggle(const std::string& onText, const std::string& offText);
|
||||
private:
|
||||
//初始化按钮
|
||||
void initButton(const std::string text, StellarX::ButtonMode mode, StellarX::ControlShape shape, COLORREF ct, COLORREF cf, COLORREF ch);
|
||||
@@ -142,5 +182,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 model() const override { return false; }
|
||||
void cutButtonText();
|
||||
// 统一隐藏&恢复背景
|
||||
void hideTooltip();
|
||||
// 根据当前 click 状态选择文案
|
||||
void refreshTooltipTextForState();
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -1,19 +1,30 @@
|
||||
#pragma once
|
||||
/*******************************************************************************
|
||||
* @类: Canvas
|
||||
* @摘要: 画布容器控件,用于分组和管理子控件
|
||||
* @描述:
|
||||
* 作为其他控件的父容器,提供统一的背景和边框样式。
|
||||
* 负责将事件传递给子控件并管理它们的绘制顺序。
|
||||
*
|
||||
* @特性:
|
||||
* - 支持四种矩形形状(普通、圆角,各有边框和无边框版本)
|
||||
* - 可自定义填充模式、边框颜色和背景颜色
|
||||
* - 自动管理子控件的生命周期和事件传递
|
||||
* - 支持嵌套容器结构
|
||||
*
|
||||
* @使用场景: 用于分组相关控件、实现复杂布局或作为对话框基础
|
||||
* @所属框架: 星垣(StellarX) GUI框架
|
||||
* @作者: 我在人间做废物
|
||||
*******************************************************************************/
|
||||
|
||||
#pragma once
|
||||
#include "Control.h"
|
||||
// 画布容器控件,可以作为其他控件的父容器
|
||||
// 功能:
|
||||
// - 包含和管理子控件
|
||||
// - 将事件传递给子控件
|
||||
// - 提供统一的背景和边框
|
||||
// 使用场景: 用于分组相关控件或实现复杂布局
|
||||
|
||||
class Canvas : public Control
|
||||
{
|
||||
private:
|
||||
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; //线宽
|
||||
@@ -21,15 +32,18 @@ private:
|
||||
COLORREF canvasBorderClor = RGB(0, 0, 0); //边框颜色
|
||||
COLORREF canvasBkClor = RGB(255,255,255); //背景颜色
|
||||
|
||||
|
||||
// 清除所有子控件
|
||||
void clearAllControls();
|
||||
public:
|
||||
Canvas();
|
||||
Canvas(int x, int y, int width, int height);
|
||||
~Canvas() {}
|
||||
//绘制容器及其子控件
|
||||
void draw() override;
|
||||
void handleEvent(const ExMessage& msg) override;
|
||||
bool handleEvent(const ExMessage& msg) override;
|
||||
//添加控件
|
||||
void addControl(std::unique_ptr<Control> control);
|
||||
|
||||
//设置容器样式
|
||||
void setShape(StellarX::ControlShape shape);
|
||||
//设置容器填充模式
|
||||
@@ -42,6 +56,15 @@ public:
|
||||
void setCanvasLineStyle(StellarX::LineStyle style);
|
||||
//设置线段宽度
|
||||
void setLinewidth(int width);
|
||||
|
||||
//设置不可见后传递给子控件重写
|
||||
void setIsVisible(bool visible) override;
|
||||
void setDirty(bool dirty) override;
|
||||
void onWindowResize() override;
|
||||
void requestRepaint(Control* parent)override;
|
||||
//获取子控件列表
|
||||
std::vector<std::unique_ptr<Control>>& getControls() { return controls; }
|
||||
private:
|
||||
//用来检查对话框是否模态,此控件不做实现
|
||||
bool model() const override { return false; };
|
||||
};
|
||||
|
||||
|
||||
@@ -1,51 +1,106 @@
|
||||
#pragma once
|
||||
/*********************************************************************
|
||||
* \文件: Control.h
|
||||
* \描述: 控件基类,所有控件都继承自此类。
|
||||
* 提供了控件的一些基本属性和方法。
|
||||
/*******************************************************************************
|
||||
* @类: Control
|
||||
* @摘要: 所有控件的抽象基类,定义通用接口和基础功能
|
||||
* @描述:
|
||||
* 提供控件的基本属性和方法,包括位置、尺寸、重绘标记等。
|
||||
* 实现绘图状态保存和恢复机制,确保控件绘制不影响全局状态。
|
||||
*
|
||||
* \作者: 我在人间做废物
|
||||
* \日期: September 2025
|
||||
*********************************************************************/
|
||||
* @特性:
|
||||
* - 定义控件基本属性(坐标、尺寸、脏标记)
|
||||
* - 提供绘图状态管理(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>
|
||||
#undef MessageBox
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <initializer_list>
|
||||
#include "CoreTypes.h"
|
||||
|
||||
class Control
|
||||
{
|
||||
protected:
|
||||
int x, y; // 左上角坐标
|
||||
int width, height; // 控件尺寸
|
||||
std::string id; // 控件ID
|
||||
int localx,x, localy,y; // 左上角坐标
|
||||
int localWidth,width, localHeight,height; // 控件尺寸
|
||||
Control* parent = nullptr; // 父控件
|
||||
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; // 圆角矩形椭圆宽度和高度
|
||||
|
||||
LOGFONT currentFont; // 保存当前字体样式和颜色
|
||||
COLORREF currentColor = 0;
|
||||
COLORREF currentBkColor = 0; // 保存当前填充色
|
||||
COLORREF currentBorderColor = 0; // 边框颜色
|
||||
LINESTYLE* currentLineStyle = new LINESTYLE; // 保存当前线型
|
||||
LOGFONT* currentFont = new LOGFONT(); // 保存当前字体样式和颜色
|
||||
COLORREF* currentColor = new COLORREF();
|
||||
COLORREF* currentBkColor = new COLORREF(); // 保存当前填充色
|
||||
COLORREF* currentBorderColor = new COLORREF(); // 边框颜色
|
||||
LINESTYLE* currentLineStyle = new LINESTYLE(); // 保存当前线型
|
||||
|
||||
public:
|
||||
Control(const Control&) = delete;
|
||||
Control& operator=(const Control&) = delete;
|
||||
Control(Control&&) = default;
|
||||
Control& operator=(Control&&) = default;
|
||||
Control(Control&&) = delete;
|
||||
Control& operator=(Control&&) = delete;
|
||||
|
||||
Control() : x(0), y(0), width(100), height(100) {}
|
||||
Control() : localx(0),x(0), localy(0),y(0), localWidth(100),width(100),height(100), localHeight(100) {}
|
||||
Control(int x, int y, int width, int height)
|
||||
: x(x), y(y), width(width), height(height) {
|
||||
}
|
||||
: localx(x), x(x), localy(y), y(y), localWidth(width), width(width), height(height), localHeight(height){}
|
||||
public:
|
||||
|
||||
virtual ~Control() {
|
||||
virtual ~Control()
|
||||
{
|
||||
delete currentFont;
|
||||
delete currentColor;
|
||||
delete currentBkColor;
|
||||
delete currentBorderColor;
|
||||
delete currentLineStyle;
|
||||
currentLineStyle = nullptr;
|
||||
}
|
||||
|
||||
currentFont = nullptr;
|
||||
currentColor = nullptr;
|
||||
currentBkColor = nullptr;
|
||||
currentBorderColor = nullptr;
|
||||
currentLineStyle = nullptr;
|
||||
discardBackground();
|
||||
}
|
||||
protected:
|
||||
//向上请求重绘
|
||||
virtual void requestRepaint(Control* parent);
|
||||
//根控件/无父时触发重绘
|
||||
virtual void onRequestRepaintAsRoot();
|
||||
protected:
|
||||
//保存背景快照
|
||||
virtual void saveBackground(int x, int y, int w, int h);
|
||||
// putimage 回屏
|
||||
virtual void restBackground();
|
||||
// 释放快照(窗口重绘/尺寸变化后必须作废)
|
||||
void discardBackground();
|
||||
public:
|
||||
//释放快照重新保存,在尺寸变化时更新背景快照避免尺寸变化导致显示错位
|
||||
void updateBackground();
|
||||
//窗口变化丢快照
|
||||
virtual void onWindowResize();
|
||||
// 获取位置和尺寸
|
||||
int getX() const { return x; }
|
||||
int getY() const { return y; }
|
||||
@@ -54,9 +109,36 @@ public:
|
||||
int getRight() const { return x + width; }
|
||||
int getBottom() const { return y + height; }
|
||||
|
||||
virtual void draw() = 0;
|
||||
virtual void handleEvent(const ExMessage& msg) = 0;
|
||||
int getLocalX() const { return localx; }
|
||||
int getLocalY() const { return localy; }
|
||||
int getLocalWidth() const { return localWidth; }
|
||||
int getLocalHeight() const { return localHeight; }
|
||||
int getLocalRight() const { return localx + localWidth; }
|
||||
int getLocalBottom() const { return localy + localHeight; }
|
||||
|
||||
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:
|
||||
|
||||
virtual void draw() = 0;
|
||||
virtual bool handleEvent(const ExMessage& msg) = 0;//返回true代表事件已消费
|
||||
//设置是否显示
|
||||
virtual void setIsVisible(bool show);
|
||||
//设置父容器指针
|
||||
void setParent(Control* parent) { this->parent = parent; }
|
||||
//设置是否重绘
|
||||
virtual void setDirty(bool dirty) { this->dirty = dirty; }
|
||||
|
||||
//检查控件是否可见
|
||||
bool IsVisible() const { return show; };
|
||||
//获取控件id
|
||||
std::string getId() const { return id; }
|
||||
//检查是否为脏
|
||||
bool isDirty() { return dirty; }
|
||||
//用来检查对话框是否模态,其他控件不用实现
|
||||
virtual bool model()const = 0;
|
||||
protected:
|
||||
void saveStyle();
|
||||
void restoreStyle();
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
/*******************************************************************************
|
||||
/*******************************************************************************
|
||||
* @文件: CoreTypes.h
|
||||
* @摘要: 星垣框架核心类型定义文件
|
||||
* @摘要: 星垣(StellarX)框架核心类型定义文件
|
||||
* @描述:
|
||||
* 集中定义框架中使用的所有枚举类型和结构体,确保类型一致性。
|
||||
* 这是框架的类型系统基础,所有控件都依赖于此文件中定义的类型。
|
||||
@@ -9,11 +8,27 @@
|
||||
* @作者: 我在人间做废物
|
||||
* @日期: September 2025
|
||||
******************************************************************************/
|
||||
#include"easyX.h"
|
||||
#pragma once
|
||||
|
||||
#include "easyx.h"
|
||||
|
||||
/**
|
||||
* @命名空间: StellarX
|
||||
*
|
||||
* @详细说明:
|
||||
* 集中定义框架中使用的所有枚举类型和结构体,确保类型一致性。
|
||||
* 这是框架的类型系统基础,所有控件都依赖于此文件中定义的类型
|
||||
*
|
||||
* @使用示例:
|
||||
* StellarX::FillStyle::Horizontal - 填充样式
|
||||
*
|
||||
* @备注:
|
||||
* 不用单独包含此头文件,已在StellarX.h中包含,包含唯一对外头文件即可
|
||||
*/
|
||||
namespace StellarX
|
||||
{
|
||||
/**
|
||||
* @枚举类名称: hatchStyle
|
||||
* @枚举类名称: FillStyle
|
||||
* @功能描述: 用来定义控件填充图案的枚举类
|
||||
*
|
||||
* @详细说明:
|
||||
@@ -31,14 +46,13 @@ namespace StellarX
|
||||
* DiagCross - 网格
|
||||
*
|
||||
* @使用示例:
|
||||
* // 示例代码展示如何使用此枚举类
|
||||
* hatchStyle var = hatchStyle::Horizontal;
|
||||
* FillStyle var = FillStyle::Horizontal;
|
||||
*
|
||||
* @备注:
|
||||
* 此枚举类仅支持图案填充模式
|
||||
* 枚举类在使用时,需要使用::进行调用,还要注意大小写
|
||||
*/
|
||||
enum class FillStyle {
|
||||
enum class FillStyle
|
||||
{
|
||||
Horizontal = HS_HORIZONTAL, // 水平线
|
||||
Vertical = HS_VERTICAL, // 垂直线
|
||||
FDiagonal = HS_FDIAGONAL, // 反斜线
|
||||
@@ -46,6 +60,7 @@ namespace StellarX
|
||||
Cross = HS_CROSS, // 十字
|
||||
DiagCross = HS_DIAGCROSS // 网格
|
||||
};
|
||||
|
||||
/**
|
||||
* @枚举类名称: FillMode
|
||||
* @功能描述: 用来定义控件填充模式的枚举类
|
||||
@@ -64,11 +79,7 @@ namespace StellarX
|
||||
* DibPattern - 自定义图像填充
|
||||
*
|
||||
* @使用示例:
|
||||
* // 示例代码展示如何使用此枚举类
|
||||
* FillMode var = FillMode::Solid;
|
||||
*
|
||||
* @备注:
|
||||
* 枚举类在使用时,需要使用::进行调用,还要注意大小写
|
||||
*/
|
||||
enum class FillMode
|
||||
{
|
||||
@@ -78,8 +89,9 @@ namespace StellarX
|
||||
Pattern = BS_PATTERN, // 自定义图案填充
|
||||
DibPattern = BS_DIBPATTERN // 自定义图像填充
|
||||
};
|
||||
|
||||
/**
|
||||
* @枚举类名称: linStyle
|
||||
* @枚举类名称: LineStyle
|
||||
* @功能描述: 此枚举类用来定义控件边框线型
|
||||
*
|
||||
* @详细说明:
|
||||
@@ -97,13 +109,10 @@ namespace StellarX
|
||||
* Null // 无线
|
||||
*
|
||||
* @使用示例:
|
||||
* // 示例代码展示如何使用此枚举类
|
||||
* LineStyle var = LineStyle::Solid;
|
||||
*
|
||||
* @备注:
|
||||
* 枚举类在使用时,需要使用::进行调用,还要注意大小写
|
||||
*/
|
||||
enum class LineStyle {
|
||||
enum class LineStyle
|
||||
{
|
||||
Solid = PS_SOLID, // 实线
|
||||
Dash = PS_DASH, // 虚线
|
||||
Dot = PS_DOT, // 点线
|
||||
@@ -130,15 +139,12 @@ namespace StellarX
|
||||
* bool bItalic = false; - 是否斜体
|
||||
* bool bUnderline = false; - 是否下划线
|
||||
* bool bStrikeOut = false; - 是否删除线
|
||||
|
||||
* bool operator!=(const ControlText& text);
|
||||
* ControlText& operator=(const ControlText& text
|
||||
*/
|
||||
struct ControlText
|
||||
{
|
||||
int nHeight = 0; //- 字体高度
|
||||
int nWidth = 0; //- 字体宽度 如果为0则自适应
|
||||
LPCTSTR lpszFace = "宋体"; //- 字体名称
|
||||
LPCTSTR lpszFace = "微软雅黑"; //- 字体名称
|
||||
COLORREF color = RGB(0, 0, 0); //- 字体颜色
|
||||
int nEscapement = 0; //- 字符串旋转角度
|
||||
int nOrientation = 0; //- 字符旋转角度
|
||||
@@ -149,7 +155,6 @@ namespace StellarX
|
||||
|
||||
bool operator!=(const ControlText& text);
|
||||
ControlText& operator=(const ControlText& text);
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -163,18 +168,15 @@ namespace StellarX
|
||||
* @取值说明:
|
||||
* RECTANGLE = 1, //有边框矩形
|
||||
* B_RECTANGLE, //无边框矩形
|
||||
|
||||
* ROUND_RECTANGLE, //有边框圆角矩形
|
||||
* B_ROUND_RECTANGLE, //无边框圆角矩形
|
||||
|
||||
* CIRCLE, //有边框圆形
|
||||
* B_CIRCLE, //无边框圆形
|
||||
|
||||
* ELLIPSE, //有边框椭圆
|
||||
* B_ELLIPSE //无边框椭圆
|
||||
*
|
||||
* @使用示例:
|
||||
* ControlShape shape = ELLIPSE;
|
||||
* ControlShape shape = ControlShape::ELLIPSE;
|
||||
*
|
||||
* @备注:
|
||||
* 按钮类支持所有形状,部分控件只支持部分形状,具体请参考控件类。
|
||||
@@ -183,16 +185,14 @@ namespace StellarX
|
||||
{
|
||||
RECTANGLE = 1, //有边框矩形
|
||||
B_RECTANGLE, //无边框矩形
|
||||
|
||||
ROUND_RECTANGLE, //有边框圆角矩形
|
||||
B_ROUND_RECTANGLE, //无边框圆角矩形
|
||||
|
||||
CIRCLE, //有边框圆形
|
||||
B_CIRCLE, //无边框圆形
|
||||
|
||||
ELLIPSE, //有边框椭圆
|
||||
B_ELLIPSE //无边框椭圆
|
||||
};
|
||||
|
||||
/**
|
||||
* @枚举类名称: TextBoxmode
|
||||
* @功能描述: 定义了文本框的两种模式
|
||||
@@ -203,13 +203,6 @@ namespace StellarX
|
||||
* @取值说明:
|
||||
* INPUT_MODE, // 用户可输入模式
|
||||
* READONLY_MODE // 只读模式
|
||||
*
|
||||
* @使用示例:
|
||||
* // 示例代码展示如何使用此枚举类
|
||||
* StellarX::TextBoxmode var = EnumClassName::VALUE1;
|
||||
*
|
||||
* @备注:
|
||||
* 枚举类的特性、与普通枚举的区别
|
||||
*/
|
||||
enum class TextBoxmode
|
||||
{
|
||||
@@ -219,7 +212,7 @@ namespace StellarX
|
||||
|
||||
/**
|
||||
* @枚举名称: ButtonMode
|
||||
* @功能描述: brief
|
||||
* @功能描述: 定义按钮的工作模式
|
||||
*
|
||||
* @详细说明:
|
||||
* 根据按钮的工作模式,按钮可以有不同的行为。
|
||||
@@ -231,18 +224,123 @@ namespace StellarX
|
||||
* DISABLED - 禁用模式,按钮不可点击,显示为灰色,文本显示删除线。
|
||||
*
|
||||
* @使用示例:
|
||||
* Button b1(100, 100, 120, 120, "测试按钮", RGB(128, 0, 0), RGB(255, 9, 9));
|
||||
*
|
||||
* ButtonMode mode = ButtonMode::NORMAL;
|
||||
*/
|
||||
enum class ButtonMode
|
||||
{
|
||||
NORMAL = 1, //普通模式,点击后触发回调,但不会保持状态。
|
||||
TOGGLE, //切换模式,点击后会在选中和未选中之间切换,触发不同的回调函数。
|
||||
DISABLED //禁用模式,按钮不可点击,显示为灰色,文本显示删除线。
|
||||
NORMAL = 1, //普通模式
|
||||
TOGGLE, //切换模式
|
||||
DISABLED //禁用模式
|
||||
};
|
||||
|
||||
/**
|
||||
* @结构体名称: RouRectangle
|
||||
* @功能描述: 定义了控件圆角矩形样式时圆角的椭圆尺寸
|
||||
*
|
||||
* @详细说明:
|
||||
* 需要修改控件圆角矩形样式时的圆角椭圆。
|
||||
*
|
||||
* @成员说明:
|
||||
* int ROUND_RECTANGLEwidth = 20; //构成圆角矩形的圆角的椭圆的宽度。
|
||||
* int ROUND_RECTANGLEheight = 20; //构成圆角矩形的圆角的椭圆的高度。
|
||||
*/
|
||||
struct RouRectangle
|
||||
{
|
||||
int ROUND_RECTANGLEwidth = 20; //构成圆角矩形的圆角的椭圆的宽度。
|
||||
int ROUND_RECTANGLEheight = 20; //构成圆角矩形的圆角的椭圆的高度。
|
||||
};
|
||||
|
||||
// 消息框类型
|
||||
enum class MessageBoxType
|
||||
{
|
||||
OK, // 只有确定按钮
|
||||
OKCancel, // 确定和取消按钮
|
||||
YesNo, // 是和否按钮
|
||||
YesNoCancel, // 是、否和取消按钮
|
||||
RetryCancel, // 重试和取消按钮
|
||||
AbortRetryIgnore, // 中止、重试和忽略按钮
|
||||
};
|
||||
|
||||
// 消息框返回值
|
||||
enum class MessageBoxResult
|
||||
{
|
||||
OK = 1, // 确定按钮
|
||||
Cancel = 2, // 取消按钮
|
||||
Yes = 6, // 是按钮
|
||||
No = 7, // 否按钮
|
||||
Abort = 3, // 中止按钮
|
||||
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: 次轴=Y;VBox: 次轴=X;Grid: 单元内
|
||||
int alignY = Start; // Grid :控制单元内垂直(HBox / VBox通常只用 alignX)
|
||||
// Grid 专用(可先不做)
|
||||
int gridRow = 0, gridCol = 0, rowSpan = 1, colSpan = 1;
|
||||
// Flow 专用(可先不做)
|
||||
int flowBreak = 0; // 1=强制换行
|
||||
};
|
||||
#endif
|
||||
|
||||
/*
|
||||
* @枚举名称: TabPlacement
|
||||
* @功能描述: 定义了选项卡页签的不同位置
|
||||
*
|
||||
* @详细说明:
|
||||
* 根据选项卡页签的位置,选项卡页签可以有不同的布局方式。
|
||||
*
|
||||
* @成员说明:
|
||||
* Top, - 选项卡页签位于顶部
|
||||
* Bottom, - 选项卡页签位于底部
|
||||
* Left, - 选项卡页签位于左侧
|
||||
* Right - 选项卡页签位于右侧
|
||||
*
|
||||
* @使用示例:
|
||||
* TabPlacement placement = TabPlacement::Top;
|
||||
*/
|
||||
enum class TabPlacement
|
||||
{
|
||||
Top,
|
||||
Bottom,
|
||||
Left,
|
||||
Right
|
||||
};
|
||||
}
|
||||
136
include/StellarX/Dialog.h
Normal file
136
include/StellarX/Dialog.h
Normal file
@@ -0,0 +1,136 @@
|
||||
/*******************************************************************************
|
||||
* @类: Dialog
|
||||
* @摘要: 模态和非模态对话框控件,提供丰富的消息框功能
|
||||
* @描述:
|
||||
* 实现完整的对话框功能,支持多种按钮组合和异步结果回调。
|
||||
* 自动处理布局、背景保存恢复和生命周期管理。
|
||||
*
|
||||
* @特性:
|
||||
* - 支持六种标准消息框类型(OK、YesNo、YesNoCancel等)
|
||||
* - 模态和非模态两种工作模式
|
||||
* - 自动文本换行和尺寸计算
|
||||
* - 背景保存和恢复机制
|
||||
* - 工厂模式下的去重检测
|
||||
*
|
||||
* @使用场景: 用户提示、确认操作、输入请求等交互场景
|
||||
* @所属框架: 星垣(StellarX) GUI框架
|
||||
* @作者: 我在人间做废物
|
||||
******************************************************************************/
|
||||
|
||||
#pragma once
|
||||
#include"StellarX.h"
|
||||
|
||||
#define closeButtonWidth 30 //关闭按钮宽度
|
||||
#define closeButtonHeight 20 //关闭按钮高度 同时作为对话框标题栏高度
|
||||
#define functionButtonWidth 70 //按钮宽度
|
||||
#define functionButtonHeight 30 //按钮高度
|
||||
#define buttonMargin 10 //按钮间距
|
||||
|
||||
#define buttonAreaHeight 50 //按钮区域高度
|
||||
#define titleToTextMargin 10 //标题到文本的距离
|
||||
#define textToBorderMargin 10 //文本到边框的距离
|
||||
#define BorderWidth 3 //边框宽度
|
||||
class Dialog : public Canvas
|
||||
{
|
||||
Window& hWnd; //窗口引用
|
||||
|
||||
int textWidth = 0; //文本宽度
|
||||
int textHeight = 0; //文本高度
|
||||
int buttonNum = 0; // 按钮数量
|
||||
|
||||
StellarX::MessageBoxType type = StellarX::MessageBoxType::OK; //对话框类型
|
||||
std::string titleText = "提示"; //标题文本
|
||||
std::unique_ptr<Label> title = nullptr; //标题标签
|
||||
|
||||
std::string message; //提示信息
|
||||
std::vector<std::string> lines; //消息内容按行分割
|
||||
|
||||
|
||||
bool needsInitialization = true; //是否需要初始化
|
||||
bool close = false; //是否关闭
|
||||
bool modal = true; //是否模态
|
||||
|
||||
COLORREF backgroundColor = RGB(240, 240, 240); //背景颜色
|
||||
COLORREF borderColor = RGB(100, 100, 100); //边框颜色
|
||||
|
||||
COLORREF buttonTrueColor = RGB(211, 190, 190); //按钮被点击颜色
|
||||
COLORREF buttonFalseColor = RGB(215, 215, 215); //按钮未被点击颜色
|
||||
COLORREF buttonHoverColor = RGB(224, 224, 224); //按钮悬浮颜色
|
||||
|
||||
|
||||
Button* closeButton = nullptr; //关闭按钮
|
||||
|
||||
StellarX::MessageBoxResult result = StellarX::MessageBoxResult::Cancel; // 对话框结果
|
||||
|
||||
bool shouldClose = false; //是否应该关闭
|
||||
bool isCleaning = false; //是否正在清理
|
||||
bool pendingCleanup = false; //延迟清理
|
||||
public:
|
||||
StellarX::ControlText textStyle; // 字体样式
|
||||
// 清理方法声明
|
||||
void performDelayedCleanup();
|
||||
//工厂模式下调用非模态对话框时用来返回结果
|
||||
std::function<void(StellarX::MessageBoxResult)> resultCallback;
|
||||
//设置非模态获取结果的回调函数
|
||||
void SetResultCallback(std::function<void(StellarX::MessageBoxResult)> cb);
|
||||
//获取对话框消息,用以去重
|
||||
std::string GetCaption() const;
|
||||
//获取对话框消息,用以去重
|
||||
std::string GetText() const;
|
||||
|
||||
|
||||
public:
|
||||
Dialog(Window& hWnd, std::string text, std::string message = "对话框", StellarX::MessageBoxType type = StellarX::MessageBoxType::OK, bool modal = true);
|
||||
~Dialog();
|
||||
//绘制对话框
|
||||
void draw() override;
|
||||
//事件处理
|
||||
bool handleEvent(const ExMessage& msg) override;
|
||||
// 设置标题
|
||||
void SetTitle(const std::string& title);
|
||||
// 设置消息内容
|
||||
void SetMessage(const std::string& message);
|
||||
// 设置对话框类型
|
||||
void SetType(StellarX::MessageBoxType type);
|
||||
// 设置模态属性
|
||||
void SetModal(bool modal);
|
||||
// 设置对话框结果
|
||||
void SetResult(StellarX::MessageBoxResult result);
|
||||
// 获取对话框结果
|
||||
StellarX::MessageBoxResult GetResult() const;
|
||||
|
||||
//获取对话框类型
|
||||
bool model() const override;
|
||||
|
||||
// 显示对话框
|
||||
void Show();
|
||||
// 关闭对话框
|
||||
void Close();
|
||||
//初始化
|
||||
void setInitialization(bool init);
|
||||
|
||||
|
||||
private:
|
||||
// 初始化按钮
|
||||
void initButtons();
|
||||
// 初始化关闭按钮
|
||||
void initCloseButton();
|
||||
// 初始化标题
|
||||
void initTitle();
|
||||
// 按行分割消息内容
|
||||
void splitMessageLines();
|
||||
// 获取文本大小
|
||||
void getTextSize();
|
||||
//初始化对话框尺寸
|
||||
void initDialogSize();
|
||||
void saveBackground(int x, int y, int w, int h)override;
|
||||
|
||||
void restBackground()override;
|
||||
|
||||
|
||||
// 清除所有控件
|
||||
void clearControls();
|
||||
//创建对话框按钮
|
||||
std::unique_ptr<Button> createDialogButton(int x, int y, const std::string& text);
|
||||
void requestRepaint(Control* parent) override;
|
||||
};
|
||||
43
include/StellarX/MessageBox.h
Normal file
43
include/StellarX/MessageBox.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/*******************************************************************************
|
||||
* @类: MessageBox
|
||||
* @摘要: 消息框工厂类,提供简化的对话框创建接口
|
||||
* @描述:
|
||||
* 封装对话框的创建和显示逻辑,提供静态方法供快速调用。
|
||||
* 支持模态阻塞和非模态回调两种使用方式。
|
||||
*
|
||||
* @特性:
|
||||
* - 静态方法调用,无需实例化
|
||||
* - 自动处理模态和非模态的逻辑差异
|
||||
* - 集成到窗口的对话框管理系统中
|
||||
* - 提供去重机制防止重复对话框
|
||||
*
|
||||
* @使用场景: 快速创建标准消息框,减少样板代码
|
||||
* @所属框架: 星垣(StellarX) GUI框架
|
||||
* @作者: 我在人间做废物
|
||||
******************************************************************************/
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include "CoreTypes.h"
|
||||
#include "Dialog.h"
|
||||
#include "Window.h"
|
||||
|
||||
namespace StellarX
|
||||
{
|
||||
class MessageBox
|
||||
{
|
||||
public:
|
||||
// 模态:阻塞直到关闭,返回结果
|
||||
static MessageBoxResult showModal(Window& wnd,
|
||||
const std::string& text,
|
||||
const std::string& caption = "提示",
|
||||
MessageBoxType type = MessageBoxType::OK);
|
||||
|
||||
// 非模态:立即返回,通过回调异步获取结果
|
||||
static void showAsync(Window& wnd,
|
||||
const std::string& text,
|
||||
const std::string& caption = "提示",
|
||||
MessageBoxType type = MessageBoxType::OK,
|
||||
std::function<void(MessageBoxResult)> onResult = nullptr);
|
||||
};
|
||||
} // namespace StellarX
|
||||
@@ -1,7 +1,7 @@
|
||||
/*******************************************************************************
|
||||
* @文件: StellarX.h
|
||||
* @摘要: 星垣(StellarX) GUI框架 - 主包含头文件
|
||||
* @版本: v1.0.0
|
||||
* @版本: v2.2.1
|
||||
* @描述:
|
||||
* 一个为Windows平台打造的轻量级、模块化C++ GUI框架。
|
||||
* 基于EasyX图形库,提供简洁易用的API和丰富的控件。
|
||||
@@ -10,8 +10,8 @@
|
||||
* 内部包含顺序经过精心设计,确保所有依赖关系正确解析。
|
||||
*
|
||||
* @作者: 我在人间做废物
|
||||
* @邮箱: [3150131407@qq.com][]
|
||||
* @仓库: https://github.com/Ysm-04/StellarX
|
||||
* @邮箱: [3150131407@qq.com] | [ysm3150131407@gmail.com]
|
||||
* @仓库: [https://github.com/Ysm-04/StellarX]
|
||||
*
|
||||
* @许可证: MIT License
|
||||
* @版权: Copyright (c) 2025 我在人间做废物
|
||||
@@ -22,16 +22,21 @@
|
||||
* @包含顺序:
|
||||
* 1. CoreTypes.h - 基础类型定义
|
||||
* 2. Control.h - 控件基类
|
||||
* 3. 其他具体控件头文件
|
||||
* 3. ...其他具体控件头文件
|
||||
* 4. Dialog:继承自 Canvas(Dialog 为可包含子控件的对话框容器)
|
||||
* 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"
|
||||
#include"TabControl.h"
|
||||
|
||||
68
include/StellarX/TabControl.h
Normal file
68
include/StellarX/TabControl.h
Normal file
@@ -0,0 +1,68 @@
|
||||
/*******************************************************************************
|
||||
* @类: TabControl
|
||||
* @摘要: 选项卡容器控件,管理“页签按钮 + 对应页面(Canvas)”
|
||||
* @描述:
|
||||
* 提供页签栏布局(上/下/左/右)、选中切换、页内容区域定位;
|
||||
* 与 Button 一起工作,支持窗口大小变化、可见性联动与脏区重绘。
|
||||
*
|
||||
* @特性:
|
||||
* - 页签栏四向排列(Top / Bottom / Left / Right)
|
||||
* - 一键添加“页签+页”或为指定页添加子控件
|
||||
* - 获取/设置当前激活页签索引
|
||||
* - 自适应窗口变化,重算页签与页面区域
|
||||
* - 与 Button 的 TOGGLE 模式联动显示/隐藏页面
|
||||
*
|
||||
* @使用场景: 在同一区域内承载多张页面,使用页签进行快速切换
|
||||
* @所属框架: 星垣(StellarX) GUI框架
|
||||
* @作者: 我在人间做废物
|
||||
******************************************************************************/
|
||||
|
||||
#pragma once
|
||||
#include "CoreTypes.h"
|
||||
#include "Button.h"
|
||||
#include "Canvas.h"
|
||||
#define BUTMINHEIGHT 15
|
||||
#define BUTMINWIDTH 30
|
||||
class TabControl :public Canvas
|
||||
{
|
||||
int tabBarHeight = BUTMINWIDTH; //页签栏高度
|
||||
StellarX::TabPlacement tabPlacement = StellarX::TabPlacement::Top ; //页签排列方式
|
||||
std::vector<std::pair<std::unique_ptr<Button>,std::unique_ptr<Canvas>>> controls; //页签/页列表
|
||||
|
||||
private:
|
||||
using Canvas::addControl; // 禁止外部误用
|
||||
void addControl(std::unique_ptr<Control>) = delete; // 精准禁用该重载
|
||||
private:
|
||||
inline void initTabBar();
|
||||
inline void initTabPage();
|
||||
public:
|
||||
TabControl();
|
||||
TabControl(int x,int y,int width,int height);
|
||||
~TabControl();
|
||||
|
||||
void draw() override;
|
||||
bool handleEvent(const ExMessage& msg) override;
|
||||
|
||||
//添加页签+页
|
||||
void add(std::pair<std::unique_ptr<Button> ,std::unique_ptr<Canvas>>&& control);
|
||||
//添加为某个页添加控件
|
||||
void add(std::string tabText,std::unique_ptr<Control> control);
|
||||
//设置页签位置
|
||||
void setTabPlacement(StellarX::TabPlacement placement);
|
||||
//设置页签栏高度 两侧排列时为宽度
|
||||
void setTabBarHeight(int height);
|
||||
//设置不可见后传递给子控件重写
|
||||
void setIsVisible(bool visible) override;
|
||||
void onWindowResize() override;
|
||||
//获取当前激活页签索引
|
||||
int getActiveIndex() const;
|
||||
//设置当前激活页签索引
|
||||
void setActiveIndex(int idx);
|
||||
//获取页签数量
|
||||
int count() const;
|
||||
//通过页签文本返回索引
|
||||
int indexOf(const std::string& tabText) const;
|
||||
void setDirty(bool dirty) override;
|
||||
void requestRepaint(Control* parent)override;
|
||||
};
|
||||
|
||||
@@ -1,31 +1,44 @@
|
||||
#pragma once
|
||||
/*******************************************************************************
|
||||
* @类: Label
|
||||
* @摘要: 简单文本标签控件,用于显示静态文本
|
||||
* @描述:
|
||||
* 提供基本的文本显示功能,支持透明背景和自定义样式。
|
||||
* 不支持用户交互,专注于文本呈现。
|
||||
*
|
||||
* @特性:
|
||||
* - 支持背景透明/不透明模式
|
||||
* - 完整的文本样式控制(字体、颜色、效果)
|
||||
* - 自动适应文本内容
|
||||
* - 轻量级无事件处理开销
|
||||
*
|
||||
* @使用场景: 显示说明文字、标题、状态信息等静态内容
|
||||
* @所属框架: 星垣(StellarX) GUI框架
|
||||
* @作者: 我在人间做废物
|
||||
******************************************************************************/
|
||||
|
||||
#pragma once
|
||||
#include "Control.h"
|
||||
// 标签控件,用于显示文本
|
||||
// 特点:
|
||||
// - 支持背景透明/不透明模式
|
||||
// - 不支持用户交互(无事件处理)
|
||||
// - 自动适应文本内容大小
|
||||
|
||||
class Label : public Control
|
||||
{
|
||||
std::string text; //标签文本
|
||||
COLORREF textColor; //标签文本颜色
|
||||
COLORREF textBkColor; //标签背景颜色
|
||||
bool textBkDisap = false; //标签背景是否透明
|
||||
|
||||
//标签事件处理(标签无事件)不实现具体代码
|
||||
void handleEvent(const ExMessage& msg) override {}
|
||||
bool handleEvent(const ExMessage& msg) override { return false; }
|
||||
//用来检查对话框是否模态,此控件不做实现
|
||||
bool model() const override { return false; };
|
||||
public:
|
||||
StellarX::ControlText textStyle; //标签文本样式
|
||||
public:
|
||||
Label();
|
||||
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);
|
||||
//设置标签文本颜色
|
||||
void setTextColor(COLORREF color);
|
||||
//设置标签背景颜色
|
||||
void setTextBkColor(COLORREF color);
|
||||
//设置标签文本
|
||||
|
||||
@@ -1,19 +1,20 @@
|
||||
/*******************************************************************************
|
||||
* @文件: Table.h
|
||||
* @摘要: 高级表格控件,支持分页显示
|
||||
* @类: Table
|
||||
* @摘要: 高级表格控件,支持分页显示和大数据量展示
|
||||
* @描述:
|
||||
* 提供完整的数据表格功能,包括表头、数据行、分页和导航按钮。
|
||||
* 自动计算列宽和行高,支持自定义样式
|
||||
* 提供完整的数据表格功能,包括表头、数据行、分页导航等。
|
||||
* 自动计算列宽行高,支持自定义样式和交互。
|
||||
*
|
||||
* @实现机制:
|
||||
* 1. 使用二维向量存储数据
|
||||
* 2. 通过分页算法计算显示范围
|
||||
* 3. 使用内部按钮和标签实现分页UI
|
||||
* 4. 通过背景缓存优化渲染性能
|
||||
* @特性:
|
||||
* - 自动分页和页码计算
|
||||
* - 可配置的每页行数
|
||||
* - 自定义边框样式和填充模式
|
||||
* - 翻页按钮和页码显示
|
||||
* - 背景缓存优化渲染性能
|
||||
*
|
||||
* @使用场景: 数据展示、报表生成、记录浏览等表格需求
|
||||
* @所属框架: 星垣(StellarX) GUI框架
|
||||
* @作者: 我在人间做废物
|
||||
* @日期: September 2025
|
||||
******************************************************************************/
|
||||
|
||||
#pragma once
|
||||
@@ -21,6 +22,34 @@
|
||||
#include "Button.h"
|
||||
#include "Label.h"
|
||||
|
||||
// === Table metrics (layout) ===
|
||||
#define TABLE_PAD_X 10 // 单元格左右内边距
|
||||
#define TABLE_PAD_Y 5 // 单元格上下内边距
|
||||
#define TABLE_COL_GAP 20 // 列间距(列与列之间)
|
||||
#define TABLE_HEADER_EXTRA 10 // 表头额外高度(若不想复用 pad 计算)
|
||||
#define TABLE_ROW_EXTRA 10 // 行额外高度(同上;或直接用 2*TABLE_PAD_Y)
|
||||
#define TABLE_BTN_GAP 12 // 页码与按钮的水平间距
|
||||
#define TABLE_BTN_PAD_H 12 // 按钮水平 padding
|
||||
#define TABLE_BTN_PAD_V 0 // 按钮垂直 padding(initButton)
|
||||
#define TABLE_BTN_TEXT_PAD_V 8 // 计算页脚高度时的按钮文字垂直 padding(initTextWaH)
|
||||
#define TABLE_FOOTER_PAD 16 // 页脚额外高度(底部留白)
|
||||
#define TABLE_FOOTER_BLANK 8 // 页脚顶部留白
|
||||
#define TABLE_PAGE_TEXT_OFFSET_X (-40) // 页码文本的临时水平修正
|
||||
|
||||
// === Table defaults (theme) ===
|
||||
#define TABLE_DEFAULT_ROWS_PER_PAGE 5
|
||||
#define TABLE_DEFAULT_BORDER_WIDTH 1
|
||||
#define TABLE_DEFAULT_BORDER_COLOR RGB(0,0,0)
|
||||
#define TABLE_DEFAULT_BG_COLOR RGB(255,255,255)
|
||||
|
||||
// === Strings (i18n ready) ===
|
||||
#define TABLE_STR_PREV "上一页"
|
||||
#define TABLE_STR_NEXT "下一页"
|
||||
#define TABLE_STR_PAGE_PREFIX "第"
|
||||
#define TABLE_STR_PAGE_MID "页/共"
|
||||
#define TABLE_STR_PAGE_SUFFIX "页"
|
||||
|
||||
|
||||
class Table :public Control
|
||||
{
|
||||
private:
|
||||
@@ -28,19 +57,16 @@ private:
|
||||
std::vector<std::string> headers; // 表格表头
|
||||
std::string pageNumtext = "页码标签"; // 页码标签文本
|
||||
|
||||
int tableBorderWidth = 1; // 边框宽度
|
||||
int tableBorderWidth = TABLE_DEFAULT_BORDER_WIDTH; // 边框宽度
|
||||
|
||||
std::vector<int> colWidths; // 每列的宽度
|
||||
std::vector<int> lineHeights; // 每行的高度
|
||||
|
||||
IMAGE* saveBkImage = nullptr;
|
||||
|
||||
int rowsPerPage = 5; // 每页显示的行数
|
||||
int rowsPerPage = TABLE_DEFAULT_ROWS_PER_PAGE; // 每页显示的行数
|
||||
int currentPage = 1; // 当前页码
|
||||
int totalPages = 1; // 总页数
|
||||
|
||||
bool isShowPageButton = true; // 是否显示翻页按钮
|
||||
bool dirty = true; // 是否需要重绘
|
||||
bool isNeedDrawHeaders = true; // 是否需要绘制表头
|
||||
bool isNeedCellSize = true; // 是否需要计算单元格尺寸
|
||||
|
||||
@@ -56,8 +82,8 @@ private:
|
||||
|
||||
StellarX::FillMode tableFillMode = StellarX::FillMode::Solid; //填充模式
|
||||
StellarX::LineStyle tableLineStyle = StellarX::LineStyle::Solid; // 线型
|
||||
COLORREF tableBorderClor = RGB(0, 0, 0); // 表格边框颜色
|
||||
COLORREF tableBkClor = RGB(255, 255, 255); // 表格背景颜色
|
||||
COLORREF tableBorderClor = TABLE_DEFAULT_BORDER_COLOR; // 表格边框颜色
|
||||
COLORREF tableBkClor = TABLE_DEFAULT_BG_COLOR; // 表格背景颜色
|
||||
|
||||
void initTextWaH(); //初始化文本像素宽度和高度
|
||||
void initButton(); //初始化翻页按钮
|
||||
@@ -67,6 +93,9 @@ private:
|
||||
void drawHeader(); //绘制表头
|
||||
void drawPageNum(); //绘制页码信息
|
||||
void drawButton(); //绘制翻页按钮
|
||||
private:
|
||||
//用来检查对话框是否模态,此控件不做实现
|
||||
bool model() const override { return false; };
|
||||
public:
|
||||
StellarX::ControlText textStyle; // 文本样式
|
||||
|
||||
@@ -77,15 +106,13 @@ public:
|
||||
// 绘制表格
|
||||
void draw() override;
|
||||
//事件处理
|
||||
void handleEvent(const ExMessage& msg) override;
|
||||
|
||||
//************************** 设置属性 *****************************/
|
||||
bool handleEvent(const ExMessage& msg) override;
|
||||
|
||||
//设置表头
|
||||
void setHeaders(std::initializer_list<std::string> headers);
|
||||
//设置表格数据
|
||||
void setData(const std::vector<std::string>& data);
|
||||
void setData(const std::initializer_list<std::vector<std::string>>& data);
|
||||
void setData(std::vector<std::string> data);
|
||||
void setData(std::initializer_list<std::vector<std::string>> data);
|
||||
//设置每页显示的行数
|
||||
void setRowsPerPage(int rows);
|
||||
//设置是否显示翻页按钮
|
||||
|
||||
@@ -1,20 +1,31 @@
|
||||
#pragma once
|
||||
/*******************************************************************************
|
||||
* @类: TextBox
|
||||
* @摘要: 文本框控件,支持输入和只读两种模式
|
||||
* @描述:
|
||||
* 提供文本输入和显示功能,集成EasyX的InputBox用于数据输入。
|
||||
* 支持有限的形状样式和视觉定制。
|
||||
*
|
||||
* @特性:
|
||||
* - 两种工作模式:输入模式和只读模式
|
||||
* - 最大字符长度限制
|
||||
* - 集成系统输入框简化文本输入
|
||||
* - 支持四种矩形形状变体
|
||||
*
|
||||
* @使用场景: 数据输入、文本显示、表单字段等
|
||||
* @所属框架: 星垣(StellarX) GUI框架
|
||||
* @作者: 我在人间做废物
|
||||
******************************************************************************/
|
||||
#pragma once
|
||||
#include "Control.h"
|
||||
|
||||
// 文本框控件,支持输入和只读两种模式
|
||||
// 特殊说明:
|
||||
// - 在INPUT_MODE下点击会调用EasyX的InputBox
|
||||
// - 在READONLY_MODE下点击会显示信息对话框
|
||||
// - 最大字符长度受maxCharLen限制
|
||||
|
||||
class TextBox : public Control
|
||||
{
|
||||
std::string text; //文本
|
||||
StellarX::TextBoxmode mode; //模式
|
||||
StellarX::ControlShape shape; //形状
|
||||
bool dirty = true; //是否重绘
|
||||
bool click = false; //是否点击
|
||||
int maxCharLen = 10;//最大字符长度
|
||||
size_t maxCharLen = 10;//最大字符长度
|
||||
COLORREF textBoxBkClor = RGB(255, 255, 255); //背景颜色
|
||||
COLORREF textBoxBorderClor = RGB(0,0,0); //边框颜色
|
||||
|
||||
@@ -23,22 +34,26 @@ public:
|
||||
|
||||
TextBox(int x, int y, int width, int height, std::string text = "", StellarX::TextBoxmode mode = StellarX::TextBoxmode::INPUT_MODE, StellarX::ControlShape shape = StellarX::ControlShape::RECTANGLE);
|
||||
void draw() override;
|
||||
void handleEvent(const ExMessage& msg) override;
|
||||
bool handleEvent(const ExMessage& msg) override;
|
||||
//设置模式
|
||||
|
||||
void setMode(StellarX::TextBoxmode mode);
|
||||
//设置可输入最大字符长度
|
||||
void setMaxCharLen(int len);
|
||||
void setMaxCharLen(size_t len);
|
||||
//设置形状
|
||||
void setTextBoxshape(StellarX::ControlShape shape);
|
||||
//设置边框颜色
|
||||
void setTextBoxBorder(COLORREF color);
|
||||
//设置背景颜色
|
||||
void setTextBoxBk(COLORREF color);
|
||||
//设置文本
|
||||
void setText(std::string text);
|
||||
|
||||
//获取文本
|
||||
std::string getText() const;
|
||||
|
||||
private:
|
||||
//用来检查对话框是否模态,此控件不做实现
|
||||
bool model() const override { return false; };
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -1,34 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#pragma once
|
||||
#include"Control.h"
|
||||
//窗口模式
|
||||
//#define EX_DBLCLKS 1 - 在绘图窗口中支持鼠标双击事件。
|
||||
//#define EX_NOCLOSE 2 - 禁用绘图窗口的关闭按钮。
|
||||
//#define EX_NOMINIMIZE 3 - 禁用绘图窗口的最小化按钮。
|
||||
//#define EX_SHOWCONSOLE 4 - 显示控制台窗口。
|
||||
/*******************************************************************************
|
||||
* @类: Window
|
||||
* @摘要: 应用程序主窗口类
|
||||
* @摘要: 应用程序主窗口类,管理窗口生命周期和消息循环
|
||||
* @描述:
|
||||
* 负责创建和管理应用程序的主窗口,是所有控件的根容器。
|
||||
* 处理消息循环、事件分发和窗口生命周期管理。
|
||||
* 创建和管理应用程序主窗口,作为所有控件的根容器。
|
||||
* 处理消息分发、事件循环和渲染调度。
|
||||
*
|
||||
* @重要说明:
|
||||
* - 使用 initgraph() 创建窗口
|
||||
* - 使用 BeginBatchDraw()/EndBatchDraw() 实现双缓冲
|
||||
* - 使用 getmessage() 处理消息循环
|
||||
* @特性:
|
||||
* - 多种窗口模式配置(双缓冲、控制台等)
|
||||
* - 背景图片和颜色支持
|
||||
* - 集成的对话框管理系统
|
||||
* - 完整的消息处理循环
|
||||
* - 控件和对话框的生命周期管理
|
||||
*
|
||||
* @使用场景: 应用程序主窗口,GUI程序的入口和核心
|
||||
* @所属框架: 星垣(StellarX) GUI框架
|
||||
* @作者: 我在人间做废物
|
||||
******************************************************************************/
|
||||
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; //对话框管理
|
||||
|
||||
|
||||
public:
|
||||
bool dialogClose = false; //是否有对话框关闭
|
||||
|
||||
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 = "窗口");
|
||||
@@ -37,9 +50,8 @@ public:
|
||||
void draw();
|
||||
void draw(std::string pImgFile);
|
||||
//事件循环
|
||||
void runEventLoop();
|
||||
int runEventLoop();
|
||||
//设置窗口背景图片
|
||||
|
||||
void setBkImage(std::string pImgFile);
|
||||
//设置窗口背景颜色
|
||||
void setBkcolor(COLORREF c);
|
||||
@@ -47,6 +59,27 @@ public:
|
||||
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();
|
||||
|
||||
};
|
||||
|
||||
|
||||
453
src/Button.cpp
453
src/Button.cpp
@@ -1,4 +1,4 @@
|
||||
#include "StellarX/Button.h"
|
||||
#include "Button.h"
|
||||
|
||||
Button::Button(int x, int y, int width, int height, const std::string text, StellarX::ButtonMode mode, StellarX::ControlShape shape)
|
||||
: Control(x, y, width, height)
|
||||
@@ -17,9 +17,134 @@ 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::setTooltipStyle(COLORREF text, COLORREF bk, bool transparent)
|
||||
{
|
||||
tipLabel.textStyle.color = text;
|
||||
tipLabel.setTextBkColor(bk);
|
||||
tipLabel.setTextdisap(transparent);
|
||||
}
|
||||
|
||||
void Button::setTooltipTextsForToggle(const std::string& onText, const std::string& offText)
|
||||
{
|
||||
tipTextOn = onText;
|
||||
tipTextOff = offText;
|
||||
tipUserOverride = true;
|
||||
}
|
||||
|
||||
void Button::initButton(const std::string text, StellarX::ButtonMode mode, StellarX::ControlShape shape, COLORREF ct, COLORREF cf, COLORREF ch)
|
||||
{
|
||||
this->id = "Button";
|
||||
this->text = text;
|
||||
this->mode = mode;
|
||||
this->shape = shape;
|
||||
@@ -28,6 +153,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.textStyle.color = (RGB(167, 170, 172));
|
||||
tipLabel.setTextBkColor(RGB(255, 255, 255));
|
||||
tipLabel.setTextdisap(false);
|
||||
tipLabel.textStyle = this->textStyle; // 复用按钮字体样式
|
||||
}
|
||||
|
||||
|
||||
@@ -40,103 +173,125 @@ Button::~Button()
|
||||
|
||||
void Button::draw()
|
||||
{
|
||||
if (dirty)
|
||||
{
|
||||
if (!dirty || !show)return;
|
||||
|
||||
//保存当前样式和颜色
|
||||
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(buttonFillMode, (int)buttonFillIma, buttonFileIMAGE);
|
||||
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))
|
||||
{
|
||||
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);
|
||||
if ((saveBkX != this->x) || (saveBkY != this->y) || (!hasSnap) || (saveWidth != this->width) || (saveHeight != this->height) || !saveBkImage)
|
||||
saveBackground(this->x, this->y, this->width, this->height);
|
||||
// 恢复背景(清除旧内容)
|
||||
restBackground();
|
||||
//根据按钮形状绘制
|
||||
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; //标记按钮不需要重绘
|
||||
}
|
||||
|
||||
}
|
||||
// 处理鼠标事件,检测点击和悬停状态
|
||||
// 根据按钮模式和形状进行不同的处理
|
||||
void Button::handleEvent(const ExMessage& msg)
|
||||
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,25 +314,31 @@ void Button::handleEvent(const ExMessage& msg)
|
||||
// 处理鼠标点击事件
|
||||
if (msg.message == WM_LBUTTONDOWN && hover && mode != StellarX::ButtonMode::DISABLED)
|
||||
{
|
||||
|
||||
if (mode == StellarX::ButtonMode::NORMAL)
|
||||
{
|
||||
click = true;
|
||||
dirty = true;
|
||||
consume = true;
|
||||
}
|
||||
else if (mode == StellarX::ButtonMode::TOGGLE)
|
||||
{
|
||||
// TOGGLE模式在鼠标释放时处理
|
||||
}
|
||||
}
|
||||
// 处理鼠标释放事件
|
||||
// NORMAL 模式:鼠标在按钮上释放时才触发点击回调,如果移出区域则取消点击状态。
|
||||
// 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;
|
||||
// 使用新的flushmessage函数刷新消息队列:cite[2]:cite[3]
|
||||
consume = true;
|
||||
hideTooltip();
|
||||
// 清除消息队列中积压的鼠标和键盘消息,防止本次点击事件被重复处理
|
||||
flushmessage(EX_MOUSE | EX_KEY);
|
||||
}
|
||||
else if (mode == StellarX::ButtonMode::TOGGLE)
|
||||
@@ -186,11 +347,15 @@ void Button::handleEvent(const ExMessage& msg)
|
||||
if (click && onToggleOnCallback) onToggleOnCallback();
|
||||
else if (!click && onToggleOffCallback) onToggleOffCallback();
|
||||
dirty = true;
|
||||
// 使用新的flushmessage函数刷新消息队列:cite[2]:cite[3]
|
||||
consume = true;
|
||||
refreshTooltipTextForState();
|
||||
hideTooltip();
|
||||
// 清除消息队列中积压的鼠标和键盘消息,防止本次点击事件被重复处理
|
||||
flushmessage(EX_MOUSE | EX_KEY);
|
||||
}
|
||||
}
|
||||
// 处理鼠标移出区域的情况
|
||||
|
||||
else if (msg.message == WM_MOUSEMOVE)
|
||||
{
|
||||
if (!hover && mode == StellarX::ButtonMode::NORMAL && click)
|
||||
@@ -199,22 +364,64 @@ void 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();
|
||||
}
|
||||
requestRepaint(parent);
|
||||
|
||||
if (tipEnabled && tipVisible)
|
||||
tipLabel.draw();
|
||||
|
||||
return consume;
|
||||
}
|
||||
|
||||
void Button::setOnClickListener(const std::function<void()>&& callback)
|
||||
@@ -233,18 +440,24 @@ void Button::setOnToggleOffListener(const std::function<void()>&& callback)
|
||||
|
||||
void Button::setbuttonMode(StellarX::ButtonMode mode)
|
||||
{
|
||||
if (this->mode == StellarX::ButtonMode::DISABLED && mode != StellarX::ButtonMode::DISABLED)
|
||||
textStyle.bStrikeOut = false;
|
||||
//取值范围参考 buttMode的枚举注释
|
||||
this->mode = mode;
|
||||
dirty = true; // 标记需要重绘
|
||||
}
|
||||
|
||||
int Button::setROUND_RECTANGLEwidth(int width)
|
||||
void Button::setROUND_RECTANGLEwidth(int width)
|
||||
{
|
||||
return rouRectangleSize.ROUND_RECTANGLEwidth = width;
|
||||
rouRectangleSize.ROUND_RECTANGLEwidth = width;
|
||||
this->dirty = true; // 标记需要重绘
|
||||
|
||||
}
|
||||
|
||||
int Button::setROUND_RECTANGLEheight(int height)
|
||||
void Button::setROUND_RECTANGLEheight(int height)
|
||||
{
|
||||
return rouRectangleSize.ROUND_RECTANGLEheight = height;
|
||||
rouRectangleSize.ROUND_RECTANGLEheight = height;
|
||||
this->dirty = true; // 标记需要重绘
|
||||
}
|
||||
|
||||
bool Button::isClicked() const
|
||||
@@ -252,26 +465,41 @@ bool Button::isClicked() const
|
||||
return this->click;
|
||||
}
|
||||
|
||||
void Button::setFillMode(int mode)
|
||||
void Button::setFillMode(StellarX::FillMode mode)
|
||||
{
|
||||
buttonFillMode = mode;
|
||||
this->buttonFillMode = mode;
|
||||
this->dirty = true; // 标记需要重绘
|
||||
}
|
||||
|
||||
void Button::setFillIma(StellarX::FillStyle ima)
|
||||
{
|
||||
buttonFillIma = ima;
|
||||
this->dirty = true;
|
||||
}
|
||||
|
||||
void Button::setFillIma(std::string imaNAme)
|
||||
{
|
||||
if (buttonFileIMAGE)
|
||||
{
|
||||
delete buttonFileIMAGE;
|
||||
buttonFileIMAGE = nullptr;
|
||||
}
|
||||
buttonFileIMAGE = new IMAGE;
|
||||
loadimage(buttonFileIMAGE, imaNAme.c_str(),width-x,height-y);
|
||||
loadimage(buttonFileIMAGE, imaNAme.c_str(),width,height);
|
||||
this->dirty = true;
|
||||
}
|
||||
|
||||
|
||||
void Button::setButtonBorder(COLORREF Border)
|
||||
{
|
||||
buttonBorderColor = Border;
|
||||
this->dirty = true;
|
||||
}
|
||||
|
||||
void Button::setButtonFalseColor(COLORREF color)
|
||||
{
|
||||
this->buttonFalseColor = color;
|
||||
this->dirty = true;
|
||||
}
|
||||
|
||||
void Button::setButtonText(const char* text)
|
||||
@@ -279,6 +507,10 @@ void Button::setButtonText(const char* text)
|
||||
this->text = 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::setButtonText(std::string text)
|
||||
@@ -287,11 +519,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)
|
||||
requestRepaint(parent);
|
||||
}
|
||||
|
||||
|
||||
@@ -315,7 +579,7 @@ StellarX::ControlShape Button::getButtonShape() const
|
||||
return this->shape;
|
||||
}
|
||||
|
||||
int Button::getFillMode() const
|
||||
StellarX::FillMode Button::getFillMode() const
|
||||
{
|
||||
return this->buttonFillMode;
|
||||
}
|
||||
@@ -345,6 +609,17 @@ StellarX::ControlText Button::getButtonTextStyle() const
|
||||
return this->textStyle;
|
||||
}
|
||||
|
||||
int Button::getButtonWidth() const
|
||||
{
|
||||
return this->width;
|
||||
}
|
||||
|
||||
int Button::getButtonHeight() const
|
||||
{
|
||||
return this->height;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Button::isMouseInCircle(int mouseX, int mouseY, int x, int y, int radius)
|
||||
{
|
||||
@@ -372,4 +647,50 @@ 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; // 用户显式设置过 tipText,保持不变
|
||||
if(mode==StellarX::ButtonMode::NORMAL)
|
||||
tipLabel.setText(tipTextClick);
|
||||
else if(mode==StellarX::ButtonMode::TOGGLE)
|
||||
tipLabel.setText(click ? tipTextOn : tipTextOff);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,10 +1,26 @@
|
||||
#include "StellarX/Canvas.h"
|
||||
#include "Canvas.h"
|
||||
|
||||
Canvas::Canvas()
|
||||
:Control(0, 0, 100, 100)
|
||||
{
|
||||
this->id = "Canvas";
|
||||
}
|
||||
|
||||
Canvas::Canvas(int x, int y, int width, int height)
|
||||
:Control(x, y, width, height) {}
|
||||
:Control(x, y, width, height)
|
||||
{
|
||||
this->id = "Canvas";
|
||||
}
|
||||
|
||||
void Canvas::clearAllControls()
|
||||
{
|
||||
controls.clear();
|
||||
}
|
||||
|
||||
|
||||
void Canvas::draw()
|
||||
{
|
||||
if (!dirty||!show)return;
|
||||
saveStyle();
|
||||
|
||||
setlinecolor(canvasBorderClor);//设置线色
|
||||
@@ -12,6 +28,10 @@ void Canvas::draw()
|
||||
setfillstyle((int)canvasFillMode);//设置填充模式
|
||||
setlinestyle((int)canvasLineStyle, canvaslinewidth);
|
||||
|
||||
if ((saveBkX != this->x) || (saveBkY != this->y) || (!hasSnap) || (saveWidth != this->width) || (saveHeight != this->height) || !saveBkImage)
|
||||
saveBackground(x, y, width, height);
|
||||
// 恢复背景(清除旧内容)
|
||||
restBackground();
|
||||
//根据画布形状绘制
|
||||
switch (shape)
|
||||
{
|
||||
@@ -30,22 +50,35 @@ void Canvas::draw()
|
||||
}
|
||||
// 绘制所有子控件
|
||||
for (auto& control : controls)
|
||||
{
|
||||
control->setDirty(true);
|
||||
control->draw();
|
||||
|
||||
}
|
||||
|
||||
restoreStyle();
|
||||
dirty = false; //标记画布不需要重绘
|
||||
}
|
||||
|
||||
void Canvas::handleEvent(const ExMessage& msg)
|
||||
bool Canvas::handleEvent(const ExMessage& msg)
|
||||
{
|
||||
for (auto& control : controls) {
|
||||
control->handleEvent(msg);
|
||||
if (!show)return false;
|
||||
bool consumed = false;
|
||||
bool anyDirty = false;
|
||||
|
||||
for (auto it = controls.rbegin(); it != controls.rend(); ++it)
|
||||
{
|
||||
consumed |= it->get()->handleEvent(msg);
|
||||
if (it->get()->isDirty()) anyDirty = true;
|
||||
}
|
||||
if (anyDirty) requestRepaint(parent);
|
||||
return consumed;
|
||||
}
|
||||
|
||||
void Canvas::addControl(std::unique_ptr<Control> control)
|
||||
{
|
||||
control->setParent(this);
|
||||
controls.push_back(std::move(control));
|
||||
dirty = true;
|
||||
}
|
||||
|
||||
void Canvas::setShape(StellarX::ControlShape shape)
|
||||
@@ -57,12 +90,14 @@ void Canvas::setShape(StellarX::ControlShape shape)
|
||||
case StellarX::ControlShape::ROUND_RECTANGLE:
|
||||
case StellarX::ControlShape::B_ROUND_RECTANGLE:
|
||||
this->shape = shape;
|
||||
dirty = true;
|
||||
break;
|
||||
case StellarX::ControlShape::CIRCLE:
|
||||
case StellarX::ControlShape::B_CIRCLE:
|
||||
case StellarX::ControlShape::ELLIPSE:
|
||||
case StellarX::ControlShape::B_ELLIPSE:
|
||||
this->shape = StellarX::ControlShape::RECTANGLE;
|
||||
dirty = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -70,26 +105,75 @@ void Canvas::setShape(StellarX::ControlShape shape)
|
||||
void Canvas::setCanvasfillMode(StellarX::FillMode mode)
|
||||
{
|
||||
this->canvasFillMode = mode;
|
||||
dirty = true;
|
||||
}
|
||||
|
||||
void Canvas::setBorderColor(COLORREF color)
|
||||
{
|
||||
this->canvasBorderClor = color;
|
||||
dirty = true;
|
||||
}
|
||||
|
||||
void Canvas::setCanvasBkColor(COLORREF color)
|
||||
{
|
||||
this->canvasBkClor = color;
|
||||
dirty = true;
|
||||
}
|
||||
|
||||
void Canvas::setCanvasLineStyle(StellarX::LineStyle style)
|
||||
{
|
||||
this->canvasLineStyle = style;
|
||||
dirty = true;
|
||||
}
|
||||
|
||||
|
||||
void Canvas::setLinewidth(int width)
|
||||
{
|
||||
this->canvaslinewidth = width;
|
||||
dirty = true;
|
||||
}
|
||||
|
||||
void Canvas::setIsVisible(bool visible)
|
||||
{
|
||||
this->show = visible;
|
||||
dirty = true;
|
||||
for (auto& control : controls)
|
||||
{
|
||||
control->setIsVisible(visible);
|
||||
control->setDirty(true);
|
||||
}
|
||||
if (!visible)
|
||||
this->updateBackground();
|
||||
}
|
||||
|
||||
void Canvas::setDirty(bool dirty)
|
||||
{
|
||||
this->dirty = dirty;
|
||||
for(auto& control : controls)
|
||||
control->setDirty(dirty);
|
||||
}
|
||||
|
||||
void Canvas::onWindowResize()
|
||||
{
|
||||
Control::onWindowResize(); // 先处理自己
|
||||
for (auto& ch : controls) // 再转发给所有子控件
|
||||
ch->onWindowResize();
|
||||
}
|
||||
|
||||
void Canvas::requestRepaint(Control* parent)
|
||||
{
|
||||
if (this == parent)
|
||||
{
|
||||
for (auto& control : controls)
|
||||
if (control->isDirty() && control->IsVisible())
|
||||
{
|
||||
control->draw();
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
onRequestRepaintAsRoot();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "StellarX/Control.h"
|
||||
#include "Control.h"
|
||||
#include<assert.h>
|
||||
|
||||
StellarX::ControlText& StellarX::ControlText::operator=(const ControlText& text)
|
||||
{
|
||||
@@ -41,24 +42,95 @@ bool StellarX::ControlText::operator!=(const ControlText& text)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
void Control::setIsVisible(bool show)
|
||||
{
|
||||
if (!show)
|
||||
this->updateBackground();
|
||||
this->show = show;
|
||||
}
|
||||
void Control::onWindowResize()
|
||||
{
|
||||
// 自己:丢快照 + 标脏
|
||||
discardBackground();
|
||||
setDirty(true);
|
||||
}
|
||||
// 保存当前的绘图状态(字体、颜色、线型等)
|
||||
// 在控件绘制前调用,确保不会影响全局绘图状态
|
||||
void Control::saveStyle()
|
||||
{
|
||||
gettextstyle(¤tFont); // 获取当前字体样式
|
||||
currentColor = gettextcolor(); // 获取当前字体颜色
|
||||
currentBorderColor = getlinecolor(); //保存当前边框颜色
|
||||
|
||||
gettextstyle(currentFont); // 获取当前字体样式
|
||||
*currentColor = gettextcolor(); // 获取当前字体颜色
|
||||
*currentBorderColor = getlinecolor(); //保存当前边框颜色
|
||||
getlinestyle(currentLineStyle); //保存当前线型
|
||||
currentBkColor = getfillcolor(); //保存当前填充色
|
||||
*currentBkColor = getfillcolor(); //保存当前填充色
|
||||
}
|
||||
// 恢复之前保存的绘图状态
|
||||
// 在控件绘制完成后调用,恢复全局绘图状态
|
||||
void Control::restoreStyle()
|
||||
{
|
||||
settextstyle(¤tFont); // 恢复默认字体样式
|
||||
settextcolor(currentColor); // 恢复默认字体颜色
|
||||
setfillcolor(currentBkColor);
|
||||
settextstyle(currentFont); // 恢复默认字体样式
|
||||
settextcolor(*currentColor); // 恢复默认字体颜色
|
||||
setfillcolor(*currentBkColor);
|
||||
setlinestyle(currentLineStyle);
|
||||
setlinecolor(currentBorderColor);
|
||||
setlinecolor(*currentBorderColor);
|
||||
setfillstyle(BS_SOLID);//恢复填充
|
||||
}
|
||||
|
||||
void Control::requestRepaint(Control* parent)
|
||||
{
|
||||
if (parent) parent->requestRepaint(parent); // 向上冒泡
|
||||
else onRequestRepaintAsRoot(); // 到根控件/窗口兜底
|
||||
}
|
||||
|
||||
void Control::onRequestRepaintAsRoot()
|
||||
{
|
||||
|
||||
discardBackground();
|
||||
setDirty(true);
|
||||
draw(); // 只有“无父”时才允许立即画,不会被谁覆盖
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
restBackground();
|
||||
discardBackground();
|
||||
}
|
||||
|
||||
696
src/Dialog.cpp
Normal file
696
src/Dialog.cpp
Normal file
@@ -0,0 +1,696 @@
|
||||
#include "Dialog.h"
|
||||
|
||||
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)
|
||||
{
|
||||
this->id = "Dialog";
|
||||
show = false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Dialog::~Dialog()
|
||||
{
|
||||
}
|
||||
|
||||
void Dialog::draw()
|
||||
{
|
||||
if(!show)
|
||||
{
|
||||
// 如果对话框不可见且需要清理,执行清理
|
||||
if (pendingCleanup && !isCleaning)
|
||||
{
|
||||
performDelayedCleanup();
|
||||
}
|
||||
return;
|
||||
}
|
||||
// 如果需要初始化,则执行初始化
|
||||
if (needsInitialization && show)
|
||||
{
|
||||
initDialogSize();
|
||||
needsInitialization = false;
|
||||
}
|
||||
|
||||
if (dirty && show)
|
||||
{
|
||||
// 保存当前绘图状态
|
||||
saveStyle();
|
||||
|
||||
Canvas::setBorderColor(this->borderColor);
|
||||
Canvas::setLinewidth(BorderWidth);
|
||||
Canvas::setCanvasBkColor(this->backgroundColor);
|
||||
Canvas::setShape(StellarX::ControlShape::ROUND_RECTANGLE);
|
||||
|
||||
if ((saveBkX != this->x) || (saveBkY != this->y) || (!hasSnap) || (saveWidth != this->width) || (saveHeight != this->height) || !saveBkImage)
|
||||
saveBackground(this->x, this->y, this->width, this->height);
|
||||
//设置所有控件为脏状态
|
||||
/*for(auto& c :this->controls)
|
||||
c->setDirty(true);*/
|
||||
restBackground();
|
||||
Canvas::draw();
|
||||
|
||||
//绘制消息文本
|
||||
settextcolor(textStyle.color);
|
||||
|
||||
//设置字体样式
|
||||
settextstyle(textStyle.nHeight, textStyle.nWidth, textStyle.lpszFace,
|
||||
textStyle.nEscapement, textStyle.nOrientation, textStyle.nWeight,
|
||||
textStyle.bItalic, textStyle.bUnderline, textStyle.bStrikeOut);
|
||||
|
||||
|
||||
int ty = y + closeButtonHeight + titleToTextMargin; // 文本起始Y坐标
|
||||
for (auto line:lines)
|
||||
{
|
||||
int tx = this->x + ((this->width - textwidth(line.c_str())) / 2); // 文本起始X坐标
|
||||
outtextxy(tx, ty, LPCTSTR(line.c_str()));
|
||||
ty = ty + textheight(LPCTSTR(line.c_str())) + 5; // 每行文本高度加5像素间距
|
||||
}
|
||||
|
||||
// 恢复绘图状态
|
||||
restoreStyle();
|
||||
|
||||
dirty = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool Dialog::handleEvent(const ExMessage& msg)
|
||||
{
|
||||
bool consume = false;
|
||||
if (!show)
|
||||
{
|
||||
if (pendingCleanup && !isCleaning)
|
||||
{
|
||||
performDelayedCleanup();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// 如果正在清理或标记为待清理,则不处理事件
|
||||
if (pendingCleanup || isCleaning)
|
||||
return false;
|
||||
// 模态对话框:点击对话框外部区域时,发出提示音(\a)并吞噬该事件,不允许操作背景内容。
|
||||
if (modal && msg.message == WM_LBUTTONUP &&
|
||||
(msg.x < x || msg.x > x + width || msg.y < y || msg.y > y + height))
|
||||
{
|
||||
std::cout << "\a" << std::endl;
|
||||
// 模态对话框不允许点击外部区域
|
||||
return true;
|
||||
}
|
||||
|
||||
// 将事件传递给子控件处理
|
||||
if (!consume)
|
||||
consume = Canvas::handleEvent(msg);
|
||||
|
||||
// 每次事件处理后检查是否需要执行延迟清理
|
||||
if (pendingCleanup && !isCleaning)
|
||||
performDelayedCleanup();
|
||||
return consume;
|
||||
}
|
||||
|
||||
void Dialog::SetTitle(const std::string& title)
|
||||
{
|
||||
this->titleText = title;
|
||||
if (this->title)
|
||||
{
|
||||
this->title->setText(title);
|
||||
}
|
||||
dirty = true;
|
||||
}
|
||||
|
||||
void Dialog::SetMessage(const std::string& message)
|
||||
{
|
||||
this->message = message;
|
||||
splitMessageLines();
|
||||
getTextSize();
|
||||
dirty = true;
|
||||
}
|
||||
|
||||
void Dialog::SetType(StellarX::MessageBoxType type)
|
||||
{
|
||||
this->type = type;
|
||||
// 重新初始化按钮
|
||||
initButtons();
|
||||
dirty = true;
|
||||
}
|
||||
|
||||
void Dialog::SetModal(bool modal)
|
||||
{
|
||||
this->modal = modal;
|
||||
}
|
||||
|
||||
|
||||
void Dialog::SetResult(StellarX::MessageBoxResult result)
|
||||
{
|
||||
this->result = result;
|
||||
}
|
||||
|
||||
StellarX::MessageBoxResult Dialog::GetResult() const
|
||||
{
|
||||
return this->result;
|
||||
}
|
||||
|
||||
bool Dialog::model() const
|
||||
{
|
||||
return modal;
|
||||
}
|
||||
|
||||
void Dialog::Show()
|
||||
{
|
||||
if (pendingCleanup)
|
||||
performDelayedCleanup();
|
||||
|
||||
show = true;
|
||||
dirty = true;
|
||||
needsInitialization = true;
|
||||
close = false;
|
||||
shouldClose = false;
|
||||
|
||||
if (modal)
|
||||
{
|
||||
// 模态对话框需要阻塞当前线程直到对话框关闭
|
||||
while (show && !close)
|
||||
{
|
||||
|
||||
// 处理消息
|
||||
ExMessage msg;
|
||||
if (peekmessage(&msg, EX_MOUSE | EX_KEY))
|
||||
{
|
||||
handleEvent(msg);
|
||||
|
||||
// 检查是否需要关闭
|
||||
if (shouldClose)
|
||||
{
|
||||
Close();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 重绘
|
||||
if (dirty)
|
||||
{
|
||||
requestRepaint(parent);
|
||||
FlushBatchDraw();
|
||||
}
|
||||
|
||||
// 避免CPU占用过高
|
||||
Sleep(10);
|
||||
}
|
||||
// 模态对话框关闭后执行清理
|
||||
if (pendingCleanup && !isCleaning)
|
||||
performDelayedCleanup();
|
||||
}
|
||||
else
|
||||
// 非模态对话框只需标记为可见,由主循环处理
|
||||
dirty = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Dialog::Close()
|
||||
{
|
||||
if (!show) return;
|
||||
|
||||
show = false;
|
||||
close = true;
|
||||
dirty = true;
|
||||
pendingCleanup = true; // 只标记需要清理,不立即执行
|
||||
|
||||
|
||||
// 工厂模式下非模态触发回调 返回结果
|
||||
if (resultCallback&& !modal)
|
||||
resultCallback(this->result);
|
||||
|
||||
|
||||
}
|
||||
|
||||
void Dialog::setInitialization(bool init)
|
||||
{
|
||||
if (init)
|
||||
{
|
||||
initDialogSize();
|
||||
saveBackground((x - BorderWidth), (y - BorderWidth), (width + 2 * BorderWidth), (height + 2 * BorderWidth));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Dialog::initButtons()
|
||||
{
|
||||
controls.clear();
|
||||
|
||||
switch (this->type)
|
||||
{
|
||||
case StellarX::MessageBoxType::OK: // 只有确定按钮
|
||||
{
|
||||
auto okbutton = createDialogButton((this->x + (this->width - (functionButtonWidth * buttonNum + buttonMargin * (buttonNum - 1))) / 2),
|
||||
((this->y + (this->height - buttonAreaHeight)) + (buttonAreaHeight - functionButtonHeight) / 2),
|
||||
"确定"
|
||||
);
|
||||
okbutton->setOnClickListener([this]()
|
||||
{
|
||||
this->SetResult(StellarX::MessageBoxResult::OK);
|
||||
this->Close(); });
|
||||
|
||||
okbutton->textStyle = this->textStyle;
|
||||
|
||||
this->addControl(std::move(okbutton));
|
||||
}
|
||||
break;
|
||||
case StellarX::MessageBoxType::OKCancel: // 确定和取消按钮
|
||||
{
|
||||
auto okButton = createDialogButton(
|
||||
(this->x + (this->width - (functionButtonWidth * buttonNum + buttonMargin * (buttonNum - 1))) / 2),
|
||||
((this->y + (this->height - buttonAreaHeight)) + (buttonAreaHeight - functionButtonHeight) / 2),
|
||||
"确定"
|
||||
);
|
||||
okButton->setOnClickListener([this]()
|
||||
{
|
||||
this->SetResult(StellarX::MessageBoxResult::OK);
|
||||
this->hWnd.dialogClose = true;
|
||||
this->Close(); });
|
||||
|
||||
auto cancelButton = createDialogButton(
|
||||
(okButton.get()->getX() + okButton.get()->getButtonWidth() + buttonMargin),
|
||||
okButton.get()->getY(),
|
||||
"取消"
|
||||
);
|
||||
cancelButton->setOnClickListener([this]()
|
||||
{
|
||||
this->SetResult(StellarX::MessageBoxResult::Cancel);
|
||||
this->hWnd.dialogClose = true;
|
||||
this->Close(); });
|
||||
|
||||
okButton->textStyle = this->textStyle;
|
||||
cancelButton->textStyle = this->textStyle;
|
||||
|
||||
this->addControl(std::move(okButton));
|
||||
this->addControl(std::move(cancelButton));
|
||||
}
|
||||
break;
|
||||
case StellarX::MessageBoxType::YesNo: // 是和否按钮
|
||||
{
|
||||
auto yesButton = createDialogButton(
|
||||
(this->x + (this->width - (functionButtonWidth * buttonNum + buttonMargin * (buttonNum - 1))) / 2),
|
||||
((this->y + (this->height - buttonAreaHeight)) + (buttonAreaHeight - functionButtonHeight) / 2),
|
||||
"是"
|
||||
);
|
||||
yesButton->setOnClickListener([this]()
|
||||
{
|
||||
this->SetResult(StellarX::MessageBoxResult::Yes);
|
||||
this->hWnd.dialogClose = true;
|
||||
this->Close(); });
|
||||
|
||||
auto noButton = createDialogButton(
|
||||
(yesButton.get()->getX() + yesButton.get()->getButtonWidth() + buttonMargin),
|
||||
yesButton.get()->getY(),
|
||||
"否"
|
||||
);
|
||||
noButton->setOnClickListener([this]()
|
||||
{
|
||||
this->SetResult(StellarX::MessageBoxResult::No);
|
||||
this->hWnd.dialogClose = true;
|
||||
this->Close(); });
|
||||
|
||||
yesButton->textStyle = this->textStyle;
|
||||
noButton->textStyle = this->textStyle;
|
||||
|
||||
this->addControl(std::move(yesButton));
|
||||
this->addControl(std::move(noButton));
|
||||
}
|
||||
break;
|
||||
case StellarX::MessageBoxType::YesNoCancel: // 是、否和取消按钮
|
||||
{
|
||||
auto yesButton = createDialogButton(
|
||||
(this->x + (this->width - (functionButtonWidth * buttonNum + buttonMargin * (buttonNum - 1))) / 2),
|
||||
((this->y + (this->height - buttonAreaHeight)) + (buttonAreaHeight - functionButtonHeight) / 2),
|
||||
"是"
|
||||
);
|
||||
yesButton->setOnClickListener([this]()
|
||||
{
|
||||
this->SetResult(StellarX::MessageBoxResult::Yes);
|
||||
this->hWnd.dialogClose = true;
|
||||
this->Close(); });
|
||||
|
||||
auto noButton = createDialogButton(
|
||||
yesButton.get()->getX() + yesButton.get()->getButtonWidth() + buttonMargin,
|
||||
yesButton.get()->getY(),
|
||||
"否"
|
||||
);
|
||||
noButton->setOnClickListener([this]()
|
||||
{
|
||||
this->SetResult(StellarX::MessageBoxResult::No);
|
||||
this->hWnd.dialogClose = true;
|
||||
this->Close(); });
|
||||
|
||||
auto cancelButton = createDialogButton(
|
||||
noButton.get()->getX() + noButton.get()->getButtonWidth() + buttonMargin,
|
||||
noButton.get()->getY(),
|
||||
"取消"
|
||||
);
|
||||
cancelButton->setOnClickListener([this]()
|
||||
{
|
||||
this->SetResult(StellarX::MessageBoxResult::Cancel);
|
||||
this->hWnd.dialogClose = true;
|
||||
this->Close(); });
|
||||
|
||||
yesButton->textStyle = this->textStyle;
|
||||
noButton->textStyle = this->textStyle;
|
||||
cancelButton->textStyle = this->textStyle;
|
||||
|
||||
|
||||
this->addControl(std::move(yesButton));
|
||||
this->addControl(std::move(noButton));
|
||||
this->addControl(std::move(cancelButton));
|
||||
}
|
||||
break;
|
||||
case StellarX::MessageBoxType::RetryCancel: // 重试和取消按钮
|
||||
{
|
||||
auto retryButton = createDialogButton(
|
||||
(this->x + (this->width - (functionButtonWidth * buttonNum + buttonMargin * (buttonNum - 1))) / 2),
|
||||
((this->y + (this->height - buttonAreaHeight)) + (buttonAreaHeight - functionButtonHeight) / 2),
|
||||
"重试"
|
||||
);
|
||||
retryButton->setOnClickListener([this]()
|
||||
{
|
||||
this->SetResult(StellarX::MessageBoxResult::Retry);
|
||||
this->hWnd.dialogClose = true;
|
||||
this->Close(); });
|
||||
|
||||
auto cancelButton = createDialogButton(
|
||||
retryButton.get()->getX() + retryButton.get()->getButtonWidth() + buttonMargin,
|
||||
retryButton.get()->getY(),
|
||||
"取消"
|
||||
);
|
||||
cancelButton->setOnClickListener([this]()
|
||||
{
|
||||
this->SetResult(StellarX::MessageBoxResult::Cancel);
|
||||
this->hWnd.dialogClose = true;
|
||||
this->Close(); });
|
||||
|
||||
retryButton->textStyle = this->textStyle;
|
||||
cancelButton->textStyle = this->textStyle;
|
||||
|
||||
this->addControl(std::move(retryButton));
|
||||
this->addControl(std::move(cancelButton));
|
||||
}
|
||||
break;
|
||||
case StellarX::MessageBoxType::AbortRetryIgnore: // 中止、重试和忽略按钮
|
||||
{
|
||||
auto abortButton = createDialogButton(
|
||||
(this->x + (this->width - (functionButtonWidth * buttonNum + buttonMargin* (buttonNum-1))) / 2),
|
||||
((this->y + (this->height - buttonAreaHeight)) + (buttonAreaHeight - functionButtonHeight) / 2),
|
||||
"中止"
|
||||
);
|
||||
abortButton->setOnClickListener([this]()
|
||||
{
|
||||
this->SetResult(StellarX::MessageBoxResult::Abort);
|
||||
this->hWnd.dialogClose = true;
|
||||
this->Close();
|
||||
});
|
||||
auto retryButton = createDialogButton(
|
||||
abortButton.get()->getX() + abortButton.get()->getButtonWidth() + buttonMargin,
|
||||
abortButton.get()->getY(),
|
||||
"重试"
|
||||
);
|
||||
retryButton->setOnClickListener([this]()
|
||||
{
|
||||
this->SetResult(StellarX::MessageBoxResult::Retry);
|
||||
this->hWnd.dialogClose = true;
|
||||
this->Close();
|
||||
});
|
||||
auto ignoreButton = createDialogButton(
|
||||
retryButton.get()->getX() + retryButton.get()->getButtonWidth() + buttonMargin,
|
||||
retryButton.get()->getY(),
|
||||
"忽略"
|
||||
);
|
||||
ignoreButton.get()->setOnClickListener([this]()
|
||||
{
|
||||
this->SetResult(StellarX::MessageBoxResult::Ignore);
|
||||
this->hWnd.dialogClose = true;
|
||||
this->Close();
|
||||
});
|
||||
|
||||
abortButton->textStyle = this->textStyle;
|
||||
retryButton->textStyle = this->textStyle;
|
||||
ignoreButton->textStyle = this->textStyle;
|
||||
|
||||
this->addControl(std::move(abortButton));
|
||||
this->addControl(std::move(retryButton));
|
||||
this->addControl(std::move(ignoreButton));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
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), // 按钮被悬停颜色
|
||||
StellarX::ButtonMode::NORMAL,
|
||||
StellarX::ControlShape::B_RECTANGLE
|
||||
);
|
||||
but.get()->setButtonFalseColor(this->backgroundColor);
|
||||
but.get()->enableTooltip(false);
|
||||
but->setOnClickListener([this]() {
|
||||
this->SetResult(StellarX::MessageBoxResult::Cancel);
|
||||
this->hWnd.dialogClose = true;
|
||||
this->Close(); });
|
||||
|
||||
this->closeButton = but.get();
|
||||
this->addControl(std::move(but));
|
||||
}
|
||||
|
||||
void Dialog::initTitle()
|
||||
{
|
||||
this->title = std::make_unique<Label>(this->x+5,this->y+5,titleText,textStyle.color);
|
||||
title->setTextdisap(true);
|
||||
title->textStyle = this->textStyle;
|
||||
|
||||
this->addControl(std::move(title));
|
||||
}
|
||||
|
||||
void Dialog::splitMessageLines()
|
||||
{
|
||||
lines.clear(); // 先清空现有的行
|
||||
|
||||
std::string currentLine;
|
||||
for (size_t i = 0; i < message.length(); i++) {
|
||||
// 处理 换行符 \r\n \n \r
|
||||
if (i + 1 < message.length() && (message[i] == '\r' || message[i] == '\n')||(message[i] == '\r' && message[i+1] == '\n'))
|
||||
{
|
||||
if (!currentLine.empty()) {
|
||||
lines.push_back(currentLine);
|
||||
currentLine.clear();
|
||||
}
|
||||
|
||||
if (message[i] == '\r' && message[i + 1] == '\n')
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
currentLine += message[i];
|
||||
}
|
||||
|
||||
// 添加最后一行(如果有内容)
|
||||
if (!currentLine.empty())
|
||||
{
|
||||
lines.push_back(currentLine);
|
||||
}
|
||||
|
||||
// 如果消息为空,至少添加一个空行
|
||||
if (lines.empty())
|
||||
{
|
||||
lines.push_back("");
|
||||
}
|
||||
}
|
||||
|
||||
void Dialog::getTextSize()
|
||||
{
|
||||
saveStyle();
|
||||
settextstyle(textStyle.nHeight, textStyle.nWidth, textStyle.lpszFace,
|
||||
textStyle.nEscapement, textStyle.nOrientation, textStyle.nWeight,
|
||||
textStyle.bItalic, textStyle.bUnderline, textStyle.bStrikeOut);
|
||||
for (auto text : lines)
|
||||
{
|
||||
int w = textwidth(LPCTSTR(text.c_str()));
|
||||
int h = textheight(LPCTSTR(text.c_str()));
|
||||
if (this->textHeight < h)
|
||||
this->textHeight = h;
|
||||
if (this->textWidth < w)
|
||||
this->textWidth = w;
|
||||
}
|
||||
restoreStyle();
|
||||
}
|
||||
// 计算逻辑:对话框宽度取【文本区域最大宽度】和【按钮区域总宽度】中的较大值。
|
||||
// 对话框高度 = 标题栏 + 文本区 + 按钮区 + 各种间距。
|
||||
void Dialog::initDialogSize()
|
||||
{
|
||||
splitMessageLines(); // 分割消息行
|
||||
getTextSize(); // 获取文本最大尺寸
|
||||
|
||||
// 获取功能按钮数量
|
||||
switch (this->type)
|
||||
{
|
||||
case StellarX::MessageBoxType::OK: // 只有确定按钮
|
||||
buttonNum = 1;
|
||||
break;
|
||||
case StellarX::MessageBoxType::OKCancel: // 确定和取消按钮
|
||||
case StellarX::MessageBoxType::YesNo: // 是和否按钮
|
||||
case StellarX::MessageBoxType::RetryCancel: // 重试和取消按钮
|
||||
buttonNum = 2;
|
||||
break;
|
||||
case StellarX::MessageBoxType::YesNoCancel: // 是、否和取消按钮
|
||||
case StellarX::MessageBoxType::AbortRetryIgnore: // 中止、重试和忽略按钮
|
||||
buttonNum = 3;
|
||||
break;
|
||||
}
|
||||
|
||||
// 计算按钮区域宽度
|
||||
int buttonAreaWidth = buttonNum * functionButtonWidth +
|
||||
(buttonNum > 0 ? (buttonNum +1) * buttonMargin : 0);
|
||||
|
||||
// 计算文本区域宽度(包括边距)
|
||||
int textAreaWidth = textWidth + textToBorderMargin * 2 ;
|
||||
|
||||
// 对话框宽度取两者中的较大值,并确保最小宽度
|
||||
this->width = buttonAreaWidth > textAreaWidth ? buttonAreaWidth : textAreaWidth;
|
||||
this->width = this->width > 200 ? this->width : 200;
|
||||
|
||||
// 计算对话框高度
|
||||
// 高度 = 标题栏高度 + 文本区域高度 + 按钮区域高度 + 间距
|
||||
int textAreaHeight = textHeight * (int)lines.size() + 5*((int)lines.size()-1); // 文本行高+行间距
|
||||
this->height = closeButtonHeight + // 标题栏高度
|
||||
titleToTextMargin + // 标题到文本的间距
|
||||
textAreaHeight + // 文本区域高度
|
||||
buttonAreaHeight; // 按钮区域高度
|
||||
|
||||
// 居中定位对话框
|
||||
this->x = (hWnd.getWidth() - this->width) / 2;
|
||||
this->y = (hWnd.getHeight() - this->height) / 2;
|
||||
|
||||
//this->textStyle.nWidth = 10;
|
||||
this->textStyle.nHeight = 20;
|
||||
|
||||
initButtons(); // 初始化按钮
|
||||
initTitle(); // 初始化标题标签
|
||||
initCloseButton(); // 初始化关闭按钮
|
||||
}
|
||||
|
||||
void Dialog::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 + BorderWidth*2, h + BorderWidth*2);
|
||||
|
||||
SetWorkingImage(nullptr); // ★抓屏幕
|
||||
getimage(saveBkImage, x - BorderWidth, y - BorderWidth, w + BorderWidth*2, h+ BorderWidth*2);
|
||||
hasSnap = true;
|
||||
}
|
||||
void Dialog::restBackground()
|
||||
{
|
||||
if (!hasSnap || !saveBkImage) return;
|
||||
// 直接回贴屏幕(与抓取一致)
|
||||
SetWorkingImage(nullptr);
|
||||
putimage(saveBkX - BorderWidth, saveBkY - BorderWidth,saveBkImage);
|
||||
}
|
||||
|
||||
// 延迟清理策略:由于对话框绘制时保存了背景快照,必须在对话框隐藏后、
|
||||
// 所有控件析构前恢复背景,否则会导致背景图像被错误覆盖。
|
||||
// 此方法在对话框不可见且被标记为待清理时由 draw() 或 handleEvent() 调用。
|
||||
void Dialog::performDelayedCleanup()
|
||||
{
|
||||
if (isCleaning) return;
|
||||
|
||||
isCleaning = true;
|
||||
|
||||
auto& c = hWnd.getControls();
|
||||
for (auto& control : c)
|
||||
control->setDirty(true);
|
||||
|
||||
controls.clear();
|
||||
|
||||
// 重置指针
|
||||
closeButton = nullptr;
|
||||
title.reset();
|
||||
// 释放背景图像资源
|
||||
if (saveBkImage && hasSnap)
|
||||
{
|
||||
restBackground();
|
||||
FlushBatchDraw();
|
||||
discardBackground();
|
||||
}
|
||||
// 重置状态
|
||||
needsInitialization = true;
|
||||
pendingCleanup = false;
|
||||
isCleaning = false;
|
||||
shouldClose = false;
|
||||
}
|
||||
|
||||
void Dialog::SetResultCallback(std::function<void(StellarX::MessageBoxResult)> cb)
|
||||
{
|
||||
resultCallback = std::move(cb);
|
||||
}
|
||||
|
||||
std::string Dialog::GetCaption() const
|
||||
{
|
||||
return titleText;
|
||||
}
|
||||
|
||||
std::string Dialog::GetText() const
|
||||
{
|
||||
return message;
|
||||
}
|
||||
|
||||
void Dialog::clearControls()
|
||||
{
|
||||
controls.clear();
|
||||
// 重置按钮指针
|
||||
closeButton = nullptr;
|
||||
title.reset(); // 释放标题资源
|
||||
}
|
||||
|
||||
std::unique_ptr<Button> Dialog::createDialogButton(int x, int y, const std::string& text)
|
||||
{
|
||||
auto btn = std::make_unique<Button>(
|
||||
x, y, functionButtonWidth, functionButtonHeight,
|
||||
text,
|
||||
buttonTrueColor, // 点击色
|
||||
buttonFalseColor, // 背景色
|
||||
buttonHoverColor, // 悬停色
|
||||
StellarX::ButtonMode::NORMAL,
|
||||
StellarX::ControlShape::RECTANGLE
|
||||
);
|
||||
|
||||
return btn;
|
||||
}
|
||||
|
||||
void Dialog::requestRepaint(Control* parent)
|
||||
{
|
||||
if (this == parent)
|
||||
{
|
||||
for (auto& control : controls)
|
||||
if (control->isDirty()&&control->IsVisible())
|
||||
{
|
||||
control->draw();
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
onRequestRepaintAsRoot();
|
||||
}
|
||||
34
src/MessageBox.cpp
Normal file
34
src/MessageBox.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include "MessageBox.h"
|
||||
|
||||
namespace StellarX
|
||||
{
|
||||
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,
|
||||
std::function<void(MessageBoxResult)> onResult)
|
||||
{
|
||||
//去重,如果窗口内已有相同的对话框被触发,则不再创建
|
||||
if (wnd.hasNonModalDialogWithCaption(caption, text))
|
||||
{
|
||||
std::cout << "\a" << std::endl;
|
||||
return;
|
||||
}
|
||||
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));
|
||||
// 交给 Window 管理生命周期
|
||||
wnd.addDialog(std::move(dlg));
|
||||
dlgPtr->Show();
|
||||
}
|
||||
}
|
||||
381
src/TabControl.cpp
Normal file
381
src/TabControl.cpp
Normal file
@@ -0,0 +1,381 @@
|
||||
#include "TabControl.h"
|
||||
|
||||
inline void TabControl::initTabBar()
|
||||
{
|
||||
if (controls.empty())return;
|
||||
int butW = max(this->width / (int)controls.size(), BUTMINWIDTH);
|
||||
int butH = max(this->height / (int)controls.size(), BUTMINHEIGHT);
|
||||
|
||||
if (this->tabPlacement == StellarX::TabPlacement::Top || this->tabPlacement == StellarX::TabPlacement::Bottom)
|
||||
for (auto& c : controls)
|
||||
{
|
||||
c.first->setHeight(tabBarHeight);
|
||||
c.first->setWidth(butW);
|
||||
}
|
||||
else if (this->tabPlacement == StellarX::TabPlacement::Left || this->tabPlacement == StellarX::TabPlacement::Right)
|
||||
for (auto& c : controls)
|
||||
{
|
||||
c.first->setHeight(butH);
|
||||
c.first->setWidth(tabBarHeight);
|
||||
}
|
||||
int i = 0;
|
||||
switch (this->tabPlacement)
|
||||
{
|
||||
case StellarX::TabPlacement::Top:
|
||||
for (auto& c : controls)
|
||||
{
|
||||
c.first->setX(this->x + i * butW);
|
||||
c.first->setY(this->y);
|
||||
i++;
|
||||
}
|
||||
break;
|
||||
case StellarX::TabPlacement::Bottom:
|
||||
for (auto& c : controls)
|
||||
{
|
||||
c.first->setX(this->x + i * butW);
|
||||
c.first->setY(this->y+this->height - tabBarHeight);
|
||||
i++;
|
||||
}
|
||||
break;
|
||||
case StellarX::TabPlacement::Left:
|
||||
for (auto& c : controls)
|
||||
{
|
||||
c.first->setX(this->x);
|
||||
c.first->setY(this->y+i* butH);
|
||||
i++;
|
||||
}
|
||||
break;
|
||||
case StellarX::TabPlacement::Right:
|
||||
for (auto& c : controls)
|
||||
{
|
||||
c.first->setX(this->x+this->width - tabBarHeight);
|
||||
c.first->setY(this->y + i * butH);
|
||||
i++;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
inline void TabControl::initTabPage()
|
||||
{
|
||||
if (controls.empty())return;
|
||||
//子控件坐标原点
|
||||
int nX = 0;
|
||||
int nY = 0;
|
||||
switch (this->tabPlacement)
|
||||
{
|
||||
case StellarX::TabPlacement::Top:
|
||||
for (auto& c : controls)
|
||||
{
|
||||
c.second->setX(this->x);
|
||||
c.second->setY(this->y + tabBarHeight);
|
||||
c.second->setWidth(this->width);
|
||||
c.second->setHeight(this->height - tabBarHeight);
|
||||
}
|
||||
nX = this->x;
|
||||
nY = this->y + tabBarHeight;
|
||||
for (auto& c : controls)
|
||||
{
|
||||
for (auto& v : c.second->getControls())
|
||||
{
|
||||
v->setX(v->getLocalX() + nX);
|
||||
v->setY(v->getLocalY() + nY);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case StellarX::TabPlacement::Bottom:
|
||||
for (auto& c : controls)
|
||||
{
|
||||
c.second->setX(this->x);
|
||||
c.second->setY(this->y);
|
||||
c.second->setWidth(this->width);
|
||||
c.second->setHeight(this->height - tabBarHeight);
|
||||
}
|
||||
nX = this->x;
|
||||
nY = this->y;
|
||||
for (auto& c : controls)
|
||||
{
|
||||
for (auto& v : c.second->getControls())
|
||||
{
|
||||
v->setX(v->getLocalX() + nX);
|
||||
v->setY(v->getLocalY() + nY);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case StellarX::TabPlacement::Left:
|
||||
for (auto& c : controls)
|
||||
{
|
||||
c.second->setX(this->x + tabBarHeight);
|
||||
c.second->setY(this->y);
|
||||
c.second->setWidth(this->width - tabBarHeight);
|
||||
c.second->setHeight(this->height);
|
||||
}
|
||||
nX = this->x + tabBarHeight;
|
||||
nY = this->y;
|
||||
for (auto& c : controls)
|
||||
{
|
||||
for (auto& v : c.second->getControls())
|
||||
{
|
||||
v->setX(v->getLocalX() + nX);
|
||||
v->setY(v->getLocalY() + nY);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case StellarX::TabPlacement::Right:
|
||||
for (auto& c : controls)
|
||||
{
|
||||
c.second->setX(this->x);
|
||||
c.second->setY(this->y);
|
||||
c.second->setWidth(this->width - tabBarHeight);
|
||||
c.second->setHeight(this->height);
|
||||
}
|
||||
nX = this->x;
|
||||
nY = this->y;
|
||||
for (auto& c : controls)
|
||||
{
|
||||
for (auto& v : c.second->getControls())
|
||||
{
|
||||
v->setX(v->getLocalX() + nX);
|
||||
v->setY(v->getLocalY() + nY);
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
TabControl::TabControl():Canvas()
|
||||
{
|
||||
this->id = "TabControl";
|
||||
}
|
||||
|
||||
TabControl::TabControl(int x, int y, int width, int height)
|
||||
: Canvas(x, y, width, height)
|
||||
{
|
||||
this->id = "TabControl";
|
||||
}
|
||||
|
||||
TabControl::~TabControl()
|
||||
{
|
||||
}
|
||||
|
||||
void TabControl::draw()
|
||||
{
|
||||
if (!dirty || !show)return;
|
||||
if ((saveBkX != this->x) || (saveBkY != this->y) || (!hasSnap) || (saveWidth != this->width) || (saveHeight != this->height) || !saveBkImage)
|
||||
saveBackground(this->x, this->y, this->width, this->height);
|
||||
// 恢复背景(清除旧内容)
|
||||
restBackground();
|
||||
Canvas::draw();
|
||||
for (auto& c : controls)
|
||||
{
|
||||
c.first->setDirty(true);
|
||||
c.first->draw();
|
||||
}
|
||||
for (auto& c : controls)
|
||||
if(c.second->IsVisible())
|
||||
{
|
||||
c.second->setDirty(true);
|
||||
c.second->draw();
|
||||
}
|
||||
dirty = false;
|
||||
|
||||
}
|
||||
|
||||
bool TabControl::handleEvent(const ExMessage& msg)
|
||||
{
|
||||
if (!show)return false;
|
||||
bool consume = false;
|
||||
for (auto& c : controls)
|
||||
if (c.first->handleEvent(msg))
|
||||
{
|
||||
consume = true;
|
||||
break;
|
||||
}
|
||||
for (auto& c : controls)
|
||||
if(c.second->IsVisible())
|
||||
if (c.second->handleEvent(msg))
|
||||
{
|
||||
consume = true;
|
||||
break;
|
||||
}
|
||||
if (dirty)
|
||||
requestRepaint(parent);
|
||||
return consume;
|
||||
}
|
||||
|
||||
void TabControl::add(std::pair<std::unique_ptr<Button>, std::unique_ptr<Canvas>>&& control)
|
||||
{
|
||||
controls.push_back(std::move(control));
|
||||
initTabBar();
|
||||
initTabPage();
|
||||
size_t idx = controls.size() - 1;
|
||||
controls[idx].first->setParent(this);
|
||||
controls[idx].first->enableTooltip(true);
|
||||
controls[idx].first->setbuttonMode(StellarX::ButtonMode::TOGGLE);
|
||||
controls[idx].first->setOnToggleOnListener([this,idx]()
|
||||
{
|
||||
controls[idx].second->setIsVisible(true);
|
||||
controls[idx].second->onWindowResize();
|
||||
for (auto& tab : controls)
|
||||
{
|
||||
if (tab.first->getButtonText() != controls[idx].first->getButtonText())
|
||||
{
|
||||
tab.first->setButtonClick(false);
|
||||
tab.second->setIsVisible(false);
|
||||
}
|
||||
}
|
||||
dirty = true;
|
||||
});
|
||||
controls[idx].first->setOnToggleOffListener([this,idx]()
|
||||
{
|
||||
controls[idx].second->setIsVisible(false);
|
||||
|
||||
dirty = true;
|
||||
});
|
||||
controls[idx].second->setParent(this);
|
||||
controls[idx].second->setLinewidth(canvaslinewidth);
|
||||
controls[idx].second->setIsVisible(false);
|
||||
}
|
||||
|
||||
void TabControl::add(std::string tabText, std::unique_ptr<Control> control)
|
||||
{
|
||||
control->setDirty(true);
|
||||
for (auto& tab : controls)
|
||||
{
|
||||
if (tab.first->getButtonText() == tabText)
|
||||
{
|
||||
control->setParent(tab.second.get());
|
||||
control->setIsVisible( tab.second->IsVisible());
|
||||
tab.second->addControl(std::move(control));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void TabControl::setTabPlacement(StellarX::TabPlacement placement)
|
||||
{
|
||||
this->tabPlacement = placement;
|
||||
setDirty(true);
|
||||
initTabBar();
|
||||
initTabPage();
|
||||
|
||||
}
|
||||
|
||||
void TabControl::setTabBarHeight(int height)
|
||||
{
|
||||
tabBarHeight = height;
|
||||
setDirty(true);
|
||||
initTabBar();
|
||||
initTabPage();
|
||||
}
|
||||
|
||||
void TabControl::setIsVisible(bool visible)
|
||||
{
|
||||
// 先让基类 Canvas 处理自己的回贴/丢快照逻辑
|
||||
Canvas::setIsVisible(visible); // <--- 新增
|
||||
|
||||
this->show = visible;
|
||||
for (auto& tab : controls)
|
||||
{
|
||||
tab.first->setIsVisible(visible);
|
||||
//页也要跟着关/开,否则它们会保留旧的 saveBkImage
|
||||
tab.second->setIsVisible(visible);
|
||||
tab.second->setDirty(true);
|
||||
}
|
||||
}
|
||||
|
||||
void TabControl::onWindowResize()
|
||||
{
|
||||
Control::onWindowResize();
|
||||
for (auto& c : controls)
|
||||
{
|
||||
c.first->onWindowResize();
|
||||
c.second->onWindowResize();
|
||||
}
|
||||
}
|
||||
|
||||
int TabControl::getActiveIndex() const
|
||||
{
|
||||
int idx = -1;
|
||||
for (auto& c : controls)
|
||||
{
|
||||
idx++;
|
||||
if (c.first->isClicked())
|
||||
return idx;
|
||||
}
|
||||
return idx;
|
||||
}
|
||||
|
||||
void TabControl::setActiveIndex(int idx)
|
||||
{
|
||||
if (idx < 0 || idx > controls.size() - 1) return;
|
||||
if (controls[idx].first->getButtonMode() == StellarX::ButtonMode::DISABLED)return;
|
||||
if (controls[idx].first->isClicked())
|
||||
{
|
||||
if (controls[idx].second->IsVisible())
|
||||
return;
|
||||
else
|
||||
controls[idx].second->setIsVisible(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
controls[idx].first->setButtonClick(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int TabControl::count() const
|
||||
{
|
||||
return (int)controls.size();
|
||||
}
|
||||
|
||||
int TabControl::indexOf(const std::string& tabText) const
|
||||
{
|
||||
int idx = -1;
|
||||
for(auto& c : controls)
|
||||
{
|
||||
idx++;
|
||||
if (c.first->getButtonText() == tabText)
|
||||
return idx;
|
||||
}
|
||||
|
||||
return idx;
|
||||
}
|
||||
|
||||
void TabControl::setDirty(bool dirty)
|
||||
{
|
||||
this->dirty = dirty;
|
||||
for (auto& c : controls)
|
||||
{
|
||||
c.first->setDirty(dirty);
|
||||
c.second->setDirty(dirty);
|
||||
}
|
||||
}
|
||||
|
||||
void TabControl::requestRepaint(Control* parent)
|
||||
{
|
||||
if (this == 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
onRequestRepaintAsRoot();
|
||||
}
|
||||
@@ -1,22 +1,26 @@
|
||||
#include "StellarX/Label.h"
|
||||
#include "Label.h"
|
||||
|
||||
Label::Label()
|
||||
:Control(0, 0, 0, 0)
|
||||
{
|
||||
this->id = "Label";
|
||||
this->text = "默认标签";
|
||||
textColor = RGB(0,0,0);
|
||||
textStyle.color = RGB(0,0,0);
|
||||
textBkColor = RGB(255, 255, 255);; //默认白色背景
|
||||
}
|
||||
|
||||
Label::Label(int x, int y, std::string text, COLORREF textcolor, COLORREF bkColor)
|
||||
:Control(x, y, 0, 0)
|
||||
{
|
||||
this->id = "Label";
|
||||
this->text = text;
|
||||
textColor = textcolor;
|
||||
textStyle.color = textcolor;
|
||||
textBkColor = bkColor; //默认白色背景
|
||||
}
|
||||
|
||||
void Label::draw()
|
||||
{
|
||||
if (dirty && show)
|
||||
{
|
||||
saveStyle();
|
||||
if (textBkDisap)
|
||||
@@ -26,30 +30,46 @@ void Label::draw()
|
||||
setbkmode(OPAQUE); //设置背景不透明
|
||||
setbkcolor(textBkColor); //设置背景颜色
|
||||
}
|
||||
settextcolor(textColor);
|
||||
settextcolor(textStyle.color);
|
||||
settextstyle(textStyle.nHeight, textStyle.nWidth, textStyle.lpszFace,
|
||||
textStyle.nEscapement, textStyle.nOrientation, textStyle.nWeight,
|
||||
textStyle.bItalic, textStyle.bUnderline, textStyle.bStrikeOut); //设置字体样式
|
||||
if (0 == this->width || 0 == this->height)
|
||||
{
|
||||
this->width = textwidth(text.c_str());
|
||||
this->height = textheight(text.c_str());
|
||||
}
|
||||
if ((saveBkX != this->x) || (saveBkY != this->y) || (!hasSnap) || (saveWidth != this->width) || (saveHeight != this->height) || !saveBkImage)
|
||||
saveBackground(this->x, this->y,this->width,this->height);
|
||||
// 恢复背景(清除旧内容)
|
||||
restBackground();
|
||||
outtextxy(x, y, LPCTSTR(text.c_str()));
|
||||
restoreStyle();
|
||||
dirty = false;
|
||||
}
|
||||
}
|
||||
//用于“隐藏提示框”时调用(还原并释放快照)
|
||||
void Label::hide()
|
||||
{
|
||||
restBackground(); // 还原屏幕像素
|
||||
discardBackground(); // 作废快照,防止错贴旧图
|
||||
dirty = false;
|
||||
}
|
||||
|
||||
void Label::setTextdisap(bool key)
|
||||
{
|
||||
textBkDisap = key;
|
||||
}
|
||||
|
||||
void Label::setTextColor(COLORREF color)
|
||||
{
|
||||
textColor = color;
|
||||
this->dirty = true;
|
||||
}
|
||||
|
||||
void Label::setTextBkColor(COLORREF color)
|
||||
{
|
||||
textBkColor = color;
|
||||
this->dirty = true;
|
||||
}
|
||||
|
||||
void Label::setText(std::string text)
|
||||
{
|
||||
this->text = text;
|
||||
this->dirty = true;
|
||||
|
||||
}
|
||||
|
||||
393
src/table.cpp
393
src/table.cpp
@@ -1,145 +1,225 @@
|
||||
#include "StellarX/Table.h"
|
||||
#include "Table.h"
|
||||
// 绘制表格的当前页
|
||||
// 使用双循环绘制行和列,考虑分页偏移
|
||||
void Table::drawTable()
|
||||
{
|
||||
dX = x;
|
||||
dY = uY;
|
||||
uY = dY + lineHeights.at(0) + 10;
|
||||
const int border = tableBorderWidth > 0 ? tableBorderWidth : 0;
|
||||
|
||||
for (int i = (currentPage * rowsPerPage - rowsPerPage); i < (currentPage*rowsPerPage) && i < data.size(); i++)
|
||||
// 表体从“表头之下”开始
|
||||
dX = x + border;
|
||||
dY = y + border + lineHeights.at(0) + TABLE_HEADER_EXTRA; // 表头高度
|
||||
uY = dY + lineHeights.at(0) + TABLE_ROW_EXTRA;
|
||||
|
||||
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 (int 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) + TABLE_COL_GAP;
|
||||
fillrectangle(dX, dY, uX, uY);
|
||||
outtextxy(dX + 10, dY + 5, LPCTSTR(data[i][j].c_str()));
|
||||
dX += this->colWidths.at(j) + 20;
|
||||
outtextxy(dX + TABLE_PAD_X, dY + TABLE_PAD_Y, LPCTSTR(data[i][j].c_str()));
|
||||
dX += colWidths.at(j) + TABLE_COL_GAP;
|
||||
}
|
||||
dX = x;
|
||||
dX = x + border;
|
||||
dY = uY;
|
||||
uY = dY + lineHeights.at(0) + 10;
|
||||
|
||||
uY = dY + lineHeights.at(0) + TABLE_ROW_EXTRA;
|
||||
}
|
||||
uY = y + lineHeights.at(0) + 10;
|
||||
|
||||
}
|
||||
|
||||
void Table::drawHeader()
|
||||
{
|
||||
|
||||
uY = dY + lineHeights.at(0) + 10;
|
||||
for(int i = 0; i < headers.size(); i++)
|
||||
const int border = tableBorderWidth > 0 ? tableBorderWidth : 0;
|
||||
// 内容区原点 = x+border, y+border
|
||||
dX = x + border;
|
||||
dY = y + border;
|
||||
uY = dY + lineHeights.at(0) + TABLE_HEADER_EXTRA;
|
||||
|
||||
for (size_t i = 0; i < headers.size(); i++)
|
||||
{
|
||||
uX = dX + colWidths.at(i) + 20;
|
||||
uX = dX + colWidths.at(i) + TABLE_COL_GAP; // 注意这里是 +20,和表体一致
|
||||
fillrectangle(dX, dY, uX, uY);
|
||||
outtextxy(dX + 10, dY + 5, LPCTSTR(headers[i].c_str()));
|
||||
dX += this->colWidths.at(i) + 20;
|
||||
outtextxy(dX + TABLE_PAD_X, dY + TABLE_PAD_Y, LPCTSTR(headers[i].c_str()));
|
||||
dX += colWidths.at(i) + TABLE_COL_GAP; // 列间距 20
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
// 初始化文本宽度和高度计算
|
||||
// 遍历所有数据和表头,计算每列的最大宽度和行高
|
||||
// 此方法在数据变更时自动调用
|
||||
// 遍历所有数据单元和表头,计算每列的最大宽度和每行的最大高度,
|
||||
// 为后续绘制表格单元格提供尺寸依据。此计算在数据变更时自动触发。
|
||||
void Table::initTextWaH()
|
||||
{
|
||||
this->colWidths.resize(this->headers.size());
|
||||
this->lineHeights.resize(this->headers.size());
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
//计算数据尺寸
|
||||
for (int i = 0; i < data.size(); i++)
|
||||
// 和绘制一致的单元内边距
|
||||
const int padX = TABLE_PAD_X; // 左右 padding
|
||||
const int padY = TABLE_PAD_Y; // 上下 padding
|
||||
const int colGap = TABLE_COL_GAP; // 列间距
|
||||
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 (int 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 (int 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 (int 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;
|
||||
|
||||
// 如果背景图像不存在或尺寸不匹配,创建或重新创建
|
||||
if (saveBkImage == nullptr) {
|
||||
saveBkImage = new IMAGE(width, height);
|
||||
}
|
||||
else if (saveBkImage->getwidth() != width || saveBkImage->getheight() != height) {
|
||||
delete saveBkImage;
|
||||
saveBkImage = new IMAGE(width, height);
|
||||
}
|
||||
// 表头高 & 行高(与 drawHeader/drawTable 内部一致:+上下 padding)
|
||||
const int headerH = maxLineH + 2 * padY;
|
||||
const int rowH = maxLineH + 2 * padY;
|
||||
const int rowsH = rowH * rowsPerPage;
|
||||
|
||||
// 页脚:
|
||||
const int pageTextH = textheight(LPCTSTR(pageNumtext.c_str()));
|
||||
const int btnTextH = textheight(LPCTSTR("上一页"));
|
||||
const int btnPadV = TABLE_BTN_TEXT_PAD_V;
|
||||
const int btnH = btnTextH + 2 * btnPadV;
|
||||
const int footerPad = TABLE_FOOTER_PAD;
|
||||
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 - 70;
|
||||
x2 = pX + textwidth(LPCTSTR(pageNumtext.c_str())) + 10;
|
||||
y1 = y2 = pY;
|
||||
this->prevButton = new Button(x1, y1, 60, textheight(LPCTSTR(pageNumtext.c_str())), "上一页", RGB(0, 0, 0), RGB(255, 255, 255));
|
||||
this->nextButton = new Button(x2, y2, 60, textheight(LPCTSTR(pageNumtext.c_str())), "下一页", RGB(0, 0, 0), RGB(255, 255, 255));
|
||||
prevButton->setOnClickListener([this]()
|
||||
{if (this->currentPage > 1)
|
||||
{
|
||||
this->currentPage--;
|
||||
this->dirty = true;
|
||||
this->draw();
|
||||
} });
|
||||
const int gap = TABLE_BTN_GAP;
|
||||
const int padH = TABLE_BTN_PAD_H;
|
||||
const int padV = TABLE_BTN_PAD_V; // 按钮垂直内边距
|
||||
|
||||
nextButton->setOnClickListener([this]()
|
||||
{if (this->currentPage < (this->totalPages))
|
||||
int pageW = textwidth(LPCTSTR(pageNumtext.c_str()));
|
||||
int lblH = textheight(LPCTSTR(pageNumtext.c_str()));
|
||||
|
||||
// 统一按钮尺寸(用按钮文字自身宽高 + padding)
|
||||
int prevW = textwidth(LPCTSTR(TABLE_STR_PREV)) + padH * 2;
|
||||
int nextW = textwidth(LPCTSTR(TABLE_STR_NEXT)) + padH * 2;
|
||||
int btnH = lblH + padV * 2;
|
||||
|
||||
|
||||
// 基于“页码标签”的矩形来摆放:
|
||||
// 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, TABLE_STR_PREV, 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, TABLE_STR_NEXT, 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+10;
|
||||
for (int 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);
|
||||
//pageNum->setTextdisap(true);
|
||||
// 统一坐标系
|
||||
const int border = tableBorderWidth > 0 ? tableBorderWidth : 0;
|
||||
const int baseH = lineHeights.empty() ? 0 : lineHeights.at(0);
|
||||
const int headerH = baseH + TABLE_HEADER_EXTRA;
|
||||
const int rowsH = baseH * rowsPerPage + rowsPerPage * TABLE_ROW_EXTRA;
|
||||
|
||||
// 内容宽度 = 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 + TABLE_FOOTER_BLANK; // +8 顶部留白
|
||||
|
||||
// 按理来说 x + (this->width - textW) / 2;就可以
|
||||
// 但是在绘制时,发现控件偏右,因此减去40
|
||||
int textW = textwidth(LPCTSTR(pageNumtext.c_str()));
|
||||
pX = x + TABLE_PAGE_TEXT_OFFSET_X +(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); // 透明文本
|
||||
}
|
||||
|
||||
void Table::drawPageNum()
|
||||
{
|
||||
if (nullptr == pageNum)
|
||||
initPageNum();
|
||||
pageNumtext = std::to_string(currentPage);
|
||||
pageNumtext += "页/第";
|
||||
|
||||
pageNumtext = "第";
|
||||
pageNumtext+= std::to_string(currentPage);
|
||||
pageNumtext += "页/共";
|
||||
pageNumtext += std::to_string(totalPages);
|
||||
pageNumtext += "页";
|
||||
if (nullptr == pageNum)
|
||||
initPageNum();
|
||||
pageNum->setText(pageNumtext);
|
||||
pageNum->textStyle = this->textStyle;
|
||||
if (StellarX::FillMode::Null == tableFillMode)
|
||||
pageNum->setTextdisap(true);
|
||||
pageNum->draw();
|
||||
|
||||
}
|
||||
@@ -148,6 +228,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();
|
||||
|
||||
@@ -156,7 +245,7 @@ void Table::drawButton()
|
||||
Table::Table(int x, int y)
|
||||
:Control(x, y, 0, 0)
|
||||
{
|
||||
//this->saveBkImage = new IMAGE(this->width,this->height);
|
||||
this->id = "Table";
|
||||
}
|
||||
|
||||
Table::~Table()
|
||||
@@ -177,7 +266,29 @@ Table::~Table()
|
||||
|
||||
void Table::draw()
|
||||
{
|
||||
if (this->dirty)
|
||||
//在这里先初始化保证翻页按钮不为空
|
||||
// 在一些容器中,Table不会被立即绘制可能导致事件事件传递时触发空指针警报
|
||||
// 由于单元格初始化依赖字体数据所以先设置一次字体样式
|
||||
// 先保存当前绘图状态
|
||||
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);
|
||||
// 是否需要计算单元格尺寸
|
||||
if (isNeedCellSize)
|
||||
{
|
||||
initTextWaH();
|
||||
isNeedCellSize = false;
|
||||
}
|
||||
restoreStyle();
|
||||
if (this->dirty && this->show)
|
||||
{
|
||||
// 先保存当前绘图状态
|
||||
saveStyle();
|
||||
@@ -193,27 +304,8 @@ void Table::draw()
|
||||
setfillstyle((int)tableFillMode);
|
||||
setbkmode(TRANSPARENT);
|
||||
|
||||
// 是否需要计算单元格尺寸
|
||||
if (isNeedCellSize)
|
||||
if (isNeedDrawHeaders)
|
||||
{
|
||||
initTextWaH();
|
||||
isNeedCellSize = false;
|
||||
}
|
||||
|
||||
// 在绘制表格之前捕获背景
|
||||
// 只有在第一次绘制或者尺寸变化时才需要重新捕获背景
|
||||
static bool firstDraw = true;
|
||||
if (firstDraw || 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);
|
||||
@@ -225,28 +317,20 @@ void Table::draw()
|
||||
setfillstyle((int)tableFillMode);
|
||||
setbkmode(TRANSPARENT);
|
||||
}
|
||||
firstDraw = false;
|
||||
}
|
||||
|
||||
//确保在绘制任何表格内容之前捕获背景
|
||||
// 临时恢复样式,确保捕获正确的背景
|
||||
if ((!hasSnap) || (saveWidth != this->width) || (saveHeight != this->height)||!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;
|
||||
dY = y;
|
||||
drawHeader();
|
||||
this->isNeedDrawHeaders = false;
|
||||
|
||||
|
||||
// 绘制当前页
|
||||
drawTable();
|
||||
|
||||
// 绘制页码标签
|
||||
drawPageNum();
|
||||
|
||||
@@ -254,21 +338,28 @@ void Table::draw()
|
||||
if (this->isShowPageButton)
|
||||
drawButton();
|
||||
|
||||
|
||||
// 恢复绘图状态
|
||||
restoreStyle();
|
||||
dirty = false; // 标记不需要重绘
|
||||
}
|
||||
}
|
||||
|
||||
void Table::handleEvent(const ExMessage& msg)
|
||||
bool Table::handleEvent(const ExMessage& msg)
|
||||
{
|
||||
if(!show)return false;
|
||||
bool consume = false;
|
||||
if(!this->isShowPageButton)
|
||||
return;
|
||||
return consume;
|
||||
else
|
||||
{
|
||||
prevButton->handleEvent(msg);
|
||||
nextButton->handleEvent(msg);
|
||||
if(prevButton)consume = prevButton->handleEvent(msg);
|
||||
if (nextButton&&!consume)
|
||||
consume = nextButton->handleEvent(msg);
|
||||
}
|
||||
if (dirty)
|
||||
requestRepaint(parent);
|
||||
return consume;
|
||||
}
|
||||
|
||||
void Table::setHeaders(std::initializer_list<std::string> headers)
|
||||
@@ -281,21 +372,32 @@ void Table::setHeaders(std::initializer_list<std::string> headers)
|
||||
dirty = true;
|
||||
}
|
||||
|
||||
void Table::setData(const std::vector<std::string>& data)
|
||||
void Table::setData( std::vector<std::string> data)
|
||||
{
|
||||
if (data.size() < headers.size())
|
||||
for (int i = 0; data.size() <= headers.size(); i++)
|
||||
data.push_back("");
|
||||
this->data.push_back(data);
|
||||
totalPages = (this->data.size() + rowsPerPage - 1) / rowsPerPage;
|
||||
totalPages = ((int)this->data.size() + rowsPerPage - 1) / rowsPerPage;
|
||||
if (totalPages < 1)
|
||||
totalPages = 1;
|
||||
isNeedCellSize = true; // 标记需要重新计算单元格尺寸
|
||||
dirty = true;
|
||||
}
|
||||
|
||||
void Table::setData(const std::initializer_list<std::vector<std::string>>& data)
|
||||
void Table::setData( std::initializer_list<std::vector<std::string>> data)
|
||||
{
|
||||
for (auto lis : data)
|
||||
if (lis.size() < headers.size())
|
||||
{
|
||||
for (size_t i = lis.size(); i< headers.size(); i++)
|
||||
lis.push_back("");
|
||||
this->data.push_back(lis);
|
||||
totalPages = (this->data.size() + rowsPerPage - 1) / rowsPerPage;
|
||||
}
|
||||
else
|
||||
this->data.push_back(lis);
|
||||
|
||||
totalPages = ((int)this->data.size() + rowsPerPage - 1) / rowsPerPage;
|
||||
if (totalPages < 1)
|
||||
totalPages = 1;
|
||||
isNeedCellSize = true; // 标记需要重新计算单元格尺寸
|
||||
@@ -305,7 +407,7 @@ void Table::setData(const std::initializer_list<std::vector<std::string>>& data)
|
||||
void Table::setRowsPerPage(int rows)
|
||||
{
|
||||
this->rowsPerPage = rows;
|
||||
totalPages = (data.size() + rowsPerPage - 1) / rowsPerPage;
|
||||
totalPages = ((int)data.size() + rowsPerPage - 1) / rowsPerPage;
|
||||
if (totalPages < 1)
|
||||
totalPages = 1;
|
||||
isNeedCellSize = true; // 标记需要重新计算单元格尺寸
|
||||
@@ -315,16 +417,19 @@ void Table::setRowsPerPage(int rows)
|
||||
void Table::showPageButton(bool isShow)
|
||||
{
|
||||
this->isShowPageButton = isShow;
|
||||
this->dirty = true;
|
||||
}
|
||||
|
||||
void Table::setTableBorder(COLORREF color)
|
||||
{
|
||||
this->tableBorderClor = color;
|
||||
this->dirty = true;
|
||||
}
|
||||
|
||||
void Table::setTableBk(COLORREF color)
|
||||
{
|
||||
this->tableBkClor = color;
|
||||
this->dirty = true;
|
||||
}
|
||||
|
||||
void Table::setTableFillMode(StellarX::FillMode mode)
|
||||
@@ -333,16 +438,30 @@ void Table::setTableFillMode(StellarX::FillMode mode)
|
||||
this->tableFillMode = mode;
|
||||
else
|
||||
this->tableFillMode = StellarX::FillMode::Solid;
|
||||
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;
|
||||
}
|
||||
|
||||
void Table::setTableLineStyle(StellarX::LineStyle style)
|
||||
{
|
||||
this->tableLineStyle = style;
|
||||
this->dirty = true;
|
||||
}
|
||||
|
||||
void Table::setTableBorderWidth(int width)
|
||||
{
|
||||
this->tableBorderWidth = width;
|
||||
this->dirty = true;
|
||||
}
|
||||
|
||||
int Table::getCurrentPage() const
|
||||
@@ -352,7 +471,7 @@ int Table::getCurrentPage() const
|
||||
|
||||
int Table::getTotalPages() const
|
||||
{
|
||||
return this->totalPages;;
|
||||
return this->totalPages;
|
||||
}
|
||||
|
||||
int Table::getRowsPerPage() const
|
||||
|
||||
@@ -1,15 +1,16 @@
|
||||
// TextBox.cpp
|
||||
#include "StellarX/TextBox.h"
|
||||
#include "TextBox.h"
|
||||
|
||||
|
||||
TextBox::TextBox(int x, int y, int width, int height, std::string text, StellarX::TextBoxmode mode, StellarX::ControlShape shape)
|
||||
:Control(x,y,width,height),text(text), mode(mode), shape(shape)
|
||||
{
|
||||
this->id = "TextBox";
|
||||
}
|
||||
|
||||
void TextBox::draw()
|
||||
{
|
||||
if(dirty)
|
||||
if (dirty && show)
|
||||
{
|
||||
saveStyle();
|
||||
setfillcolor(textBoxBkClor);
|
||||
@@ -27,7 +28,10 @@ void TextBox::draw()
|
||||
int text_width = textwidth(LPCTSTR(text.c_str()));
|
||||
int text_height = textheight(LPCTSTR(text.c_str()));
|
||||
|
||||
|
||||
if ((saveBkX != this->x) || (saveBkY != this->y) || (!hasSnap) || (saveWidth != this->width) || (saveHeight != this->height) || !saveBkImage)
|
||||
saveBackground(this->x, this->y, this->width, this->height);
|
||||
// 恢复背景(清除旧内容)
|
||||
restBackground();
|
||||
//根据形状绘制
|
||||
switch (shape)
|
||||
{
|
||||
@@ -48,16 +52,17 @@ void TextBox::draw()
|
||||
outtextxy(x + 10, (y + (height - text_height) / 2), LPCTSTR(text.c_str()));
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
restoreStyle();
|
||||
dirty = false; //标记不需要重绘
|
||||
}
|
||||
|
||||
void TextBox::handleEvent(const ExMessage& msg)
|
||||
}
|
||||
|
||||
bool TextBox::handleEvent(const ExMessage& msg)
|
||||
{
|
||||
bool hover = false;
|
||||
bool oldClick = click;
|
||||
bool consume = false;
|
||||
|
||||
switch (shape)
|
||||
{
|
||||
@@ -66,36 +71,44 @@ void TextBox::handleEvent(const ExMessage& msg)
|
||||
case StellarX::ControlShape::ROUND_RECTANGLE:
|
||||
case StellarX::ControlShape::B_ROUND_RECTANGLE:
|
||||
hover = (msg.x > x && msg.x < (x + width) && msg.y > y && msg.y < (y + height));//判断鼠标是否在矩形按钮内
|
||||
consume = false;
|
||||
break;
|
||||
}
|
||||
if (hover && msg.message == WM_LBUTTONUP)
|
||||
{
|
||||
click = true;
|
||||
if(StellarX::TextBoxmode::INPUT_MODE == mode)
|
||||
dirty = InputBox(LPTSTR(text.c_str()), maxCharLen,"输入框",NULL,text.c_str(), NULL ,NULL,false);
|
||||
{
|
||||
dirty = InputBox(LPTSTR(text.c_str()), (int)maxCharLen, "输入框", NULL, text.c_str(), NULL, NULL, false);
|
||||
consume = true;
|
||||
}
|
||||
else if (StellarX::TextBoxmode::READONLY_MODE == mode)
|
||||
{
|
||||
dirty = false;
|
||||
InputBox(NULL, maxCharLen, "输出框(输入无效!)", NULL, text.c_str(), NULL, NULL, false);
|
||||
InputBox(NULL, (int)maxCharLen, "输出框(输入无效!)", NULL, text.c_str(), NULL, NULL, false);
|
||||
consume = true;
|
||||
}
|
||||
flushmessage(EX_MOUSE | EX_KEY);
|
||||
}
|
||||
if (dirty)
|
||||
draw();
|
||||
requestRepaint(parent);
|
||||
|
||||
if (click)
|
||||
click = false;
|
||||
return consume;
|
||||
}
|
||||
|
||||
void TextBox::setMode(StellarX::TextBoxmode mode)
|
||||
{
|
||||
this->mode = mode;
|
||||
this->dirty = true;
|
||||
}
|
||||
|
||||
void TextBox::setMaxCharLen(int len)
|
||||
void TextBox::setMaxCharLen(size_t len)
|
||||
{
|
||||
if (len > 0)
|
||||
maxCharLen = len;
|
||||
this->dirty = true;
|
||||
}
|
||||
|
||||
void TextBox::setTextBoxshape(StellarX::ControlShape shape)
|
||||
@@ -107,12 +120,14 @@ void TextBox::setTextBoxshape(StellarX::ControlShape shape)
|
||||
case StellarX::ControlShape::ROUND_RECTANGLE:
|
||||
case StellarX::ControlShape::B_ROUND_RECTANGLE:
|
||||
this->shape = shape;
|
||||
this->dirty = true;
|
||||
break;
|
||||
case StellarX::ControlShape::CIRCLE:
|
||||
case StellarX::ControlShape::B_CIRCLE:
|
||||
case StellarX::ControlShape::ELLIPSE:
|
||||
case StellarX::ControlShape::B_ELLIPSE:
|
||||
this->shape = StellarX::ControlShape::RECTANGLE;
|
||||
this->dirty = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -120,11 +135,22 @@ void TextBox::setTextBoxshape(StellarX::ControlShape shape)
|
||||
void TextBox::setTextBoxBorder(COLORREF color)
|
||||
{
|
||||
textBoxBorderClor = color;
|
||||
this->dirty = true;
|
||||
}
|
||||
|
||||
void TextBox::setTextBoxBk(COLORREF color)
|
||||
{
|
||||
textBoxBkClor = 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
|
||||
|
||||
309
src/window.cpp
309
src/window.cpp
@@ -1,24 +1,27 @@
|
||||
#include "StellarX/Window.h"
|
||||
#include "Window.h"
|
||||
#include "Dialog.h"
|
||||
#include <windows.h> // 确保包含 Windows API 头文件
|
||||
|
||||
Window::Window(int width, int height, int mode)
|
||||
{
|
||||
this->width = width;
|
||||
this->height = height;
|
||||
|
||||
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->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->pendingW = this->width = width;
|
||||
this->pendingH = this->height = height;
|
||||
this->windowMode = mode;
|
||||
this->wBkcolor = bkcloc;
|
||||
this->headline = headline;
|
||||
@@ -32,53 +35,200 @@ Window::~Window()
|
||||
closegraph(); // 确保关闭图形上下文
|
||||
}
|
||||
|
||||
void Window::draw()
|
||||
{
|
||||
|
||||
void Window::draw() {
|
||||
// 使用 EasyX 创建基本窗口
|
||||
if (!hWnd)
|
||||
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(); // 开始批量绘制
|
||||
// 绘制所有子控件
|
||||
// 初次绘制所有控件(双缓冲)
|
||||
BeginBatchDraw();
|
||||
for (auto& control : controls)
|
||||
control->draw();
|
||||
EndBatchDraw(); // 结束批量绘制
|
||||
}
|
||||
// 使用背景图片绘制窗口
|
||||
// @参数 pImgFile: 图片文件路径,支持常见图片格式
|
||||
// @备注: 会拉伸图片以适应窗口尺寸
|
||||
void Window::draw(std::string pImgFile)
|
||||
{
|
||||
control->draw();
|
||||
}
|
||||
// (如果有初始对话框,也可绘制 dialogs)
|
||||
EndBatchDraw();
|
||||
}
|
||||
|
||||
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, pImgFile.c_str(), width, height, true);
|
||||
loadimage(background, imagePath.c_str(), width, height, true);
|
||||
if(background)
|
||||
putimage(0, 0, background);
|
||||
|
||||
// 绘制所有子控件
|
||||
BeginBatchDraw(); // 开始批量绘制
|
||||
for (auto& control : controls)
|
||||
control->draw();
|
||||
EndBatchDraw(); // 结束批量绘制
|
||||
|
||||
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)
|
||||
{
|
||||
control->setDirty(true);
|
||||
control->draw();
|
||||
}
|
||||
for (auto& dlg : dialogs) dlg->draw();
|
||||
EndBatchDraw();
|
||||
}
|
||||
|
||||
// 运行主事件循环,处理用户输入和窗口消息
|
||||
// 此方法会阻塞直到窗口关闭
|
||||
void Window::runEventLoop()
|
||||
// 主消息循环优先级:对话框 > 普通控件。
|
||||
// 重绘策略:为保证视觉一致性,每次有对话框状态变化(打开/关闭)时,
|
||||
// 会强制重绘所有控件。先绘制普通控件,再绘制对话框(确保对话框在最上层)。
|
||||
int Window::runEventLoop()
|
||||
{
|
||||
ExMessage msg;
|
||||
bool running = true;
|
||||
while (running) {
|
||||
msg = getmessage(EX_MOUSE | EX_KEY);
|
||||
if (msg.message == WM_CLOSE) {
|
||||
|
||||
while (running)
|
||||
{
|
||||
bool consume = false;// 是否处理了消息
|
||||
|
||||
// 处理所有消息
|
||||
if (peekmessage(&msg, EX_MOUSE | EX_KEY | EX_WINDOW, true))
|
||||
{
|
||||
if (msg.message == WM_CLOSE)
|
||||
{
|
||||
running = false;
|
||||
continue;
|
||||
return 0;
|
||||
}
|
||||
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 it = controls.rbegin(); it != controls.rend(); ++it)
|
||||
{
|
||||
consume = (*it)->handleEvent(msg);
|
||||
if (consume)
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
//如果有对话框打开或者关闭强制重绘
|
||||
bool needredraw = false;
|
||||
for (auto& d : dialogs)
|
||||
{
|
||||
needredraw = d->IsVisible();
|
||||
if (needredraw)break;
|
||||
}
|
||||
if (needredraw || dialogClose)
|
||||
{
|
||||
// 对话框关闭后,需要手动合成一个鼠标移动消息并分发给所有普通控件,
|
||||
// 以便它们能及时更新悬停状态(hover),否则悬停状态可能保持错误状态。
|
||||
// 先把当前鼠标位置转换为客户区坐标,并合成一次 WM_MOUSEMOVE,先分发给控件更新 hover 状态
|
||||
POINT pt;
|
||||
if (GetCursorPos(&pt))
|
||||
{
|
||||
ScreenToClient(this->hWnd, &pt);
|
||||
ExMessage mm;
|
||||
mm.message = WM_MOUSEMOVE;
|
||||
mm.x = (short)pt.x;
|
||||
mm.y = (short)pt.y;
|
||||
// 只分发给 window 层控件(因为 dialog 已经关闭或即将关闭)
|
||||
for (auto& c : controls)
|
||||
c->handleEvent(msg);
|
||||
flushmessage(EX_MOUSE |EX_KEY |EX_CHAR|EX_WINDOW);
|
||||
c->handleEvent(mm);
|
||||
}
|
||||
|
||||
BeginBatchDraw();
|
||||
// 先绘制普通控件
|
||||
for (auto& c : controls)
|
||||
c->draw();
|
||||
// 然后绘制对话框(确保对话框在最上层)
|
||||
for (auto& d : dialogs)
|
||||
{
|
||||
if (!d->model() && d->IsVisible())
|
||||
d->setDirty(true);
|
||||
d->draw();
|
||||
}
|
||||
EndBatchDraw();
|
||||
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->onWindowResize();
|
||||
c->draw();
|
||||
}
|
||||
for (auto& d : dialogs)
|
||||
{
|
||||
auto dd = dynamic_cast<Dialog*>(d.get());
|
||||
dd->setDirty(true);
|
||||
dd->setInitialization(true);
|
||||
d->draw();
|
||||
}
|
||||
needResizeDirty = false;
|
||||
}
|
||||
|
||||
// 降低占用
|
||||
Sleep(10);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void Window::setBkImage(std::string pImgFile)
|
||||
@@ -88,13 +238,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)
|
||||
@@ -108,6 +289,66 @@ void Window::addControl(std::unique_ptr<Control> control)
|
||||
this->controls.push_back(std::move(control));
|
||||
}
|
||||
|
||||
void Window::addDialog(std::unique_ptr<Control> dialogs)
|
||||
{
|
||||
this->dialogs.push_back(std::move(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 && d->GetText() == message)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
HWND Window::getHwnd() const
|
||||
{
|
||||
return hWnd;
|
||||
}
|
||||
|
||||
int Window::getWidth() const
|
||||
{
|
||||
return this->pendingW;
|
||||
}
|
||||
|
||||
int Window::getHeight() const
|
||||
{
|
||||
return this->pendingH;
|
||||
}
|
||||
|
||||
std::string Window::getHeadline() const
|
||||
{
|
||||
return this->headline;
|
||||
}
|
||||
|
||||
COLORREF Window::getBkcolor() const
|
||||
{
|
||||
return this->wBkcolor;
|
||||
}
|
||||
|
||||
IMAGE* Window::getBkImage() const
|
||||
{
|
||||
return this->background;
|
||||
}
|
||||
|
||||
std::string Window::getBkImageFile() const
|
||||
{
|
||||
return this->bkImageFile;
|
||||
}
|
||||
|
||||
std::vector<std::unique_ptr<Control>>& Window::getControls()
|
||||
{
|
||||
return this->controls;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user