網站首頁 編程語言 正文
前言
在使用c++做界面開發的時候,需要涉及到到撤銷重做操作,尤其是實現白板功能時需要自己實現一套撤銷重做功能,如果是qt則有QUndoable對象,可以直接拿來用。但是如果是使用gdi繪圖,則可能需要自己實現了。
一、完整代碼
由于需要的功能相對簡單,這里就不做原理以及接口設計思路說明了,直接展示完整代碼。
Undoable.h
#ifndef AC_UNDOABLE_H
#define AC_UNDOABLE_H
#include<functional>
#include<vector>
namespace AC {
/// <summary>
/// 撤銷重做管理對象
/// 最少行數實現,采用vector+下標浮動實現,性能也是很好的。
/// ceate by xin 2022.7.5
/// </summary>
class Undoable
{
public:
/// <summary>
/// 撤銷
/// </summary>
void Undo();
/// <summary>
/// 重做
/// </summary>
void Redo();
/// <summary>
/// 清除
/// </summary>
void Clear();
/// <summary>
/// 添加操作
/// </summary>
/// <param name="undo">撤銷</param>
/// <param name="redo">重做</param>
void Add(std::function<void()> undo, std::function<void()> redo);
private:
//記錄的步驟
std::vector<std::pair<std::function<void()>, std::function<void()>>> _steps;
//當前操作的下標
int _index = -1;
};
}
#endif
Undoable.cpp
#include "Undoable.h"
namespace AC {
void Undoable::Undo(){
if (_index > -1)
_steps[_index--].first();
}
void Undoable::Redo(){
if (_index < (int)_steps.size() - 1)
_steps[++_index].second();
}
void Undoable::Clear(){
_steps.clear();
_index = -1;
}
void Undoable::Add(std::function<void()> undo, std::function<void()> redo){
if (++_index < (int)_steps.size())
_steps.resize(_index);
_steps.push_back({ undo ,redo });
}
}
二、使用示例
1、基本用法
//1、定義撤銷重做管理對象
AC::Undoable undoable;
//2、添加操作
undoable.Add(
[=]() {
//撤銷邏輯
},
[=]() {
//重做邏輯
}
);
//3、撤銷重做
//執行撤銷
undoable.Undo();
//執行重做
undoable.Redo();
2、gdi畫線撤銷
#include<Windows.h>
#include<Windowsx.h>
#include<vector>
#include "Undoable.h"
//筆畫集合
std::vector<std::vector<POINT>*> strokes;
//窗口上下文
HDC hdc;
int xPos = 0;
int yPos = 0;
//撤銷重做管理對象
AC::Undoable undoable;
//窗口過程
static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_LBUTTONDOWN:
//記錄筆畫起點
xPos = GET_X_LPARAM(lParam);
yPos = GET_Y_LPARAM(lParam);
strokes.push_back(new std::vector<POINT>());
strokes.back()->push_back({ xPos ,yPos });
MoveToEx(hdc, xPos, yPos, NULL);
break;
case WM_MOUSEMOVE:
if ((wParam & MK_LBUTTON) != 0)
//繪制筆畫
{
xPos = GET_X_LPARAM(lParam);
yPos = GET_Y_LPARAM(lParam);
LineTo(hdc, xPos, yPos);
strokes.back()->push_back({ xPos ,yPos });
}
break;
case WM_LBUTTONUP:
//記錄筆畫撤銷重做
{
auto currentStroke = strokes.back();
//補全一個點
if (strokes.back()->size()==1)
{
xPos = GET_X_LPARAM(lParam);
yPos = GET_Y_LPARAM(lParam);
LineTo(hdc, xPos, yPos);
}
//記錄操作
undoable.Add(
[=]() {
//撤銷邏輯
strokes.pop_back();
RECT rect;
GetClientRect(hwnd, &rect);
InvalidateRect(hwnd, &rect, 0);
},
[=]() {
//重做邏輯
strokes.push_back(currentStroke);
RECT rect;
GetClientRect(hwnd, &rect);
InvalidateRect(hwnd, &rect, 0);
}
);
}
break;
case WM_PAINT:
{
//重繪筆畫
RECT rect;
GetClientRect(hwnd, &rect);
FillRect(hdc, &rect, (HBRUSH)GetStockObject(WHITE_BRUSH));
for (auto i : strokes)
{
MoveToEx(hdc, i->front().x, i->front().y, NULL);
for (auto j : *i)
{
LineTo(hdc, j.x, j.y);
}
}
}
break;
case WM_KEYDOWN:
//響應消息
if (wParam == 'Z')
{
//執行撤銷
undoable.Undo();
}
if (wParam == 'Y')
{
//執行重做
undoable.Redo();
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
void main() {
//創建win32窗口
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = GetModuleHandle(NULL);
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = 0;
wndclass.lpszClassName = L"Win32Window";
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("創建窗口失敗!"), L"", MB_ICONERROR);
}
auto hwnd = CreateWindowW(
L"Win32Window",
L"重做撤銷",
WS_OVERLAPPEDWINDOW,
0,
0,
640,
360,
NULL,
NULL,
GetModuleHandle(NULL),
NULL
);
//顯示窗口
ShowWindow(hwnd, SW_SHOW);
//獲取窗口上下文
hdc = GetDC(hwnd);
HPEN pen = ::CreatePen(PS_SOLID, 9, RGB(255, 0, 0));//創建一支紅色的畫筆
auto oldPen = SelectObject(hdc, pen);
MSG msg;
HACCEL hAccelTable = NULL;
// 主消息循環:
while (GetMessage(&msg, NULL, 0, 0))
{
switch (msg.message)
{
case WM_PAINT:
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
//銷毀資源
SelectObject(hdc, oldPen);
DeleteObject(oldPen);
ReleaseDC(hwnd, hdc);
DestroyWindow(hwnd);
}
效果預覽
總結
以上就是今天要講的內容,本文展示的撤銷重做管理對象包含的功能可以滿足大部分使用場景,而且由于實現非常簡潔,很容易修改和拓展。這個對象經過筆者多個項目修改演變而成,最初的實現是使用兩個棧來記錄操作,其實并不是最優的,變長數組加下標記錄應該是更好的方式。
原文鏈接:https://blog.csdn.net/u013113678/article/details/125630013
相關推薦
- 2022-12-14 C++利用類實現矩陣的數乘,乘法以及點乘_C 語言
- 2022-08-03 python中filter,map,reduce的作用_python
- 2022-04-28 python中對列表的刪除和添加方法詳解_python
- 2022-10-24 Django使用Redis進行緩存詳細步驟_Redis
- 2023-07-31 elementui中el-tabs切換實時更新數據
- 2022-05-20 子查詢關鍵字-ALL、ANY、SOME、IN、EXISTS
- 2022-05-25 python?序列去重并保持原始順序操作_python
- 2023-06-19 Linux下使用Shell腳本實現進程監控的流程_linux shell
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支