※上記の広告は60日以上更新のないWIKIに表示されています。更新することで広告が下部へ移動します。

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

utf16.c
#include <tchar.h>
#include <Windows.h>
#include "resource.h"
 
// 関数プロトタイプ宣言
ATOM			MyRegisterClass(HINSTANCE hInstance);
BOOL			InitInstance(HINSTANCE hInstance, int nCmdShow);
LRESULT CALLBACK	WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int			OnCommand(HWND hWnd, WPARAM wParam);
void			OnFormFont(HWND hWnd);
void			OnCreate(HWND hWnd);
void			OnXScroll(HWND hWnd, WPARAM wParam, int nBar, LPSCROLLINFO lpsi);
void			OnPaint(HWND hWnd);
 
// グローバル変数
TCHAR			g_atcClassName[] = _T("utf16");
TCHAR			g_atcWindowName[] = _T("UTF-16");
HINSTANCE		g_hInstance;
SCROLLINFO		g_siHorz;
SCROLLINFO		g_siVert;
LOGFONT			g_lf;
int			g_iUnitSize = 32;
 
//==============================================================================
int APIENTRY _tWinMain(
	HINSTANCE	hInstance,
	HINSTANCE	hPrevInstance,
	LPTSTR		lpCmdLine,
	int		nCmdShow)
{
	MSG	msg;
	HACCEL	hAccelTable;
 
	MyRegisterClass(hInstance);
	if (InitInstance(hInstance, nCmdShow) == FALSE) {
		return 0;
	}
	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDR_MAINFRAME);
	while (GetMessage(&msg, NULL, 0, 0)) {
		if (TranslateAccelerator(msg.hwnd, hAccelTable, &msg) == 0) {
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	return msg.wParam;
}
 
//------------------------------------------------------------------------------
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX	wcex;
 
	wcex.cbSize		= sizeof (WNDCLASSEX);
	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_WINDOW + 1);
	wcex.lpszMenuName	= (LPCTSTR)IDR_MAINFRAME;
	wcex.lpszClassName	= g_atcClassName;
	wcex.hIconSm		= LoadIcon(NULL, IDI_APPLICATION);
	return RegisterClassEx(&wcex);
}
 
//------------------------------------------------------------------------------
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
	HWND	hWnd;
 
	g_hInstance = hInstance;
	hWnd = CreateWindowEx(
		WS_EX_ACCEPTFILES,
		g_atcClassName,
		g_atcWindowName,
		WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL,
		CW_USEDEFAULT, 0,
		720, 720,
		NULL, NULL,
		hInstance,
		NULL);
	if (hWnd == NULL) {
		return FALSE;
	}
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);
	return TRUE;
}
 
//------------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg) {
	case WM_PAINT:
		OnPaint(hWnd);
		break;
	case WM_COMMAND:
		if (OnCommand(hWnd, wParam)) {
			return DefWindowProc(hWnd, uMsg, wParam, lParam);
		}
		break;
	case WM_HSCROLL:
		OnXScroll(hWnd, wParam, SB_HORZ, &g_siHorz);
		break;
	case WM_VSCROLL:
		OnXScroll(hWnd, wParam, SB_VERT, &g_siVert);
		break;
	case WM_CREATE:
		OnCreate(hWnd);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, uMsg, wParam, lParam);
	}
	return 0;
}
 
