Setting Console Font in C on Windows 10 - c

I am trying to change the size of console font in my console application during runtime.
Looking into the Windows documentation, I found the following functions:
GetCurrentConsoleFont (and GetCurrentConsoleFontEx)
and SetCurrentConsoleFontEx.
CONSOLE_FONT_INFO prevFontInfo = {sizeof(prevFontInfo)};
GetCurrentConsoleFont( GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &prevFontInfo
);
CONSOLE_FONT_INFO fontInfo = {0};
//fontInfo.cbSize = sizeof(prevFontInfo);
fontInfo.dwFontSize.X = 30;
fontInfo.dwFontSize.Y = 70;
//fontInfo.FontWeight = 700;
SetCurrentConsoleFontEx( GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &fontInfo );
... (Some Irrelevant Code Here)
SetCurrentConsoleFontEx( GetStdHandle(STD_OUTPUT_HANDLE), FALSE,
&prevFontInfo );
Currently I am getting the following warnings:
implicit declaration of function 'GetCurrentConsoleFont'
and
implicit declaration of function 'SetCurrentConsoleFont'
I tried using GetConsoleFontEx (the reason FontWeight and cbSize are commented out) to no prevail. I used CONSOLE_FONT_INFOEX as well, which resulted in the following error:
Unkown type name 'CONSOLE_FONT_INFOEX'
I read that it is necessary to use the following:
#define _WIN32_WINNT 0x0500
I also tried several variations of this (0x0502, 0x0600, ect.), but nothing seemed to change the warnings/errors.
I include the following windows headers, and I compile with -lkernel32.
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <Wincon.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
I wish to change the font size in C for the Windows terminal at runtime.
How do I do that? Why is this not working?

Related

#include <windows.h> causes errors

