Club Delphi  
    Paypal   FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Otros temas > Trucos
Registrarse FAQ Miembros Calendario Guía de estilo Buscar Temas de Hoy Marcar Foros Como Leídos

Los mejores trucos

Respuesta
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 16-01-2022
guspx guspx is offline
Miembro
 
Registrado: jun 2019
Posts: 17
Poder: 0
guspx Va por buen camino
Corrección

Para incluir si la letra es negrita o itálica:

Código Delphi [-]
function GdiPlusTextOut(Dc: HDC; x, y: Integer; stext: string): Integer;
var
  TempDc: HDC;
  oldbit, hbit: HBITMAP;
  fam: IgpFontFamily;
  f: IgpFont;
  tm: TTextMetric;
  oldfont, newfont: HFONT;
  lf: TLogFont;
  graphics: IgpGraphics;
  r: TGpRectF;
  w, h: Integer;
  curalign, curstretch, asc, desc: Integer;
  style: TgpFontStyle;
begin
  Result:= 0;
  oldfont:= GetCurrentObject(Dc, OBJ_FONT);
  if oldfont = 0 then
    exit;
  if GetObject(oldfont, sizeof(TLogFont), @lf) = 0 then
    exit;
  newfont:= CreateFontIndirect(lf);
  if newfont = 0 then
    exit;
  style:= [];
  if lf.lfWeight = FW_BOLD then
    style:= style + [FontStyleBold];
  if lf.lfItalic <> 0 then
    style:= style + [FontStyleItalic];
  if lf.lfUnderline <> 0 then
    style:= style + [FontStyleUnderline];
  TempDc:= CreateCompatibleDC(DC);
  fam:= tgpfontfamily.Create(lf.lfFaceName);
  graphics:= TgpGraphics.Create(Dc);
  f:= TGpFont.Create(Dc, newfont);
  GetTextMetrics(Dc, tm);
  curstretch:= SetStretchBltMode(Dc, COLORONCOLOR);
  try
    r:= graphics.MeasureString(stext, f, TGpPointF.Create(X, Y));
    w:= Ceil(r.Width);
    h:= tm.tmHeight;
    hbit:= CreateCompatibleBitmap(Dc, w, h);
    oldbit:= SelectObject(TempDc, hbit);
    oldfont:= SelectObject(TempDc, newfont);
    curalign:= SetTextAlign(TempDC, TA_LEFT or TA_TOP);
    try
      PatBlt(TempDc, 0, 0, w, h, WHITENESS);
      SetTextColor(TempDc, GetTextColor(Dc));
      SetBkMode(TempDc, TRANSPARENT);
      TextOut(TempDc, 0, 0, PChar(stext), Length(stext));
      asc:= Round(F.Size * fam.GetCellAscent(style) /
         fam.GetEmHeight(style));
      desc:= Round(F.Size * fam.GetCellDescent(style) /
         fam.GetEmHeight(style));
      TransparentBlt(Dc, x, y, w, asc + desc, TempDc, 0, tm.tmAscent - asc,
        w, asc + desc, $FFFFFF);
      Result:= asc + desc;
    finally
      SetTextAlign(TempDc, curalign);
      SelectObject(TempDC, oldfont);
      DeleteObject(newfont);
      SelectObject(TempDc, oldbit);
      DeleteObject(hbit);
    end;
  finally
    graphics:= nil;
    f:= nil;
    fam:= nil;
    SetStretchBltMode(Dc, curstretch);
    DeleteDC(TempDc);
  end;
end;

Última edición por Casimiro Noteví fecha: 16-01-2022 a las 19:19:28.
Responder Con Cita
  #2  
Antiguo 16-01-2022
Avatar de Casimiro Noteví
Casimiro Noteví Casimiro Noteví is offline
Merodeador
 
Registrado: sep 2004
Ubicación: En algún lugar.
Posts: 32.669
Poder: 10
Casimiro Noteví Tiene un aura espectacularCasimiro Noteví Tiene un aura espectacular
Gracias por el aporte
Responder Con Cita
  #3  