//------------------------------------------------------------------------------
int OnCommand(HWND hWnd, WPARAM wParam)
{
	static struct {
		UINT	uiMsg;
		int	nScrollCode;
	} a[] = {
		{WM_HSCROLL, SB_LINEUP},	// ID_LEFT
		{WM_VSCROLL, SB_LINEUP},	// ID_UP
		{WM_HSCROLL, SB_LINEDOWN},	// ID_RIGHT
		{WM_VSCROLL, SB_LINEDOWN},	// ID_DOWN
		{WM_HSCROLL, SB_TOP},		// ID_HTOP
		{WM_VSCROLL, SB_TOP},		// ID_VTOP
		{WM_HSCROLL, SB_BOTTOM},	// ID_HBOTTOM
		{WM_VSCROLL, SB_BOTTOM},	// ID_VBOTTOM
	};
	WORD	wID;
	WORD	w;
 
	wID = LOWORD(wParam);
	w = wID - ID_LEFT;
	if (w < _countof(a)) {
		SendMessage(hWnd, a[w].uiMsg, a[w].nScrollCode, (LPARAM)NULL);
		return 0;
	}
	switch (wID) {
	case ID_APP_EXIT:
		DestroyWindow(hWnd);
		break;
	case ID_FORM_FONT:
		OnFormFont(hWnd);
		break;
	default:
		return 1;
	}
	return 0;
}
 
//------------------------------------------------------------------------------
void OnFormFont(HWND hWnd)
{
	CHOOSEFONT	cf;
 
	ZeroMemory(&cf, sizeof cf);
	cf.lStructSize	= sizeof cf;
	cf.hwndOwner	= hWnd;
	cf.lpLogFont	= &g_lf;
	cf.Flags	= CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
	cf.nFontType	= SCREEN_FONTTYPE;
	if (ChooseFont(&cf) == FALSE) {
		return;
	}
	InvalidateRect(hWnd, NULL, TRUE);
}
 
//------------------------------------------------------------------------------
void OnCreate(HWND hWnd)
{
	g_siHorz.cbSize	= sizeof (SCROLLINFO);
	g_siHorz.fMask	= SIF_RANGE | SIF_PAGE | SIF_POS;
	g_siHorz.nMin	= 0;
	g_siHorz.nMax	= 15;
	g_siHorz.nPage	= 1;
	g_siHorz.nPos	= 0;
	g_siVert = g_siHorz;
	SetScrollInfo(hWnd, SB_HORZ, &g_siHorz, FALSE);
	SetScrollInfo(hWnd, SB_VERT, &g_siVert, FALSE);
 
	ZeroMemory(&g_lf, sizeof g_lf);
	g_lf.lfHeight = g_iUnitSize;
	_tcscpy_s(g_lf.lfFaceName, _countof(g_lf.lfFaceName), _T("メイリオ"));
}
 
//------------------------------------------------------------------------------
void OnXScroll(HWND hWnd, WPARAM wParam, int nBar, LPSCROLLINFO lpsi)
{
	int	iPos;
 
	iPos = lpsi->nPos;
 
	switch (LOWORD(wParam)) {
	case SB_LINEUP:		// 0
	case SB_PAGEUP:		// 2
		iPos--;
		break;
	case SB_LINEDOWN:	// 1
	case SB_PAGEDOWN:	// 3
		iPos++;
		break;
	case SB_THUMBTRACK:	// 5
		iPos = HIWORD(wParam);
		break;
	case SB_TOP:		// 6
		iPos = 0;
		break;
	case SB_BOTTOM:		// 7
		iPos = 15;
		break;
	}
 
	iPos = min(iPos, 15);
	iPos = max(iPos, 0);
	if (iPos == lpsi->nPos) {
		return;
	}
	lpsi->nPos = iPos;
	SetScrollInfo(hWnd, nBar, lpsi, TRUE);
	InvalidateRect(hWnd, NULL, TRUE);
}
 
