Trong C/C++, người dùng có thể tùy chỉnh đầu ra. Các hàm đồ họa nằm trong header graphics.h. Thư viện này cho phép tạo đối tượng, đặt màu chữ, thay đổi font và cỡ chữ, thay đổi nền.
Các hàm chính
- setcolor() - Thay đổi màu chữ đầu ra.
Cú pháp
setcolor(int)
Ví dụ
#include <stdio.h>
#include <graphics.h>
int main() {
int gdriver = DETECT, gmode, i;
initgraph(&gdriver, &gmode, "C:\\Turboc3\\BGI");
setcolor(5); // Màu hồng/magenta
return 0;
}
- settextstyle() - Thay đổi font, hướng, cỡ chữ.
Cú pháp
settextstyle(int font, int direction, int size);
Ví dụ
#include <stdio.h>
#include <graphics.h>
int main() {
int gdriver = DETECT, gmode, i;
initgraph(&gdriver, &gmode, "C:\\Turboc3\\BGI");
settextstyle(3, 1, 4); // Font 3, hướng 1, cỡ 4
return 0;
}
- outtextxy() - In thông điệp tại tọa độ (x, y).
Cú pháp
outtextxy(int x, int y, const char* text)
Ví dụ
#include <stdio.h>
#include <graphics.h>
int main() {
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "C:\\Turboc3\\BGI");
settextstyle(3, 1, 4);
outtextxy(100, 100, "Hello!");
return 0;
}
- textheight() - Trả về chiều cao của text (pixel).
Cú pháp
textheight(const char* text)
- textwidth() - Trả về chiều rộng của text (pixel).
Cú pháp
textwidth(const char* text)