I'm trying to use GetTickCount() from the windows.h header file. Right now my code looks like this:
#include <stdio.h>
#include <string.h>
#include "declarations.h"
#define INPUTBUFFER 400 * 6
#define START_POS "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
#include <windows.h>
void interface() {
.....
.....
}
The compiler returns 2 errors:
Expected '{' before '(' token (pointing to the '(' after "interface"
2 or more data types in declaration specifiers (pointing to "interface")
When I comment out #include < windows.h >, the interface function works just fine. I don't even have GetTickCount() in my code yet. I checked the gcc path to make sure windows.h is there, so I'm not sure why I'm getting this error.
As RbMm points out in a comment, the windows headers contain "#define interface struct", so the compiler sees your code as "void struct() { ....}" which is not valid C++
You need to either rename your function (probablly the better option) or add a "#undef interface" before your function.

How to change console program for unicode support in windows?

The following program can be compiled using msvc or mingw. However, the mingw version cannot display unicode correctly. Why? How can I fix that?
Code:
#include <stdio.h>
#include <windows.h>
#include <io.h>
#include <fcntl.h>
int wmain(void)
{
_setmode(_fileno(stdout), _O_U16TEXT);
_putws(L"哈哈哈");
system("pause");
return 0;
}
Mingw64 Compile Command:
i686-w64-mingw32-gcc -mconsole -municode play.c
MSVC Compiled:
Mingw Compiled:
Edit:
After some testing, the problem seems not causing by mingw. If I run the program directly by double clicking the app. The unicode string cannot be displayed correct either. The code page however, is the same, 437.
It turns out the problem is related to console font instead of the compiler. See the following demo code for changing console font.
This is happening because of missing #define UNICODE & #define _UNICODE . You should try adding it along with other headers. The _UNICODE symbol is used with headers such as tchar.h to direct standard C functions such as printf() and fopen() to the Unicode versions.
Please Note - The -municode option is still required when linking if Unicode mode is used.
After doing some research, it turns out the default console font does not support chainese glyphs. One can change the console font by using SetCurrentConsoleFontEx function.
Demo Code:
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <windows.h>
#define FF_SIMHEI 54
int main(int argc, char const *argv[])
{
CONSOLE_FONT_INFOEX cfi = {0};
cfi.cbSize = sizeof(CONSOLE_FONT_INFOEX);
cfi.nFont = 0;
cfi.dwFontSize.X = 8;
cfi.dwFontSize.Y = 16;
cfi.FontFamily = FF_SIMHEI;
cfi.FontWeight = FW_NORMAL;
wcscpy(cfi.FaceName, L"SimHei");
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
/* UTF-8 String */
SetConsoleOutputCP(CP_UTF8); /* Thanks for Eryk Sun's notice: Remove this line if you are using windows 7 or 8 */
puts(u8"UTF-8你好");
/* UTF-16 String */
_setmode(_fileno(stdout), _O_U16TEXT);
_putws(L"UTF-16你好");
system("pause");
return 0;
}

Why does the agwrite function in the cgraph library unexpectedly fail on any config/platform but Win64 release?

I've been trying to get cgraph (https://graphviz.gitlab.io/_pages/pdf/cgraph.pdf) working so I read and write some graph files. I tried writing some very basic code:
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <memory.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>
#include <mysql.h>
#include <graphviz/cgraph.h>
int main() {
FILE *fp = NULL;
fp = fopen("test.dot", "w+");
if (fp == NULL) {
return -1;
}
Agraph_t *g;
g = agopen("test", Agdirected, NULL);
Agnode_t *signal1;
signal1 = agnode(g, "Signal1_ON", TRUE);
Agnode_t *signal2;
signal2 = agnode(g, "Signal1_OFF", TRUE);
Agedge_t *link = agedge(g, signal1, signal2, "link1", TRUE);
agattr(g, AGEDGE, "label", "transitionlink");
agwrite(g, fp);
fclose(fp);
system("pause");
return 0;
}
What should be happening is that the file should be written to test.dot. This code works perfectly fine on Win64 release, but fails on Win64 debug, Win32 debug, and Win32 release. I have double checked the .lib files and .dll files settings in visual studio and in the file directories, making sure to copy the release and debug versions of each platform correctly. However, the agwrite keeps causing a "Microsoft Visual Studio C Runtime Library has detected a fatal error" crash on Win64 debug, Win32 debug, and Win32 release. The weird thing is if I change
agwrite(g, fp); to agwrite(g, stdout);, the code works on all platforms/configurations. I am so confused why this is happening. Here is the source file which contains the code for agwrite if that helps: https://github.com/ellson/MOTHBALLED-graphviz/blob/master/lib/cgraph/write.c
I cannot debug the issue because the source has been compiled into .dlls, and .libs for each platform/configuration.
I appreciate any suggestions/feedback,
Thank you
Edit:
For anyone godly enough to try and get this working on their own system, here are all my binaries, libs, and include files: https://www.dropbox.com/sh/o9tjz7txu4m0k5q/AAAnp8Wu99q9IsFN7kvqZP7Ta?dl=0
Edit 2:
The compiler I am using is MSVC 14 on Windows 10.
I found out that using cgraph directly results in an error when trying to use agwrite(). The solution is to use the GVC abstraction layer which comes with the Graphviz C API to do file I/O. Here is the code that worked:
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <memory.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>
#include <mysql.h>
#include <graphviz/gvc.h>
int main() {
GVC_t *gvc;
gvc = gvContext();
Agraph_t *g;
g = agopen("test", Agdirected, NULL);
Agnode_t *signal1;
signal1 = agnode(g, "Signal1_ON", TRUE);
Agnode_t *signal2;
signal2 = agnode(g, "Signal1_OFF", TRUE);
Agedge_t *link = agedge(g, signal1, signal2, "link1", TRUE);
agattr(g, AGEDGE, "label", "transitionlink");
gvLayout(gvc, g, "dot");
gvRenderFilename(gvc, g, "dot", "test.dot");
gvFreeLayout(gvc, g);
agclose(g);
gvFreeContext(gvc);
system("pause");
return 0;
}
Edit:
Here is the documentation for GVC: https://graphviz.gitlab.io/_pages/pdf/gvc.3.pdf
The reason of crashing is described on official Graphviz site:
This usually happens when the Graphviz library is built using one version of the stdio library, and the user’s program is compiled using another. If the FILE structure of stdio is different, the call to agread() will cause a crash. This is mainly a problem on Windows where we just provide a binary release built with one version of Visual Studio and stdio changes depending on the version of Visual Studio. It can also occur if the user tries to use cygwin or something similar which may also use an incompatible stdio.
https://graphviz.org/faq/#FaqAgreadCrash

How to hide the console window of a C program?

I've been looking around but I couldn't find the solution to my problem, even with some supposedly solved problems that resemble mine.
I want to hide the console window when my C program runs.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#define _WIN32_WINNT 0x0500
int main(){
HWND hWnd = GetConsoleWindow();
ShowWindow( hWnd, SW_MINIMIZE ); //won't hide the window without SW_MINIMIZE
ShowWindow( hWnd, SW_HIDE );
}
This is what I tried but the compiler gives me
initialization makes pointer from integer without a cast
and the fatal one which actually stops the compiling:
undefined reference to 'GetConsoleWindow'
PS: I've checked wincon.h and the GetConsoleWindow() function is defined.
Your
#define _WIN32_WINNT 0x0500
(which is needed to use GetConsoleWindow - see the documentation) must be before
#include <windows.h>
That #define is used by windows.h to know which version of Windows you are targeting (and thus which declarations it has to provide/which additional fields it has to add to structures/other magic that may be related to that linker error); if you define it after you include windows.h it will be useless.

Initializer is not a constant error

sorry if this has been asked over and over but i just don't get what's wrong with this C code, since it was compiling with no problems until someday it started complaining about "C2009: Initializer is not a constant" in lines 9 and 10 of this header:
// CONIO2.H
#ifndef CONIO2_H_INCLUDED
#define CONIO2_H_INCLUDED
#ifndef _WINDOWS_
#include <windows.h>
#endif
void clrscr(void) {
int Written = 0;
COORD ord;
ord.X = 0;
ord.Y = 0;
FillConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), 32, 80 * 25, ord, &Written);
}
//MAIN.C
#include <stdio.h>
#include <conio2.h>
Edit: I found the error. I was using the Eclipse CDT plugin for developing C applications but it wasn't setting up the path correctly. I had to play with the configs to make it work, but thanks anyways!
COORD ord;
You are missing the definition of the COORD type alias. My guess is you are not including the right header: Wincon.h
See here for the requirements (the header to include) to use the COORD type alias:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682119(v=vs.85).aspx
EDIT: moreover you seems to have an issue with your #ifndef directives: there are two #ifndef but only one #endif in your header. For each #ifndef you need an #endif. And are you sure you want to include windows.h only when _WINDOWS_ is not defined?
This link describes the error along-with some examples. It may help you. As per the link, the compiler initializes non-automatic variables at the start of the program and the values they are initialized with must be constant. http://msdn.microsoft.com/en-us/library/t801az8a(v=vs.80).aspx
Your code has several pre-processor oddities. The below code works well on a standard C compiler for Windows. Please note that VC++ is not a standard C compiler, so it could toss all kinds of strange errors at you.
// CONIO2.H
#ifndef CONIO2_H_INCLUDED
#define CONIO2_H_INCLUDED
#include <windows.h>
void clrscr (void)
{
DWORD Written = 0;
COORD ord;
ord.X = 0;
ord.Y = 0;
FillConsoleOutputCharacter (GetStdHandle(STD_OUTPUT_HANDLE),
32,
80 * 25,
ord,
&Written);
}
#endif /* CONIO2_H_INCLUDED */
//MAIN.C
#include <stdio.h>
#include "conio2.h"

Resources