win32_taskbar

#include <windows.h>

#define IDR_PAUSE 12
#define IDR_START 13

LPCTSTR szAppName = TEXT("Iec104Proxy");
LPCTSTR szWndName = TEXT("Iec104ProxyMainWnd");
HMENU hmenu;

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
    NOTIFYICONDATA nid;
    UINT WM_TASKBARCREATED;
    POINT pt;
    int xx;
    WM_TASKBARCREATED = RegisterWindowMessage(TEXT("TaskbarCreated"));
    switch (message){
        case WM_CREATE:
            nid.cbSize = sizeof(nid);
            nid.hWnd = hwnd;
            nid.uID = 0;
            nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
            nid.uCallbackMessage = WM_USER;
            nid.hIcon = LoadIcon(NULL, IDI_APPLICATION);
            lstrcpy(nid.szTip, szAppName);Shell_NotifyIcon(NIM_ADD, &nid);
            hmenu=CreatePopupMenu();
            AppendMenu(hmenu,MF_STRING,IDR_PAUSE,TEXT("暂停服务"));
            AppendMenu(hmenu,MF_STRING,IDR_START,TEXT("恢复服务"));
            break;
        case WM_USER:{
            if(lParam==WM_LBUTTONDOWN)
                MessageBox(hwnd, TEXT("Win32 API 实现系统托盘程序,双击托盘可以退出!"), szAppName, MB_OK);
            if(lParam==WM_LBUTTONDBLCLK)
                SendMessage(hwnd, WM_CLOSE, wParam, lParam);
            if(lParam==WM_RBUTTONDOWN){
                GetCursorPos(&pt);
                SetForegroundWindow(hwnd);// 解决在菜单外单击菜单不消失的问题
                EnableMenuItem(hmenu,IDR_PAUSE,MF_GRAYED);
                xx=TrackPopupMenu(hmenu,TPM_RETURNCMD,pt.x,pt.y,NULL,hwnd,NULL);
                if(xx==IDR_PAUSE) MessageBox(hwnd, TEXT("111"), szAppName, MB_OK);
                if(xx==IDR_START) MessageBox(hwnd, TEXT("222"), szAppName, MB_OK);
                if(xx==0) PostMessage(hwnd,WM_LBUTTONDOWN,NULL,NULL);
            }
                     }
            break;
        case WM_DESTROY:
            Shell_NotifyIcon(NIM_DELETE, &nid);
            PostQuitMessage(0);
            break;
        default:
             /*
               Explorer崩溃后,程序的系统托盘就会消失
               原理:Explorer.exe 重新载入后会重建系统任务栏。当系统任务栏建立的时候会向系统内所有
               注册接收TaskbarCreated 消息的顶级窗口发送一条消息,我们只需要捕捉这个消息,并重建系
               统托盘的图标即可.
            */
            if(message == WM_TASKBARCREATED)
                SendMessage(hwnd, WM_CREATE, wParam, lParam);
            break;
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR szCmdLine, int iCmdShow)
{
    HWND hwnd;
    MSG msg;
    WNDCLASS wndclass;
    HWND handle = FindWindow(NULL, szWndName);
    if(handle != NULL){
        MessageBox(NULL, TEXT("Application is already running"), szAppName, MB_ICONERROR);
        return 0;
    }wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = szAppName;
    if(!RegisterClass(&wndclass))
    {
        MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
        return 0;
    }
    // 此处使用WS_EX_TOOLWINDOW 属性来隐藏显示在任务栏上的窗口程序按钮
    hwnd = CreateWindowEx(WS_EX_TOOLWINDOW,
                      szAppName, szWndName,
                      WS_POPUP,
                      CW_USEDEFAULT,
                      CW_USEDEFAULT,
                      CW_USEDEFAULT,
                      CW_USEDEFAULT,
                      NULL, NULL, hInstance, NULL);
    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);
    while (GetMessage(&msg, NULL, 0, 0)){
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}

猜你喜欢

转载自blog.csdn.net/mzr122/article/details/89881880
今日推荐