Menu

Menu
Menu

This is a sample of a window with a Menu. Download the solution(MS Visual Studio.NET)
#define   STRICT

#include  <windows.h>
#include  <stdio.h>

#define     ID_OPEN        1000
#define     ID_CLOSE       1001
#define     ID_EXIT        1002
#define     ID_CUT         1003
#define     ID_COPY        1004
#define     ID_PASTE       1005

#define     ID_1           1006
#define     ID_2           1007
#define     ID_3           1008

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
   CREATESTRUCT            *cs;
   HMENU                   hMenu, hSubMenu, hSubMenu2;
   char                    szText[64];
   // Menu-item identifiers
   static   char           szMenuItem[6][10] = {"Open", "Close", "Exit", "Cut", "Copy", "Paste"};

   switch (iMsg)
   {
   case WM_CREATE :
      cs = (LPCREATESTRUCT)lParam;
      hMenu = CreateMenu();
      SetMenu(hwnd, hMenu);

      hSubMenu = CreatePopupMenu();
      AppendMenu(hSubMenu, MF_STRING, ID_OPEN, "&Open");
      AppendMenu(hSubMenu, MF_STRING, ID_CLOSE, "&Close");
      AppendMenu(hSubMenu, MF_STRING, ID_EXIT, "E&xit");
      AppendMenu(hSubMenu, MF_SEPARATOR, 0, NULL);
      InsertMenu(hMenu, 0, MF_POPUP|MF_BYPOSITION, (UINT_PTR)hSubMenu, "File");
     
      hSubMenu2 = CreatePopupMenu();
      AppendMenu(hSubMenu2, MF_STRING, ID_1, "File 1");
      AppendMenu(hSubMenu2, MF_STRING, ID_2, "File 2");
      AppendMenu(hSubMenu2, MF_STRING, ID_3, "File 3");
      InsertMenu(hSubMenu, 5, MF_POPUP|MF_BYPOSITION, (UINT_PTR)hSubMenu2, "Recent &Files");
      DestroyMenu(hSubMenu2);
      DestroyMenu(hSubMenu);

      hSubMenu = CreatePopupMenu();
      AppendMenu(hSubMenu, MF_STRING, ID_CUT, "Cut");
      AppendMenu(hSubMenu, MF_STRING, ID_COPY, "Copy");
      AppendMenu(hSubMenu, MF_STRING, ID_PASTE, "Paste");
      InsertMenu(hMenu, 1, MF_POPUP|MF_BYPOSITION, (UINT_PTR)hSubMenu, "Edit");
      DestroyMenu(hSubMenu);
      DestroyMenu(hMenu);

      DrawMenuBar(hwnd);
      break;

   case WM_COMMAND :
      switch(LOWORD(wParam))
      {
      case ID_OPEN :
      case ID_CLOSE :
      case ID_EXIT :
      case ID_CUT :
      case ID_COPY :
      case ID_PASTE :
         sprintf(szText, "You chose %s", szMenuItem[LOWORD(wParam) - 1000]);
         MessageBox(hwnd, szText, "MenuApp", MB_OK);
         break;
      }
      break;

   case WM_CLOSE :
      DestroyWindow(hwnd);
      break;

   case WM_DESTROY :
      PostQuitMessage(0);
      break;
   }
   return DefWindowProc(hwnd, iMsg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
   HWND        hwnd;
   MSG         msg;
   WNDCLASS    wndclass;
   char        szAppName[] = "MenuApp";

   wndclass.style         = 0;
   wndclass.lpfnWndProc   = WndProc;
   wndclass.cbClsExtra    = 0;
   wndclass.cbWndExtra    = 0;
   wndclass.hInstance     = hInstance;
   wndclass.hIcon         = LoadIcon(hInstance, szAppName);
   wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
   wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
   wndclass.lpszMenuName  = szAppName;
   wndclass.lpszClassName = szAppName;
   RegisterClass(&wndclass);

   hwnd = CreateWindow(szAppName,
      szAppName,
      WS_OVERLAPPEDWINDOW | WS_VISIBLE,
      0, 0, 400, 400,
      NULL, NULL, hInstance, szCmdLine);

   while (GetMessage(&msg, NULL, 0, 0))
   {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
   }
   return (int)msg.wParam;
}

Articles
info@win32apicode.com