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

  • Home
  • SEARCH
  • 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 7609287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:08:14+00:00 2026-05-31T01:08:14+00:00

I’m trying to setup a context menu using WinAPI in my application, but for

  • 0

I’m trying to setup a context menu using WinAPI in my application, but for some reason I’m not successful. The code that would pop up the menu is placed in the WM_RBUTTONUP case. However, nothing happens after right-click. Could someone tell me what am I doing wrong?

.cpp:

#include "stdafx.h"
#include "Growing_children_PAWEL_MICHNA.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst;                                // current instance
TCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
//TCHAR szChild1Title[MAX_LOADSTRING];
//TCHAR szChild2Title[MAX_LOADSTRING];
TCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name
TCHAR szChild1WindowClass[MAX_LOADSTRING];
TCHAR szChild2WindowClass[MAX_LOADSTRING];

HWND hWnd, child1hWnd, child2hWnd;
HMENU hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_CONTEXT_MENU));

int mywidth = 25;
int myheight = 25;
bool greater = true;
// Forward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
ATOM                Child1RegisterClass(HINSTANCE hInstance);
ATOM                Child2RegisterClass(HINSTANCE hInstance);

BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    MSG msg;
    HACCEL hAccelTable;

    // Initialize global strings
    LoadString(hInstance, IDS_APP_TITLE, NULL, MAX_LOADSTRING);
    LoadString(hInstance, IDC_GROWING_CHILDREN_PAWEL_MICHNA, szWindowClass, MAX_LOADSTRING);
    LoadString(hInstance, IDC_CHILD1, szChild1WindowClass, MAX_LOADSTRING);
    LoadString(hInstance, IDC_CHILD2, szChild2WindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);
    Child1RegisterClass(hInstance);
    Child2RegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_GROWING_CHILDREN_PAWEL_MICHNA));

    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int) msg.wParam;
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    HBRUSH redBrush = CreateSolidBrush(RGB(255, 0, 0));
    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = NULL; //CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_GROWING_CHILDREN_PAWEL_MICHNA));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = redBrush;
    wcex.lpszMenuName   = MAKEINTRESOURCE(NULL);
    wcex.lpszClassName  = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassEx(&wcex);
}

ATOM Child1RegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    HBRUSH blueBrush = CreateSolidBrush(RGB(0, 0, 255));
    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_PARENTDC; //CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_GROWING_CHILDREN_PAWEL_MICHNA));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = blueBrush;
    wcex.lpszMenuName   = MAKEINTRESOURCE(NULL);
    wcex.lpszClassName  = szChild1WindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassEx(&wcex);
}

ATOM Child2RegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    HBRUSH greenBrush = CreateSolidBrush(RGB(0, 255, 0));
    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style          = CS_PARENTDC; //CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_GROWING_CHILDREN_PAWEL_MICHNA));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = greenBrush;
    wcex.lpszMenuName   = MAKEINTRESOURCE(NULL);
    wcex.lpszClassName  = szChild2WindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassEx(&wcex);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindowEx(WS_EX_TOPMOST | WS_EX_LAYERED, szWindowClass, NULL, WS_POPUPWINDOW, CW_USEDEFAULT, 0, 500, 500, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   // Set windows transparency to 70%
   SetLayeredWindowAttributes(hWnd, 0, (255 * 70) / 100, LWA_ALPHA);

   RECT rc; 

   // get the center of the work area of the system 
   SystemParametersInfo(SPI_GETWORKAREA, 0, &rc, 0); 
   int centerX = (rc.left + rc.right + 1) / 2; 
   int centerY = (rc.top + rc.bottom + 1) / 2; 

   //move the main windows to the center
   MoveWindow(hWnd, centerX-250, centerY-250, 500, 500, true);

   child1hWnd = CreateWindow(szChild1WindowClass, NULL, WS_CHILD, 0, 0, 25, 25, hWnd, NULL, hInstance, NULL);
   SetLayeredWindowAttributes(child1hWnd, 0, (255 * 70) / 100, LWA_ALPHA);

   child2hWnd = CreateWindow(szChild2WindowClass, NULL, WS_CHILD, 475, 476, 25, 25, hWnd, NULL, hInstance, NULL);
   SetLayeredWindowAttributes(child2hWnd, 0, (255 * 70) / 100, LWA_ALPHA);

   ShowWindow(hWnd, nCmdShow);
   ShowWindow(child1hWnd, nCmdShow);
   ShowWindow(child2hWnd, nCmdShow);

   UpdateWindow(hWnd);
   UpdateWindow(child1hWnd);
   UpdateWindow(child2hWnd);

   return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;
    POINT point;

    switch (message)
    {
    case WM_COMMAND:
        wmId    = LOWORD(wParam);
        wmEvent = HIWORD(wParam);
        // Parse the menu selections:
        switch (wmId)
        {
        case IDM_ABOUT:
            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
            break;
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        break;
    case WM_CREATE:
    {
        SetTimer(hWnd, 1, 100, NULL);
        return 0; 
    }
        break;
    case WM_RBUTTONUP: 
    {
        point.x = LOWORD(lParam); 
        point.y = HIWORD(lParam); 

        ClientToScreen(hWnd, &point); 
        TrackPopupMenu(hMenu, TPM_RIGHTBUTTON, point.x, point.y, 0, hWnd, NULL); 
        return 0;
    }
    case WM_TIMER:
        if(mywidth < 250 && greater == true)
        {
            MoveWindow(child1hWnd, 0, 0, ++mywidth, ++myheight, true);
            MoveWindow(child2hWnd, 500-mywidth, 500-myheight, mywidth, myheight, true);
            if(mywidth == 250)
                greater = false;
        }
        else
        {
            MoveWindow(child1hWnd, 0, 0, --mywidth, --myheight, true);
            MoveWindow(child2hWnd, 500-mywidth, 500-myheight, mywidth, myheight, true);
            if(mywidth == 0)
                greater = true;
        }
        break;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    case WM_NCHITTEST:
        if (hWnd == child1hWnd || hWnd == child2hWnd) return HTTRANSPARENT;
        return HTCAPTION; 
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
    UNREFERENCED_PARAMETER(lParam);
    switch (message)
    {
    case WM_INITDIALOG:
        return (INT_PTR)TRUE;

    case WM_COMMAND:
        if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
        {
            EndDialog(hDlg, LOWORD(wParam));
            return (INT_PTR)TRUE;
        }
        break;
    }
    return (INT_PTR)FALSE;
}

.rc fragment:

IDR_CONTEXT_MENU MENU
BEGIN
    MENUITEM "Color",                       ID_COLOR32778
    POPUP "Figures"
    BEGIN
        MENUITEM "None",                        ID_FIGURES_NONE, CHECKED
        MENUITEM "Square",                      ID_FIGURES_SQUARE
        MENUITEM "Triangle",                    ID_FIGURES_TRIANGLE
        MENUITEM "Circle",                      ID_FIGURES_CIRCLE
    END
    MENUITEM "Exit",                        ID_EXIT
END
  • 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-31T01:08:15+00:00Added an answer on May 31, 2026 at 1:08 am

    Your hMenu is a static variable, which is loaded before hInst has a value. I believe the LoadMenu call is failing.

    You’ll also note in the TrackPopupMenu documentation that not just any menu handle can be used, it has to be a popup menu handle. In the resources, create a single element for the top-level menu and put your popup menu underneath it; then use LoadMenu to load the top-level, and GetSubMenu to get the handle to your drop-down.

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

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.