LOGFONT構造体を継承して MyFontクラスを作ります。 そして、MyDCクラスで作成します。
#ifndef ___MYFONT #define ___MYFONT // // MyFont クラス // class MyFont : public LOGFONT{ private: HFONT hFont; public: // コンストラクタ MyFont(){ // LOGFONT構造体 lfHeight = 17; // 文字の高さ lfWidth = 0; // 文字幅 lfEscapement = 0; // 文字方向とX軸との角度 lfOrientation = 0; // 各文字とX軸との角度 lfWeight = FW_NORMAL; // 太さ lfItalic = FALSE; // イタリック体 lfUnderline = FALSE; // 下線 lfStrikeOut = FALSE; // 打ち消し線 lfCharSet = SHIFTJIS_CHARSET; // キャラクタセット lfOutPrecision = OUT_DEFAULT_PRECIS; // 出力精度 lfClipPrecision = CLIP_DEFAULT_PRECIS; // クリッピングの精度 lfQuality = ANTIALIASED_QUALITY; // 出力品質 lfPitchAndFamily = VARIABLE_PITCH | FF_MODERN; // ピッチとファミリ lstrcpy(lfFaceName, TEXT("MS UI Gothic")); // フォント名 } public: // メソッド HFONT Create(){ hFont = CreateFont(lfHeight, lfWidth, lfEscapement, lfOrientation, lfWeight, lfItalic, lfUnderline, lfStrikeOut, lfCharSet, lfOutPrecision, lfClipPrecision, lfQuality, lfPitchAndFamily, lfFaceName); return hFont; } void Delete(){ DeleteObject(hFont); } void Add(HWND hwnd){ SendMessage(hwnd, WM_SETFONT, (WPARAM)hFont, 0); } void Add(HDC hdc){ SelectObject(hdc, hFont); } // フォントを変えるプロパティ void Name(LPCTSTR txt){ lstrcpy(lfFaceName, txt); } void Size(LONG height, LONG width = 0){ lfHeight = height; lfWidth = width; } void Bold(BOOL tf){ if(tf == TRUE){ lfWeight = FW_BOLD; }else{ lfWeight = FW_NORMAL; } } void Italic(BOOL tf){ lfItalic = TRUE; } void Strikeout(BOOL tf){ lfStrikeOut = tf; } void Underline(BOOL tf){ lfUnderline = tf; } }; #endif
"MyDC.h" の一部です。 赤い部分を追加します。
#ifndef ___MYDC #define ___MYDC // // MyDC クラス(デバイスコンテキスト) // MyDC // #include <MyFont.h> class MyDC{ public: void Add(MyFont& font){ font.Create(); font.Add(hdc); } }; #endif
"MyCreateStruct.h" の一部です。 こちらはメンバーにします。
#ifndef ___MYCREATESTRUCT #define ___MYCREATESTRUCT #include <MyFont.h> // MyCreateStruct クラス(CREATESTRUCT構造体) class MyCreateStruct : public CREATESTRUCT{ public: MyFont Font; HWND Create(){ hwnd = CreateWindowEx( dwExStyle, lpszClass, lpszName, style, x, y, cx, cy, hwndParent, hMenu, hInstance, lpCreateParams); Font.Create(); Font.Add(hwnd); return hwnd; } }; #endif
この MyFontクラスを使ってみます。 フォントを画面に追加するという感じで使います。
// // MyApp.h // class MyApp : public MyMainWnd{ public: // WM_PAINT void wmPaint(HDC hdc){ MyDC g(hdc); MyFont Font; Font.Name(L"MS P明朝"); Font.Size(25); g.Add(Font); g.TextOut(10, 10, L"Hello!"); } };
実行画面です。フォントが変わりました。