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


//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;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值