The little nephew has been unhappy for the past two days, and complained to me that learning C language faces the console window of Windows all day long, and I don't think it's interesting at all.
I think this feeling is normal, a boring black hole window, after a long time, it is difficult to raise the interest of beginners in learning.
Today we are going to carry out a comprehensive "magic change" of the Windows console (CMD).
With C**, the console can be set in size, and the window is displayed in the center of the screen every time the program is launched. Of course, you can also make any changes to the text and window background, and so on.
Without further ado, let's take it step by step, follow the ** movement, and see the effect.
Functions of this paragraph:
Sets the title name of the current window
#include
#include
int main(void)
Function name: setconsoletitle
Parameter: char* (string).
char* title = "New title name";
setconsoletitle(title);
return 0;
Functions of this paragraph:
Gets the title name of the current window
#include
#include
int main(void)
max_path
The macro that has been defined in the Windows header file has a value of 260
Indicates the maximum number of characters in which the title text of the console can be stored
title[max_path]
Holds the title string that you get.
Function name: getconsoletitle
Parameter 1: char*(the address of the stored character array).
Parameter 2: int (default max path).
char title[max_path];
getconsoletitle(title,max_path);
printf("%c",title);
return 0;
#include
#include
int main()
char title[1000];
hwnd hwnd;
getconsoletitle(title,1000);
hwnd=findwindow(null,title);
movewindow(hwnd,200,300,100,150,true);
return 0;
Comment: movewindow(hwnd,200,300,100,150,true);
200 is the distance from the left side of the computer screen;
300 is the distance from the top of the computer screen.
100 is the width of the form.
150 is the height of the form.
These 4 values can be set as needed.
#include
#include
int main(void)
Set the console size
unsigned cols,lines;
cols=80,lines=40;
char setsize_cmd[100];
sprintf(setsize_cmd,"mode con cols=%d lines=%d",cols,lines);
system(setsize_cmd);
Note: cols is the width of the window, and lines is the height of the window.
Set the global background color of the console
system("color 0e");
In the number 0e, 0 has a strip foreground color (i.e., the color of the text), and e is the background color of the screen.
The values of the foreground color can be as follows:
0 = Black 1 = Blue 2 = Green
3 = light green, 4 = red, 5 = purple
6 = Yellow 7 = White
The value range of the background color is as follows:
8 = gray 9 = light blue a = light green.
b = pale green c = pale red d = lavender.
e = pale yellow f = bright white.
handle handle=getstdhandle(std_output_handle);
coord pos= ;
Start behavior 0
setconsolecursorposition(handle, pos);
This section allows you to set the cursor to rest anywhere in the window.
The first value of the coord variable pos is the value of x, and the second value is the value of y.
Set the text size
console_font_infoex cfi;
cfi.cbsize = sizeof cfi;
cfi.nfont = 0;
cfi.dwfontsize.x = 0;
cfi.dwfontsize.y = 20;
cfi.fontfamily = ff_dontcare;
cfi.fontweight = fw_bold;
setcurrentconsolefontex(getstdhandle(std_output_handle), false, &cfi);
printf("Console");
Force Pin window:
setwindowpos(getconsolewindow(),hwnd_topmost,0,0,0,0,swp_nomove|swp_nosize|swp_drawframe);
Unforce Sticky:
etwindowpos(getconsolewindow(),hwnd_notopmost,0,0,0,0,swp_nomove|swp_nosize|swp_drawframe);
Automatically hide the current form:
showwindow(hwnd,sw_hide);
If you don't understand the usage, please leave a message in the comment area to ask questions. List of high-quality authors