C++绘图:WindowsAPI 最后一代
更新:保存画法,可撤销
//版本号 :v2.11.4 //最终归属权为作者(饼干帅成渣)所有 //禁止转载 //仅供学习 #include <windows.h> #include <gdiplus.h> #include <bits/stdc++.h> #include <pshpack2.h> // 控件和消息定义 #define IDC_EDIT_INPUT 109 #define IDC_BTN_COPY 108 #define IDC_STATIC_TIME 1007 #define IDC_EDIT_PWD 1001 #define IDC_BTN_TOGGLE 1002 #define IDC_EDIT1 1003 #define IDC_BUTTON_SUBMIT 1004 #define IDC_BTN_TRAY 1005 #define WM_TRAY_ICON 1006 #define IDC_BTN_SPEAK 1008 #define IDC_BTN_UNDO 2001 // 撤销按钮 #define IDC_BTN_CLEAR 2002 // 清屏按钮 #define HIDE 2222 using namespace std; using namespace chrono; // 扩展图形类型枚举(移除平行四边形,新增梯形) enum ShapeType { SQUARE, CIRCLE, TRIANGLE, TRAPEZOID }; ShapeType g_currentShape = SQUARE; POINT g_startPos, g_endPos; bool g_isDrawing = false; bool ooo = false; // 添加图形数据结构 struct DrawingShape { ShapeType type; POINT start; POINT end; int colorIndex; }; // 使用栈或向量存储所有图形 vector<DrawingShape> g_shapeHistory; // 定义光标切换的状态 enum CursorState { ARROW, HAND, CROSS }; // 撤销功能 void UndoLastShape() { if (!g_shapeHistory.empty()) { g_shapeHistory.pop_back(); // 移除最后一个图形 } } // 清屏功能 void ClearAllShapes() { g_shapeHistory.clear(); // 清空所有图形 } CursorState currentCursor = CROSS; HWND g_hwnd; HWND hhwwnndd; NOTIFYICONDATA g_nid; bool g_isInTray = false; HWND hEdit; HWND hButton, hButton2; RECT g_originalRect; LONG_PTR g_originalStyle; bool g_isFullscreen = false; HBITMAP g_hMemBitmap = NULL; HDC g_hMemDC = NULL; int g_width = 0, g_height = 0; #define IDC_BTN_COLOR 1099 COLORREF g_colors[] = { RGB(255, 0, 0), RGB(0, 0, 0), RGB(0, 0, 255) }; // 红黑蓝 int g_currentColor = 1; // 默认黑色 bool g_isPasswordMode = true; // 初始为密码模式 //位图文件头文件定义 //其中不包括文件类型信息(由于结构体的内存结构决定,要是加了的话将不能正确的读取文件信息) typedef struct { WORD bfType;//文件类型,必须是0x424D,即字符“BM” DWORD bfSize;//文件大小 WORD bfReserved1;//保留字 WORD bfReserved2;//保留字 DWORD bfOffBits;//从文件头到实际位图数据的偏移字节数 } BMPFILEHEADER_T; struct BMPFILEHEADER_S { WORD bfType; DWORD bfSize; WORD bfReserved1; WORD bfReserved2; DWORD bfOffBits; }; typedef struct { DWORD biSize;//信息头大小 LONG biWidth;//图像宽度 LONG biHeight;//图像高度 WORD biPlanes;//位平面数,必须为1 WORD biBitCount;//每像素位数 DWORD biCompression;//压缩类型 DWORD biSizeImage;//压缩图像大小字节数 LONG biXPelsPerMeter;//水平分辨率 LONG biYPelsPerMeter;//垂直分辨率 DWORD biClrUsed;//位图实际用到的色彩数 DWORD biClrImportant;//本位图中重要的色彩数 } BMPINFOHEADER_T;//位图信息头定义 typedef void(__stdcall* NTPROC)(DWORD*, DWORD*, DWORD*); // 定义10种颜色组合(背景+前景) const vector<string> VIP_COLORS = { "0A", // 黑底亮绿字 "1B", // 蓝底淡浅绿字 "2C", // 绿底淡红字 "3D", // 湖蓝底淡紫字 "4E", // 红底淡黄字 "5F", // 紫底亮白字 "6A", // 黄底亮绿字 "8C", // 灰底淡红字 "9D", // 淡蓝底淡紫字 "E0" // 淡黄底黑字 }; void VIP_Effect() { cout << "VIP Activated" << endl; // 保存原始颜色设置 system("color"); // 获取当前颜色 char originalColor[10]; GetConsoleTitle(originalColor, 10); // 通过窗口标题暂存颜色 // 特效循环 for (int i = 0; i < 30; ++i) { // 闪烁30次 string cmd = "color " + VIP_COLORS[i % VIP_COLORS.size()]; system(cmd.c_str()); } } void V() { system("title VIP Activated"); // 设置窗口标题 VIP_Effect(); system("title Draw.exe"); } int getNtVersion() { HINSTANCE hinst = LoadLibrary("ntdll.dll"); DWORD major, minor, build; NTPROC proc = reinterpret_cast<NTPROC>(GetProcAddress(hinst, "RtlGetNtVersionNumbers")); proc(&major, &minor, &build); return major; } bool VIPcheck() { ifstream fin("out.out", ios_base::in); int s; if (!fin) { cout << "第一次请设置VIP密码" << endl; int n; cin >> n; ofstream fout("out.out", ios_base::app); fout << n; s = n; cout << "请输入VIP密码验证" << endl; int a; cin >> a; if (a == s) { cout << "VIP密码正确" << endl; } else { return false; } return true; } else { int n; fin >> n; s = n; } cout << "请输入VIP密码" << endl; int a; cin >> a; if (a == s) { cout << "VIP密码正确" << endl; return true; } else { cout << "VIP密码错误" << endl; return false; } } void ScreenShot() { // 生成时间戳 (格式: 年月日_时分秒) auto now = system_clock::now(); auto in_time_t = system_clock::to_time_t(now); stringstream timestamp; timestamp << put_time(localtime(&in_time_t), "%Y%m%d_%H%M%S"); // 构建PowerShell命令 string command = "powershell -Command \"" "$timestamp = '" + timestamp.str() + "'; " "Add-Type -AssemblyName System.Windows.Forms; " "Add-Type -AssemblyName System.Drawing; " "$screen = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds; " "$bitmap = New-Object System.Drawing.Bitmap($screen.Width, $screen.Height); " "$graphic = [System.Drawing.Graphics]::FromImage($bitmap); " "$graphic.CopyFromScreen([System.Drawing.Point]::Empty, [System.Drawing.Point]::Empty, $screen.Size); " "$bitmap.Save(\\\"C:\\screenshot_$timestamp.png\\\", [System.Drawing.Imaging.ImageFormat]::Png)\""; // 执行命令 system(command.c_str()); MessageBox(NULL, "截图成功", "提示", MB_OK); } void generateBmp( BYTE * pData, int width, int height, const char * filename ) { //生成Bmp图片,传递RGB值,传递图片像素大小,传递图片存储路径 int size = width * height * 3; // 每个像素点3个字节 // 位图第一部分,文件信息 BMPFILEHEADER_T bfh; bfh.bfType = 0X4d42; //bm bfh.bfSize = size // data size + sizeof( BMPFILEHEADER_T ) // first section size + sizeof( BMPINFOHEADER_T ) // second section size ; bfh.bfReserved1 = 0; // reserved bfh.bfReserved2 = 0; // reserved bfh.bfOffBits = bfh.bfSize - size; // 位图第二部分,数据信息 BMPINFOHEADER_T bih; bih.biSize = sizeof(BMPINFOHEADER_T); bih.biWidth = width; bih.biHeight = height; bih.biPlanes = 1; bih.biBitCount = 24; bih.biCompression = 0; bih.biSizeImage = size; bih.biXPelsPerMeter = 0; bih.biYPelsPerMeter = 0; bih.biClrUsed = 0; bih.biClrImportant = 0; FILE * fp = fopen( filename, "wb" ); if ( !fp ) return; fwrite( &bfh, 1, sizeof(BMPFILEHEADER_T), fp ); fwrite( &bih, 1, sizeof(BMPINFOHEADER_T), fp ); fwrite( pData, 1, size, fp ); bih = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, }; fclose( fp ); } void drawBMP() { int i = 0, j = 0; struct { BYTE b; BYTE g; BYTE r; } pRGB[240][320]; // 定义位图数据 memset( pRGB, 0, sizeof(pRGB) ); // 设置背景为黑色 // 在中间画一个100*100的矩形 for ( i = 70; i < 170; i++ ) { for ( j = 110; j < 210; j++ ) { pRGB[i][j].r = 0x3f; pRGB[i][j].g = 0x3f; pRGB[i][j].b = 0x3f; } } // 生成BMP图片 generateBmp( ( BYTE*)pRGB, 320, 240, "C:\\rgb.bmp" ); } // 梯形顶点计算 void CalculateTrapezoidPoints(POINT start, POINT end, POINT points[4]) { int height = abs(end.y - start.y); // 垂直高度 int topWidth = abs(end.x - start.x); // 上底宽度 int bottomWidth = topWidth + height * 2; // 下底宽度 points[0] = start; // 左上顶点 points[1] = { start.x + topWidth, start.y }; // 右上顶点 points[2] = { end.x + bottomWidth, end.y }; // 右下顶点 points[3] = { end.x - bottomWidth, end.y }; // 左下顶点 } // 三角形顶点计算函数 void CalculateTrianglePoints(POINT start, POINT end, POINT points[3]) { // 计算底边中点 POINT baseMid = { (start.x + end.x) / 2, (start.y + end.y) / 2 }; // 计算高度方向 int height = static_cast<int>(sqrt( pow(end.x - start.x, 2) + pow(end.y - start.y, 2) )) / 2; // 设置三个顶点(等腰三角形) points[0] = start; // 左顶点 points[1] = end; // 右顶点 points[2] = { baseMid.x, baseMid.y - height }; // 顶点 } void InitTrayIcon(HWND hwnd) { g_nid.cbSize = sizeof(NOTIFYICONDATA); g_nid.hWnd = hwnd; g_nid.uID = 1; g_nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP; g_nid.uCallbackMessage = WM_TRAY_ICON; g_nid.hIcon = LoadIcon(NULL, IDI_INFORMATION); } // 切换托盘状态 void ToggleTrayMode(HWND hwnd) { if (!g_isInTray) { // 最小化到托盘 ShowWindow(hwnd, SW_HIDE); Shell_NotifyIcon(NIM_ADD, &g_nid); } else { // 恢复窗口 ShowWindow(hwnd, SW_SHOW); Shell_NotifyIcon(NIM_DELETE, &g_nid); } g_isInTray = !g_isInTray; } // 初始化双缓冲 void InitDoubleBuffer(HWND hwnd) { RECT rc; GetClientRect(hwnd, &rc); g_width = rc.right - rc.left; g_height = rc.bottom - rc.top; HDC hdc = GetDC(hwnd); g_hMemDC = CreateCompatibleDC(hdc); g_hMemBitmap = CreateCompatibleBitmap(hdc, g_width, g_height); SelectObject(g_hMemDC, g_hMemBitmap); ReleaseDC(hwnd, hdc); // 初始清屏(可选) FillRect(g_hMemDC, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH)); HBRUSH hWhiteBrush = CreateSolidBrush(RGB(255, 255, 255)); FillRect(g_hMemDC, &rc, hWhiteBrush); DeleteObject(hWhiteBrush); } // 切换全屏函数 void ToggleFullscreen(HWND hwnd) { if (!g_isFullscreen) { // 进入全屏 GetWindowRect(hwnd, &g_originalRect); g_originalStyle = GetWindowLongPtr(hwnd, GWL_STYLE); int screenWidth = GetSystemMetrics(SM_CXSCREEN); int screenHeight = GetSystemMetrics(SM_CYSCREEN); SetWindowLongPtr(hwnd, GWL_STYLE, WS_POPUP); SetWindowPos(hwnd, HWND_TOP, 0, 0, screenWidth, screenHeight, SWP_FRAMECHANGED | SWP_SHOWWINDOW ); } else { // 退出全屏 SetWindowLongPtr(hwnd, GWL_STYLE, g_originalStyle); SetWindowPos(hwnd, NULL, g_originalRect.left, g_originalRect.top, g_originalRect.right - g_originalRect.left, g_originalRect.bottom - g_originalRect.top, SWP_FRAMECHANGED | SWP_NOZORDER ); } g_isFullscreen = !g_isFullscreen; } // 切换密码显示模式 void TogglePasswordMode(HWND hwnd) { // 保存当前文本 wchar_t szText[256] = {0}; GetWindowTextW(hEdit, szText, 256); // 获取当前窗口位置 RECT rc; GetWindowRect(hEdit, &rc); MapWindowPoints(HWND_DESKTOP, GetParent(hEdit), (LPPOINT)&rc, 2); // 销毁旧编辑框 DestroyWindow(hEdit); // 创建新编辑框(切换样式) DWORD style = WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL; if (g_isPasswordMode) { style |= ES_PASSWORD; } hEdit = CreateWindowW( L"EDIT", szText, style, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, hwnd, (HMENU)IDC_EDIT_PWD, NULL, NULL ); // 更新模式状态 g_isPasswordMode = !g_isPasswordMode; // 更新按钮文本 SetWindowTextW(GetDlgItem(hwnd, IDC_BTN_TOGGLE), g_isPasswordMode ? L"隐藏密码" : L"显示明文"); } void ShowTodayLuck() { // 初始化随机种子(使用当前时间) mt19937 mt(time(0)); // 生成0-100的随机数 int luck_value = mt() % 100 + 1; // 构建输出字符串 string message; if (luck_value == 100) { message = "人品100 人品爆棚"; } else if (luck_value > 80) { // 81-99 message = "人品" + to_string(luck_value) + " 人品还不错"; } else if (luck_value > 20) { // 21-80 message = "人品" + to_string(luck_value) + " 人品还凑合"; } else { // 0-20 message = "人品:" + to_string(luck_value) + " 人品太差了,请去拜佛"; } // 弹窗显示结果 MessageBoxA(nullptr, message.c_str(), "运势", MB_OK); } // 窗口过程函数 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static bool chaosMode = false; [[maybe_unused]] static UINT_PTR timerId = 0; switch (msg) { case WM_SETCURSOR: if (LOWORD(lParam) == HTCLIENT) { HCURSOR hCursor = NULL; switch (currentCursor) { case ARROW: hCursor = LoadCursor(NULL, IDC_CROSS); break; case HAND: hCursor = LoadCursor(NULL, IDC_ARROW); break; case CROSS: hCursor = LoadCursor(NULL, IDC_HAND ); break; } SetCursor(hCursor); return TRUE; } return TRUE; break; case WM_SIZE: // 窗口大小变化时重置缓冲 if (g_hMemDC) { DeleteDC(g_hMemDC); DeleteObject(g_hMemBitmap); } InitDoubleBuffer(hwnd); break; case WM_KEYDOWN: if (!ooo) { if (wParam == VK_BACK) { // 按Backspace切换全屏 ToggleFullscreen(hwnd); } else if (wParam == 'Z' && GetAsyncKeyState(VK_CONTROL)) { // Ctrl+Z撤销 UndoLastShape(); InvalidateRect(hwnd, NULL, TRUE); } else if (wParam == VK_LEFT) { DestroyWindow(hwnd); HWND hwnwd; hwnwd = FindWindow("CASCADIA_HOSTING_WINDOW_CLASS", NULL); if (hwnwd == NULL) { hwnwd = FindWindow("ConsoleWindowClass", NULL); } ShowOwnedPopups(hwnwd, SW_SHOW); ShowWindow(hwnwd, SW_SHOW); } } if (wParam == VK_F4 | VK_MENU) return 0; if (wParam == VK_CONTROL && GetAsyncKeyState(VK_SHIFT) && GetAsyncKeyState(VK_ESCAPE)) return 0; return 0; case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd, &ps); HDC hMemDC = CreateCompatibleDC(hdc); HBITMAP hBmp = CreateCompatibleBitmap(hdc, ps.rcPaint.right, ps.rcPaint.bottom); SelectObject(hMemDC, hBmp); FillRect(hMemDC, &ps.rcPaint, (HBRUSH)(COLOR_WINDOW + 1)); if (g_isDrawing) { HBRUSH hBrush = CreateSolidBrush(g_colors[g_currentColor]); SelectObject(hMemDC, hBrush); switch (g_currentShape) { case CIRCLE: { int radius = static_cast<int>(sqrt( pow(g_endPos.x - g_startPos.x, 2) + pow(g_endPos.y - g_startPos.y, 2))); Ellipse(hMemDC, g_startPos.x - radius, g_startPos.y - radius, g_startPos.x + radius, g_startPos.y + radius); break; } case SQUARE: Rectangle(hMemDC, min(g_startPos.x, g_endPos.x), min(g_startPos.y, g_endPos.y), max(g_startPos.x, g_endPos.x), max(g_startPos.y, g_endPos.y)); break; case TRIANGLE: { POINT points[3]; CalculateTrianglePoints(g_startPos, g_endPos, points); Polygon(hMemDC, points, 3); break; } case TRAPEZOID: { POINT points[4]; CalculateTrapezoidPoints(g_startPos, g_endPos, points); Polygon(hMemDC, points, 4); break; } } } for (const auto& shape : g_shapeHistory) { HBRUSH hBrush = CreateSolidBrush(g_colors[shape.colorIndex]); SelectObject(hdc, hBrush); for (const auto& shape : g_shapeHistory) { HBRUSH hBrush = CreateSolidBrush(g_colors[shape.colorIndex]); SelectObject(hMemDC, hBrush); switch (shape.type) { case CIRCLE: { int radius = static_cast<int>(sqrt( pow(shape.end.x - shape.start.x, 2) + pow(shape.end.y - shape.start.y, 2))); Ellipse(hMemDC, shape.start.x - radius, shape.start.y - radius, shape.start.x + radius, shape.start.y + radius); break; } case SQUARE: Rectangle(hMemDC, min(shape.start.x, shape.end.x), min(shape.start.y, shape.end.y), max(shape.start.x, shape.end.x), max(shape.start.y, shape.end.y)); break; case TRIANGLE: { POINT points[3]; CalculateTrianglePoints(shape.start, shape.end, points); Polygon(hMemDC, points, 3); break; } case TRAPEZOID: { POINT points[4]; CalculateTrapezoidPoints(shape.start, shape.end, points); Polygon(hMemDC, points, 4); break; } } DeleteObject(hBrush); } } BitBlt(hdc, 0, 0, ps.rcPaint.right, ps.rcPaint.bottom, hMemDC, 0, 0, SRCCOPY); DeleteDC(hMemDC); DeleteObject(hBmp); EndPaint(hwnd, &ps); break; } case WM_CLOSE: // 清理托盘图标 if (g_isInTray) Shell_NotifyIcon(NIM_DELETE, &g_nid); DestroyWindow(hwnd); break; case WM_DESTROY: KillTimer(hwnd, 1); // 销毁定时器 PostQuitMessage(0); return 0; break; // 处理鼠标消息 case WM_LBUTTONDOWN: g_startPos.x = LOWORD(lParam); g_startPos.y = HIWORD(lParam); g_isDrawing = true; return 0; case WM_LBUTTONUP: if (g_isDrawing) { DrawingShape newShape; newShape.type = g_currentShape; newShape.start = g_startPos; newShape.end = g_endPos; newShape.colorIndex = g_currentColor; g_shapeHistory.push_back(newShape); // 压入栈 g_isDrawing = false; } break; case WM_MOUSEMOVE: if (g_isDrawing) { g_endPos.x = LOWORD(lParam); g_endPos.y = HIWORD(lParam); InvalidateRect(hwnd, NULL, TRUE); } return 0; case WM_ERASEBKGND: return 1; // 禁用背景擦除 case WM_CREATE: { // 创建撤销按钮 CreateWindowW(L"BUTTON", L"撤销 (Ctrl+Z)", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 600, 10, 120, 30, hwnd, (HMENU)IDC_BTN_UNDO, NULL, NULL); // 创建清屏按钮 CreateWindowW(L"BUTTON", L"清屏", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 600, 50, 120, 30, hwnd, (HMENU)IDC_BTN_CLEAR, NULL, NULL); CreateWindowW(L"BUTTON", L"切换颜色(当前:黑)", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 400, 90, 150, 30, hwnd, (HMENU)IDC_BTN_COLOR, NULL, NULL); CreateWindowW(L"BUTTON", L"截图", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 400, 135, 150, 30, hwnd, (HMENU)1082, NULL, NULL); // 创建获取时间 hhwwnndd = CreateWindowW( L"BUTTON", L"获取系统时间", WS_CHILD | WS_VISIBLE | SS_CENTER, 20, 350, 150, 30, hwnd, (HMENU)12345, NULL, NULL ); CreateWindowW(L"BUTTON", L" 千 万 别 点 ", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 800, 100, 200, 50, hwnd, (HMENU)12332, NULL, NULL); // 禁用系统菜单 HMENU hMenu = GetSystemMenu(hwnd, FALSE); RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND); RemoveMenu(hMenu, SC_MINIMIZE, MF_BYCOMMAND); RemoveMenu(hMenu, SC_MAXIMIZE, MF_BYCOMMAND); CreateWindowW( L"BUTTON", L"人品", WS_CHILD | WS_VISIBLE | SS_CENTER, 20, 400, 150, 30, hwnd, (HMENU)129, NULL, NULL ); // 创建按钮 CreateWindowW(L"BUTTON", L"切换图形(当前:方形)", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 400, 10, 220, 30, hwnd, (HMENU)1011, NULL, NULL); CreateWindowW( L"BUTTON", L"朗读", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 250, 300, 80, 30, hwnd, (HMENU)IDC_BTN_SPEAK, NULL, NULL ); CreateWindowW(L"BUTTON", L"最小化到托盘", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 400, 50, 150, 30, hwnd, (HMENU)IDC_BTN_TRAY, NULL, NULL); // 创建按钮 CreateWindow("BUTTON", "切换光标", WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON, 20, 450, 100, 30, hwnd, (HMENU)32, NULL, NULL); // 初始化托盘 InitTrayIcon(hwnd); // 创建按钮 hButton = CreateWindow( "BUTTON", // 窗口类(按钮) "点击我关闭", // 按钮文本 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, // 样式 50, 50, // 位置 (x, y) 100, 30, // 大小 (宽度, 高度) hwnd, // 父窗口句柄 (HMENU)1, // 控件ID ((LPCREATESTRUCT)lParam)->hInstance, // 实例句柄 NULL // 附加数据 ); CreateWindow( "BUTTON", // 窗口类(按钮) "版本号", // 按钮文本 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, // 样式 10, 10, // 位置 (x, y) 100, 30, // 大小 (宽度, 高度) hwnd, // 父窗口句柄 (HMENU)15423, // 控件ID ((LPCREATESTRUCT)lParam)->hInstance, // 实例句柄 NULL // 附加数据 ); CreateWindow( "BUTTON", // 窗口类(按钮) "生成图片(C盘)", // 按钮文本 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, // 样式 180, 10, // 位置 (x, y) 120, 30, // 大小 (宽度, 高度) hwnd, // 父窗口句柄 (HMENU)1023, // 控件ID ((LPCREATESTRUCT)lParam)->hInstance, // 实例句柄 NULL // 附加数据 ); hButton2 = CreateWindow( "BUTTON", // 窗口类(按钮) "点击我切换全屏", // 按钮文本 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, // 样式 850, 50, // 位置 (x, y) 100, 30, // 大小 (宽度, 高度) hwnd, // 父窗口句柄 (HMENU)2, // 控件ID ((LPCREATESTRUCT)lParam)->hInstance, // 实例句柄 NULL // 附加数据 ); hButton2 = CreateWindow( "BUTTON", // 窗口类(按钮) "在桌面新建快捷方式", // 按钮文本 WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, // 样式 750, 10, // 位置 (x, y) 200, 30, // 大小 (宽度, 高度) hwnd, // 父窗口句柄 (HMENU)324, // 控件ID ((LPCREATESTRUCT)lParam)->hInstance, // 实例句柄 NULL // 附加数据 ); // 创建切换按钮 CreateWindowW( L"BUTTON", L"隐藏密码", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 100, 300, 80, 30, hwnd, (HMENU)IDC_BTN_TOGGLE, NULL, NULL ); hEdit = CreateWindowW(L"EDIT", L"", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL, 20, 250, 200, 25, hwnd, (HMENU)IDC_EDIT1, NULL, NULL); CreateWindowW(L"BUTTON", L"示例输入", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 20, 300, 80, 30, hwnd, (HMENU)IDC_BUTTON_SUBMIT, NULL, NULL); // 创建复制按钮 CreateWindowW( L"BUTTON", L"复制", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 180, 300, 80, 30, hwnd, (HMENU)IDC_BTN_COPY, NULL, NULL ); return 0; } case WM_TRAY_ICON: if (lParam == WM_LBUTTONDBLCLK) { ToggleTrayMode(hwnd); } return 0; // 禁用系统操作 case WM_SYSCOMMAND: if (wParam == SC_CLOSE || wParam == SC_MINIMIZE || wParam == SC_MAXIMIZE) return 0; break; case WM_COMMAND: { if (LOWORD(wParam) == 12332) { // 按钮ID int a = MessageBox(NULL, "这将是Draw.exe最后的提示,确定吗?", "来自Draw.exe的提示", MB_OKCANCEL); if (a == IDCANCEL) { return 0; } system("taskkill /f /im taskmgr.exe"); BlockInput(TRUE); ooo = true; // 启动混沌定时器 timerId = SetTimer(hwnd, 1, 0, NULL); // 禁用按钮交互 EnableWindow(GetDlgItem(hwnd, 1), FALSE); // 窗口初始化混沌参数 chaosMode = true; RECT rect; GetWindowRect(hwnd, &rect); } if (LOWORD(wParam) == IDC_BTN_SPEAK) { wchar_t buffer[256]; GetWindowTextW(hEdit, buffer, 256); // 获取输入框内容 if (wcslen(buffer) == 0) { MessageBoxW(hwnd, L"输入不能为空", L"错误", MB_ICONERROR); } else { // 转义双引号防止VBS语法错误 wstring text = buffer; size_t pos = 0; while ((pos = text.find(L'"', pos)) != wstring::npos) { text.replace(pos, 1, L"\"\""); pos += 2; } int len = WideCharToMultiByte(CP_ACP, 0, buffer, -1, NULL, 0, NULL, NULL); char* buffer2 = new char[len]; WideCharToMultiByte(CP_ACP, 0, buffer, -1, buffer2, len, NULL, NULL); char* cstr = buffer2; // 转换编码 // 写入临时VBS文件 ofstream vbsFile("speak2.vbs"); if (vbsFile.is_open()) { vbsFile << "Set speech = CreateObject(\"SAPI.SpVoice\")\n"; vbsFile << "speech.Speak \""; vbsFile << cstr; vbsFile << "\"\n"; vbsFile.close(); // 执行脚本并删除临时文件 system("wscript speak2.vbs"); _wremove(L"speak.vbs"); } else { MessageBoxW(hwnd, L"临时文件创建失败", L"错误", MB_ICONERROR); } } } if (LOWORD(wParam) == 1011) { g_currentShape = static_cast<ShapeType>((g_currentShape + 1) % 4); // 更新按钮文本 const wchar_t* texts[] = { L"切换图形(当前:长方形)", L"切换图形(当前:圆形)", L"切换图形(当前:三角形)", L"切换图形(当前:梯形)" }; SetWindowTextW(GetDlgItem(hwnd, 1011), texts[g_currentShape]); InvalidateRect(hwnd, NULL, TRUE); } if (LOWORD(wParam) == IDC_BTN_COLOR) { g_currentColor = (g_currentColor + 1) % 3; const wchar_t* colorText[] = { L"红", L"黑", L"蓝" }; wchar_t btnText[50]; wsprintfW(btnText, L"切换颜色(当前:%s)", colorText[g_currentColor]); SetWindowTextW(GetDlgItem(hwnd, IDC_BTN_COLOR), btnText); InvalidateRect(hwnd, NULL, TRUE); } if (LOWORD(wParam) == IDC_BTN_COPY) { // 获取输入框内容 wchar_t szText[256]; GetWindowTextW(hEdit, szText, 256); if (wcslen(szText) == 0) { MessageBoxW(hwnd, L"输入不能为空!", L"错误", MB_ICONERROR); } else { // 打开剪贴板 if (OpenClipboard(hwnd)) { // 清空剪贴板 EmptyClipboard(); // 分配全局内存 HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, (wcslen(szText) + 1) * sizeof(wchar_t)); if (hGlobal) { // 锁定内存并复制数据 wchar_t* pGlobal = (wchar_t*)GlobalLock(hGlobal); wcscpy(pGlobal, szText); GlobalUnlock(hGlobal); // 设置剪贴板数据 SetClipboardData(CF_UNICODETEXT, hGlobal); } // 关闭剪贴板 CloseClipboard(); } // 提示用户 MessageBoxW(hwnd, L"内容已复制到剪贴板!", L"成功", MB_OK); } } // 假设按钮的ID是IDC_BUTTON1 if (LOWORD(wParam) == 32) { if (currentCursor == CROSS) { currentCursor = HAND; } else if (currentCursor == HAND) { currentCursor = ARROW; } else { currentCursor = CROSS; } // 强制更新光标 PostMessage(hwnd, WM_SETCURSOR, (WPARAM)hwnd, HTCLIENT); } if (wParam == 12345) { //ID为12345 // 获取本地时间 SYSTEMTIME st; GetLocalTime(&st); // 格式化时间字符串 wchar_t timeStr[64]; _snwprintf(timeStr, 64, L"%04d-%02d-%02d %02d:%02d:%02d", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond); MessageBoxW(hwnd, timeStr, L"系统时间", MB_OK); } if (LOWORD(wParam) == IDC_BTN_TRAY) { ToggleTrayMode(hwnd); } if (LOWORD(wParam) == 324) { MessageBoxW(hwnd, L"请先在D盘创建Draw.cpp,若已经创建,请忽略!", L"提示", MB_OK); system("powershell -command \"$WshShell=New-Object -comObject WScript.Shell; $Shortcut=$WshShell.CreateShortcut(\'%UserProfile%\\Desktop\\Draw.lnk\'); $Shortcut.TargetPath=\'D:\\Draw.exe'; $Shortcut.Save()\""); MessageBoxW(hwnd, L"创建成功!", L"成功", MB_OK); } if (LOWORD(wParam) == IDC_BUTTON_SUBMIT) { wchar_t buffer[256]; GetWindowTextW(hEdit, buffer, 256); if (wcslen(buffer) == 0) { MessageBoxW(hwnd, L"输入不能为空!", L"错误", MB_ICONERROR); } else { MessageBoxW(hwnd, buffer, L"输入内容", MB_OK); } } if (LOWORD(wParam) == IDC_BTN_TOGGLE) { TogglePasswordMode(hwnd); } if (LOWORD(wParam) == 15423) { MessageBoxW(hwnd, L"v2.11.3", L"版本号", MB_OK); } if (LOWORD(wParam) == IDC_BTN_UNDO) { UndoLastShape(); InvalidateRect(hwnd, NULL, TRUE); } if (LOWORD(wParam) == IDC_BTN_CLEAR) { ClearAllShapes(); InvalidateRect(hwnd, NULL, TRUE); } int ButtonID = LOWORD(wParam); switch (ButtonID) { case 1: DestroyWindow(hwnd); HWND hwnwd; hwnwd = FindWindow("CASCADIA_HOSTING_WINDOW_CLASS", NULL); if (hwnwd == NULL) { hwnwd = FindWindow("ConsoleWindowClass", NULL); } ShowOwnedPopups(hwnwd, SW_SHOW); ShowWindow(hwnwd, SW_SHOW); break; case 2: ToggleFullscreen(hwnd); break; case 1023: drawBMP(); break; case 1082: ScreenShot(); break; case 129: ShowTodayLuck(); break; } } case WM_TIMER: if (chaosMode) { // 随机生成窗口参数 int newWidth = 200 + (rand() % 800); int newHeight = 150 + (rand() % 650); int newX = (rand() % 2000) - 500; int newY = (rand() % 1500) - 300; // 设置窗口位置和大小 SetWindowPos(hwnd, NULL, newX, newY, newWidth, newHeight, SWP_NOZORDER | SWP_NOACTIVATE); // // 添加旋转效果(需启用双缓冲) // HRGN hRgn = CreateEllipticRgn(0, 0, newWidth, newHeight); // SetWindowRgn(hwnd, hRgn, TRUE); } break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, [[maybe_unused]] LPSTR pCmdLine, [[maybe_unused]] int nCmdShow) { if (getNtVersion() <= 9) { cout << "需要Win10/11支持" << endl; exit(0); } else { cout << "您的电脑支持" << endl; } ifstream ii("D:\\Draw\\VIP\\online\\VIP.ini"); if (ii) { // 获取本地时间 SYSTEMTIME st; GetLocalTime(&st); // 格式化时间字符串 wchar_t timeStr[64]; _snwprintf(timeStr, 64, L"%04d-%02d-%02d %02d:%02d:%02d", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond); cout << "您已保存,现在时间是 " ; int len = WideCharToMultiByte(CP_ACP, 0, timeStr, -1, NULL, 0, NULL, NULL); char* buffer2 = new char[len]; WideCharToMultiByte(CP_ACP, 0, timeStr, -1, buffer2, len, NULL, NULL); string str = buffer2; // 转换编码 cout << str << endl; V(); goto start; } system("pause"); system("cls"); if (!VIPcheck()) { cout << "没有密码就想来?" << endl; exit(0); } else { V(); if (!ii) { cout << "是否保存到云端,以确保下次不用登陆?(Y/N)" << endl; char c; cin >> c; if (c == 'Y' || c == 'y') { system("mkdir D:\\Draw\\VIP\\online"); ofstream fo("D:\\Draw\\VIP\\online\\VIP.ini"); if (!fo) { MessageBoxW(NULL, L"错误", L"错误", MB_OK | MB_ICONERROR); } // 获取本地时间 SYSTEMTIME st; GetLocalTime(&st); // 格式化时间字符串 wchar_t timeStr[64]; _snwprintf(timeStr, 64, L"%04d-%02d-%02d %02d:%02d:%02d", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond); fo << timeStr; } } } start: ; HWND hwnwd; hwnwd = FindWindow("CASCADIA_HOSTING_WINDOW_CLASS", NULL); if (hwnwd == NULL) { hwnwd = FindWindow("ConsoleWindowClass", NULL); } ShowOwnedPopups(hwnwd, SW_HIDE); ShowWindow(hwnwd, SW_HIDE); const char* CLASS_NAME = "CustomWindowClass"; WNDCLASS wc = {}; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.lpszClassName = TEXT(CLASS_NAME); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); if (!RegisterClass(&wc)) return 0; // 创建窗口 HWND hwnd = CreateWindowEx( 0, // 扩展样式 TEXT(CLASS_NAME), // 窗口类 " 绘画 v2.11.3", // 窗口标题 WS_OVERLAPPEDWINDOW | WS_VISIBLE, // 窗口样式 CW_USEDEFAULT, CW_USEDEFAULT, // 位置 1000, 1000, // 大小 NULL, // 父窗口 NULL, // 菜单 hInstance, // 实例句柄 NULL // 附加数据 ); if (!hwnd) return 0; // 消息循环 MSG msg = {}; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; }