掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
ダイアログのフォントの取得と変更 (ID:72103)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
散発的に、本題かどうか関係無く幾つかの内容に対してレス。 > 1の、肝心のHFONTの取得が、SelectObject()で(したくないのに)変更する以外に GetCurrentObject() を使うと HDC に結び付けられているgdiオブジェクトを直で参照できる。 > (GetDC()してSelectObject()で旧ハンドル取得とかやってました。お恥ずかしい限り) WM_SETFONT したからと言って HDC にデフォルトで結びつけられるフォントが指定フォントになるワケじゃ無い。 文字書く時はこのフォントで書けと(システムに)指示するだけ。HDC に結びつけるのは別段階の処理。 なので SelectObject(GetCurrentObject)の戻り値を視ても意味は無かったりする…ハズ(少なくとも何年か前に検証した時は) > そういう心配があったので、リソースでの指定みたく一括でやる方法がないものかと思ったのですが 無いわけではない。日本語よりC言語の方が伝わると思うのでソース貼付け。長いケド… INT_PTR DialogBoxMk2(HINSTANCE instance, LPCTSTR id, HWND parent, DLGPROC func, short point, LPCWSTR typeface) { #pragma pack(push, 1) struct DLGTEMPLATEEX { WORD dlgVer; WORD signature; DWORD helpID; DWORD exStyle; DWORD style; WORD cDlgItems; short x; short y; short cx; short cy; }; #pragma pack(pop) INT_PTR result = 0; HRSRC src = ::FindResource(instance, id, RT_DIALOG); if (src != NULL) { HGLOBAL res = ::LoadResource(instance, src); if (res != NULL) { const DWORD length = ::SizeofResource(instance, src); BYTE* chg = new BYTE[length + 2 + 2 + 1 + 1 + sizeof(wchar_t) * (wcslen(typeface) + 1) + 3]; // 大は小を兼ねる if (chg != NULL) { BYTE* org = static_cast<BYTE*>(::LockResource(res)); if (org != NULL) { const bool ex = (*reinterpret_cast<WORD*>(org + 2) == 0xffff); BYTE* src = org; BYTE* dst = chg; // skip DLGTEMPLATE or DLGTEMPLATEEX src += ex ? sizeof(DLGTEMPLATEEX) : sizeof(DLGTEMPLATE); // skip menu if (*reinterpret_cast<WORD*>(src) == 0xffff) { src += 4; } else { src += sizeof(wchar_t) * (wcslen(reinterpret_cast<LPWSTR>(src)) + 1); } // skip class if (*reinterpret_cast<WORD*>(src) == 0xffff) { src += 4; } else { src += sizeof(wchar_t) * (wcslen(reinterpret_cast<LPWSTR>(src)) + 1); } // skip title BYTE* p = (ex ? src : org); src += sizeof(wchar_t) * (wcslen(reinterpret_cast<LPWSTR>(src)) + 1); memcpy(dst, org, src - org); bool srcfontexist; if (ex) { srcfontexist = ((reinterpret_cast<DLGTEMPLATEEX*>(org)->style & DS_SETFONT) != 0); reinterpret_cast<DLGTEMPLATEEX*>(chg)->style |= DS_SETFONT; } else { srcfontexist = ((reinterpret_cast<DLGTEMPLATE*>(org)->style & DS_SETFONT) != 0); reinterpret_cast<DLGTEMPLATE*>(chg)->style |= DS_SETFONT; } dst += src - org; // skip font if (srcfontexist) { if (ex) { p = src; } src += (ex ? 2 + 2 + 1 + 1 : 2); src += sizeof(wchar_t) * (wcslen(reinterpret_cast<LPWSTR>(src)) + 1); } src = p + ((src - p + 3) / 4) * 4; // 4byte境界調整 // change font p = (ex ? dst : chg); *reinterpret_cast<short*>(dst) = point; // pointsize dst += 2; if (ex) { *reinterpret_cast<short*>(dst) = FW_NORMAL; // weight dst += 2; *dst++ = 0; // italic *dst++ = DEFAULT_CHARSET; // charset(面倒なので DEFAULT で…) } wcscpy(reinterpret_cast<LPWSTR>(dst), typeface); // fontface dst += sizeof(wchar_t) * (wcslen(reinterpret_cast<LPWSTR>(dst)) + 1); dst = p + ((dst - p + 3) / 4) * 4; // 4byte境界調整 // copy DLGITEMTEMPLATEs or DLGITEMTEMPLATEEXs memcpy(dst, src, org + length - src); result = ::DialogBoxIndirect(instance, reinterpret_cast<DLGTEMPLATE*>(chg), parent, func); } delete []chg; } } } return result; } void xxxxxxxx() { // dialog 表示 // ::DialogBox(module, MAKEINTRESOURCE(IDD_DIALOG), parent, dialogProc); DialogBoxMk2(module, MAKEINTRESOURCE(IDD_DIALOG), parent, dialogProc, 16, L"メイリオ"); } 稼動実績あるコードじゃないんで当然bugは在るという前提で。
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2020 Takeshi Okamoto All Rights Reserved.