Re: How to get the correct width of italic font?



Besides, I've wrttien a demo function to display different font in a
line (derived from msdn), but as you can run and see, the italic font
won't display properly.

<CODE>
void demoFunc(HDC hdc)
{
int XIncrement;
int YStart;
HFONT hfntDefault;
HFONT hfntItalic;
HFONT hfntNormal;
SIZE sz;
LPSTR lpszString = "dd";
int stringLen = 2;
LPWSTR wString = (LPWSTR)malloc(WCHAR_STRING_LENGTH * sizeof(LPWSTR));

// Create a normal and an italic logical font
hfntItalic = MyCreateFont(ITALIC_FONT);
hfntNormal = MyCreateFont(NORMAL_FONT);

// translate the LPSTR string to LPWSTR string
MultiByteToWideChar(CP_ACP, 0, lpszString, stringLen, wString,
WCHAR_STRING_LENGTH);

// Select the normal font and draw the first string
// beginning at the specified point (XIncrement, YStart).
XIncrement = 10;
YStart = 50;
hfntDefault = (HFONT)SelectObject(hdc, hfntNormal);

TextOut(hdc, XIncrement, YStart, (LPCWSTR)wString, stringLen);

SelectObject(hdc, hfntNormal);
GetTextExtentPoint32(hdc, (LPCWSTR)wString, stringLen, &sz);
XIncrement += sz.cx;

// Select an italic font and draw the second string
// beginning at the point (XIncrement, YStart).
hfntNormal = (HFONT)SelectObject(hdc, hfntItalic);
TextOut(hdc, XIncrement, YStart, (LPCWSTR)wString, stringLen);

// Compute the length of the second string and add
// this value to the x-increment that is used for the
// text-output operation.
SelectObject(hdc, hfntItalic);
GetTextExtentPoint32(hdc, (LPCWSTR)wString, stringLen, &sz);
XIncrement += sz.cx;

// Reselect the normal font and draw the third string
// beginning at the point (XIncrement, YStart).
SelectObject(hdc, hfntNormal);
TextOut(hdc, XIncrement, YStart, (LPCWSTR)wString, stringLen);

// Reselect the original font.
SelectObject(hdc, hfntDefault);

// Delete the bold and italic fonts.
DeleteObject(hfntItalic);
DeleteObject(hfntNormal);

free(wString);
}
</CODE>

some additional stuffs:
<CODE>
#include "strsafe.h"
#include "assert.h"
#include "Winnls.h"

typedef enum _FontType{
ITALIC_FONT = 1,
BOLD_FONT = 2,
NORMAL_FONT = 3,
}FontType;

#define WCHAR_STRING_LENGTH 1024

HFONT MyCreateFont(FontType ftype)
{
LOGFONT logfont = {0};

switch (ftype)
{
case ITALIC_FONT:
logfont.lfItalic = TRUE;
break;
case BOLD_FONT:
logfont.lfWeight = FW_BOLD;
break;
case NORMAL_FONT:
break;
default:
assert(0);
break;
}

return CreateFontIndirect(&logfont);
}
</CODE>

Thanks for your any consideration.

Regards,
Johnny

.



Relevant Pages

  • Re: [Q]How to display Tahoma italic font
    ... I can't display Tahoma font in italic properly using ... string and incrementing XIncrement, you should do a second increment as ... character of the non-italic string that follows). ... // beginning at the specified point (XIncrement, YStart). ...
    (microsoft.public.win32.programmer.gdi)
  • Re: [Q]How to display Tahoma italic font
    ... I suspect it is an issue of different screen resolutions or font size. ... Thus adding any of these to XIncrement reduces XIncrement, ... // Select the normal font and draw the first string ... // beginning at the specified point (XIncrement, YStart). ...
    (microsoft.public.win32.programmer.gdi)
  • Re: [Q]How to display Tahoma italic font
    ... I suspect it is an issue of different screen resolutions or font size. ... Thus adding any of these to XIncrement reduces XIncrement, ... // Select the normal font and draw the first string ... // beginning at the specified point (XIncrement, YStart). ...
    (microsoft.public.win32.programmer.gdi)
  • [Q]How to display Tahoma italic font
    ... I can't display Tahoma font in italic properly using ... // translate the LPSTR string to LPWSTR string ... // beginning at the specified point (XIncrement, YStart). ...
    (microsoft.public.win32.programmer.gdi)
  • Re: [Q]How to display Tahoma italic font
    ... right hand normal letters overlap the last italic letter. ... other fonts, here i mean any font i've tried. ... space you need for the string. ... // beginning at the specified point (XIncrement, ...
    (microsoft.public.win32.programmer.gdi)

Loading