用c++编写一个摇号软件,并用windows.h实现界面(使用户能输入最大值、最小值和摇号数量)
时间: 2024-05-04 12:17:44 浏览: 129
以下是一个简单的摇号软件的 C 代码,使用了 Windows API 来实现图形用户界面:
```c
#include <windows.h>
#define ID_MIN_EDIT 1
#define ID_MAX_EDIT 2
#define ID_NUM_EDIT 3
#define ID_RESULT_EDIT 4
#define ID_SHAKE_BUTTON 5
HWND hMinEdit, hMaxEdit, hNumEdit, hResultEdit, hShakeButton;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASS wc = {0};
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = TEXT("MainWindowClass");
if (!RegisterClass(&wc))
return 1;
HWND hWnd = CreateWindow(TEXT("MainWindowClass"), TEXT("摇号软件"), WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 400, 300, NULL, NULL, hInstance, NULL);
if (!hWnd)
return 2;
hMinEdit = CreateWindow(TEXT("EDIT"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | ES_NUMBER,
10, 10, 100, 20, hWnd, (HMENU)ID_MIN_EDIT, hInstance, NULL);
hMaxEdit = CreateWindow(TEXT("EDIT"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | ES_NUMBER,
120, 10, 100, 20, hWnd, (HMENU)ID_MAX_EDIT, hInstance, NULL);
hNumEdit = CreateWindow(TEXT("EDIT"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | ES_NUMBER,
230, 10, 100, 20, hWnd, (HMENU)ID_NUM_EDIT, hInstance, NULL);
hResultEdit = CreateWindow(TEXT("EDIT"), NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | ES_MULTILINE | ES_READONLY,
10, 40, 360, 200, hWnd, (HMENU)ID_RESULT_EDIT, hInstance, NULL);
hShakeButton = CreateWindow(TEXT("BUTTON"), TEXT("摇号"), WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
150, 250, 100, 30, hWnd, (HMENU)ID_SHAKE_BUTTON, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
if (LOWORD(wParam) == ID_SHAKE_BUTTON && HIWORD(wParam) == BN_CLICKED)
{
int minVal = GetDlgItemInt(hWnd, ID_MIN_EDIT, NULL, FALSE);
int maxVal = GetDlgItemInt(hWnd, ID_MAX_EDIT, NULL, FALSE);
int num = GetDlgItemInt(hWnd, ID_NUM_EDIT, NULL, FALSE);
if (minVal > maxVal)
{
MessageBox(hWnd, TEXT("最小值不能大于最大值"), TEXT("错误"), MB_OK | MB_ICONERROR);
break;
}
if (num > maxVal - minVal + 1)
{
MessageBox(hWnd, TEXT("摇号数量不能超过范围内的数字数量"), TEXT("错误"), MB_OK | MB_ICONERROR);
break;
}
int* nums = (int*)malloc(sizeof(int) * (maxVal - minVal + 1));
for (int i = 0; i < maxVal - minVal + 1; i++)
nums[i] = i + minVal;
for (int i = 0; i < num; i++)
{
int j = rand() % (maxVal - minVal + 1 - i) + i;
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
TCHAR result[1024] = TEXT("");
for (int i = 0; i < num; i++)
{
TCHAR numStr[16];
_stprintf_s(numStr, TEXT("%d\n"), nums[i]);
_tcscat_s(result, numStr);
}
SetDlgItemText(hWnd, ID_RESULT_EDIT, result);
free(nums);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
```
这个程序创建了一个窗口,并在窗口中添加了三个编辑框和一个按钮。用户可以在编辑框中输入最小值、最大值和摇号数量,然后点击按钮来摇号。
在按钮被点击时,程序会读取编辑框中的数值,并检查它们是否合法。如果最小值大于最大值,或者摇号数量超过范围内的数字数量,程序会弹出错误提示框。否则,程序会生成一个包含所有可能的数字的数组,并随机交换数组中的元素来生成摇号结果。最后,程序将摇号结果显示在一个多行编辑框中。
阅读全文