開発環境 Microsoft Visual C++ 2010 Express (SP1)
実行環境 Microsoft Windows XP Home Edition (SP3)
プロジェクトの種類 Win32 プロジェクト
プロジェクト名 DlgFrame
アプリケーションの種類 Windows アプリケーション
追加のオプション 空のプロジェクト
文字セット マルチ バイト

参考
リソースを使わないモーダルダイアログ http://www.sm.rim.or.jp/~shishido/mdialog.html

DlgFrame.c
// マルチバイト文字セット
 
#include <stdio.h>
#include <string.h>
#include <Windows.h>
 
#define IDC_STATIC	-1
#define IDC_EDIT	100
 
static HINSTANCE	g_hInstance;
 
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void OnCreate(HWND hWnd);
 
int WINAPI WinMain(
	HINSTANCE	hInstance,
	HINSTANCE	hPrevInstance,
	LPSTR		lpCmdLine,
	int		nCmdShow)
{
	const char	acClassName[] = "DlgFrame";
	WNDCLASSEX	wcex;
	HWND		hWnd;
	MSG		msg;
 
	g_hInstance = hInstance;
 
	// ウィンドウクラスの登録
	wcex.cbSize		= sizeof wcex;
	wcex.style		= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon		= LoadIcon(NULL, IDI_APPLICATION);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_BTNFACE + 1);
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= acClassName;
	wcex.hIconSm		= LoadIcon(NULL, IDI_APPLICATION);
	if (RegisterClassEx(&wcex) == 0) {
		return 0;
	}
 
	// ウィンドウの作成
	hWnd = CreateWindow(
		acClassName,		// ClassName
		"Dialog Frame",		// WindowName
		WS_OVERLAPPEDWINDOW,	// Style
		CW_USEDEFAULT,		// x
		0,			// y
		CW_USEDEFAULT,		// Width
		0,			// Height
		NULL,			// WndParent
		NULL,			// Menu
		hInstance,
		NULL);
	if (hWnd == NULL) {
		return 0;
	}
 
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);
 
	while (GetMessage(&msg, NULL, 0, 0)) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return (int)msg.wParam;
}
 
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	HWND	hWndEdit;
 
	switch (uMsg) {
	case WM_COMMAND:
		hWndEdit = GetDlgItem(hWnd, IDC_EDIT);
		switch (LOWORD(wParam)) {
		case IDOK:
			SetWindowText(hWndEdit, "OK");
			break;
		case IDCANCEL:
			SetWindowText(hWndEdit, "Cancel");
			break;
		}
		break;
	case WM_CREATE:
		OnCreate(hWnd);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, uMsg, wParam, lParam);
	}
	return 0;
}
 
void OnCreate(HWND hWnd)
{
	CreateWindow("STATIC", "Push button",
		WS_CHILD | WS_VISIBLE,
		8, 8, 123, 32, hWnd, (HMENU)IDC_STATIC, g_hInstance, NULL);
	CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "Edit",
		WS_CHILD | WS_VISIBLE,
		144, 8, 123, 32, hWnd, (HMENU)IDC_EDIT, g_hInstance, NULL);
	CreateWindow("BUTTON", "OK",
		WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
		8, 48, 128, 32, hWnd, (HMENU)IDOK, g_hInstance, NULL);
	CreateWindow("BUTTON", "Cancel",
		WS_CHILD | WS_VISIBLE,
		144, 48, 128, 32, hWnd, (HMENU)IDCANCEL, g_hInstance, NULL);
}
 
最終更新:2012年09月01日 16:53