桌面插件示例
此示例展示如何开发桌面上的小控件。
重载 UiWidget::StartWidget() 函数,使用 SW_GetFreeID() 获得空闲的控件ID
bool UiWidget_Clock::StartWidget() { UINT id = SW_GetFreeID(); if(id==0) return false; SetID(id); SetTimer(GetParentWnd(), GetID(), 500, 0); return true; }
重载 UiWidget::OnCalcItemSize(__out int &xSize, __out int &ySize) ,指定Widget的尺寸(以桌面图标格子为单位,值的范围是1~4)
void UiWidget_Clock::OnCalcItemSize( __out int &xSize, __out int &ySize ) { xSize = 2; ySize = 2; }
ClockWidget插件的注册表信息
[HKEY_LOCAL_MACHINE\SOFTWARE\Meizu\MiniOneShell\Main\ClockWidget] "DisplayName"="ClockWidget" "ExecFileName"="\\windows\\ClockWidget.dll" "Order"=dword:00000004 "Page"=dword:00000000 "AnimateIcon"=dword:00000000 "ProgramID"="{8A3916DD-6BE9-42fd-9218-DE40C425CB1A}" "IsWidget"=dword:00000001 "WidgetLeft"=dword:00000000 "WidgetTop"=dword:00000000 "WidgetRight"=dword:00000000 "WidgetBottom"=dword:00000000 "IsHide"=dword:00000000
完整示例:
头文件 WidgetTest.h
#pragma once #include <ShellWidget/ShellWidget.h> // Widget必须从UiWidget派生 // 根据需要,可重载StartWidget()以自定义Widget的启动行为 // UiWidget_Clock是一个简易的时钟Widget,显示当前时间,点击会变颜色。 class UiWidget_Clock: public UiWidget { public: UiWidget_Clock(); virtual ~UiWidget_Clock(); virtual void PaintWin(HDC hdcDst, RECT* prcWin, RECT* prcUpdate); virtual int OnLButtonDown(UINT fwKeys, int xPos, int yPos); virtual int OnLButtonUp(UINT fwKeys, int xPos, int yPos); virtual int OnMouseMove(UINT fwKeys, int xPos, int yPos); virtual bool StartWidget(); virtual void EndWidget(); virtual void OnCalcItemSize(__out int &xSize, __out int &ySize); virtual int OnTimer(UINT_PTR nIDEvent); protected: unsigned int Rand(); unsigned int m_rand; ImageContainer m_imgContainer; COLORREF m_clrA; COLORREF m_clrB; COLORREF m_clrC; private: };
源文件 WidgetTest.cpp
#include "WidgetTest.h" void UiWidget_Clock::PaintWin( HDC hdcDst, RECT* prcWin, RECT* prcUpdate ) { COLORREF clr = RGB(255,0,0); RECT rcWin = *prcWin; #if 1 // draw the controls's rect DrawColorLine(hdcDst, rcWin.left, rcWin.top, rcWin.right, rcWin.top, clr); DrawColorLine(hdcDst, rcWin.left, rcWin.bottom-1, rcWin.right, rcWin.bottom-1, clr); DrawColorLine(hdcDst, rcWin.left, rcWin.top, rcWin.left, rcWin.bottom, clr); DrawColorLine(hdcDst, rcWin.right-1, rcWin.top, rcWin.right-1, rcWin.bottom, clr); #endif RECT rcBg = *prcWin; InflateRect(&rcBg, -15,-15); //DrawGradRoundRect(hdcDst, &rcBg, 15, m_clrA, m_clrB, m_clrC); ImagingHelper* pImgBg = m_imgContainer.LoadImage(GetMzResV2ModuleHandle(), MZRESV2_IDR_PNG_BUTTON_BLACK_PRESSED, true); pImgBg->Draw(hdcDst, &rcBg, true); SYSTEMTIME st={0}; GetLocalTime(&st); HFONT font = FontHelper::GetFont(28, FW_NORMAL,0,0,0,FONT_QUALITY_DEFAULT); HGDIOBJ oldfont = SelectObject(hdcDst, font); ::SetTextColor(hdcDst, GetTextColor()); ::SetBkMode(hdcDst, TRANSPARENT); CMzString strTime(MAX_PATH); wsprintf(strTime.C_Str(), L"%02u:%02u:%02u", st.wHour, st.wMinute, st.wSecond); RECT rc = *prcWin; rc.bottom = RECT_CENTER_V(*prcWin); MzDrawText(hdcDst, L"桌面插件Widget", &rc, DT_VCENTER|DT_CENTER); rc.top = rc.bottom; rc.bottom = prcWin->bottom; MzDrawText(hdcDst, strTime.C_Str(), &rc, DT_VCENTER|DT_CENTER); } int UiWidget_Clock::OnLButtonDown( UINT fwKeys, int xPos, int yPos ) { int iRet = UiWidget::OnLButtonDown(fwKeys, xPos, yPos); SetTextColor(Rand()); return iRet; } int UiWidget_Clock::OnLButtonUp( UINT fwKeys, int xPos, int yPos ) { int iRet = UiWidget::OnLButtonUp(fwKeys, xPos, yPos); return iRet; } int UiWidget_Clock::OnMouseMove( UINT fwKeys, int xPos, int yPos ) { int iRet = UiWidget::OnMouseMove(fwKeys, xPos, yPos); return iRet; } UiWidget_Clock::UiWidget_Clock() { m_clrA = RGB(255,128,128); m_clrB = RGB(255,128,255); m_clrC = RGB(128,255,128); } UiWidget_Clock::~UiWidget_Clock() { } bool UiWidget_Clock::StartWidget() { UINT id = SW_GetFreeID(); if(id==0) return false; SetID(id); SetTimer(GetParentWnd(), GetID(), 500, 0); return true; } void UiWidget_Clock::EndWidget() { SW_ReleaseID(GetID()); } int UiWidget_Clock::OnTimer( UINT_PTR nIDEvent ) { if (nIDEvent==GetID()) { //SetTextColor(Rand()); //m_clrA = Rand(); //m_clrB = Rand(); //m_clrC = Rand(); Invalidate(); Update(); } return 0; } unsigned int UiWidget_Clock::Rand() { static bool b = true; if(b) { m_rand = GetTickCount(); b = false; } srand( m_rand); unsigned int r = rand(); srand( (unsigned)r); r = rand(); m_rand = r; return r; } void UiWidget_Clock::OnCalcItemSize( __out int &xSize, __out int &ySize ) { xSize = 2; ySize = 2; }
源码安装在以下目录(假设你将M8SDK安装在盘符C:\):