Windows API Primer   メニュー (AppendMenu)

ホーム   目次

メニュー (AppendMenu)

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

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

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

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

appendmenu.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    hMenuFile;
  HMENU    hMenuEdit;
  HMENU    hMenuHelp;
  
  WNDCLASS wc;
  HWND     hwnd;
  MSG      msg;

  hMenubar  = CreateMenu();
  hMenuFile = CreateMenu();
  hMenuEdit = CreateMenu();
  hMenuHelp = CreateMenu();

  AppendMenu(hMenuFile, MF_STRING, NEW , TEXT("&New" ));
  AppendMenu(hMenuFile, MF_STRING, OPEN, TEXT("&Open"));
  AppendMenu(hMenuFile, MF_STRING, SAVE, TEXT("&Save"));
  AppendMenu(hMenuFile, MF_SEPARATOR, 0, NULL);
  AppendMenu(hMenuFile, MF_STRING, QUIT, TEXT("&Quit"));
  AppendMenu(hMenubar , MF_POPUP ,(UINT_PTR)hMenuFile, TEXT("&File"));

  AppendMenu(hMenuEdit, MF_STRING, UNDO, TEXT("&Undo"));
  AppendMenu(hMenuEdit, MF_STRING, REDO, TEXT("&Redo"));
  AppendMenu(hMenuEdit, MF_SEPARATOR, 0, NULL);
  AppendMenu(hMenuEdit, MF_STRING, CUT , TEXT("Cut"  ));
  AppendMenu(hMenuEdit, MF_STRING, COPY, TEXT("&Copy"));
  AppendMenu(hMenuEdit, MF_STRING, PAST, TEXT("&Paste"));
  AppendMenu(hMenubar , MF_POPUP , (UINT_PTR)hMenuEdit, TEXT("&Edit"));

  AppendMenu(hMenuHelp, MF_STRING, ABOU, TEXT("&About"));
  AppendMenu(hMenubar , MF_POPUP , (UINT_PTR)hMenuHelp, TEXT("&Help"));
  
  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("Window 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("AppendMenu Ver. 1.0"),TEXT("About"),MB_OK);
	  break;
	}
      break;
    case WM_DESTROY:
      PostQuitMessage(0);
      break;
    }
  return DefWindowProc(hwnd, msg, wParam, lParam);
}
    

実行結果


addmenu.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);
void             AddMenu(HWND);

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

  wc.style         = CS_HREDRAW | CS_VREDRAW;
  wc.lpfnWndProc   = WndProc;
  wc.cbClsExtra    = 0;
  wc.cbWndExtra    = 0;
  wc.hInstance     = hInstance;
  wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  wc.hIcon         = NULL;
  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, NULL, 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:
      AddMenu(hwnd);
      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(label, 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("AddMenu Ver. 1.0"), TEXT("About"), MB_OK);
	  break;
	}
      break;
    case WM_DESTROY:
      PostQuitMessage(0);
      break;
    }
  return DefWindowProc(hwnd, msg, wParam, lParam);
}

void AddMenu(HWND hwnd)
{
  HMENU hMenubar;
  HMENU hMenuFile;
  HMENU hMenuEdit;
  HMENU hMenuHelp;

  hMenubar  = CreateMenu();
  hMenuFile = CreateMenu();
  hMenuEdit = CreateMenu();
  hMenuHelp = CreateMenu();

  AppendMenu(hMenuFile, MF_STRING, NEW , TEXT("&New" ));
  AppendMenu(hMenuFile, MF_STRING, OPEN, TEXT("&Open"));
  AppendMenu(hMenuFile, MF_STRING, SAVE, TEXT("&Save"));
  AppendMenu(hMenuFile, MF_SEPARATOR, 0, NULL         );
  AppendMenu(hMenuFile, MF_STRING, QUIT, TEXT("&Quit"));
  AppendMenu(hMenubar , MF_POPUP , (UINT_PTR)hMenuFile, TEXT("&File"));

  AppendMenu(hMenuEdit, MF_STRING, UNDO, TEXT("&Undo"));
  AppendMenu(hMenuEdit, MF_STRING, REDO, TEXT("&Redo"));
  AppendMenu(hMenuEdit, MF_SEPARATOR, 0, NULL         );
  AppendMenu(hMenuEdit, MF_STRING, CUT , TEXT("Cut"  ));
  AppendMenu(hMenuEdit, MF_STRING, COPY, TEXT("&Copy"));
  AppendMenu(hMenuEdit, MF_STRING, PAST, TEXT("&Paste"));
  AppendMenu(hMenubar , MF_POPUP , (UINT_PTR)hMenuEdit, TEXT("&Edit"));

  AppendMenu(hMenuHelp, MF_STRING, ABOU, TEXT("&About"));
  AppendMenu(hMenubar , MF_POPUP , (UINT_PTR)hMenuHelp, TEXT("&Help"));
  
  SetMenu(hwnd, hMenubar);
}
    


実行結果



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

ホーム   目次