Windows API:为什么buttonhover效果在一段时间后卡住了?

最近开始使用Windows API开发,我决定有一些交互性。 所以计划是使用Owner Drawnbutton,当鼠标进入矩形区域时会改变背景颜色,当鼠标离开时会变回原来的颜色。

我做了3个这样的button,并用他们自己的自定义窗口过程对它们进行了分类。 现在因为没有WM_MOUSEENTER消息,所以我使用了WM_MOUSEMOVE和一些鼠标跟踪来填充窗口。 这基本上意味着每当光标在窗口内移动时,矩形就会被填充。

当鼠标指针移动到button的矩形外时,我也使用WM_MOUSELEAVE来填充button。

而这似乎开始工作很好,但一段时间后,矩形填充将停止,背景将卡住hover颜色或默认颜色。

这是代码

 #ifndef UWMENU_H_INCLUDED #define UWMENU_H_INCLUDED #endif // UWMENU_H_INCLUDED #include <iostream> using namespace std; LRESULT CALLBACK DBWndProc(HWND, UINT, WPARAM, LPARAM); WNDPROC DBWndProcOld[3]; class UWMenu{ public: //WINDOW PROCEDURE FOR NEWS BUTTON static LRESULT CALLBACK DBWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){ HBRUSH BG_btnOne = CreateSolidBrush(NAV_BTN_DEF_CLR); HBRUSH BG_btnOneOver = CreateSolidBrush(NAV_BTN_OVER_CLR); int btnID = GetDlgCtrlID(hwnd); cout << btnID << "\n"; switch(msg){ case WM_MOUSEMOVE:{ cout << "Mouse is hovering over button\n"; SetCursor(LoadCursor(NULL, IDC_HAND)); HDC hdc = GetDC(hwnd); RECT rc; rc.left = 0; //Top left X coordinate of button's rectangular area rc.top = 0; //Top left Y coordinate of button's rectangular area rc.right = NAV_BTN_WIDTH; //Bottom right X coordinate of button's rectangular area rc.bottom = NAV_BTN_HEIGHT; //Bottom right Y coordinate of button's rectangular area printf("Repainting button background color\n"); FillRect(hdc, &rc, BG_btnOneOver); SetTextColor(hdc, RGB(255,255,255)); SetBkMode(hdc, TRANSPARENT); if(btnID == NBID_NEWS){DrawText(hdc, TXT_BN_NEWS, -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);} if(btnID == NBID_TOOLS){DrawText(hdc, TXT_BN_TOOLS, -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);} if(btnID == NBID_EDITOR){DrawText(hdc, TXT_BN_EDITORS, -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);} ReleaseDC(hwnd, hdc); TRACKMOUSEEVENT tme; tme.cbSize = sizeof(TRACKMOUSEEVENT); tme.dwFlags = TME_LEAVE; tme.hwndTrack = hwnd; tme.dwHoverTime = HOVER_DEFAULT; TrackMouseEvent(&tme); } break; case WM_MOUSELEAVE:{ printf("You left me\n"); SetCursor(LoadCursor(NULL, IDC_ARROW)); HDC hdc = GetDC(hwnd); RECT rc; rc.left = 0; //Top left X coordinate of button's rectangular area rc.top = 0; //Top left Y coordinate of button's rectangular area rc.right = NAV_BTN_WIDTH; //Bottom right X coordinate of button's rectangular area rc.bottom = NAV_BTN_HEIGHT; //Bottom right Y coordinate of button's rectangular area FillRect(hdc, &rc, BG_btnOne); SetTextColor(hdc, RGB(255,255,255)); SetBkMode(hdc, TRANSPARENT); if(btnID == NBID_NEWS){DrawText(hdc, TXT_BN_NEWS, -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);} if(btnID == NBID_TOOLS){DrawText(hdc, TXT_BN_TOOLS, -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);} if(btnID == NBID_EDITOR){DrawText(hdc, TXT_BN_EDITORS, -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);} ReleaseDC(hwnd, hdc); TRACKMOUSEEVENT tme; tme.cbSize = sizeof(TRACKMOUSEEVENT); tme.dwFlags = TME_CANCEL; tme.hwndTrack = hwnd; tme.dwHoverTime = HOVER_DEFAULT; TrackMouseEvent(&tme); } break; } return CallWindowProc(DBWndProcOld[btnID-200], hwnd, msg, wParam, lParam); } }; 

mouse_move消息正在作为printf()的输出进行处理,当鼠标移动到button上时, cout以debugging模式显示。

有没有什么办法解决这一问题? 可能跟鼠标跟踪有什么关系?

我认为问题是这个button有点像FillRect()而指针在矩形里面。

任何帮助表示赞赏。

这个问题现在已经解决了。 这是由于GDI泄漏。 通过删除在父窗口过程中重复创建的画笔解决了问题。