diff --git a/lib/EasyWin32.cpp b/lib/EasyWin32.cpp index 6771ffa..f19f2cd 100644 --- a/lib/EasyWin32.cpp +++ b/lib/EasyWin32.cpp @@ -2,7 +2,7 @@ // // EasyWin32.cpp // -// Ver 3.1.0 +// Ver 3.1.1 // // @@ -723,8 +723,8 @@ int SetWindowExStyle(long lNewExStyle) HICON GetDefaultAppIcon() { IMAGE img = GetDefaultIconImage(); - HBITMAP hBmp = GetImageHBitmap(&img); - HICON hIcon = HICONFromHBitmap(hBmp); + HBITMAP hBmp = Image2Bitmap(&img); + HICON hIcon = Bitmap2Icon(hBmp); DeleteObject(hBmp); return hIcon; } @@ -856,16 +856,17 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) BOOL repeatFlag = (keyFlags & KF_REPEAT) == KF_REPEAT; // previous key-state flag, 1 on autorepeat WORD repeatCount = LOWORD(lParam); // repeat count, > 0 if several keydown messages was combined into one message BOOL upFlag = (keyFlags & KF_UP) == KF_UP; // transition-state flag, 1 on keyup - + + // 功能键:不区分左右 // if we want to distinguish these keys: - switch (vkCode) - { - case VK_SHIFT: // converts to VK_LSHIFT or VK_RSHIFT - case VK_CONTROL: // converts to VK_LCONTROL or VK_RCONTROL - case VK_MENU: // converts to VK_LMENU or VK_RMENU - vkCode = LOWORD(MapVirtualKeyW(scanCode, MAPVK_VSC_TO_VK_EX)); - break; - } + //switch (vkCode) + //{ + //case VK_SHIFT: // converts to VK_LSHIFT or VK_RSHIFT + //case VK_CONTROL: // converts to VK_LCONTROL or VK_RCONTROL + //case VK_MENU: // converts to VK_LMENU or VK_RMENU + // vkCode = LOWORD(MapVirtualKeyW(scanCode, MAPVK_VSC_TO_VK_EX)); + // break; + //} ExMessage msgKey = {}; msgKey.message = msg; @@ -877,7 +878,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) g_vecWindows[indexWnd].vecMessage.push_back(msgKey); // 给控制台发一份,支持 _getch() 系列函数 - SendMessage(g_hConsole, msg, wParam, lParam); + PostMessage(g_hConsole, msg, wParam, lParam); } break; @@ -890,7 +891,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) g_vecWindows[indexWnd].vecMessage.push_back(msgChar); // 通知控制台 - SendMessage(g_hConsole, msg, wParam, lParam); + PostMessage(g_hConsole, msg, wParam, lParam); } break; @@ -1198,23 +1199,29 @@ HWND initgraph_win32(int w, int h, int flag, LPCTSTR strWndTitle, bool(*WindowPr } } - -EASY_WIN32_END - ////////////****** 其他函数 ******//////////// -void HpSleep(int ms) +HBITMAP Image2Bitmap(IMAGE* img) { - static clock_t oldclock = clock(); // 静态变量,记录上一次 tick + return CreateBitmap(img->getwidth(), img->getheight(), 1, 32, (void*)GetImageBuffer(img)); +} - oldclock += ms * CLOCKS_PER_SEC / 1000; // 更新 tick +HICON Bitmap2Icon(HBITMAP hBmp) +{ + BITMAP bmp = {}; + GetObject(hBmp, sizeof(BITMAP), &bmp); - if (clock() > oldclock) // 如果已经超时,无需延时 - oldclock = clock(); - else - while (clock() < oldclock) // 延时 - Sleep(1); // 释放 CPU 控制权,降低 CPU 占用率 -// Sleep(0); // 更高精度、更高 CPU 占用率 + HBITMAP hbmMask = CreateCompatibleBitmap(GetDC(NULL), bmp.bmWidth, bmp.bmHeight); + + ICONINFO ii = { 0 }; + ii.fIcon = TRUE; + ii.hbmColor = hBmp; + ii.hbmMask = hbmMask; + + HICON hIcon = CreateIconIndirect(&ii); + DeleteObject(hbmMask); + + return hIcon; } void outtextxy_format(int x, int y, int _Size, const wchar_t* _Format, ...) @@ -1228,25 +1235,19 @@ void outtextxy_format(int x, int y, int _Size, const wchar_t* _Format, ...) delete buf; } -HBITMAP GetImageHBitmap(IMAGE* img) -{ - return CreateBitmap(img->getwidth(), img->getheight(), 1, 32, (void*)GetImageBuffer(img)); -} +EASY_WIN32_END -HICON HICONFromHBitmap(HBITMAP hBmp) +void HpSleep(int ms) { - BITMAP bmp = {}; - GetObject(hBmp, sizeof(BITMAP), &bmp); - - HBITMAP hbmMask = CreateCompatibleBitmap(GetDC(NULL), bmp.bmWidth, bmp.bmHeight); - - ICONINFO ii = { 0 }; - ii.fIcon = TRUE; - ii.hbmColor = hBmp; - ii.hbmMask = hbmMask; + static clock_t oldclock = clock(); // 静态变量,记录上一次 tick - HICON hIcon = CreateIconIndirect(&ii); - DeleteObject(hbmMask); + oldclock += ms * CLOCKS_PER_SEC / 1000; // 更新 tick - return hIcon; + if (clock() > oldclock) // 如果已经超时,无需延时 + oldclock = clock(); + else + while (clock() < oldclock) // 延时 + Sleep(1); // 释放 CPU 控制权,降低 CPU 占用率 +// Sleep(0); // 更高精度、更高 CPU 占用率 } + diff --git a/lib/EasyWin32.h b/lib/EasyWin32.h index 98f3443..423a11c 100644 --- a/lib/EasyWin32.h +++ b/lib/EasyWin32.h @@ -4,11 +4,11 @@ // 基于 EasyX 图形库的 Win32 拓展库 // // 作  者:huidong -// 版  本:Ver 3.1.0 +// 版  本:Ver 3.1.1 // 编译环境:VisualStudio 2022 | EasyX_20220610 | Windows 10 // 项目地址:https://github.com/zouhuidong/EasyWin32 // 创建日期:2020.12.06 -// 最后修改:2022.07.15 +// 最后修改:2022.07.16 // #pragma once @@ -66,6 +66,65 @@ struct EasyWindow int nSkipPixels; // 绘制时跳过的像素点数量(降质性速绘) }; +// 鼠标拖动消息 +// 调用方法: +// 需要在鼠标消息循环中每次都调用 UpdateMessage 更新鼠标消息 +// 调用 isLeftDrag,isMiddleDrag,isRightDrag 函数判断正在拖动的鼠标按键 +// 调用 GetDragX,GetDragY 获取鼠标拖动时鼠标坐标的变化量 +class MouseDrag +{ +private: + ExMessage old, msg; + int dx, dy; + bool lbtn = false, mbtn = false, rbtn = false; + bool newmsg = false; + + bool UpdateDragInfo(bool& btn, int msgid_down, int msgid_up) + { + if (newmsg) + { + if (btn) + { + dx = msg.x - old.x; + dy = msg.y - old.y; + old = msg; + if (msg.message == msgid_up) btn = false; + if (dx != 0 || dy != 0) return true; + else return false; + } + else + { + if (msg.message == msgid_down) + { + btn = true; + old = msg; + } + return false; + } + newmsg = false; + } + else + { + return false; + } + } + +public: + + void UpdateMessage(ExMessage m) + { + msg = m; + newmsg = true; + } + + bool isLeftDrag() { return UpdateDragInfo(lbtn, WM_LBUTTONDOWN, WM_LBUTTONUP); } + bool isMiddleDrag() { return UpdateDragInfo(mbtn, WM_MBUTTONDOWN, WM_MBUTTONUP); } + bool isRightDrag() { return UpdateDragInfo(rbtn, WM_RBUTTONDOWN, WM_RBUTTONUP); } + + int GetDragX() { return dx; } + int GetDragY() { return dy; } +}; + ////////////****** 窗体相关函数 ******//////////// // 创建支持 win32 的绘图窗口(默认支持窗口双击消息) @@ -241,6 +300,18 @@ MOUSEMSG To_MouseMsg(ExMessage msgEx); UINT GetExMessageType(ExMessage msg); +////////////****** 图像处理相关函数 ******//////////// + +// 得到 IMAGE 对象的 HBITMAP +HBITMAP Image2Bitmap(IMAGE* img); + +// HBITMAP 转 HICON +HICON Bitmap2Icon(HBITMAP hBmp); + +// 在屏幕指定位置输出格式化文本 +void outtextxy_format(int x, int y, int _Size, LPCTSTR lpFormat, ...); + + EASY_WIN32_END ////////////****** 任务指令宏定义 ******//////////// @@ -321,20 +392,10 @@ EASY_WIN32_END ////////////****** 其他函数 ******//////////// - // 精确延时函数(可以精确到 1ms,精度 ±1ms) // by yangw80, 2011-5-4 void HpSleep(int ms); -// 在屏幕指定位置输出格式化文本 -void outtextxy_format(int x, int y, int _Size, LPCTSTR lpFormat, ...); - -// 得到 IMAGE 对象的 HBITMAP -HBITMAP GetImageHBitmap(IMAGE* img); - -// HBITMAP 转 HICON -HICON HICONFromHBitmap(HBITMAP hBmp); - // 常用色彩扩展 enum COLORS { DARKBLUE = RGB(0x00, 0x00, 0x8B),