//------------------------------------------------------------------------------
void OnPaint(HWND hWnd)
{
	PAINTSTRUCT	ps;
	HDC		hdc;
	HFONT		hFont;
	HGDIOBJ		hFontOld;
	RECT		rc;
	TCHAR		atcStr[16];
	int		iPage;
	int		iHigh;
	int		iLow;
 
	hdc = BeginPaint(hWnd, &ps);
 
	// フォント
//	g_lf.lfHeight = g_iUnitSize;
	hFont = CreateFontIndirect(&g_lf);
	hFontOld = SelectObject(hdc, hFont);
 
	// 見出し
	iPage = (g_siVert.nPos << 4) + g_siHorz.nPos;
	SetRect(&rc, g_iUnitSize, 0, 200, g_iUnitSize);
	_stprintf_s(atcStr, _countof(atcStr), _T("%02X区"), iPage);
	DrawText(hdc, atcStr, -1, &rc, DT_LEFT);
 
	// 下位ヘッダ
	rc.left		= g_iUnitSize * 2;
	rc.top		= g_iUnitSize;
	rc.bottom	= rc.top + g_iUnitSize;
	for (iLow = 0; iLow < 16; iLow++) {
		rc.right = rc.left + g_iUnitSize;
		_stprintf_s(atcStr, _countof(atcStr), _T("%02X"), iLow);
		DrawText(hdc, atcStr, -1, &rc, DT_CENTER);
		rc.left = rc.right;
	}
 
	// 上位ヘッダ
	rc.left		= g_iUnitSize;
	rc.top		= g_iUnitSize * 2;
	rc.right	= rc.left + g_iUnitSize;
	for (iHigh = 0; iHigh < 16; iHigh++) {
		rc.bottom = rc.top + g_iUnitSize;
		_stprintf_s(atcStr, _countof(atcStr), _T("%02X"), iHigh << 4);
		DrawText(hdc, atcStr, -1, &rc, DT_CENTER);
		rc.top = rc.bottom;
	}
 
	atcStr[0] = iPage << 8;
	for (iHigh = 0; iHigh < 16; iHigh++) {
		rc.left		= g_iUnitSize * 2;
		rc.top		= g_iUnitSize * (2 + iHigh);
		rc.bottom	= rc.top + g_iUnitSize;
		for (iLow = 0; iLow < 16; iLow++) {
			rc.right = rc.left + g_iUnitSize;
			DrawText(hdc, atcStr, 1, &rc, DT_CENTER);
			atcStr[0]++;
			rc.left = rc.right;
		}
	}
 
	SelectObject(hdc, hFontOld);
	DeleteObject(hFont);
	EndPaint(hWnd, &ps);
}
 

resource.h
#define ID_APP_EXIT	100
#define ID_FORM_FONT	101
 
#define IDR_MAINFRAME	128
#define ID_LEFT		129
#define ID_UP		130
#define ID_RIGHT	131
#define ID_DOWN		132
#define ID_HTOP		133
#define ID_VTOP		134
#define ID_HBOTTOM	135
#define ID_VBOTTOM	136
 

utf16.rc
// リソーススクリプト
 
#include <windows.h>
#include "resource.h"
 
//------------------------------------------------------------------------------
// メニュー
IDR_MAINFRAME MENU
BEGIN
	POPUP "ファイル(&F)"
	BEGIN
		MENUITEM "アプリケーションの終了(&X)", ID_APP_EXIT
	END
	POPUP "書式(&O)"
	BEGIN
		MENUITEM "フォント(&F)...\tCtrl+F", ID_FORM_FONT
	END
END
 
//------------------------------------------------------------------------------
// アクセラレータ
IDR_MAINFRAME ACCELERATORS
BEGIN
	VK_LEFT,	ID_LEFT,	VIRTKEY			// 0x25
	VK_UP,		ID_UP,		VIRTKEY			// 0x26
	VK_RIGHT,	ID_RIGHT,	VIRTKEY			// 0x27
	VK_DOWN,	ID_DOWN,	VIRTKEY			// 0x28
	VK_LEFT,	ID_HTOP,	VIRTKEY, CONTROL
	VK_UP,		ID_VTOP,	VIRTKEY, CONTROL
	VK_RIGHT,	ID_HBOTTOM,	VIRTKEY, CONTROL
	VK_DOWN,	ID_VBOTTOM,	VIRTKEY, CONTROL
	"F",		ID_FORM_FONT,	VIRTKEY, CONTROL
END
 
最終更新:2012年09月01日 16:44