NO

Author Topic: Vertical text  (Read 7575 times)

xsilvergs

  • Guest
Vertical text
« on: May 09, 2007, 10:43:35 AM »
Is there a better way to write text vertically than this:

      ExtTextOut(hDC,1,100,0,NULL,L"D",1,NULL);
      ExtTextOut(hDC,1,112,0,NULL,L"o",1,NULL);
      ExtTextOut(hDC,2,124,0,NULL,L"s",1,NULL);
      ExtTextOut(hDC,1,136,0,NULL,L"e",1,NULL);

I'm trying to write the word "Dose" beside the Y axis on a graph, and as a beginner thought I'd ask for some advice.

Thanks

Tony

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Vertical text
« Reply #1 on: May 09, 2007, 12:56:31 PM »
You can try DrawText, it will not write vertically as you mean (one litter at time), but if you define a rectangle starting from below and ending up it will just rotate 90 degrees the text (or at least should!  ;D).
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

xsilvergs

  • Guest
Re: Vertical text
« Reply #2 on: May 09, 2007, 04:28:06 PM »
Thanks frankie

I have written this and it seems to work:

      SetRect(&vbox,12,100,20,200);
      DrawText(hDC,L"D\no\ns\ne\n",8, &vbox,DT_CENTER);

Is that how you would have written it?

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Vertical text
« Reply #3 on: May 10, 2007, 07:52:49 PM »
Your solution is a good one.
If you want to write text with any angle use this function that I wrote for my use:
Code: [Select]
/*-@@+@@--------------------------------[Do not edit manually]------------
 Procedure: DrawRotatedText
 Created  : Thu May 10 19:26:35 2007
 Modified : Thu May 10 19:48:22 2007

 Synopsys : Draws rotated text
 Input    : hDC  : pointer to your device-context
            str  : the text
            x    : x start position
            y    : y start position
            Opts : see documentation of ExtTextOut for more details
            sFont: Font name or NULL to use default
 Output   : TRUE if success.
 Errors   : None
 ------------------------------------------------------------------@@-@@-*/
BOOL DrawRotatedText(HDC hDC, const wchar_t *str, UINT x, UINT y, float angle, UINT Opts, LPCTSTR sFont)
{
// convert angle to radian
HFONT hFont, hOldFont;

hFont = CreateFont( 0, 0, angle * 10.0, angle * 10.0, FW_DONTCARE, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, sFont);
if (!hFont)
return FALSE;

hOldFont = SelectObject(hDC, hFont);

ExtTextOutW(hDC, x, y, Opts, NULL, str, wcslen(str), NULL);

hOldFont = SelectObject(hDC, hOldFont);
DeleteObject(hFont);

return TRUE;
}

You can call the functios as:
Code: [Select]
if (!DrawRotatedText(ps.hdc,L"Dose", 50, 100, 45, 0, NULL))
                 //Error code
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

joerg

  • Guest
Re: Vertical text
« Reply #4 on: June 13, 2007, 12:13:23 PM »
@frankie

I tried to use some parts of your function, but I always get a Linker-Error
Unresolved external CreateFontW

Should I include special headers or what can I do ?

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: Vertical text
« Reply #5 on: June 13, 2007, 03:23:11 PM »
Sorry it's my fault :-[
I tested the routine under XP, but under wince you must use CreateFontIndirect.
This is the correct code for WINCE:
Code: [Select]
/*-@@+@@--------------------------------[Do not edit manually]------------
 Procedure: DrawRotatedText
 Created  : Thu May 10 19:26:35 2007
 Modified : Wed Jun 13 15:29:21 2007

 Synopsys : Draws rotated text
 Input    : hDC  : pointer to your device-context
            str  : the text
            x    : x start position
            y    : y start position
            Opts : see documentation of ExtTextOut for more details
            sFont: Font name or NULL to use default
 Output   : TRUE if success.
 Errors   : None
 ------------------------------------------------------------------@@-@@-*/
BOOL DrawRotatedText(HDC hDC, const wchar_t *str, UINT x, UINT y, float angle, UINT Opts, LPCWSTR sFont)
{
HFONT hFont, hOldFont;
LOGFONT sLogFont;

//Init structure
sLogFont.lfHeight = 0;
sLogFont.lfWidth  = 0;
sLogFont.lfEscapement = angle * 10.0;
sLogFont.lfOrientation = angle * 10.0;
sLogFont.lfWeight = FW_DONTCARE;
sLogFont.lfItalic = FALSE;
sLogFont.lfUnderline = FALSE;
sLogFont.lfStrikeOut = FALSE;
sLogFont.lfCharSet = DEFAULT_CHARSET;
sLogFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
sLogFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
sLogFont.lfQuality = DEFAULT_QUALITY;
sLogFont.lfPitchAndFamily = DEFAULT_PITCH;
if ( !sFont )
sLogFont.lfFaceName[0] = '\0';
else
wcscpy(sLogFont.lfFaceName, sFont);

hFont = CreateFontIndirect(&sLogFont);

if (!hFont)
return FALSE;

hOldFont = SelectObject(hDC, hFont);

ExtTextOutW(hDC, x, y, Opts, NULL, str, wcslen(str), NULL);

hOldFont = SelectObject(hDC, hOldFont);
DeleteObject(hFont);

return TRUE;
}
« Last Edit: June 13, 2007, 03:31:29 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

joerg

  • Guest
Re: Vertical text
« Reply #6 on: June 13, 2007, 07:43:22 PM »
@frankie

many thanks... now it works  :)