(VC++)(Console)(GDI) How to output the graphics in Full Screen Mode?

Hello all!

My program outputs the graphics successfully in the console window.
And I can see the white star!
But... When I press "Alt+Enter" to set the window in Full Screen Mode,
the white star is gone!
And then I press "Alt+Enter" again.
The white star is return!

How to let the white star be outputted successfully in Full Screen Mode?

Here are my C++ codes:
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*========================================
Turbo C graphics in VC++
========================================*/
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string>
using namespace std;
//for Thread
#define _MT
#include <process.h>
#pragma comment(lib, "libcmt.lib")
#pragma comment(linker, "/NODEFAULTLIB:libcd.lib")

//for GetConsoleWindow()
extern "C" WINBASEAPI HWND WINAPI GetConsoleWindow();
//global variables for Console
CONSOLE_CURSOR_INFO cInfo;
HANDLE hConOut;
COORD NewSBSize;
SMALL_RECT DisplayArea;
//Set Console Window to Maximal size
void SetConsoleMaxSize();
//Set Console Window to full screen
void SetConsoleFullScreen();

//global variables for graphic functions
HWND hWnd;
int GraphFlag = 0;
HDC hdc;
RECT rc;
POINT pt;
HPEN hNewPen, hOldPen;
HBRUSH hNewBrush, hOldBrush;
COLORREF Color;
int PenStyle;
int PenSize;

//graphic functions
void initgraph(); //Enable Graphic Mode
void closegraph(); //Disable Graphic Mode
void line(int x1, int y1, int x2, int y2); //Draw a line
void clr(); //Clear the screen

//global variables for thread
HANDLE hThread;
unsigned ThreadID;
DWORD dwExitCode;
//Thread for drawing
unsigned __stdcall DrawThread(LPVOID lp){

	initgraph();
	while(!kbhit()){ //Redraw in Console Window. And press any key to stop...

		line(100, 200, 350, 350);
		line(100, 200, 400, 200);
		line(400, 200, 150, 350);
		line(350, 350, 250, 100);
		line(150, 350, 250, 100);

		clr();

		Sleep(100); //To lower CPU usage
	} //while() End
	closegraph();

	return 1; //Return Thread Exit Code
};

int main(){
	Color = RGB(255,255,255); //default graphic color
	PenStyle = PS_SOLID; //default pen style
	PenSize = 1; //default pen size
	SetConsoleMaxSize(); //Set Console Window to Maximal size
	//SetConsoleFullScreen(); //Set Console Window to full screen


	//Create a thread and pause it
	hThread = (HANDLE)_beginthreadex(NULL, 0, DrawThread, NULL, CREATE_SUSPENDED, &ThreadID);
	//Failed to create a thread
	if(hThread == 0) printf("Failed to create DrawThread\n");
	//Get Thread Exit Code
	GetExitCodeThread(hThread, &dwExitCode);
	//Continue a thread
	ResumeThread(hThread);
	//Wait for the Thread signal
	WaitForSingleObject(hThread, INFINITE);
	//Get Thread Exit Code
	GetExitCodeThread(hThread, &dwExitCode );
	//Close Thread Handle
	CloseHandle(hThread);

	system("pause");
	return 0;
}


//Draw a line
void line(int x1, int y1, int x2, int y2){
	if(GraphFlag==1){
		hNewPen = CreatePen(PenStyle, PenSize, Color);
		hOldPen = (HPEN)SelectObject(hdc, hNewPen);
		SelectObject(hdc, hNewPen);
		MoveToEx(hdc, x1, y1, NULL);
		LineTo(hdc, x2, y2);
		DeleteObject(SelectObject(hdc, hOldPen));
	}else{
		printf("Call initgraph() first!\n");
	}
}

//Clear the screen
void clr(){
	if(GraphFlag==1){
		COORD coordScreen = {0, 0}; 
		DWORD cCharsWritten; 
		CONSOLE_SCREEN_BUFFER_INFO csbi; 
		DWORD dwConSize; 
		HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); 

		GetConsoleScreenBufferInfo(hConsole, &csbi); 
		dwConSize = csbi.dwSize.X * csbi.dwSize.Y; 
		FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten); 
		GetConsoleScreenBufferInfo(hConsole, &csbi); 
		FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten); 
		SetConsoleCursorPosition(hConsole, coordScreen);
	}else{
		printf("Call initgraph() first!\n");
	}
}

//Enable Graphic Mode
void initgraph(){
	GraphFlag = 1; //Set graphic flag to 1 and allow DrawThread() to call the graphic function
	hWnd = GetConsoleWindow(); //Get the handle of Console Window
	//Hide the Console Cursor
	cInfo.dwSize = 1;
	cInfo.bVisible = false;
	hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorInfo(hConOut, &cInfo);

	hdc = GetDC(hWnd); //Get the drawing area
}

//Disable Graphic Mode
void closegraph(){
	GraphFlag = 0; //Set graphic flag to 0 and disallow DrawThread() to call the graphic function
	//Show the Console Cursor
	cInfo.dwSize = 25;
	cInfo.bVisible = true;
	hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorInfo(hConOut, &cInfo);

	ReleaseDC(hWnd, hdc); //Free the drawing area
}

//Set Console Window to Maximal size
void SetConsoleMaxSize(){
	DisplayArea.Left = 0;
	DisplayArea.Top = 0;
	DisplayArea.Right = 0;
	DisplayArea.Bottom = 0;

    hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
    NewSBSize = GetLargestConsoleWindowSize(hConOut);
    SetConsoleScreenBufferSize(hConOut, NewSBSize);

	DisplayArea.Right = NewSBSize.X - 1;
	DisplayArea.Bottom = NewSBSize.Y - 1;

    SetConsoleWindowInfo(hConOut, TRUE, &DisplayArea);
	hWnd = GetConsoleWindow();
}

//Set Console Window to full screen
void SetConsoleFullScreen(){
        keybd_event(VK_MENU,0x38,0,0); 
        keybd_event(VK_RETURN,0x1c,0,0); 
        keybd_event(VK_MENU,0xb8,KEYEVENTF_KEYUP,0);
        keybd_event(VK_RETURN,0x9c,KEYEVENTF_KEYUP,0);

		hWnd = GetConsoleWindow(); //How to do???
}
Last edited on
I'm not surprised that it doesn't work / acts strange. You really should not be doing this.

If you want to draw graphics, you should not be using a console window.

If you want to use WinAPI to draw, you should just make a normal window. Or you can get a graphic lib. The console window is not designed for this and it will fight with you about what will be displayed.
Thank for your reply, Disch.
I'll think about it...
Topic archived. No new replies allowed.