h_sw_registro/src/gui.cpp
2025-07-26 10:32:02 +08:00

136 lines
5.0 KiB
C++

#include <windows.h>
#include "registration_api.h"
#include <string>
#include <algorithm>
#include <cctype>
#define IDC_STATUS_LABEL 101
#define IDC_MACHINE_CODE_LABEL 102
#define IDC_MACHINE_CODE_EDIT 103
#define IDC_EXPIRATION_LABEL 104
#define IDC_EXPIRATION_EDIT 105
#define IDC_GENERATE_BUTTON 106
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
static HWND hMachineCodeEdit, hExpirationEdit, hStatusLabel;
void UpdateValidationStatus(HWND hwnd) {
int result = ValidateLicenseFile();
std::string statusText;
switch (result) {
case 0:
statusText = "License Status: VALID";
break;
case 1:
statusText = "License Status: INVALID or NOT FOUND";
break;
case 2:
statusText = "License Status: EXPIRED";
break;
}
SetWindowText(hStatusLabel, statusText.c_str());
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc = {0};
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = "RegistrationWindowClass";
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
HWND hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
"RegistrationWindowClass",
"Software Registration",
(WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX),
CW_USEDEFAULT, CW_USEDEFAULT, 480, 250,
NULL, NULL, hInstance, NULL);
if (hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
MSG Msg;
while (GetMessage(&Msg, NULL, 0, 0) > 0) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE: {
// --- Top Part: Validation Status ---
hStatusLabel = CreateWindow("STATIC", "Checking license...", WS_VISIBLE | WS_CHILD | SS_CENTER, 20, 20, 420, 20, hwnd, (HMENU)IDC_STATUS_LABEL, NULL, NULL);
UpdateValidationStatus(hwnd);
// --- Bottom Part: License Generation ---
CreateWindow("STATIC", "--- License Generation ---", WS_VISIBLE | WS_CHILD | SS_CENTER, 20, 60, 420, 20, hwnd, NULL, NULL, NULL);
CreateWindow("STATIC", "Machine Code:", WS_VISIBLE | WS_CHILD, 20, 90, 150, 20, hwnd, (HMENU)IDC_MACHINE_CODE_LABEL, NULL, NULL);
hMachineCodeEdit = CreateWindow("EDIT", "", WS_VISIBLE | WS_CHILD | WS_BORDER, 180, 90, 260, 20, hwnd, (HMENU)IDC_MACHINE_CODE_EDIT, NULL, NULL);
CreateWindow("STATIC", "Expiration (YYYY-MM-DD):", WS_VISIBLE | WS_CHILD, 20, 120, 150, 20, hwnd, (HMENU)IDC_EXPIRATION_LABEL, NULL, NULL);
hExpirationEdit = CreateWindow("EDIT", "2099-12-31", WS_VISIBLE | WS_CHILD | WS_BORDER, 180, 120, 120, 20, hwnd, (HMENU)IDC_EXPIRATION_EDIT, NULL, NULL);
CreateWindow("BUTTON", "Generate License File", WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 180, 160, 180, 30, hwnd, (HMENU)IDC_GENERATE_BUTTON, NULL, NULL);
break;
}
case WM_COMMAND: {
switch (LOWORD(wParam)) {
case IDC_GENERATE_BUTTON: {
char machineCode[256];
char expirationDate[11];
GetWindowText(hMachineCodeEdit, machineCode, 256);
GetWindowText(hExpirationEdit, expirationDate, 11);
if (strlen(machineCode) == 0) {
MessageBox(hwnd, "Please enter a machine code.", "Error", MB_ICONERROR);
break;
}
// Note: We are now using the function that takes the machine code as an argument
int result = GenerateLicenseFileFromCode(machineCode, expirationDate);
if (result == 0) {
MessageBox(hwnd, "License.data generated successfully!", "Success", MB_OK);
// Re-validate and update the status label
UpdateValidationStatus(hwnd);
} else {
MessageBox(hwnd, "Failed to generate license file!", "Error", MB_ICONERROR);
}
break;
}
}
break;
}
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}