Fontgröße an Editfield anpassen



  • Hallo,
    im Dialogeditor kann man die Höhe eines Editfields ja einfach verändern. Wie kann man die Schriftart des Textes automatisch auf die Höhe des Edits anpassen?

    Aktuell ändere ich den Font (auf Courier New), schaffe es aber nicht die Schrifthöhe automatisch anzupassen.

    Viele Grüße!!


  • Mod

    Selber rechnen:
    - Höhe ermitteln.
    - Rand abziehen.
    - Neuen Font errechnen.
    - Neuen Font setzen.



  • Na Du kannst jeden Font mit CWnd::SetFont() an das Fenster binden.

    Oder du selektierst einen Font in den -GeräteContext

    CFont *pFnt = CDC::SelectObject(&MyFont);

    Dann kannst Du sogar die benötigte Rechteckgröße mit einer
    Spezialversion von DrawText berechnen lassen :

    CString srtMsg("Komplextext");

    CRect ccr; CDC::DrawText(strMsg, strMsg.GetLength(), &ccr, DT_CALCRECT);

    Das kannst Du sogar "zu fuß" dynamisch errechnen:

    _SIZE MeasureTextA(LPSTR lpszText, LPTSTR lpszFontName, long fontSize, BOOL bBold, BOOL bItalic)
    {
    	_SIZE szText = {0, 0};
    
    	// Check for valid bitmap
    	if (IsValid() && (lpszText != NULL))
    	{
    		// Measure text size
    		long iLen = (long)strlen(lpszText);
    		HDC hDC = ::GetDC(NULL);
    		long iFontHeight = -MulDiv(fontSize, ::GetDeviceCaps(hDC, LOGPIXELSY), 72);
    		long iWeight = FW_NORMAL;
    		if (bBold)
    			iWeight = FW_BOLD;
    		HFONT hFont = ::CreateFont(iFontHeight, 0, 0, 0, iWeight, bItalic, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, lpszFontName);
    		HFONT hOldFont = (HFONT)::SelectObject(hDC, hFont);
    		SIZE sz;
    		::GetTextExtentExPointA(hDC, lpszText, iLen, 0, NULL, NULL, &sz);
    		GLYPHMETRICS gm;
    		MAT2 m2 = {{0,1}, {0,0}, {0,0}, {0,1}};
    		long iLetterOffset = 0;
    		long iLineOffset = 0;
    		for (long k=0; k<iLen; k++)
    		{
    			// Check for libe-break character
    			if (lpszText[k] == '\n')
    			{
    				if (szText.cx < iLetterOffset)
    					szText.cx = iLetterOffset;
    				szText.cy += sz.cy;
    				iLineOffset += sz.cy;
    				iLetterOffset = 0;
    			}
    			else
    			{
    				// Get font letter info
    				::GetGlyphOutlineA(hDC, lpszText[k], GGO_METRICS, &gm, 0, NULL, &m2);
    
    				// Increment letter horizontal offset
    				iLetterOffset += gm.gmCellIncX;
    			}
    		}
    		if (szText.cx < iLetterOffset)
    			szText.cx = iLetterOffset;
    		szText.cy += sz.cy;
    		::SelectObject(hDC, hOldFont);
    		::DeleteObject(hFont);
    		::ReleaseDC(NULL, hDC);
    	}
    
    	return szText;
    }
    

    Fonts haben eine weitreichende Thermik in der GDI , und waren mit ein Grund warum Windows gegen OS2 siegreich war 🙂

    Lg.
    K


Anmelden zum Antworten