In addition to the solutions provided by Richard and Espen, you can achieve this using a call to DrawText, using DT_CALCRECT as the text-drawing flag.
For single line text, just set the rect members to 0 before use.
For multi-line text, set the 'right' member to the desired width.
HDC hDC;
TEXTMETRIC textMetric;
HFONT hFont, hOldFont;
int sizeInPpoints, lineHeight;
RECT textRect;
char *buffer = "Comprimento em pixels desta string!";
hDC = GetDC(hwnd);
sizeInPpoints = MulDiv(10, GetDeviceCaps(hDC, LOGPIXELSY), 72);
hFont = CreateFont(-sizeInPpoints, 0, 0, 0, FW_NORMAL, 0, 0, 0, 0, 0, 0, 0, 0, "Courier New");
hOldFont = (HFONT)SelectObject(hDC, hFont);
GetTextMetrics(hDC, &textMetric);
lineHeight = textMetric.tmHeight;
textRect.left = textRect.right = textRect.top = textRect.bottom = 0;
DrawText(hDC, buffer, strlen(buffer), &textRect, DT_CALCRECT);
printf("Size of text calculated by DrawText: [%d x %d]\n", textRect.right, textRect.bottom);
printf("Height of text calculated by GetTextMetrics: %d\n", lineHeight);
Output:
Size of text calculated by DrawText: [280 x 16]
Height of text calculated by GetTextMetrics: 16