Windows API Primer   メニュー (InsertMenuItem)

ホーム   目次

メニュー

メニューは関数で作る方法とメニュー用のリソースファイルを用意する方法があります。ここでは関数で作る方法を説明します。

メニューは CreateMenu( )関数で作ります。そのメニューにメニュー項目を追加するには、AppendMenu( )関数と InsertMenuItem( )関数の2通りがあります。

InsertMenuItem( )関数の方が後で作られた関数です。

ここでは、両方を説明します。

insertmenu.c


#include <windows.h>

#define FILE 10
#define NEW  11
#define OPEN 12
#define SAVE 13
#define QUIT 14
#define EDIT 20
#define UNDO 21
#define REDO 22
#define CUT  23
#define COPY 24
#define PAST 25
#define HELP 30
#define ABOU 31

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int     WINAPI   WinMain(HINSTANCE hInstance,
			 HINSTANCE hPrevInstance,
			 LPSTR     lpCmdLine,
			 int       nCmdShow)
{
  HMENU        hMenubar;
  HMENU        hMenuPopupFile;
  HMENU        hMenuPopupEdit;
  HMENU        hMenuPopupHelp;

  WNDCLASS     wc;
  HWND         hwnd;
  MSG          msg;
  
  hMenubar       = CreateMenu();
  hMenuPopupFile = CreatePopupMenu();
  hMenuPopupEdit = CreatePopupMenu();
  hMenuPopupHelp = CreatePopupMenu();

  MENUITEMINFO mii;
  mii.cbSize     = sizeof(MENUITEMINFO);
  mii.fMask      = MIIM_ID | MIIM_TYPE;
  mii.fType      = MFT_STRING;

  mii.wID        = NEW;
  mii.dwTypeData = TEXT("&New");
  InsertMenuItem(hMenuPopupFile, NEW , FALSE, &mii);
  mii.wID        = OPEN;
  mii.dwTypeData = TEXT("&Open");
  InsertMenuItem(hMenuPopupFile, OPEN, FALSE, &mii);
  mii.wID        = SAVE;
  mii.dwTypeData = TEXT("&Save");
  InsertMenuItem(hMenuPopupFile, SAVE, FALSE, &mii);
  mii.wID        = 0;
  mii.fType      = MFT_SEPARATOR;
  InsertMenuItem(hMenuPopupFile, 0   , FALSE, &mii);
  mii.wID        = QUIT;
  mii.fType      = MFT_STRING;
  mii.dwTypeData = TEXT("&Quit");
  InsertMenuItem(hMenuPopupFile, QUIT, FALSE, &mii);
  mii.wID        = FILE;
  mii.fMask     |= MIIM_SUBMENU;
  mii.hSubMenu   = hMenuPopupFile;
  mii.dwTypeData = TEXT("&File");
  InsertMenuItem(hMenubar,       FILE, FALSE, &mii);

  mii.wID        = UNDO;
  mii.fType      = MFT_STRING;
  mii.fMask      = MIIM_ID | MIIM_TYPE;
  mii.dwTypeData = TEXT("&Undo");
  InsertMenuItem(hMenuPopupEdit, UNDO, FALSE, &mii);
  mii.wID        = REDO;
  mii.dwTypeData = TEXT("&Redo");
  InsertMenuItem(hMenuPopupEdit, REDO, FALSE, &mii);
  mii.wID        = 0;
  mii.fType      = MFT_SEPARATOR;
  InsertMenuItem(hMenuPopupEdit, 0   , FALSE, &mii);
  mii.wID        = CUT;
  mii.fType      = MFT_STRING;
  mii.dwTypeData = TEXT("Cut");
  InsertMenuItem(hMenuPopupEdit, CUT , FALSE, &mii);
  mii.wID        = COPY;
  mii.dwTypeData = TEXT("Copy");
  InsertMenuItem(hMenuPopupEdit, COPY, FALSE, &mii);
  mii.wID        = PAST;
  mii.dwTypeData = TEXT("Paste");
  InsertMenuItem(hMenuPopupEdit, PAST, FALSE, &mii);
  mii.wID        = EDIT;
  mii.fMask     |= MIIM_SUBMENU;
  mii.hSubMenu   = hMenuPopupEdit;
  mii.dwTypeData = TEXT("Edit");
  InsertMenuItem(hMenubar,       EDIT, FALSE, &mii);

  mii.wID        = ABOU;
  mii.fType      = MFT_STRING;
  mii.fMask      = MIIM_ID | MIIM_TYPE;
  mii.dwTypeData = TEXT("About");
  InsertMenuItem(hMenuPopupHelp, ABOU, FALSE, &mii);
  mii.wID        = HELP;
  mii.fMask     |= MIIM_SUBMENU;
  mii.hSubMenu   = hMenuPopupHelp;
  mii.dwTypeData = TEXT("Help");
  InsertMenuItem(hMenubar,       HELP, FALSE, &mii);

  wc.style         = CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc   = WndProc;
  wc.cbClsExtra    = 0;
  wc.cbWndExtra    = 0;
  wc.hInstance     = hInstance;
  wc.hIcon         = NULL;
  wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  wc.hbrBackground = (HBRUSH)COLOR_BACKGROUND + 1;
  wc.lpszMenuName  = NULL;
  wc.lpszClassName = TEXT("MENU");

  RegisterClass(&wc);

  hwnd             = CreateWindow(TEXT("MENU"),
				  TEXT("Windows API Primer"),
				  WS_OVERLAPPEDWINDOW | WS_VISIBLE,
				  CW_USEDEFAULT, CW_USEDEFAULT, 300, 200,
				  NULL, hMenubar, hInstance, NULL);

  while (GetMessage(&msg, NULL, 0, 0))
    {
      DispatchMessage(&msg);
    }

  return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  static HWND  label;
         RECT  rect;
	 HFONT font;
	 
  switch (msg)
    {
    case WM_CREATE:
      GetClientRect(hwnd, &rect);
      font  = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
      label = CreateWindow(TEXT("STATIC"),
			   TEXT("♪( ´▽`)"),
			   WS_CHILD | WS_VISIBLE |SS_CENTER,
			   (rect.right  - 100) / 2,
			   (rect.bottom -  15) / 2,
			   100, 15, hwnd, NULL, NULL, NULL);
      SendMessage(label, WM_SETFONT, (WPARAM)font, 0);
      break;
    case WM_SIZE:
      GetClientRect(hwnd, &rect);
      SetWindowPos (label, HWND_TOP,
		    (rect.right  - 100) / 2,
		    (rect.bottom -  15) / 2,
		    0, 0, SWP_NOSIZE);
      break;
    case WM_COMMAND:
      switch (wParam)
	{
	case NEW:
	  SendMessage(label, WM_SETTEXT, 0, (LPARAM)TEXT("New"));
	  break;
	case OPEN:
	  SendMessage(label, WM_SETTEXT, 0, (LPARAM)TEXT("Open"));
	  break;
	case SAVE:
	  SendMessage(label, WM_SETTEXT, 0, (LPARAM)TEXT("Save"));
	  break;
	case QUIT:
	  SendMessage(hwnd, WM_CLOSE, 0, 0);
	  break;
	case UNDO:
	  SendMessage(label, WM_SETTEXT, 0, (LPARAM)TEXT("Undo"));
	  break;
	case REDO:
	  SendMessage(label, WM_SETTEXT, 0, (LPARAM)TEXT("Redo"));
	  break;
	case CUT:
	  SendMessage(label, WM_SETTEXT, 0, (LPARAM)TEXT("Cut"));
	  break;
	case COPY:
	  SendMessage(label, WM_SETTEXT, 0, (LPARAM)TEXT("Copy"));
	  break;
	case PAST:
	  SendMessage(label, WM_SETTEXT, 0, (LPARAM)TEXT("Paste"));
	  break;
	case ABOU:
	  SendMessage(label, WM_SETTEXT, 0, (LPARAM)TEXT("About"));
	  MessageBox(hwnd,TEXT("InsertMenu Ver. 1.0"),TEXT("About"),MB_OK);
	  break;
	}
      break;
    case WM_DESTROY:
      PostQuitMessage(0);
      break;
    }
  return DefWindowProc(hwnd, msg, wParam, lParam);
}
    

