You are not logged in.

  • Login

1

Saturday, March 19th 2011, 8:19pm

C Konsolenschriftgröße ändern

Hallo Leute,

ich hab da mal eine Frage,

Ist es irgendwie möglich, die Schriftgröße (und vielleicht auch die Schriftart) der Windows-Konsole im C-Programm selbst zu ändern, also nicht über die Einstellungen? (ich nehme auch C++ Code an ;) )

Wäre wirklich schön wenn das ginge :)

2

Saturday, March 19th 2011, 8:36pm

Nein, es geht leider nicht.

Du kannst aber tricksen und dir die Buchstaben "bauen".
Also wie ich hier jetzt ein D baue:

C/C++ Quellcode

1
2
3
4
5
//**
//*   *
//*      *
//*   *
//**

Das machst du dann über cout oder printf und fertig.
Ich empfehle für Sonderzeichen "(unsigend char)ASCII-NUMMER" nutzen. Also zum Beispiel würde dann

C/C++ Quellcode

1
cout << (unsigned char)178;

das "▓" generieren.

So könntest du dann so was bauen:
▓ ......... ▓
..... ▓
▓ ......... ▓
. ▓▓▓▓▓▓
Denk dir die Punkte mal weg. :D

MfG
Check

3

Saturday, March 19th 2011, 8:40pm

Nagut, so gehts auch :)

Aber wenn ich mir die Buchstaben selber baue ist es wieder zu groß ;)

Soll hald nur ne Spur größer sein^^ und nicht so Schriftgröße 40 oder noch ärger :P

4

Saturday, March 19th 2011, 8:44pm

Sorry, hab eben einfach drauf los geantwortet. Es klappt. ;)
Hier mal ein Beispiel des "Hello World" Programms in C++.

C/C++ Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <windows.h> 
#include <iostream> 
 
using namespace std; 
 
PCONSOLE_FONT_INFOEX testFont; 
 
int main() 
{ 
     HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    testFont->dwFontSize.X = 10; 
    testFont->dwFontSize.Y = 20; 
 
       SetCurrentConsoleFontEx(hOut, TRUE, testFont); 
 
       cout<<"Hello World"<<endl; 
 
       getchar();
       return 0; 
}


MfG
Check

PS: Farbe usw. sind auch so ziemlich alle änderbar. Sonst könnte man ja die CMD in Windows auch nicht verändern, zwar so ziemlich nicht das Gleiche, aber zur Veranschaulichung... ;) Lässt sich nämlich schön in Windows ändern.

5

Saturday, March 19th 2011, 8:50pm

ne, da spuckt er nur nen Error aus, ...nagut, eher 3 Errors:

Compiling: main.cpp
9: error: 'PCONSOLE_FONT_INFOEX' does not name a type
In function 'int main()':
14: error: 'testFont' was not declared in this scope
17: error: 'SetCurrentConsoleFontEx' was not declared in this scope

ahja, und das mit Farbe und so weiß ich eh,....mein neuestes Programm ist auch etwas verrückt in der Hinsicht ;) jetzt fehlt hald nurmehr das wegen der Schriftgröße
(ich brauch das Programm prinzipiell für die Schule, aber ich bin über die Angabe schon so ziemlich 300% hinausgeschossen ;) )

This post has been edited 1 times, last edit by "Gamemaster1994" (Mar 19th 2011, 8:56pm)


6

Saturday, March 19th 2011, 8:55pm

Hmm....
Ja stimmt, sorry².
Ein blick in die MSDN geworfen und der Fehler wurde gefunden. ^^

C/C++ Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <Windows.h>
#include <iostream>
 
using namespace std;
 
int main()
{
     HANDLE outcon = GetStdHandle(STD_OUTPUT_HANDLE); 
 
    CONSOLE_FONT_INFOEX font; 
    font.cbSize=sizeof(CONSOLE_FONT_INFOEX); 
    GetCurrentConsoleFontEx(outcon, false, &font); 
    font.dwFontSize.X = 10; 
    font.dwFontSize.Y = 10; 
    SetCurrentConsoleFontEx(outcon, false, &font);
 
   cout << "Hallo Welt!" << endl;
 
   getchar();
   return(0);
}

