-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
153 lines (136 loc) · 5.8 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <windows.h>
#include <string>
#include <regex>
#include <cstdio>
#include "resource.h"
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
// Determina si una cadena es una cadena de escape Unicode pura
bool isPureUnicodeEscape(const std::wstring& str) {
std::wregex unicodePattern(L"^(\\\\u[0-9A-Fa-f]{4})+$");
return std::regex_match(str, unicodePattern);
}
// Determinar si se trata de una cadena normal pura
bool isPureNormalString(const std::wstring& str) {
std::wregex unicodeEscapePattern(L".*\\\\u[0-9A-Fa-f]{4}.*");
return !std::regex_search(str, unicodeEscapePattern);
}
// Prototipo de función de procesamiento de mensajes de diálogo
INT_PTR CALLBACK DialogProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam) {
int x, y;
switch (message) {
case WM_INITDIALOG:
// Obtener las dimensiones de la pantalla
RECT rectDesktop;
GetWindowRect(GetDesktopWindow(), &rectDesktop);
// Obtener las dimensiones del cuadro de diálogo
RECT rectDlg;
GetWindowRect(hwndDlg, &rectDlg);
// Calcular las coordenadas para centrar la ventana
x = (rectDesktop.right - rectDesktop.left - (rectDlg.right - rectDlg.left)) / 2;
y = (rectDesktop.bottom - rectDesktop.top - (rectDlg.bottom - rectDlg.top)) / 2;
// Mover la ventana a las coordenadas calculadas
MoveWindow(hwndDlg, x, y, rectDlg.right - rectDlg.left, rectDlg.bottom - rectDlg.top, TRUE);
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDC_BTN_CONVERT:
{
wchar_t buffer[1024];
GetDlgItemTextW(hwndDlg, IDC_EDIT1, buffer, 1024);
if (wcslen(buffer) == 0) return TRUE;
// Compruebe si la entrada es Unicode
if (isPureUnicodeEscape(std::wstring(buffer))) {
// Convertir de Unicode a cadena normal
std::wstring convertedString;
for (size_t i = 0; i < wcslen(buffer);) {
if (buffer[i] == L'\\' && buffer[i + 1] == L'u') {
unsigned int code;
swscanf_s(buffer + i + 2, L"%4x", &code);
convertedString += static_cast<wchar_t>(code);
i += 6; // Salta \uxxxx
}
else {
convertedString += buffer[i];
i++;
}
}
SetDlgItemTextW(hwndDlg, IDC_EDIT2, convertedString.c_str());
}
else if(isPureNormalString(std::wstring(buffer))) {
// Convertir de cadena normal a Unicode
std::wstring unicodeString;
for (wchar_t* p = buffer; *p; ++p) {
wchar_t tmp[10];
swprintf_s(tmp, L"\\u%04X", *p);
unicodeString.append(tmp);
}
SetDlgItemTextW(hwndDlg, IDC_EDIT2, unicodeString.c_str());
}
else {
MessageBox(hwndDlg, L"La cadena de entrada no es válida ni para el formato de cadena Unicode ni para el de normal.", L"Error", MB_ICONERROR);
}
return TRUE;
}
case IDC_BTN_COPY: {
wchar_t buffer[1024];
GetDlgItemTextW(hwndDlg, IDC_EDIT2, buffer, 1024);
if (wcslen(buffer) == 0) return TRUE;
// Abrir portapapeles
if (!OpenClipboard(hwndDlg)) {
MessageBox(hwndDlg, L"No se pudo abrir el portapapeles.", L"Error", MB_ICONERROR);
return TRUE;
}
// Vaciar portapapeles
if (!EmptyClipboard()) {
CloseClipboard();
MessageBox(hwndDlg, L"No se pudo borrar el contenido del portapapeles.", L"Error", MB_ICONERROR);
return TRUE;
}
// Asignar memoria global y copie datos en la memoria
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, (wcslen(buffer) + 1) * sizeof(wchar_t));
if (hMem == NULL) {
CloseClipboard();
MessageBox(hwndDlg, L"No se pudo asignar memoria para el contenido del portapapeles.", L"Error", MB_ICONERROR);
return TRUE;
}
// Bloquear la memoria y copiar datos en ella
wchar_t* pMem = (wchar_t*)GlobalLock(hMem);
if (pMem == NULL) {
GlobalFree(hMem);
CloseClipboard();
MessageBox(hwndDlg, L"No se pudo bloquear la memoria para el contenido del portapapeles.", L"Error", MB_ICONERROR);
return TRUE;
}
wcscpy_s(pMem, wcslen(buffer) + 1, buffer);
GlobalUnlock(hMem);
// Establecer identificador de datos en el portapapeles
SetClipboardData(CF_UNICODETEXT, hMem);
// Cerrar portapapeles
CloseClipboard();
return TRUE;
}
case IDOK:
case IDCANCEL:
EndDialog(hwndDlg, LOWORD(wParam));
PostQuitMessage(0);
return TRUE;
}
break;
}
return FALSE;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
// Cargue el cuadro de diálogo y muéstrelo.
HWND hwndMainDlg = CreateDialogParam(hInstance, MAKEINTRESOURCE(IDD_MAINDLG), NULL, DialogProc, 0);
ShowWindow(hwndMainDlg, nCmdShow);
UpdateWindow(hwndMainDlg);
// El bucle de mensajes
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
if (!IsDialogMessage(hwndMainDlg, &msg)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int)msg.wParam;
}