您的位置:首页 > 其它

在任务栏上的时钟区域显示自己的内容 MFC 封装

2016-07-15 22:18 471 查看
//ClockTrayWnd.h

#pragma once

class CColockTrayWnd : public CWnd
{
CWnd *m_pNotifyWnd; //通知窗口
UINT m_uNotifyMessage; //通知消息

public:
CColockTrayWnd()
{
m_pNotifyWnd = NULL;
m_uNotifyMessage = 0;
}
virtual ~CColockTrayWnd()
{
}

protected:
virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case(WM_LBUTTONDOWN):
case(WM_LBUTTONUP):
case(WM_LBUTTONDBLCLK):
case(WM_RBUTTONDBLCLK):
case(WM_RBUTTONDOWN):
case(WM_RBUTTONUP):
{
//发送通知 wParam发送消息, 更细节的可以分析lParam (MSG)
if(m_pNotifyWnd && m_uNotifyMessage)
{
DWORD dwPtr = GetMessagePos();
MSG msg =
{
m_hWnd, message, wParam, lParam,
GetMessageTime(),
(GET_X_LPARAM(dwPtr), GET_Y_LPARAM(dwPtr))
};

return m_pNotifyWnd->SendMessage(m_uNotifyMessage, message, (LPARAM)&msg);
}
break;
}
case(WM_TIMER):
{
InvalidateRect(NULL, FALSE);
break;
}
case(WM_PAINT):
{
TCHAR szBuff[256];

//获取当前时间
SYSTEMTIME tmCur;
GetLocalTime(&tmCur);
//格式化时间
_stprintf_s(szBuff, _T("%02d:%02d\n%02dW%d"),
tmCur.wHour, tmCur.wMinute,
tmCur.wSecond, tmCur.wDayOfWeek);

PAINTSTRUCT ps;
CDC *pDC = BeginPaint(&ps);

CRect rt;
GetClientRect(&rt);
pDC->DrawText(szBuff, &rt, DT_LEFT|DT_TOP);

EndPaint(&ps);
break;
}
}

return CWnd::WindowProc(message, wParam, lParam);
}

public:
BOOL CreateTrayWnd(CWnd *pNotifyWnd, UINT uMessage)
{
do
{
//查找到目标窗口
HWND hShellTrayWnd = ::FindWindow( _T("Shell_TrayWnd"), NULL);//任务栏
HWND hTrayNotifyWnd = ::FindWindowEx(hShellTrayWnd, NULL, _T("TrayNotifyWnd"), NULL);//托盘区域
HWND hTrayClock = ::FindWindowEx(hTrayNotifyWnd, NULL, _T("TrayClockWClass"), NULL); //时钟区域
if(hTrayClock == NULL)
break;

//令父窗口剪裁客户区域
DWORD dwNewStyle = ::GetWindowLongPtr(hTrayClock, GWL_STYLE) | WS_CLIPSIBLINGS;
::SetWindowLongPtr(hTrayClock, GWL_STYLE, dwNewStyle);

//获取窗口尺寸
RECT rcCtrl = {0,0, 100, 50};
::GetClientRect(hTrayClock, &rcCtrl);

//以目标窗口为父窗口创建窗口
if(!CreateEx(WS_EX_TOOLWINDOW,
AfxRegisterWndClass(CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS | CS_OWNDC | CS_SAVEBITS),
_T("TrayClockWnd"), WS_CHILD|WS_VISIBLE, //|WS_EX_TOPMOST,
0, 0, rcCtrl.right - rcCtrl.left, rcCtrl.bottom - rcCtrl.top,
hTrayClock, NULL, NULL))
{
break;
}

//启动刷新定时器
SetTimer(10, 100, NULL);

m_pNotifyWnd = pNotifyWnd;
m_uNotifyMessage = uMessage;
}while(0);

return m_hWnd != NULL;
}
};



//在对话框类上做管理

#include "ColockTrayWnd.h"

/////////////////////////////////////////////////////////////////////////////
// CT1Dlg dialog

class CT1Dlg : public CDialog
{
CColockTrayWnd m_TrayWnd;
#define WM_MYTRAYNOTIFY (WM_USER + 0x200)
afx_msg LRESULT TrayNotifyProc(WPARAM wParam, LPARAM lParam);


//T1Dlg.cpp

BEGIN_MESSAGE_MAP(CT1Dlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_MESSAGE(WM_MYTRAYNOTIFY, TrayNotifyProc)
END_MESSAGE_MAP()

BOOL CT1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
……
VERIFY( m_TrayWnd.CreateTrayWnd(this, WM_MYTRAYNOTIFY) );

return TRUE;  // return TRUE  unless you set the focus to a control
}

//消息处理
LRESULT CT1Dlg::TrayNotifyProc(WPARAM wParam, LPARAM lParam)
{
MSG *pMSG = (MSG*)lParam;
if(wParam == WM_RBUTTONDBLCLK)
{
m_TrayWnd.DestroyWindow();
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: