Problem mit GetTextExtentPoint32
-
Hi,
ich will die Laenge eines strings in Pixeln oder Koordinatenpunkten berechnen. Dabei bin ich auf GetTextExtentPoint32 gestossen, die eine size struct zurueck gibt.
1. Jedoch schien die size struct nur eine Laenge zurueck geben, die sich am max. Index eines arrays orientiert. Ich muss aber nur die Laenge der belegten Plaetze haben.
2. Wenn ich die size struct auf dem screen ausgegeben habe, & das Fenster neu gezeichnet habe, dann hat sich somit auch die size struct ausgabe geaendert, wobei ich jedoch nicht verstehe, wie das im Zusammenhang stehen koennte.
3. Wenn die Schrift groesser wird, reagiert die size struct nicht daraufEs waere nett, wenn ihr mich dort ein wenig aufklaeren koenntet.
Oder kennt jemand eine andere Loesungsmethode? Wie gesagt brauche ich die Koordinaten eines strings, damit ich es innerhalb eines Fensters positionieren kann. Jedoch darf der string nicht ueber den Rand hinauslaufen, also: stringlaenge < FensterrahmenX - stringstartX.
Danke im Vorraus.
-
Hast Du Dir das Beispiel hier mal angeschaut?
Drawing Text from Different Fonts on the Same Line
http://msdn.microsoft.com/library/en-us/gdi/fontext_8f6t.asp
-
Danke, aber irgendwie ist das Problem immer noch vorhganden. Vielleicht ist es ein allgemeines problem, & hat weniger etwas mit der FUnktion zu tun. ich versuch' das Problem nochmal kurz zu schildern: die Stringlaenge, bei GettextExtentPoint32 als sizestruct, bei GetTabbedTextExtent als DWORD, varriert die Angabe, wenn beispielsweise das Fenster maximiert wird, die Angabe ueber den Rand hinaus gezoigen wird, oder man die Fenstergroesser veraendert. Wenn beispielsweise beim Programm Start eine 136 steht, dann steht nach einer Maximierung bei einer CreateFont Schriftgroesse von 22 eine 172 da, wenn die Zahl jetzt ueber den Monitor Rand gezigen wird oder die Fenstergroesser veraendert wird eine 140. Meine Frage: Wie kommt das?
So sieht die Prozedur aus mit GetTextExtentPoint32:
char s[22]; SIZE sSize; HFONT hFont; HDC hDC; PAINTSTRUCT ps; hDC = BeginPaint(hWnd, &ps); hFont = CreateFont(22,0,0,0,700,0,0,0,0,3,2,1,0x22,"Arial"); SelectObject(hDC,hFont); GetTextExtentPoint32(hDC,s, strlen(s),&sSize); sprintf(s,"%i",sSize.cx); TextOut(hDC,1,1,s,strlen(s)); EndPaint(hWnd, &ps);
So mit GetTabbedText:
char s[22]; HFONT hFont; HDC hDC; PAINTSTRUCT ps; DWORD sWidth = 0; hDC = BeginPaint(hWnd, &ps); sWidth = LOWORD(GetTabbedTextExtent(hDC,s2,strlen(s2),NULL,NULL)); hFont = CreateFont(22,0,0,0,700,0,0,0,0,3,2,1,0x22,"Arial"); SelectObject(hDC,hFont); sprintf(s,"%i",sWidth); TextOut(hDC,1,1,s,strlen(s)); EndPaint(hWnd, &ps);
Das Problem tritt bei beiden Funtkionen auf.
& hier nochmal die MSDN Beschreibung beider Funktionen:
GetTextExtentPoint32
The GetTextExtentPoint32 function computes the width and height of the specified string of text.BOOL GetTextExtentPoint32(
HDC hdc, // handle to device context
LPCTSTR lpString, // pointer to text string
int cbString, // number of characters in string
LPSIZE lpSize // pointer to structure for string size
);Parameters
hdc
Handle to the device context.
lpString
Pointer to the string of text. The string does not need to be zero-terminated, since cbString specifies the length of the string.
cbString
Specifies the number of characters in the string.
lpSize
Pointer to a SIZE structure in which the dimensions of the string are to be returned.GetTabbedTextExtent
The GetTabbedTextExtent function computes the width and height of a character string. If the string contains one or more tab characters, the width of the string is based upon the specified tab stops. The GetTabbedTextExtent function uses the currently selected font to compute the dimensions of the string.DWORD GetTabbedTextExtent(
HDC hDC, // handle to device context
LPCTSTR lpString, // pointer to character string
int nCount, // number of characters in string
int nTabPositions, // number of tab positions
LPINT lpnTabStopPositions // pointer to array of tab positions
);Parameters
hDC
Handle to the device context.
lpString
Pointer to a character string.
nCount
Specifies the number of characters in the text string.
nTabPositions
Specifies the number of tab-stop positions in the array pointed to by the lpnTabStopPositions parameter.
lpnTabStopPositions
Pointer to an array containing the tab-stop positions, in device units. The tab stops must be sorted in increasing order; the smallest x-value should be the first item in the array.MfG
-
Was machst Denn Du hier?
char s[22]; GetTextExtentPoint32(hDC,s, strlen(s),&sSize); sprintf(s,"%i",sSize.cx);
Du musst natürlich zuerst den String "s" korrekt setzten, bevor die Du "GetTextExtentPoint32" Funktion aufrufst!
-
wenn du mit setzten initialisieren meinst: das hab ich jetzt einfach im beispielcode weggelessen. in meinem Prog steht natuerlich etwas im String.
stellt sich immernoch die Frage, wieso die Funktionen unter den genannten Vorraussetztungen immer andere Ergebnisse liefern.