#include <windows.h>
#define NEW 101
#define OPEN 102
#define SAVE 103
#define QUIT 104
#define UNDO 201
#define REDO 202
#define CUT 203
#define COPY 204
#define PAST 205
#define ABOU 301
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int PASCAL WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASS wc;
HWND hwnd;
MSG msg;
HMENU menubar = CreateMenu();
HMENU menufile = CreateMenu();
HMENU menuedit = CreateMenu();
HMENU menuhelp = CreateMenu();
AppendMenu(menufile, MF_STRING, NEW, "New" );
AppendMenu(menufile, MF_STRING, OPEN, "Open");
AppendMenu(menufile, MF_STRING, SAVE, "Save");
AppendMenu(menufile, MF_SEPARATOR, 0, NULL );
AppendMenu(menufile, MF_STRING, QUIT, "Quit");
AppendMenu(menubar, MF_POPUP, (UINT)menufile, "File");
AppendMenu(menuedit, MF_STRING, UNDO, "Undo");
AppendMenu(menuedit, MF_STRING, REDO, "Redo");
AppendMenu(menuedit, MF_SEPARATOR, 0, NULL );
AppendMenu(menuedit, MF_STRING, CUT, "Cut" );
AppendMenu(menuedit, MF_STRING, COPY, "Copy");
AppendMenu(menuedit, MF_STRING, PAST, "Paste");
AppendMenu(menubar, MF_POPUP, (UINT)menuedit, "Edit");
AppendMenu(menuhelp, MF_STRING, ABOU, "About");
AppendMenu(menubar, MF_POPUP, (UINT)menuhelp, "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)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "MENU";
RegisterClass(&wc);
hwnd = CreateWindow("MENU",
"Menu",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, 300, 200,
NULL, menubar, 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;
switch (msg)
{
case WM_CREATE:
GetClientRect(hwnd, &rect);
label = CreateWindow("STATIC",
"",
WS_CHILD | WS_VISIBLE | SS_CENTER,
(rect.right - 50) / 2,
(rect.bottom - 15) / 2,
50, 15, hwnd, NULL, NULL, NULL);
return 0;
case WM_SIZE:
GetClientRect(hwnd, &rect);
SetWindowPos (label, HWND_TOP, (rect.right - 50) / 2,
(rect.bottom - 15) / 2, 0, 0, SWP_NOSIZE);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_COMMAND:
switch (wParam)
{
case NEW:
SendMessage(label, WM_SETTEXT, 0, (LPARAM)"New");
break;
case OPEN:
SendMessage(label, WM_SETTEXT, 0, (LPARAM)"Open");
break;
case SAVE:
SendMessage(label, WM_SETTEXT, 0, (LPARAM)"Save");
break;
case QUIT:
SendMessage(hwnd, WM_CLOSE, 0, 0);
//PostQuitMessage(0);
break;
case UNDO:
SendMessage(label, WM_SETTEXT, 0, (LPARAM)"Undo");
break;
case REDO:
SendMessage(label, WM_SETTEXT, 0, (LPARAM)"Redo");
break;
case CUT:
SendMessage(label, WM_SETTEXT, 0, (LPARAM)"Cut");
break;
case COPY:
SendMessage(label, WM_SETTEXT, 0, (LPARAM)"Copy");
break;
case PAST:
SendMessage(label, WM_SETTEXT, 0, (LPARAM)"Paste");
break;
case ABOU:
MessageBox(hwnd, "Menu Ver. 1.0", "About", MB_OK);
break;
}
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
MB_YESNO | Yes と No を表示します |
MB_OKCANCEL | OK と Cancel を表示します |
MB_YESNOCANCEL | Yes と No と Cancel を表示します |
上記の menu.c は、メニューを作る箇所を別の関数にまとめると見やすいコードになります。
#include <windows.h>
#define NEW 101
#define OPEN 102
#define SAVE 103
#define QUIT 104
#define UNDO 201
#define REDO 202
#define CUT 203
#define COPY 204
#define PAST 205
#define ABOU 301
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void AddMenu (HWND);
int PASCAL 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.hIcon = NULL;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "MENU";
RegisterClass(&wc);
hwnd = CreateWindow("MENU",
"Menu",
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;
switch (msg)
{
case WM_CREATE:
AddMenu(hwnd);
GetClientRect(hwnd, &rect);
label = CreateWindow("STATIC",
"",
WS_CHILD | WS_VISIBLE | SS_CENTER,
(rect.right - 50) / 2,
(rect.bottom - 15) / 2,
50, 15, hwnd, NULL, NULL, NULL);
return 0;
case WM_SIZE:
GetClientRect(hwnd, &rect);
SetWindowPos (label, HWND_TOP, (rect.right - 50) / 2,
(rect.bottom - 15) / 2, 0, 0, SWP_NOSIZE);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_COMMAND:
switch (wParam)
{
case NEW:
SendMessage(label, WM_SETTEXT, 0, (LPARAM)"New");
break;
case OPEN:
SendMessage(label, WM_SETTEXT, 0, (LPARAM)"Open");
break;
case SAVE:
SendMessage(label, WM_SETTEXT, 0, (LPARAM)"Save");
break;
case QUIT:
SendMessage(hwnd, WM_CLOSE, 0, 0);
//PostQuitMessage(0);
break;
case UNDO:
SendMessage(label, WM_SETTEXT, 0, (LPARAM)"Undo");
break;
case REDO:
SendMessage(label, WM_SETTEXT, 0, (LPARAM)"Redo");
break;
case CUT:
SendMessage(label, WM_SETTEXT, 0, (LPARAM)"Cut");
break;
case COPY:
SendMessage(label, WM_SETTEXT, 0, (LPARAM)"Copy");
break;
case PAST:
SendMessage(label, WM_SETTEXT, 0, (LPARAM)"Paste");
break;
case ABOU:
MessageBox(hwnd, "Menu Ver. 1.0", "About", MB_OK);
break;
}
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
void AddMenu(HWND hwnd)
{
HMENU menubar = CreateMenu();
HMENU menufile = CreateMenu();
HMENU menuedit = CreateMenu();
HMENU menuhelp = CreateMenu();
AppendMenu(menufile, MF_STRING, NEW, "New" );
AppendMenu(menufile, MF_STRING, OPEN, "Open");
AppendMenu(menufile, MF_STRING, SAVE, "Save");
AppendMenu(menufile, MF_SEPARATOR, 0, NULL );
AppendMenu(menufile, MF_STRING, QUIT, "Quit");
AppendMenu(menubar, MF_POPUP, (UINT)menufile, "File");
AppendMenu(menuedit, MF_STRING, UNDO, "Undo");
AppendMenu(menuedit, MF_STRING, REDO, "Redo");
AppendMenu(menuedit, MF_SEPARATOR, 0, NULL );
AppendMenu(menuedit, MF_STRING, CUT, "Cut" );
AppendMenu(menuedit, MF_STRING, COPY, "Copy");
AppendMenu(menuedit, MF_STRING, PAST, "Paste");
AppendMenu(menubar, MF_POPUP, (UINT)menuedit, "Edit");
AppendMenu(menuhelp, MF_STRING, ABOU, "About");
AppendMenu(menubar, MF_POPUP, (UINT)menuhelp, "Help");
SetMenu (hwnd, menubar);
}