基于duilib实现的激光笔小工具
Posted a1aa
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于duilib实现的激光笔小工具相关的知识,希望对你有一定的参考价值。
需求描述:模拟真实的激光笔,用于在ppt播放时可通过移动端进行控制;
实现思路:c++ + win32 api + UI(duilib)+鼠标事件+定时器策略,激光笔跟随鼠标的移动而移动,鼠标双击退出;
具体效果:
1 #pragma once 2 #include "stdafx.h" 3 class wnd_laserpen : public CWindowWnd, public INotifyUI 4 { 5 public: 6 wnd_laserpen() { 7 Create(NULL, _T("wnd_laserpen"), WS_VISIBLE, WS_EX_TOOLWINDOW | WS_EX_TOPMOST); 8 }; 9 public: 10 //显示隐藏下载管理器事件处理 11 bool OnRootEvent(void* param) 12 { 13 TEventUI* pEvent = (TEventUI*)param; 14 //鼠标双击 15 if (pEvent->Type == UIEVENT_DBLCLICK) 16 { 17 KillTimer(m_hWnd, 8888); 18 exit(0); 19 } 20 return true; 21 } 22 void Init() 23 { 24 ShowCursor(FALSE); 25 SetTimer(m_hWnd, 8888, 50, NULL); 26 m_root= static_cast<CVerticalLayoutUI*>(m_pm.FindControl(_T("root"))); 27 if (m_root) 28 { 29 m_root->OnEvent += DuiLib::MakeDelegate(this, &wnd_laserpen::OnRootEvent); 30 } 31 } 32 public: 33 LPCTSTR GetWindowClassName() const { return _T("wnd_laserpen"); }; 34 UINT GetClassStyle() const { return CS_DBLCLKS; }; 35 void OnFinalMessage(HWND /*hWnd*/) { delete this; }; 36 void Notify(TNotifyUI& msg) 37 { 38 39 } 40 41 void OnLClick(CControlUI *pControl) 42 { 43 44 } 45 46 47 LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) 48 { 49 // ICONS 图标加载 50 SetIcon(IDI_SMALL); 51 52 LONG styleValue = ::GetWindowLong(*this, GWL_STYLE); 53 styleValue &= ~WS_CAPTION; 54 ::SetWindowLong(*this, GWL_STYLE, styleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN); 55 56 m_pm.Init(m_hWnd); 57 CDialogBuilder builder; 58 CControlUI* pRoot = builder.Create(_T("wnd_laserpen.xml"), (UINT)0, NULL, &m_pm); 59 ASSERT(pRoot && "Failed to parse XML"); 60 m_pm.AttachDialog(pRoot); 61 m_pm.AddNotifier(this); 62 return 0; 63 } 64 65 LRESULT OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) 66 { 67 bHandled = FALSE; 68 return 0; 69 } 70 71 LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) 72 { 73 bHandled = FALSE; 74 return 0; 75 } 76 77 LRESULT OnNcActivate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) 78 { 79 if (::IsIconic(*this)) bHandled = FALSE; 80 return (wParam == 0) ? TRUE : FALSE; 81 } 82 83 LRESULT OnNcCalcSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) 84 { 85 return 0; 86 } 87 88 LRESULT OnNcPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) 89 { 90 return 0; 91 } 92 //用于控制自我关闭 93 LRESULT OnTimer(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) 94 { 95 POINT p; 96 GetCursorPos(&p);//获取鼠标坐标 97 ::SetWindowPos(m_hWnd, HWND_TOPMOST, p.x-11, p.y-11, 0, 0, SWP_NOSIZE); 98 bHandled = TRUE; 99 return 0; 100 } 101 LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) 102 { 103 LRESULT lRes = 0; 104 BOOL bHandled = TRUE; 105 106 switch (uMsg) { 107 case WM_CREATE: lRes = OnCreate(uMsg, wParam, lParam, bHandled); break; 108 case WM_CLOSE: lRes = OnClose(uMsg, wParam, lParam, bHandled); break; 109 case WM_DESTROY: lRes = OnDestroy(uMsg, wParam, lParam, bHandled); break; 110 case WM_NCACTIVATE: lRes = OnNcActivate(uMsg, wParam, lParam, bHandled); break; 111 case WM_NCCALCSIZE: lRes = OnNcCalcSize(uMsg, wParam, lParam, bHandled); break; 112 case WM_NCPAINT: lRes = OnNcPaint(uMsg, wParam, lParam, bHandled); break; 113 case WM_KILLFOCUS: lRes = OnKillFocus(uMsg, wParam, lParam, bHandled); break; 114 case WM_TIMER: 115 if (wParam == 8888) 116 { 117 //KillTimer(m_hWnd, 8888); 118 OnTimer(uMsg, wParam, lParam, bHandled); 119 //SetTimer(m_hWnd, 8888, 50, NULL); 120 } 121 bHandled = TRUE; 122 default: 123 bHandled = FALSE; 124 } 125 126 if (uMsg == WM_LASERPEN_CLOSE) 127 { 128 KillTimer(m_hWnd, 8888); 129 exit(0); 130 bHandled = FALSE; 131 return 0; 132 } 133 if (bHandled) return lRes; 134 if (m_pm.MessageHandler(uMsg, wParam, lParam, lRes)) return lRes; 135 return CWindowWnd::HandleMessage(uMsg, wParam, lParam); 136 } 137 LRESULT OnKillFocus(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) 138 { 139 //Close(0); 140 return 0; 141 } 142 public: 143 CPaintManagerUI m_pm; 144 CVerticalLayoutUI* m_root; 145 };
xml文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <Window size="22,22" bktrans="true"> 3 <VerticalLayout name="root" width="22" height="22" bkimage="laserpen1.png"> 4 5 </VerticalLayout> 6 </Window>
以上是关于基于duilib实现的激光笔小工具的主要内容,如果未能解决你的问题,请参考以下文章