在任务栏上的时钟区域显示自己的内容 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);
#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; }
相关文章推荐
- 补码反码原码
- 滑动窗口的最大值
- 谈谈javaweb中的重定向
- stm32 i2c通信 [操作寄存器+库函数]
- Android CoordinatorLayout + AppBarLayout(向上滚动隐藏指定的View)
- python IO多路复用,初识多线程
- 网络基本功(六):链路聚合
- LeetCode 258. Add Digits两种方法
- shell脚本 - 学习if语句和变量赋值
- c#之checkbox是否被选中
- UVA 437 巴比伦塔
- 监听手机来电与手机短信往蓝牙发送协议实现通讯
- STM32学习笔记之定时器输入捕获实验
- Codeforces Round #362 (Div. 2) C. Lorenzo Von Matterhorn (二叉树+map)
- 网络基本功(五):细说路由(下)
- bzoj 2435 DFS
- 网络基本功(四):细说路由(上)
- 枚举类型小结
- 解决复杂问题的思路(二)
- 攻击中东欧的间谍工具集20160714 反连 邮件-http隧道的后门