実行結果


initmenu.c


#include <windows.h>

#define FILE 10
#define NEW  11
#define OPEN 12
#define SAVE 13
#define QUIT 14
#define EDIT 20
#define UNDO 21
#define REDO 22
#define CUT  23
#define COPY 24
#define PAST 25
#define HELP 30
#define ABOU 31

void        InitMenuItem(HMENU, LPSTR, int, HMENU);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int     WINAPI   WinMain(HINSTANCE hInstance,
			 HINSTANCE hPrevInstance,
			 LPSTR     lpCmdLine,
			 int       nCmdShow)
{
  HMENU    hmenu;
  HMENU    hmenuPopupFile;
  HMENU    hmenuPopupEdit;
  HMENU    hmenuPopupHelp;
  WNDCLASS wc;
  HWND     hwnd;
  MSG      msg;

  hmenu          = CreateMenu();
  hmenuPopupFile = CreatePopupMenu();
  hmenuPopupEdit = CreatePopupMenu();
  hmenuPopupHelp = CreatePopupMenu();

  InitMenuItem(hmenuPopupFile, TEXT("&New" ), NEW , NULL);
  InitMenuItem(hmenuPopupFile, TEXT("&Open"), OPEN, NULL);
  InitMenuItem(hmenuPopupFile, TEXT("&SAVE"), SAVE, NULL);
  InitMenuItem(hmenuPopupFile, NULL         , 0   , NULL);
  InitMenuItem(hmenuPopupFile, TEXT("&Quit"), QUIT, NULL);

  InitMenuItem(hmenuPopupEdit, TEXT("&Undo"), UNDO, NULL);
  InitMenuItem(hmenuPopupEdit, TEXT("&Redo"), REDO, NULL);
  InitMenuItem(hmenuPopupEdit, NULL         , 0   , NULL);
  InitMenuItem(hmenuPopupEdit, TEXT("Cut"  ), CUT , NULL);
  InitMenuItem(hmenuPopupEdit, TEXT("&Copy"), COPY, NULL);
  InitMenuItem(hmenuPopupEdit, TEXT("&Paste"),PAST, NULL);

  InitMenuItem(hmenuPopupHelp, TEXT("&About"),ABOU, NULL);
  
  InitMenuItem(hmenu,          TEXT("&File"), FILE, hmenuPopupFile);
  InitMenuItem(hmenu,          TEXT("&Edit"), EDIT, hmenuPopupEdit);
  InitMenuItem(hmenu,          TEXT("&Help"), HELP, hmenuPopupHelp);

  wc.style         = CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc   = WndProc;
  wc.cbClsExtra    = 0;
  wc.cbWndExtra    = 0;
  wc.hInstance     = hInstance;
  wc.hIcon         = NULL;
  wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  wc.hbrBackground = (HBRUSH)COLOR_BACKGROUND + 1;
  wc.lpszMenuName  = NULL;
  wc.lpszClassName = TEXT("MENU");

  RegisterClass (&wc);

  hwnd            = CreateWindow(TEXT("MENU"),
				 TEXT("Windows API Primer"),
				 WS_OVERLAPPEDWINDOW | WS_VISIBLE,
				 CW_USEDEFAULT, CW_USEDEFAULT, 300, 200,
				 NULL, hmenu, hInstance, NULL);

  while (GetMessage(&msg, NULL, 0, 0))
    {
      DispatchMessage(&msg);
    }

  return msg.wParam;
}
void InitMenuItem(HMENU hmenu, LPSTR lpszItemName, int nId, HMENU hmenuSub)
{
  MENUITEMINFO mii;

  mii.cbSize     = sizeof(MENUITEMINFO);
  mii.fMask      = MIIM_ID | MIIM_TYPE;
  mii.wID        = nId;
  if (lpszItemName != NULL)
    {
      mii.fType      = MFT_STRING;
      mii.dwTypeData = lpszItemName;
    }
  else
    {
      mii.fType      = MFT_SEPARATOR;
    }
  if (hmenuSub != NULL)
    {
      mii.fMask   |= MIIM_SUBMENU;
      mii.hSubMenu = hmenuSub;
    }

  InsertMenuItem(hmenu, nId, FALSE, &mii);
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  static HWND  label;
         RECT  rect;
	 HFONT font;
	 
  switch (msg)
    {
    case WM_CREATE:
      GetClientRect(hwnd, &rect);
      font  = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
      label = CreateWindow(TEXT("STATIC"),
			   TEXT("♪( ´▽`)"),
			   WS_CHILD | WS_VISIBLE | SS_CENTER,
			   (rect.right  - 100) / 2,
			   (rect.bottom -  15) / 2,
			   100, 15, hwnd, NULL, NULL, NULL);
      SendMessage(label, WM_SETFONT, (WPARAM)font, 0);
      break;
    case WM_SIZE:
      GetClientRect(hwnd, &rect);
      SetWindowPos (label, HWND_TOP,
		    (rect.right  - 100) / 2,
		    (rect.bottom -  15) / 2,
		    0, 0, SWP_NOSIZE);
      break;
    case WM_COMMAND:
      switch (wParam)
	{
	case NEW:
	  SendMessage(label, WM_SETTEXT, 0, (LPARAM)TEXT("New"));
	  break;
	case OPEN:
	  SendMessage(label, WM_SETTEXT, 0, (LPARAM)TEXT("Open"));
	  break;
	case SAVE:
	  SendMessage(label, WM_SETTEXT, 0, (LPARAM)TEXT("Save"));
	  break;
	case QUIT:
	  SendMessage(hwnd,  WM_CLOSE,   0, 0);
	  break;
	case UNDO:
	  SendMessage(label, WM_SETTEXT, 0, (LPARAM)TEXT("Undo"));
	  break;
	case REDO:
	  SendMessage(label, WM_SETTEXT, 0, (LPARAM)TEXT("Redo"));
	  break;
	case CUT:
	  SendMessage(label, WM_SETTEXT, 0, (LPARAM)TEXT("Cut"));
	  break;
	case COPY:
	  SendMessage(label, WM_SETTEXT, 0, (LPARAM)TEXT("Copy"));
	  break;
	case PAST:
	  SendMessage(label, WM_SETTEXT, 0, (LPARAM)TEXT("Paste"));
	  break;
	case ABOU:
	  SendMessage(label, WM_SETTEXT, 0, (LPARAM)TEXT("About"));
	  MessageBox(hwnd, TEXT("InitMenu Ver. 1.0"),TEXT("About"),MB_OK);
	  break;
	}
      break;
    case WM_DESTROY:
      PostQuitMessage(0);
      break;
    }
  return DefWindowProc(hwnd, msg, wParam, lParam);
}
    

実行結果



49429 visits
Posted: Jun. 02, 2020
Update: Jun. 05, 2020

ホーム   目次