代码:
#include <iostream>
#include <random>
#include <string>
#include <windows.h>
using namespace std;
const unsigned char x [ 36 ] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J',
'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U',
'V', 'W', 'X', 'Y', 'Z',
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'
};
HWND hEditOutput , hEditInput , hButton , hStaticText;
LRESULT CALLBACK WndProc (HWND hWnd , UINT msg , WPARAM wParam , LPARAM lParam) {
switch ( msg ) {
case WM_CREATE:
// 创建静态文本
hStaticText = CreateWindow (L"STATIC" , L"输入生成的兑换码个数" , WS_CHILD | WS_VISIBLE ,
10 , 10 , 200 , 30 , hWnd , NULL , NULL , NULL);
// 创建输入编辑框
hEditInput = CreateWindowEx (WS_EX_CLIENTEDGE , L"EDIT" , L"" , WS_CHILD | WS_VISIBLE | ES_NUMBER | ES_AUTOHSCROLL ,
10 , 40 , 200 , 30 , hWnd , NULL , NULL , NULL);
// 创建输出编辑框
hEditOutput = CreateWindowEx (WS_EX_CLIENTEDGE , L"EDIT" , L"" , WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOVSCROLL | ES_READONLY ,
10 , 80 , 370 , 200 , hWnd , NULL , NULL , NULL);
// 创建按钮
hButton = CreateWindow (L"BUTTON" , L"生成兑换码" , WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON ,
220 , 40 , 160 , 30 , hWnd , NULL , NULL , NULL);
break;
case WM_COMMAND:
if ( ( HWND ) lParam == hButton && HIWORD (wParam) == BN_CLICKED ) { // 处理按钮点击事件
int textLength = GetWindowTextLength (hEditInput);
if ( textLength > 0 ) {
wchar_t * buffer = new wchar_t [ textLength + 1 ];
GetWindowText (hEditInput , buffer , textLength + 1);
int a = _wtoi (buffer); // 将输入的文本转换为整数
SendMessage (hEditOutput , WM_SETTEXT , NULL , ( LPARAM ) L""); // 清空输出框
random_device rd;
mt19937 gen (rd ());
for ( int i = 0; i < a; i++ ) {
string code = "";
for ( int j = 0; j < 12; j++ ) {
code += x [ gen () % 36 ];
}
code += "\r\n";
SendMessageA (hEditOutput , EM_REPLACESEL , TRUE , ( LPARAM ) code.c_str ());
}
delete [ ] buffer; // 释放内存
}
}
break;
case WM_DESTROY:
PostQuitMessage (0);
break;
default:
return DefWindowProc (hWnd , msg , wParam , lParam);
}
return 0;
}
int WINAPI WinMain (HINSTANCE hInstance , HINSTANCE hPrevInstance , LPSTR lpCmdLine , int nShowCmd) {
// 注册窗口类
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = L"MainWindowClass";
RegisterClass (&wc);
// 创建窗口
HWND hWnd = CreateWindow (L"MainWindowClass" , L"原神兑换码生成器" , WS_OVERLAPPEDWINDOW ,
CW_USEDEFAULT , CW_USEDEFAULT , 400 , 340 , NULL , NULL , hInstance , NULL);
// 显示窗口
ShowWindow (hWnd , nShowCmd);
UpdateWindow (hWnd);
MSG msg;
while ( GetMessage (&msg , NULL , 0 , 0) ) {
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return int (0); msg.wParam;
}
效果:
还是有一些瑕疵,就是生成的兑换码长度不一,望大佬指点