//TESTED


MfG
Check

7

Saturday, March 19th 2011, 8:59pm

gut, dann liegt das wohl an meinem Compiler, weil irgendwie kommen jetzt noch mehr Errors...
er nimmt nämlich das CONSOLE_FONT_INFOEX irgendwie nicht :(

8

Saturday, March 19th 2011, 9:03pm

Bitte?
Ich habe das eben selbst ausprobiert!
Was benutzt du denn für einen Compiler?
Ich benutze momentan Microsoft Visual C++ 2010 Express...

MfG
Check

9

Saturday, March 19th 2011, 9:07pm

gut, da liegt das Problem ;)

aber den Fehler den ich gerade gesagt hab hab ich schon gelöst, jetzt muss ich schaffen, dass er "Set(und Get)CurrentConsoleFontEx" nimmt

10

Saturday, March 19th 2011, 9:12pm

Unomomento!
Was nutzt du denn jetzt für einen Compiler? Gcc? Der Code ist nämlich C++. ;D

MfG
Check

11

Saturday, March 19th 2011, 9:15pm

dass das C++ ist hab ich auch schon bemerkt ;)
und ja GCC

12

Saturday, March 19th 2011, 9:17pm

Denk mal da wird es am Compiler liegen.
Da kann ich jetzt aber leider nicht mehr helfen, da ich ausschließen MVC2010E nutze, war auch mal beim GCC aber der ist nicht so meines.... :D

MfG
Check

13

Saturday, March 19th 2011, 9:20pm

muss noch eins dazu sagen,...ich wette dass es sicherlich an einer inkompletten Bibliothek liegt, kenne ich nämlich schon...meine Lösung war: meine eigene Bibliothek schreiben mit dem was ich noch dazu brauchte......quelle für die teile der Bibliothek: Google; nur hier find ich nichts -.-

14

Saturday, March 19th 2011, 9:24pm

Hast du ganz sicher Windows.h als erstes drin?
Manchmal liegt es auch daran. :rolleyes:

MfG
Check

15

Saturday, March 19th 2011, 9:30pm

jaja, hab isch :)

mittlerweile bin ich bei dem hier:

C/C++ Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <Windows.h>
#include <iostream>
 
using namespace std;
typedef struct _CONSOLE_FONT_INFOEX {
  ULONG cbSize;
  DWORD nFont;
  COORD dwFontSize;
  UINT  FontFamily;
  UINT  FontWeight;
  WCHAR FaceName[LF_FACESIZE];
} CONSOLE_FONT_INFOEX, *PCONSOLE_FONT_INFOEX;
 
int main()
{
     HANDLE outcon = GetStdHandle(STD_OUTPUT_HANDLE);
 
    CONSOLE_FONT_INFOEX font;
    font.cbSize=sizeof(CONSOLE_FONT_INFOEX);
    GetCurrentConsoleFontEx(outcon, false, &font);
    font.dwFontSize.X = 25;
    font.dwFontSize.Y = 25;
    SetCurrentConsoleFontEx(outcon, false, &font);
 
   cout << "Hallo Welt!" << endl;
   getch();
   return(0);
}


Nur wie schon gesagt, nurmehr die Definition von GetCurrentConsoleFontEx und SetCurrentConsoleFontEx dann passt es^^
oder hab ich schon wieder irgendwo Blödsinn gebaut?

16

Saturday, March 19th 2011, 9:47pm

gib mir deine windows.h ;)

17

Saturday, March 19th 2011, 10:02pm

Nein, das dürfte eher an deinem OS liegen. Ich meine GetCurrentConsoleFontEx ist erst ab Vista+ vorhanden bzw. verwendbar. Hab ich mal gelesen.

