LouZhang

导航

对话框学习4

基于对话框的拖盘程序

首先为C**Dlg类添加自定义消息WM_TRAY_NOTIFICATION

并添加代码如下:

afx_msg LRESULT CTrayDlg::OnTrayNotification(WPARAM wParam, LPARAM lParam)
{
    switch (lParam){
    case WM_RBUTTONUP:
    case WM_LBUTTONDBLCLK:
        ShowWindow(SW_SHOW);
        TrayMessage(m_hWnd, NIM_DELETE, NULL, "");
    }

    return 0;
}

响应方式为右键单击或者左键双击

然后添加全局函数TrayMessage

BOOL TrayMessage(HWND hWnd, DWORD dwMessage, HICON hIcon, PSTR pszTip){
    BOOL bReturn;
    NOTIFYICONDATA NofityData;
    NofityData.cbSize = sizeof(NOTIFYICONDATA);
    NofityData.hWnd = hWnd;
    NofityData.uID = IDR_MAINFRAME;
    NofityData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
    NofityData.uCallbackMessage = WM_TRAY_NOTIFICATION;
    NofityData.hIcon = hIcon;
    lstrcpyn(NofityData.szTip, pszTip, sizeof(NofityData.szTip));
    bReturn = Shell_NotifyIcon(dwMessage, &NofityData);
    if(hIcon)
        DestroyIcon(hIcon);
    return bReturn;
}

关键部分:

NOTIFYICONDATA  http://baike.baidu.com/view/1873525.htm

然后加入最小化处理事件(当然也可以不需要)

void CTrayDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
        CAboutDlg dlgAbout;
        dlgAbout.DoModal();
    }else if(nID == SC_MINIMIZE){
        TrayMessage(m_hWnd, NIM_ADD, NULL, "托盘实例");
        TrayMessage(m_hWnd, NIM_MODIFY, NULL, "托盘实例");
        ShowWindow(SW_HIDE);
    }
    else
    {
        CDialogEx::OnSysCommand(nID, lParam);
    }
}

最后为对话框添加一个按钮,并添加单击事件

void CTrayDlg::OnBnClickedButton1()
{
    // TODO: 在此添加控件通知处理程序代码
    TrayMessage(m_hWnd, NIM_ADD, NULL, "托盘实例");
    TrayMessage(m_hWnd, NIM_MODIFY, NULL, "托盘实例");
    ShowWindow(SW_HIDE);
}

 

现在还不会加载资源图标。。。慢慢学习吧!

posted on 2012-09-28 13:27  louzhang_swk  阅读(305)  评论(0编辑  收藏  举报