• 隐藏基于对话框程序


     
    定义托盘响应消息:
    #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);
    }
     
  • 相关阅读:
    dubbo 学习
    JSTL 实现 为Select赋多个值
    Spring MVC 单元测试Demo
    IDEA git commit push revert
    高并发处理
    Redis Expire TTL命令
    Redis 原子操作INCR
    Redis 安装
    慢日志查询
    angularJs 处理多选框(checkbox)
  • 原文地址:https://www.cnblogs.com/tyjsjl/p/2156084.html
Copyright © 2020-2023  润新知