-1

I compiled this code without any errors and warnings with GCC mingw32:

#define WINVER 0x0500
#include <windows.h>
HWND StdHandle = GetStdHandle (STD_OUTPUT_HANDLE);
CONSOLE_FONT_INFO GETFONT;
GetCurrentConsoleFont (StdHandle, FALSE, &GETFONT);
if (GETFONT.dwFontSize.X != 8 || GETFONT.dwFontSize.Y != 12)
  printf ("Font-Size is not 8 x 12");
else
  printf("Font-Size is 8 x 12");

It runs perfectly in Windows 7.

But, when it runs in Windows XP and I adjust the console font-size to "8 x 12", GETFONT.dwFontSize.X always equals 80, and GETFONT.dwFontSize.Y always equals 25.

Then, I added

DWORD ErrorCode = GetLastError();

And, it returns 0x0 (ERROR_SUCCESS(:The operation completed successfully.))

Why it always get the wrong value when using GetCurrentConsoleFont in Windows XP?

Kevin Dong
  • 5,001
  • 9
  • 29
  • 62
  • 1
    You are not performing any error checking at all. You need to check the return values of the functions you call. Don't just call `GetLastError`. Only do that if the MSDN docs say it has meaning. – David Heffernan Nov 21 '13 at 12:44
  • 1
    OT (?): To target XP use: `#define WINVER 0x0501`! `0x0500` is Windows 2000, for which `GetCurrentConsoleFont()` is **not** available. For reference: http://msdn.microsoft.com/en-us/library/vstudio/6sehtctf.aspx – alk Nov 21 '13 at 15:33
  • @alk THX for your advice about changing `0x0500` to `0x0501`. And I have just found the perfect solution to this problem: – Kevin Dong Nov 21 '13 at 15:38
  • Did this change solved the issue? – alk Nov 21 '13 at 15:39
  • @alk Not but Thanks. Where can I post the solution to this issue? Is it suitable to answer my own question? – Kevin Dong Nov 21 '13 at 15:45

2 Answers2

2

MSDN documentation on CONSOLE_FONT_INFO says

To obtain the size of the font, pass the font index to the GetConsoleFontSize function.

Erdogan Kurtur
  • 3,630
  • 21
  • 39
  • But, why cannot just use `GetCurrentConsoleFont()`? It also can get the value of font-size by passing a pointer to a CONSOLE_FONT_INFO structure. Doesn't it? – Kevin Dong Nov 21 '13 at 12:55
  • I don't know. Interesting thing is that both pages for `CONSOLE_FONT_INFO` and `GetConsoleFontSize` says the exact same thing. You can open a ticket on MS connect or you can just write an if and call it a day. – Erdogan Kurtur Nov 21 '13 at 12:58
-1
BOOL WINAPI GetCurrentConsoleFont (HANDLE hConsoleOutput, BOOL bMaximumWindow, PCONSOLE_FONT_INFO lpConsoleCurrentFont);
COORD WINAPI GetConsoleFontSize (HANDLE hConsoleOutput, DWORD nFont);

CONSOLE_FONT_INFO GETFONT;
GetCurrentConsoleFont (StdHandle, FALSE, &GETFONT);
COORD Fontsize = GetConsoleFontSize (StdHandle, GETFONT.nFont);
SHORT Font_X = Fontsize.X;
SHORT Font_Y = Fontsize.Y;
if (Font_X != 8 || Font_Y != 12)
{
     printf ("Font-Size is not 8 x 12");
}
Kevin Dong
  • 5,001
  • 9
  • 29
  • 62
  • Please try to explain this rather than just throwing code at us. Why have you added this when it seems to offer nothing more than edokan's answer? And please do heed what I said about error checking. You don't do any! – David Heffernan Nov 21 '13 at 15:53
  • @David Heffernan Oh, I know that. But..., I just have learned C Language for 1 month, and Windows API for 3 days. And...I am just a 13-year-old student. Please give me several days to enhance the quality of this code. Thanks for your advice about error checking – Kevin Dong Nov 21 '13 at 15:58