MfG
Check

18

Saturday, March 19th 2011, 10:21pm

hmmmm, ich glaub weniger dass das an meinem System liegt,...bei Windows 7 Ultimate ;)

19

Saturday, March 19th 2011, 10:50pm

C/C++ Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <windows.h> 
#include <iostream> 
 
using namespace std;
 
typedef struct CONSOLE_FONT { 
  DWORD index; 
  COORD dim; 
} *PCONSOLE_FONT; 
 
typedef BOOL  (WINAPI *GetConsoleFontInfoFunc)(HANDLE,BOOL,DWORD,PCONSOLE_FONT); 
typedef COORD (WINAPI *GetConsoleFontSizeFunc)(HANDLE, DWORD); 
typedef BOOL  (WINAPI *GetCurrentConsoleFontFunc)(HANDLE, BOOL, PCONSOLE_FONT); 
typedef DWORD (WINAPI *GetNumberOfConsoleFontsFunc)(); 
typedef BOOL  (WINAPI *SetConsoleFontFunc)(HANDLE, DWORD); 
 
GetConsoleFontInfoFunc pGetConsoleFontInfo; 
GetConsoleFontSizeFunc pGetConsoleFontSize; 
GetCurrentConsoleFontFunc pGetCurrentConsoleFont; 
GetNumberOfConsoleFontsFunc pGetNumberOfConsoleFonts; 
SetConsoleFontFunc pSetConsoleFont; 
 
PCONSOLE_FONT fonts = NULL; 
 
 
int GetAvailableFonts(HANDLE hCon,PCONSOLE_FONT *fonts) { 
    int fontcount = pGetNumberOfConsoleFonts(); 
    *fonts = new CONSOLE_FONT[fontcount]; 
    pGetConsoleFontInfo(hCon,0,fontcount,*fonts); 
 
    return fontcount; 
} 
 
BOOL SetFont(HANDLE hCon,int index) { 
    if (!pSetConsoleFont(hCon,index)) { 
        return FALSE;    
    } 
    return TRUE; 
} 
 
BOOL Init() { 
    HINSTANCE hLib = NULL; 
    BOOL bRet = TRUE; 
 
    hLib = LoadLibrary("KERNEL32.DLL"); 
 
    if (hLib == NULL) { 
        return FALSE; 
    } 
 
    pGetConsoleFontInfo = (GetConsoleFontInfoFunc)GetProcAddress(hLib,"GetConsoleFontInfo"); 
    pGetConsoleFontSize = (GetConsoleFontSizeFunc)GetProcAddress(hLib,"GetConsoleFontSize"); 
    pGetCurrentConsoleFont = (GetCurrentConsoleFontFunc)GetProcAddress(hLib,"GetCurrentConsoleFont"); 
    pGetNumberOfConsoleFonts = (GetNumberOfConsoleFontsFunc)GetProcAddress(hLib,"GetNumberOfConsoleFonts"); 
    pSetConsoleFont = (SetConsoleFontFunc)GetProcAddress(hLib,"SetConsoleFont"); 
 
    return bRet; 
} 
 
 
int main() { 
    PCONSOLE_FONT fonts = NULL; 
 
    HANDLE hConsole = GetStdHandle( STD_ERROR_HANDLE ); 
 
    Init(); 
 
    cout << "Hello World!" << endl;
 
    getchar();
    return(0);
}


Das sollte denke ich auch auf dem GCC laufen!

MfG
Check

20

Saturday, March 19th 2011, 11:08pm

dass das soviel wird hätte ich mir nicht gedacht :S

aber DANKE!

▓▓.........▓▓
................
......▓▓.......
......▓▓.......
▓.............▓
▓▓.........▓▓
..▓▓▓▓▓▓▓..

jetzt muss ich mich in dem code nurmehr soviel auskennen um ihn zu verwenden ;)

Similar threads

Social bookmarks