|
|
Comments and Discussions
|
|
 |

|
Hi, Alexandru Matei.
I've been read and reread your article.
It's very helpful to my programming.
But I have a question:
In your demo project, how do you process unicode characters (for example written in Japanese)?
How can I get a text width written in Japanese or Chinese?
Can you give me the answer?
Please help me as soon as possible.
Hope your reply.
Best regards.
modified on Monday, May 19, 2008 8:25 AM
|
|
|
|

|
Hello,
- I would need a screen capture of the application running on your computer.
- Unfortunately I can't do a test myself using the two fonts you've mentioned above (PMingLiU, Calibri)
- It is possible that the algorithm described in the article fails for Japanese characters, I can't say.
|
|
|
|

|
I read your article.
Your article is useful.
I'm a VC++ programming beginner.
I need your advice on a personal matter.
I understood your program.
But I have a questions.
If I click the "Font to use" button , and then select the name, size, style of Font.
I guess your program will send this information to LOGFONT structure.
At that time, You will change the font size to tmheight of LOGFONT.
How do you translate the information?
If you select the font name "Arial", font size "17", what value is tmheight ?
help me.
|
|
|
|

|
I have been read your article.
It's interesting.
I need your advice on a personal matter.
I'm a VC++ Programming Beginner.
In your Project Demo, If I click the "Font to Use" button, FontSelection Dialog is appeared.
I select the name, size , color of Font, and then click "OK" button.
I guess that your program do mapping the font information to LOGFONT struct.
And then will use the GetTextExtentpoint32 function.
Here's my questions.
1. How do you send the Font information to LOGFONT struct?
2. What relation is between the Font size and tmheight of LOGFONT.
My master gives a Task.
I have to carry my Task.
help me.
Regard.
|
|
|
|

|
1) Declare a LOGFONT structure
LOGFONT logfOut;
Then initialize the fields of the structure with some values:
logfOut.lfHeight = -21;
logfOut.lfWidth = 0;
logfOut.lfEscapement = 0;
logfOut.lfOrientation = 0;
logfOut.lfWeight = FW_NORMAL;
logfOut.lfItalic = (BYTE)1;
logfOut.lfUnderline = (BYTE)0;
logfOut.lfStrikeOut = (BYTE)0;
logfOut.lfCharSet = (BYTE)DEFAULT_CHARSET;
logfOut.lfOutPrecision = (BYTE)OUT_STROKE_PRECIS;
logfOut.lfClipPrecision = (BYTE)CLIP_STROKE_PRECIS;
logfOut.lfQuality =(BYTE)DRAFT_QUALITY;
logfOut.lfPitchAndFamily =(BYTE)34;
lstrcpyn(logfOut.lfFaceName, _T("Tahoma"), sizeof(logf.lfFaceName));
2) Then open the font selection common dialog box
CHOOSEFONT cf;
ZeroMemory(&cf,sizeof(cf));
cf.lStructSize = sizeof(CHOOSEFONT);
cf.hwndOwner = hWndParent;
cf.hDC = NULL;
cf.lpLogFont = &logfOut; //the selected font LOGFONT structure
cf.iPointSize = 0;
cf.Flags = CF_ENABLEHOOK|CF_INITTOLOGFONTSTRUCT|CF_EFFECTS|CF_SCREENFONTS;
cf.rgbColors = rgbInit; //the selected font color
cf.lCustData = (LPARAM)szTitle ;
cf.lpfnHook = ChooseFontHookProc ;
cf.lpTemplateName = NULL;
cf.hInstance = NULL;
cf.lpszStyle = NULL;
cf.nFontType = 0;
cf.nSizeMin = 0;
cf.nSizeMax = 0;
BOOL result = ChooseFont(&cf);
if(!result)
{
return FALSE;
}
UINT CALLBACK ChooseFontHookProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_INITDIALOG)
{
CHOOSEFONT* pCf = (CHOOSEFONT*)lParam;
if(pCf->lCustData)
SetWindowText(hDlg, (TCHAR*)(pCf->lCustData));
return 1;
}
return 0;
}
You can see that before and after calling ChooseFont, the font information is set in the cf.lpLogFont field, which is a pointer to a LOGFONT structure.
cf.lpLogFont = &logfOut;
When you close the dialog box with OK, you'll automatically have the selected font in the same cf.lpLogFont variable and the selected font color in cf.rgbColors;
3)
TEXTMETRIC tm;
HFONT hFontOld = (HFONT)SelectObject(hDC,hFont);
GetTextMetrics(hDC, &tm);
int cyChar = tm.tmHeight+tm.tmExternalLeading;
int cxChar = tm.tmAveCharWidth;
SelectObject(hDC,hFontOld);
After you select a font in a device context, you can obtain TEXTMETRIC information. The fields of the TEXTMETRIC structure and their meaning can be found in the MSDN and by searching the web.
Read the "Programming Windows" book, written by Charles Petzold:
http://www.microsoft.com/mspress/books/sampchap/2344a.aspx[^]
modified on Saturday, May 17, 2008 2:29 AM
|
|
|
|

|
This is a similar to what I have just coded. (I just submited an article). I wanted to draw vertical text for which there are no magic getCharABCD() functions. My approach was to create a bitmap large enough for a single character and scan the bits. i.e. not use GetPixel(). When the mods have finished with my article you will see what I mean.
|
|
|
|

|
As I see, the width of text string in italic font is usually a bit wider than "sizeText.cx". But the difference is not more than width of the ending character.
Rectangle of width "2*sizeText.cx" seems to be too large and brings more works to do.
|
|
|
|

|
- Yes, that's correct.
- I will modify the right-limit of the rectangle to be scanned to just 'sizeText.cx+widthOfTheLastCharacter'.
Like this:
SIZE sizeLastCharacter;
GetTextExtentPoint32(hMemDC, &szText[-1+lstrlen(szText)], 1, &sizeLastCharacter);
RECT rect={0,0,sizeText.cx+sizeLastCharacter.cx,sizeText.cy}; Thank you very much.
|
|
|
|

|
An improvement would be to do a SetBkColor first, then search for that color on a horizontal line only.
|
|
|
|

|
Please give more details about your idea. What do you mean by "a horizontal line only" and how would it be possible?
In what conditions did you find the calculation is not efficient enough?
The article is also presenting how to use the GetCharABCWidthsFloat with references to MSDN articles.
Feel free to choose whatever method you prefer.
-- modified at 16:16 Wednesday 16th August, 2006
|
|
|
|
 |
|
|
General News Suggestion Question Bug Answer Joke Rant Admin
|
Shows an alternative to GetCharABCWidths and GetCharABCWidthsFloat.
| Type | Article |
| Licence | |
| First Posted | 24 Jul 2006 |
| Views | 74,564 |
| Bookmarked | 28 times |
|
|