|
|
1.1 ! root 1: #include <windows.h> ! 2: #include <windowsx.h> ! 3: #include <commdlg.h> ! 4: #include <string.h> ! 5: #include <memory.h> ! 6: #include "app.h" ! 7: ! 8: #include "font.hxx" ! 9: //+-------------------------------------------------------- ! 10: // Class: CFont ! 11: // ! 12: // Purpose: Abstract a font ! 13: // ! 14: // History: 22-Jan-1993 asmusf created ! 15: //---------------------------------------------------------- ! 16: CFont::CFont(HFONT hfont) ! 17: { ! 18: _hfont = hfont; ! 19: _fDel = FALSE; // owned elsewhere ! 20: } ! 21: ! 22: CFont::CFont(TCHAR *szFace, int iHeight, BOOL fBold, BOOL fItalic, BOOL fUnder) ! 23: { ! 24: LOGFONT lf; ! 25: _fDel = FALSE; // not initialized ! 26: ! 27: memset(&lf,0,sizeof (LOGFONT)); ! 28: // rude but effective: truncate ! 29: if( lstrlen( szFace ) > sizeof(lf.lfFaceName) ) ! 30: { ! 31: szFace[sizeof(lf.lfFaceName)] = 0; ! 32: } ! 33: lstrcpy(lf.lfFaceName, szFace); ! 34: ! 35: lf.lfHeight = iHeight * 20; // twips internal, points in API ! 36: ! 37: if( fBold ) ! 38: { ! 39: lf.lfWeight = FW_BOLD; ! 40: } ! 41: lf.lfItalic = fItalic; ! 42: lf.lfUnderline = fUnder; ! 43: Create(lf); ! 44: } ! 45: ! 46: BOOL CFont::Create(LOGFONT &lf) ! 47: { ! 48: if( _fDel ) ! 49: { ! 50: DeleteObject(_hfont); ! 51: _fDel = FALSE; ! 52: } ! 53: if( _hfont = CreateFontIndirect(&lf) ) ! 54: { ! 55: _fDel = TRUE; ! 56: return TRUE; ! 57: } ! 58: return FALSE; ! 59: } ! 60: ! 61: CFont::~CFont() ! 62: { ! 63: if( _fDel ) ! 64: { ! 65: DeleteObject(_hfont); ! 66: } ! 67: } ! 68: ! 69: BOOL CFont::Update(int iHeight, BOOL fBold) ! 70: { ! 71: LOGFONT lf; ! 72: GetObject(_hfont, sizeof(LOGFONT), &lf); ! 73: lf.lfWeight = (fBold ? FW_BOLD : FW_NORMAL ); ! 74: ! 75: lf.lfHeight = iHeight * 20; // twips internal, points in API ! 76: ! 77: ! 78: return Create(lf); ! 79: } ! 80: ! 81: ! 82: BOOL CFont::Choose(HWND hwnd) ! 83: { ! 84: CHOOSEFONT cf ; ! 85: LOGFONT lf ; ! 86: ! 87: GetObject(_hfont, sizeof(LOGFONT), &lf); ! 88: ! 89: ! 90: // match from TWIPS to LOGPIXELS used in common Dialog ! 91: CScreenCanvas canvas(NULL); ! 92: lf.lfHeight = MulDiv(lf.lfHeight,GetDeviceCaps(canvas, LOGPIXELSY),INCH1); ! 93: ! 94: lf.lfCharSet = ANSI_CHARSET; ! 95: ! 96: cf.lStructSize = sizeof (CHOOSEFONT) ; ! 97: cf.hwndOwner = hwnd ; ! 98: cf.hDC = NULL ; ! 99: cf.lpLogFont = &lf ; ! 100: cf.iPointSize = 0 ; ! 101: cf.Flags = CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS ! 102: | CF_EFFECTS ; ! 103: cf.rgbColors = 0L ; ! 104: cf.lCustData = 0L ; ! 105: cf.lpfnHook = NULL ; ! 106: cf.lpTemplateName = NULL ; ! 107: cf.hInstance = NULL ; ! 108: cf.lpszStyle = NULL ; ! 109: cf.nFontType = 0 ; // Returned from ChooseFont ! 110: cf.nSizeMin = 0 ; ! 111: cf.nSizeMax = 0 ; ! 112: ! 113: if( !ChooseFont (&cf) ) ! 114: { ! 115: return FALSE; ! 116: } ! 117: // MessageBox( hwnd, lf.lfFaceName, TEXT("Font Chosen"), MB_OK ); ! 118: ! 119: // adjust back to using TWIPS ! 120: lf.lfHeight = MulDiv(lf.lfHeight,INCH1,GetDeviceCaps(canvas, LOGPIXELSY)); ! 121: lf.lfWidth=0; // font mapper knows best ! 122: ! 123: // create the font ! 124: return Create(lf); ! 125: } ! 126: ! 127:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.