Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7560935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:02:07+00:00 2026-05-30T13:02:07+00:00

I am creating a system tray program with a shortcut/context menu, but I can’t

  • 0

I am creating a system tray program with a shortcut/context menu, but I can’t seem to receive WM_COMMAND messages in the Windows Procedure. It simply doesn’t send when I click the menu item, and I’ve been checking for ages whether I have set up the menu correctly.

Here is my code:

#include <Windows.h>
#include <stdio.h>
#include "resource.h"
#define WM_TRAYICON (WM_USER + 0x0001) //a custom message for the notification icon

HWND hwnd; //window handle
HINSTANCE hinst; //module handle
WNDCLASSEX wnd; //window class
MSG msg; //event message or notification
NOTIFYICONDATA nid; //notification icon object
HMENU cmenu;
MENUITEMINFO menuitem1;
MENUITEMINFO menuitem2;
CURSORINFO cursor;

LRESULT CALLBACK MainWProc ( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
    switch( uMsg )
    {
    case WM_COMMAND:
        printf("asfd\r\n");
        break;
    case WM_CREATE:
        printf("just created\r\n");
        break;
    case WM_TRAYICON:
        switch( LOWORD(lParam) )
        {
        case WM_CONTEXTMENU:
            GetCursorInfo( &cursor );
            //printf("xPos: %d\r\nyPos = %d\r\n\r\n", xPos, yPos );
            TrackPopupMenuEx( cmenu, TPM_RETURNCMD | TPM_LEFTBUTTON | TPM_NOANIMATION | TPM_HORIZONTAL | TPM_VERTICAL, cursor.ptScreenPos.x, cursor.ptScreenPos.y,  hwnd, NULL );
            //DestroyMenu(
            break;
        }
        break;
    case WM_INITMENU:
        printf("open menu\r\n");
        break;
    case WM_DESTROY:
        //clean things up
        Shell_NotifyIcon( NIM_DELETE, &nid );
        break;
    default:
        break;
    }

    return DefWindowProc( hwnd, uMsg, wParam, lParam );
}

void main()
{
    int result;

    hinst = GetModuleHandle( NULL );
    cursor.cbSize = sizeof( cursor );

    memset( &wnd, 0, sizeof( wnd ) );
    wnd.cbSize = sizeof( wnd );
    wnd.lpszClassName = "MainWClass";
    wnd.lpfnWndProc = MainWProc;
    wnd.hInstance = hinst;
    result = RegisterClassEx( &wnd );

    hwnd = CreateWindowEx
        (
        0, //extended styles
        wnd.lpszClassName, //class name
        "Main Window", //window name
        WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL | WS_MINIMIZEBOX, //style tags
        CW_USEDEFAULT, //horizontal position
        CW_USEDEFAULT, //vertical position
        CW_USEDEFAULT, //width
        CW_USEDEFAULT, //height
        (HWND) NULL, //parent window
        (HMENU) NULL, //class menu
        (HINSTANCE) wnd.hInstance, //some HINSTANCE pointer
        NULL //Create Window Data?
        );
    if( !hwnd )
    {
        printf("CreateWindowEx failed: %d\n", GetLastError() );
        Sleep( INFINITE );
    }

    nid.cbSize = sizeof( nid );
    nid.hWnd = hwnd;
    nid.uID = 1;
    nid.uVersion = NOTIFYICON_VERSION_4;
    nid.uCallbackMessage = WM_TRAYICON;
    nid.hIcon = LoadIcon( hinst, MAKEINTRESOURCE( IDI_ICON1 ) );
    strcpy( nid.szTip, "My Tooltip!" );
    nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP | NIF_SHOWTIP;

    cmenu = CreatePopupMenu();
    menuitem1.cbSize = sizeof( menuitem1 );
    menuitem1.fMask = MIIM_TYPE;
    menuitem1.fType = MFT_STRING;
    menuitem1.hSubMenu = NULL;
    //menuitem1.cch = ;
    menuitem1.dwTypeData = "Open a world of wonder!";
    InsertMenuItem( cmenu, 0, true, &menuitem1 );

    if( ! Shell_NotifyIcon( NIM_ADD, &nid ) )
    {
        printf("Shell_NotifyIcon( NIM_ADD, &nid ) failed.\r\n");
        Sleep( INFINITE );
    }
    if( ! Shell_NotifyIcon( NIM_SETVERSION, &nid ) )
    {
        printf("Shell_NotifyIcon( NIM_SETVERSION, &nid ) failed.\r\n");
        Sleep( INFINITE );
    }

    UpdateWindow( hwnd );
    for( ; ; )
    {
        if( GetMessage(&msg, hwnd, 0, 0) )
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:02:08+00:00Added an answer on May 30, 2026 at 1:02 pm

    Um, you passed the TPM_RETURNCMD flag which means “don’t post a WM_COMMAND message. Just return the value you would have posted.”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a system tray app which monitors mouse clicks in Windows. I
I'm creating a WPF app and have a system tray icon with a context
I'm displaying messages on the system tray via trayIcon.displayMessage(...) but they don't look nice.
I'm creating a system where users can refer other users and in turn receive
I'm interested in creating a system where the user can define the steps in
I'm creating Content management system in asp.net. But I need open source cascading stylesheet
I am creating a menu system using a UL/LI structure. I'm trying to use
I am creating a system where users can setup mailings to go out at
I'm writing a Windows application that displays its tray icon on the Windows system
I am creating a system where a user can select any combination of four

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.