win32: menu and WM_SYSCOMMAND... what i'm doing wrong with mouse click?

i added a menu item on System Menu:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
HINSTANCE hinstance=GetModuleHandle(NULL);

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    static UINT ID=0;
    static HMENU mnTopMost=NULL;
    static HMENU mnSelTopMost=NULL;

    switch(msg)
    {
        case WM_CLOSE:
        {
            DestroyWindow(hwnd);
        }
        break;
        case WM_CREATE:
        {
            mnTopMost=GetSystemMenu(hwnd,FALSE);
            ID=GetMenuItemCount(mnTopMost);
            AppendMenu(mnTopMost, MF_STRING, ID, "Top Most");
        }
        break;
        case WM_SIZE:
        case WM_SIZING:
        {
            RECT a;
            GetClientRect(hwnd,&a);
            HWND edit=FindWindowEx(hwnd,NULL,"edit",NULL);
            SetWindowPos(edit,0,0,0,a.right,a.bottom,SWP_NOMOVE | SWP_NOZORDER | SWP_NOREPOSITION);
        }
        break;

        case WM_INITMENUPOPUP:
        {
            mnSelTopMost=(HMENU)wParam;
            return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        break;
        case WM_SYSCOMMAND:
        {
            if (GetMenuState(mnSelTopMost,ID,MF_BYPOSITION)!=0xFFFFFFFF)
            {
                MessageBox(NULL,"clicked","hey",MB_OK);
            }
            mnSelTopMost=NULL;
            return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

the menu item is showed correctly. my problem is with WM_SYSCOMMAND :(
or the click works for all items or don't works :(
please someone can correct me? :(
these don't make sence to me... i'm trying without sucess :(
Last edited on
after reading these i found it:
https://www-user.tu-chemnitz.de/~heha/petzold/ch10c.htm

the wParam give me the menu ID. but i didn't find how can i get the menu handle :(
(i had tryied the lParam, but don't works... that's why i continue with WM_INITMENUPOPUP, for get the menu handle.
but i will continue waiting for an answer.
from MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/ms646360%28v=vs.85%29.aspx
even the ID, i can't find it :(
Last edited on
closed account (E0p9LyTq)
You don't need the menu handle when dealing with your custom system menu items being selected, the handle is only needed when adding custom menu items:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// some global defines for the system menu addons
#define IDM_SYS_ABOUT   1
#define IDM_SYS_HELP    2
#define IDM_SYS_REMOVE  3

// ....

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
   switch (message)
   {
   case WM_CREATE:
   {
      HMENU hMenu = GetSystemMenu(hwnd, FALSE);

      AppendMenu(hMenu, MF_SEPARATOR, 0, NULL);
      AppendMenu(hMenu, MF_STRING, IDM_SYS_ABOUT, TEXT("About..."));
      AppendMenu(hMenu, MF_STRING, IDM_SYS_HELP, TEXT("Help..."));
      AppendMenu(hMenu, MF_STRING, IDM_SYS_REMOVE, TEXT("Remove Additions"));
      break;
   }

   case WM_SYSCOMMAND:
      switch (LOWORD (wParam))
      {
      case IDM_SYS_ABOUT:
         MessageBox(hwnd, TEXT("A Poor-Person's Menu Program\n(c) Me, 2016"),
                    szAppName, MB_OK | MB_ICONINFORMATION);
         return 0;

      case IDM_SYS_HELP:
         MessageBox(hwnd, TEXT ("Help not yet implemented!"),
                    szAppName, MB_OK | MB_ICONEXCLAMATION);
         return 0;

      case IDM_SYS_REMOVE:
         GetSystemMenu(hwnd, TRUE);
         return 0;
      }
      break ;

   case WM_DESTROY:
      PostQuitMessage(0);
      return 0;
   }
   return DefWindowProc(hwnd, message, wParam, lParam);
}
Last edited on
Topic archived. No new replies allowed.