Trayicon verschwindet sobald mit maus darüber



  • Was passiert siehe Threadtitel. Liegt das eventuell daran dass ich für hwnd 0 angebe? Ich habe kein Fenster in meiner Anwendung, soll nur über das Icon gesteuert werden.

    Hab den Code aus den FAQ.

    nidTrayIcon.cbSize = sizeof(nidTrayIcon);
        nidTrayIcon.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_TRAYICON));;
        nidTrayIcon.hWnd = 0;
        nidTrayIcon.uCallbackMessage = WM_USER;
        nidTrayIcon.uFlags = NIF_ICON; // zum Test nur Icon!
        nidTrayIcon.uID = 0x0200;
    
        if(!Shell_NotifyIcon(NIM_ADD, &nidTrayIcon))
            MessageBox(0, 
                L"BOOOM! ",
                L"Error", MB_ICONEXCLAMATION | MB_OK);
    

    Wie gesagt zuerst ist das Icon da, nur sobald man mit dem Mauszeiger darüber fährt geht nix mehr.

    Wie geht das ohne Window?


  • Mod

    Du musst ein Fenster angeben!



  • MSDN schrieb:

    Adding and Deleting Taskbar Icons in the Status Area
    You add an icon to the taskbar status area by filling in a NOTIFYICONDATA structure and
    then passing the structure to Shell_NotifyIcon with dwMessage set to NIM_ADD. The
    structure members must specify the handle to the window that is adding the icon, as
    well as the icon identifier and icon handle. You can also specify ToolTip text for the
    icon. If you need to receive mouse messages for the icon, specify the identifier of the
    callback message that the system should use to send the message to the window procedure.

    The function in the following example demonstrates how to add an icon to the taskbar.

    // MyTaskBarAddIcon - adds an icon to the taskbar status area. 
    // Returns TRUE if successful, or FALSE otherwise. 
    // hwnd - handle to the window to receive callback messages. 
    // uID - identifier of the icon. 
    // hicon - handle to the icon to add. 
    // lpszTip - ToolTip text. 
    BOOL MyTaskBarAddIcon(HWND hwnd, UINT uID, HICON hicon, LPSTR lpszTip) 
    { 
        BOOL res; 
        NOTIFYICONDATA tnid; 
     
        tnid.cbSize = sizeof(NOTIFYICONDATA); 
        tnid.hWnd = hwnd; 
        tnid.uID = uID; 
        tnid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; 
        tnid.uCallbackMessage = MYWM_NOTIFYICON; 
        tnid.hIcon = hicon; 
        if (lpszTip) 
            lstrcpyn(tnid.szTip, lpszTip, sizeof(tnid.szTip)); 
        else 
            tnid.szTip[0] = '\0'; 
     
        res = Shell_NotifyIcon(NIM_ADD, &tnid); 
     
        if (hicon) 
            DestroyIcon(hicon); 
     
        return res; 
    }
    

    To delete an icon from the taskbar status area, fill a NOTIFYICONDATA structure and call Shell_NotifyIcon with dwMessage set to NIM_DELETE. When deleting a taskbar icon, specify only the cbSize, hWnd, and uID members of the structure. For example

    // MyTaskBarDeleteIcon - deletes an icon from the taskbar status area. 
    // Returns TRUE if successful, or FALSE otherwise. 
    // hwnd - handle to the window that added the icon. 
    // uID - identifier of the icon to delete. 
    BOOL MyTaskBarDeleteIcon(HWND hwnd, UINT uID) 
    { 
        BOOL res; 
        NOTIFYICONDATA tnid; 
     
        tnid.cbSize = sizeof(NOTIFYICONDATA); 
        tnid.hWnd = hwnd; 
        tnid.uID = uID; 
             
        res = Shell_NotifyIcon(NIM_DELETE, &tnid); 
        return res; 
    }
    

    Receiving Mouse Events
    If you specify a callback message for a taskbar icon, the system sends the message to your application whenever a mouse event occurs in the icon's bounding rectangle. The wParam parameter of the message specifies the identifier of the taskbar icon, and the lParam parameter of the message specifies the message that the system generated as a result of the mouse event.

    The function in the following example is from an application that adds both battery and printer icons to the taskbar. The application calls the function when it receives a callback message. The function determines whether the user has clicked one of the icons and, if a click has occurred, calls an application-defined function to display status information.

    // On_MYWM_NOTIFYICON - processes callback messages for taskbar icons. 
    // wParam - first message parameter of the callback message. 
    // lParam - second message parameter of the callback message. 
    void On_MYWM_NOTIFYICON(WPARAM wParam, LPARAM lParam) 
    { 
        UINT uID; 
        UINT uMouseMsg; 
     
        uID = (UINT) wParam; 
        uMouseMsg = (UINT) lParam; 
     
        if (uMouseMsg == WM_LBUTTONDOWN) { 
            switch (uID) { 
                case IDI_MYBATTERYICON: 
     
                    // The user clicked the battery icon. Display the 
                    // battery status. 
                    ShowBatteryStatus(); 
                    break; 
     
                case IDI_MYPRINTERICON: 
     
                    // The user clicked the printer icon. Display the 
                    // status of the print job. 
                    ShowJobStatus(); 
                    break; 
     
                default: 
                    break; 
            } 
         } 
         return; 
     }
    

    HIH
    Greetz, Swordfish



  • OT: Hätte es nicht auch einen Link getan!? Es macht nicht gerade viel Sinn die MSDN seitenweise zu zitieren...



  • Lieber Jochen!
    Ich benutze die Online-MSDN sehr selten und es hätte wahrscheinlich Tage
    gedauert bis ich diesen Artikel gefunden hätte. Lokal wusst ich sofort, wo
    er ist. Ich verstehe natürlich deine Einwand, bin aber beim Posten davon
    ausgegangen, daß dieß (außer einem netten "Danke" 😉 der letzte Post dieses
    Threads sein würde (womit dann auch die Übersichtlichkeit allzusehr gelitten
    hätte. Ich werde mich trotzdem in Zukunft etwas mit längeren Zitaten zurück-
    halten und lieber Links spenden.

    beste Grüße
    Erich Reiter



  • Swordfish schrieb:

    Ich benutze die Online-MSDN sehr selten und es hätte wahrscheinlich Tage gedauert bis ich diesen Artikel gefunden hätte.

    Einfach den ersten Satz in Google eingegegen führt sofort zu:
    http://msdn.microsoft.com/library/en-us/shellcc/platform/shell/programmersguide/shell_int/shell_int_programming/taskbar.asp

    Dann hätte man halt auch die restlichen Infos aus diesem Artikel gehabt... aber ist nicht so tragisch...


Anmelden zum Antworten