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
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).
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?
Your solution is a good one.
If you want to write text with any angle use this function that I wrote for my use:
/*-@@+@@--------------------------------[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:
if (!DrawRotatedText(ps.hdc,L"Dose", 50, 100, 45, 0, NULL))
//Error code
@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 ?
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:
/*-@@+@@--------------------------------[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;
}
@frankie
many thanks... now it works :)