ファイル名:menuctrl.h

#define IDM_OPEN 101
#define IDM_SAVE 102
#define IDM_HELP 104
#define IDM_ABOUT 105

#define IDC_EDIT     200
#define IDC_BUTTON   201
#define IDC_BUTTON2  202

ファイル名:menuctrl.c

#pragma resource "menuctrl.res"
#define STRICT
#include <windows.h>
#include "menuctrl.h"

HWND hwndApp, hwndChild[4];
int yoko, tate;

#define movewin(hwnd,x,y) SetWindowPos(hwnd,NULL,x,y,0,0,SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE)
#define chgsize(hwnd,x,y) SetWindowPos(hwnd,NULL,0,0,x,y,SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE)

void resize(void){
	int takasa;
	RECT rc;
	GetWindowRect (hwndApp, &rc);
	takasa=rc.bottom-rc.top;
	GetClientRect (hwndApp, &rc);
	takasa=takasa-rc.bottom;
	chgsize(hwndApp, yoko*31, takasa+(tate*10));
}

#ifdef __cplusplus
extern "C"
#endif
long FAR PASCAL MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
	switch (message) {
	case WM_COMMAND:
		switch ((WORD)wParam){
		case IDM_HELP:
			MessageBox(hWnd,"子ウィンドウ貼り付けサンプル","ヘルプ",MB_OK);
			return 0;
		case IDM_ABOUT:
			MessageBox(hWnd,"子ウィンドウ貼り付けサンプル","バージョン情報",MB_OK);
			return 0;
		case IDC_BUTTON:
			MessageBox(hWnd,"実行ボタンが押された","BUTTON",MB_OK);
			return 0;
		case IDC_BUTTON2:
			SendMessage(hWnd, WM_CLOSE, 0, 0L);
			return 0;
		}
		break;
	case WM_SETFOCUS:
		if (hwndChild[1]) SetFocus(hwndChild[1]);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	case WM_WININICHANGE: //WM_SETTINGCHANGE:
		resize();
		return 0;
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}

BOOL InitApplication(HINSTANCE hInstance){
	WNDCLASS  wc;
	wc.style = 0;
	wc.lpfnWndProc = MainWndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	//wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wc.hIcon = LoadIcon(hInstance, "MenuCtrlIcon");
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	//wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszMenuName =  "MenuCtrlMenu";
	wc.lpszClassName = "MenuCtrlWClass";
	return RegisterClass(&wc);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow){
	HWND	hWnd;
	int takasa;

	/* ディスプレイ・デバイスコンテキストから各種情報を取得 */
	TEXTMETRIC tm;
	HDC hdc=GetDC(NULL);
	HFONT hfont = (HFONT)GetStockObject(OEM_FIXED_FONT);
	HFONT hfontOld = (HFONT)SelectObject(hdc, hfont);
	GetTextMetrics(hdc,&tm);
	tate=tm.tmAscent/2;
	yoko=tm.tmAveCharWidth;
	SelectObject(hdc, hfontOld);
	ReleaseDC(NULL, hdc);
	takasa=GetSystemMetrics(SM_CYCAPTION)+GetSystemMetrics(SM_CYMENU);

	hWnd = CreateWindow(
		"MenuCtrlWClass", "MenuCtrl", WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX,
		CW_USEDEFAULT, CW_USEDEFAULT, yoko*31, takasa+(tate*10),
		NULL, NULL, hInstance, NULL);
	if (!hWnd) return FALSE;
	hwndApp=hWnd;
	ShowWindow(hWnd, nCmdShow);
	resize();

	#define CTRL_DY  (tate*13/4)
	hwndChild[0] = CreateWindow( "static", "ファイル名(&N)",
		WS_CHILD | WS_VISIBLE,
		yoko, tate*5/4, yoko*13, tate*5/2,
		hWnd, (HMENU)-1, hInstance, NULL );
	if (!hwndChild[0]) return FALSE;

	hwndChild[1] = CreateWindow("EDIT","",
		WS_CHILD|WS_VISIBLE|WS_BORDER|ES_AUTOHSCROLL|WS_TABSTOP,
		yoko*15, tate,	yoko*14, CTRL_DY,
		hWnd, (HMENU)IDC_EDIT, hInstance, NULL);
	if (!hwndChild[1]) return FALSE;

	hwndChild[2] = CreateWindow( "BUTTON", "実行",
		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON|WS_TABSTOP,
		yoko*10, tate*5, yoko*7, CTRL_DY,
		hWnd, (HMENU)IDC_BUTTON, hInstance, NULL );
	if (!hwndChild[2]) return FALSE;

	hwndChild[3] = CreateWindow( "BUTTON", "終了",
		WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON|WS_TABSTOP,
		yoko*22, tate*5, yoko*7, CTRL_DY,
		hWnd, (HMENU)IDC_BUTTON2, hInstance, NULL );
	if (!hwndChild[3]) return FALSE;

	UpdateWindow(hWnd);

	SetFocus(hwndChild[1]);//エディットコントロールにフォーカスを当てる
	return TRUE;
}
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
	MSG msg;
	HACCEL hAccel=LoadAccelerators(hInstance, "MenuCtrlAccel");
	if (!hPrevInstance)
		if (!InitApplication(hInstance))
			return FALSE;
	if (!InitInstance(hInstance, nCmdShow))
		return FALSE;
	while (GetMessage(&msg,	NULL, 0, 0)) {
		if (!TranslateAccelerator(hwndApp, hAccel, &msg)) {
			if(!IsDialogMessage(hwndApp, &msg)){
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}
	}
	return msg.wParam;
}

ファイル名:menuctrl.rc

//#include <windows.h>
#include "menuctrl.h"
MenuCtrlIcon  ICON "MenuCtrl.ICO"
MenuCtrlMenu MENU
BEGIN
	POPUP        "ファイル(\036F\037フ)"
	BEGIN
		MENUITEM "開く(\036O\037ヒ)...", IDM_OPEN
		MENUITEM "保存(\036S\037セ)", IDM_SAVE
		MENUITEM SEPARATOR
		MENUITEM "終了(\036X\037シ)",    IDC_BUTTON2
	END
	POPUP        "編集(\036E\037エ)"
	BEGIN
		MENUITEM "実行(\036H\037ヘ)", IDC_BUTTON
	END
	POPUP        "ヘルプ(\036H\037ヘ)"
	BEGIN
		MENUITEM "ヘルプの表示(\036H\037ヘ)...	F1", IDM_HELP
		MENUITEM "バージョン情報(\036A\037シ)...", IDM_ABOUT
	END
END
MenuCtrlAccel ACCELERATORS
BEGIN
	VK_F1, IDM_HELP, VIRTKEY
END

ファイル名:menuctrl.def

NAME	     MenuCtrl
DESCRIPTION  'Microsoft Windows サンプル アプリケーション'
EXETYPE      WINDOWS
STUB	     'WINSTUB.EXE'
CODE  PRELOAD MOVEABLE DISCARDABLE
DATA  PRELOAD MOVEABLE MULTIPLE
HEAPSIZE     1024
STACKSIZE    8192
EXPORTS
	MainWndProc   @1   ; ウィンドウ処理関数の名前
戻る