Antiguo 16-01-2022
guspx guspx is offline
Miembro
 
Registrado: jun 2019
Posts: 17
Poder: 0
guspx Va por buen camino
He podido comprobar que hay algunas fuentes que no soporta, como las que comienzan por @. También que gdi plus no soporta tipos de letra no true type, al parecer, pero quitando esas con @, la mayoría sí que las soporta.
Responder Con Cita
  #4  
Antiguo 17-01-2022
guspx guspx is offline
Miembro
 
Registrado: jun 2019
Posts: 17
Poder: 0
guspx Va por buen camino
Perdón, última edición. Cambiar:

Código Delphi [-]
f:= TGpFont.Create(Dc, newfont);

Por:

Código Delphi [-]
f:= TGpFont.Create(fam, -lf.lfHeight * 72 div GetDeviceCaps(DC, LOGPIXELSY), style, UnitPixel);

Esto resuelve algunos problemas, siempre que fam no sea nil, claro. Lo que sucede con algunas fuentes en las que es posible detectarlo antes de usarla, aunque es algo complicado, lo diré muy sucintamente. Se basa en llamar a EnumFontFamiliesEx y en la función callback EnumFontFamExProc detectar que en la estructura PNEWTEXTMETRICEX, el bit 21 del elemento ntmFlags sea 1. Ver la página de microsoft sobre "NEWTEXTMETRICA". Cuando la fuente no tiene una fontsignature válida, gdiplus no la acepta.

Última edición por Casimiro Noteví fecha: 17-01-2022 a las 11:31:17.
Responder Con Cita
  #5  
Antiguo 17-01-2022
Avatar de ElKurgan
[ElKurgan] ElKurgan is offline
Miembro Premium
 
Registrado: nov 2005
Posts: 1.242
Poder: 22
ElKurgan Va camino a la fama
Thumbs up

Gracias por la información

Saludos
Responder Con Cita
  #6  
Antiguo 17-01-2022
guspx guspx is offline
Miembro
 
Registrado: jun 2019
Posts: 17
Poder: 0
guspx Va por buen camino
Perdón, siempre cometo el error de precipitarme en publicar algo sin comprobarlo bien.

En realidad la TGpFont debe ser creada así:

Código Delphi [-]
      f:= TGpFont.Create(fam, -lf.lfHeight, style, UnitWorld);

Última edición por Casimiro Noteví fecha: 17-01-2022 a las 11:31:33.
Responder Con Cita
  #7  
Antiguo 17-01-2022
guspx guspx is offline
Miembro
 
Registrado: jun 2019
Posts: 17
Poder: 0
guspx Va por buen camino
Y hay veces en que TransparentBlt falla, para corregir esto:

Código Delphi [-]
if not TransparentBlt(Dc, x, y, w, Min(h, asc + desc),
            TempDc, 0, Max(0, tm.tmAscent - asc), w,
            Min(h, asc + desc), $FFFFFF) then
  exit;
Result:= MIn(h, asc + desc);

Cuando la funciòn devuelva 0 porque hay algún fallo se puede llamar a TextOut normal.

Última edición por Casimiro Noteví fecha: 17-01-2022 a las 11:31:57.
Responder Con Cita
Respuesta


Herramientas Buscar en Tema
Buscar en Tema:

Búsqueda Avanzada
Desplegado

Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro

Temas Similares
Tema Autor Foro Respuestas Último mensaje
Listado de tipos de letra jandro Varios 5 17-11-2009 23:49:46
TextBox escribir automaticamente letra por letra? Ejemplo Dentro! Gattaca Varios 2 21-03-2009 17:41:32
Relacion familias y articulos Espartaco SQL 7 20-06-2008 09:31:54
Codigos para impresoras: tipos de letra, orientacion hoja, etc Meneleo Impresión 1 11-03-2007 07:40:29
Familias de productos con TTreeView thunor Varios 0 10-08-2003 01:51:01


La franja horaria es GMT +2. Ahora son las 18:54:44.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi