ファイル名:button.c

#pragma resource "button.res"
#define STRICT
#include <windows.h>
#define IDM_ABOUT    100
#define IDC_EDIT     200
#define IDC_BUTTON   201
#define IDC_BUTTON2  202

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

#ifdef __cplusplus
extern "C"
#endif
long FAR PASCAL MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
	switch (message) {
	case WM_SYSCOMMAND:
		switch (wParam)	{
		case IDM_ABOUT:
			MessageBox(hWnd,"子ウィンドウ貼り付けサンプル","バージョン情報",MB_OK);
			return 0;
		}
		break;
	case WM_COMMAND:
		switch ((WORD)wParam){
		case IDC_BUTTON:
			MessageBox(hWnd,"実行ボタンが押された","BUTTON",MB_OK);
			return 0;
		case IDC_BUTTON2:
			SendMessage(hWnd, WM_CLOSE, 0, 0L);
			return 0;
		}
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		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, "ButtonIcon");
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	//wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
	wc.lpszMenuName =  NULL;
	wc.lpszClassName = "ButtonWClass";
	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(
		"ButtonWClass", "Button", 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);

	#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);

	/* コントロールメニューに[バージョン情報]を追加 */
	AppendMenu(GetSystemMenu(hWnd, FALSE), MF_SEPARATOR, -1, "-");
	AppendMenu(GetSystemMenu(hWnd, FALSE), MF_STRING | MF_ENABLED, IDM_ABOUT,
		"バージョン情報(\036A\037シ)...");

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

ファイル名:button.rc

//#include <windows.h>
ButtonIcon  ICON "button.ICO"

ファイル名:button.def

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