|
写个桌面可以移动插件,该插件被限制在屏幕的最上方,可左右拖拽移动,无法上下移动,拖拽下拉该控件有回弹的效果,松开可以切换壁纸。右键点击该插件可以弹出菜单。
以下是基本的C++程序代码思路和一些需要用到的API函数 可以翻译下吗
- 1. 定义控件的初位置和大小,设置控件可以接受鼠标消息。
- ```
- HWND hWnd = CreateWindowEx(WS_EX_TRANSPARENT, L"STATIC", L"", WS_VISIBLE | WS_CHILD | WS_CLIPSIBLINGS | SS_BITMAP, xPos, yPos, width, height, parentHwnd, NULL, hInstance, NULL);
- SetWindowLongPtr(hWnd, GWL_STYLE, GetWindowLongPtr(hWnd, GWL_STYLE) | WS_EX_ACCEPTFILES);
- ```
- 2. 实现鼠标拖拽和回弹的效果。需要用到以下函数:GetCursorPos、SetCursorPos、ScreenToClient、ClientToScreen、SendMessage、ReleaseCapture。
- ```
- case WM_LBUTTONDOWN:
- {
- ReleaseCapture();
- POINT cursorPos;
- GetCursorPos(&cursorPos);
- ScreenToClient(hWnd, &cursorPos);
- SetCursorPos(cursorPos.x, cursorPos.y);
- m_bDragging = true;
- m_ptStartPos = cursorPos;
- break;
- }
- case WM_MOUSEMOVE:
- {
- if (m_bDragging)
- {
- POINT cursorPos;
- GetCursorPos(&cursorPos);
- ScreenToClient(hWnd, &cursorPos);
- int delta = cursorPos.x - m_ptStartPos.x;
- int xPos = m_ptStartPos.x + delta;
- xPos = max(xPos, 0);
- RECT rcParent;
- GetClientRect(GetParent(hWnd), &rcParent);
- xPos = min(xPos, rcParent.right - m_width);
- SetWindowPos(hWnd, NULL, xPos, m_yPos, 0, 0, SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
- m_ptLastPos = cursorPos;
- }
- break;
- }
- case WM_LBUTTONUP:
- {
- if (m_bDragging)
- {
- m_bDragging = false;
- if (m_ptLastPos.x < m_ptStartPos.x)
- {
- SendMessage(hWnd, WM_MOVE, 0, MAKELPARAM(0, m_yPos));
- }
- else if (m_ptLastPos.x > m_ptStartPos.x)
- {
- SendMessage(hWnd, WM_MOVE, 0, MAKELPARAM(rcParent.right - m_width, m_yPos));
- }
- }
- break;
- }
- ```
- 3. 实现切换壁纸的功能,需要用到以下函数:SystemParametersInfo。
- ```
- case WM_LBUTTONUP:
- {
- if (m_bDragging)
- {
- // ...
- // 切换壁纸
- m_bmpIndex = (m_bmpIndex + 1) % m_bmpList.size();
- SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (PVOID)m_bmpList[m_bmpIndex].c_str(), SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
- break;
- }
- }
- ```
- 4. 实现右键菜单的弹出,需要用到以下函数:CreatePopupMenu、AppendMenu、TrackPopupMenu、DestroyMenu。
- ```
- case WM_CONTEXTMENU:
- {
- HMENU hPopupMenu = CreatePopupMenu();
- AppendMenu(hPopupMenu, MF_STRING, ID_MENU_ITEM1, L"菜单项1");
- AppendMenu(hPopupMenu, MF_STRING, ID_MENU_ITEM2, L"菜单项2");
- AppendMenu(hPopupMenu, MF_STRING, ID_MENU_ITEM3, L"菜单项3");
- TrackPopupMenu(hPopupMenu, TPM_TOPALIGN | TPM_LEFTALIGN, ptCursor.x, ptCursor.y, 0, hWnd, NULL);
- DestroyMenu(hPopupMenu);
- break;
- }
- ```
复制代码 |
|