開発環境 |
Microsoft Visual Studio Community 2017 |
実行環境 |
Microsoft Windows 10 Home (64bit) |
プロジェクトの種類 |
Visual C++/空のプロジェクト |
プロジェクト名 |
FontSample |
FontSample.cpp
// Unicode文字セット
#include <Windows.h>
#include <windowsx.h>
#include <set>
#include <string>
#include "Resource.h"
using namespace std;
typedef set<wstring> STRLIST;
// 関数プロトタイプ宣言
INT_PTR CALLBACK DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
void OnInitDialog(HWND hDlg);
void OnDrawItem(LPARAM lParam);
int CALLBACK EnumFontFamExProc(const LOGFONT *lpelfe, const TEXTMETRIC *lpntme, DWORD FontType, LPARAM lParam);
void Redraw();
// 外部変数
STRLIST facenames;
HWND hMainDlg;
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAINDLG), NULL, DlgProc);
return 0;
}
INT_PTR CALLBACK DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
INT_PTR ret = TRUE;
switch (uMsg) {
case WM_DRAWITEM:
OnDrawItem(lParam);
break;
case WM_COMMAND:
WORD cid;
cid = LOWORD(wParam);
switch (cid) {
case IDC_FACENAME:
switch (HIWORD(wParam)) {
case LBN_SELCHANGE:
Redraw();
break;
}
break;
case IDOK:
Redraw();
break;
case IDCANCEL:
EndDialog(hDlg, LOWORD(wParam));
break;
default:
ret = FALSE;
}
break;
case WM_INITDIALOG:
hMainDlg = hDlg;
OnInitDialog(hDlg);
return FALSE;
default:
ret = FALSE;
}
return ret;
}
void OnInitDialog(HWND hDlg)
{
SetDlgItemText(hDlg, IDC_SAMPLE,
L"東京都千代○区\r\n永○町1丁目10ー1\r\n\r\n図書 館太郎様\r\n 花子様\r\n1ー1-1―-");
SetDlgItemText(hDlg, IDC_HEIGHT, L"50");
HDC hdc = GetDC(NULL);
LOGFONT lf = { 0 };
lf.lfCharSet = DEFAULT_CHARSET;
EnumFontFamiliesEx(hdc, &lf, EnumFontFamExProc, 0, 0);
ReleaseDC(NULL, hdc);
HWND hList = GetDlgItem(hDlg, IDC_FACENAME);
for (auto&& e : facenames) {
ListBox_AddString(hList, e.c_str());
}
ListBox_SetCurSel(hList, 0);
}
void OnDrawItem(LPARAM lParam)
{
LPDRAWITEMSTRUCT pdi = (LPDRAWITEMSTRUCT)lParam;
HDC hdc = pdi->hDC;
RECT rcView;
GetClientRect(pdi->hwndItem, &rcView);
FillRect(hdc, &rcView, GetSysColorBrush(COLOR_WINDOW));
// 書体名
wchar_t facename[256];
HWND hList = GetDlgItem(hMainDlg, IDC_FACENAME);
int index = ListBox_GetCurSel(hList);
ListBox_GetText(hList, index, facename);
bool vert = (facename[0] == L'@');
// 例文
wchar_t buf[256];
GetDlgItemText(hMainDlg, IDC_SAMPLE, buf, _countof(buf));
// フォントの高さ
BOOL trans;
int height = GetDlgItemInt(hMainDlg, IDC_HEIGHT, &trans, TRUE);
if (trans == FALSE) {
trans = 10;
}
// フォント作成
LOGFONT lf = { 0 };
lf.lfCharSet = DEFAULT_CHARSET;
lf.lfEscapement = (vert ? 2700 : 0);
//lf.lfOrientation = 2700;
lf.lfHeight = height;
wcscpy_s(lf.lfFaceName, facename);
HFONT hFont = CreateFontIndirect(&lf);
HGDIOBJ hFontOld = SelectObject(hdc, hFont);
if (vert) {
LPTSTR p = buf;
for (int x = rcView.right; ; x -= abs(height)) {
LPTSTR n = wcsstr(p, L"\r\n");
if (n) {
*n = 0;
TextOut(hdc, x, 0, p, wcslen(p));
p = n + 2;
}
else {
TextOut(hdc, x, 0, p, wcslen(p));
break;
}
}
}
else {
DrawText(hdc, buf, -1, &rcView, 0);
}
SelectObject(hdc, hFontOld);
DeleteObject(hFont);
}
int CALLBACK EnumFontFamExProc(const LOGFONT *lpelfe, const TEXTMETRIC *lpntme, DWORD FontType, LPARAM lParam)
{
if (facenames.find(lpelfe->lfFaceName) == facenames.end()) {
facenames.insert(lpelfe->lfFaceName);
}
return TRUE;
}
void Redraw()
{
RedrawWindow(GetDlgItem(hMainDlg, IDC_PREVIEW), NULL, NULL, RDW_INVALIDATE);
}
Resource.h
#define IDD_MAINDLG 100
#define IDC_FACENAME 101
#define IDC_SAMPLE 102
#define IDC_HEIGHT 103
#define IDC_PREVIEW 104
#define IDC_STATIC -1
FontSample.rc
#include <windows.h>
#include "resource.h"
IDD_MAINDLG DIALOGEX 100, 100, 480, 500
//STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
STYLE DS_CENTER | WS_SYSMENU | WS_MINIMIZEBOX
CAPTION "Font Sample"
FONT 9, "MS UI Gothic"
BEGIN
LISTBOX IDC_FACENAME,10,10,150,400,WS_TABSTOP | WS_VSCROLL
EDITTEXT IDC_SAMPLE,10,420,150,45,ES_MULTILINE | WS_VSCROLL
LTEXT "Font Height:",IDC_STATIC,10,475,50,14
EDITTEXT IDC_HEIGHT,50,475,50,14,
DEFPUSHBUTTON "OK",IDOK,110,475,50,14,WS_GROUP
LTEXT "",IDC_PREVIEW,170,10,300,400,WS_BORDER | SS_OWNERDRAW
END
最終更新:2018年12月17日 20:58