隐藏基于对话框程序

 
定义托盘响应消息:
#define WM_SYSTRAY WM_USER + 120
 
在头文件中定义变量:
WINDOWPLACEMENT m_wp;    // 以便在恢复时使用
NOTIFYICONDATA m_tmid;      // 托盘结构
CRect        m_rectWnd;       // 保存窗口区域
 
在初始化函数中加入:
              GetWindowRect(&m_rectWnd); // 恢复时使用
              GetWindowPlacement(&m_wp); //恢复时用
              ModifyStyleEx (WS_EX_APPWINDOW, WS_EX_TOOLWINDOW); // 不在工具栏显示
                           
              WINDOWPLACEMENT wp;
             
              wp.length = sizeof(WINDOWPLACEMENT);
              wp.flags = WPF_RESTORETOMAXIMIZED;
              wp.showCmd = SW_HIDE;
              SetWindowPlacement(&wp);
             
              m_tmid.cbSize = sizeof (NOTIFYICONDATA);
              m_tmid.hWnd = this->m_hWnd;
              m_tmid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
              m_tmid.uCallbackMessage = WM_SYSTRAY;
              m_tmid.uID = IDR_MAINFRAME;
             
              CString sz = _T("");
              strcpy(m_tmid.szTip, "");
              HICON hIcon;
              hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
              m_tmid.hIcon = hIcon;
              PNOTIFYICONDATA m_ptnid = &m_tmid;
              ::Shell_NotifyIcon(NIM_ADD, m_ptnid);
              if (hIcon)
                     ::DestroyIcon(hIcon);
 
增加窗口消息函数 WM_DESTROY 增加如下
void CClientDlg::OnDestroy()
{
       CDialog::OnDestroy();
       ::Shell_NotifyIcon(NIM_DELETE ,&m_tmid);   
}
响应WindowProc消息
LRESULT CClientDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
        switch (message)
          {
          case WM_SYSTRAY:
                 if (lParam == WM_LBUTTONDBLCLK)
                 {
                        // 恢复窗口
                        ModifyStyleEx(WS_EX_TOOLWINDOW, WS_EX_APPWINDOW);
                        SetWindowPlacement(&m_wp);
                        MoveWindow(m_rectWnd, TRUE);
                        CenterWindow();
                 }
                 break;
          case WM_SYSCOMMAND:
                 if (wParam == SC_MINIMIZE)
                 {
                        AfxGetMainWnd()->ShowWindow (SW_HIDE);
                        return 0;
                 }
 
                 if (wParam == SC_CLOSE)
                 {
                        OnCancel();
                        return 0;
                 }
                 break;
          }
      
       return CDialog::WindowProc(message, wParam, lParam);
}
 

你可能感兴趣的:(对话框)