基本框架
#include "graphics.h"
main()
{
int driver=VGA,model=VGAHI;
initgraph(&driver,&model,"");
setbkcolor(EGERGB(0x00, 0xFF, 0x00));
setcolor(RED);
circle(200,200,20);
getch();
}
随机圆
#include "graphics.h"
#include "time.h"
main()
{
srand(time(0));
int driver=VGA,model=VGAHI;
initgraph(&driver,&model,"");
setbkcolor(BLACK);
while(1)
{
setcolor(RGB(rand()%256, rand()%256, rand()%256));
circle(rand()%1000,rand()%800,rand()%500);
Sleep(100);
}
getch();
}
随机方块
#include "graphics.h"
#include "time.h"
main()
{
srand(time(0));
int x1,y1,x2,y2;
int driver=VGA,model=VGAHI;
initgraph(&driver,&model,"");
ege::initgraph(1000,800);
setbkcolor(BLACK);
while(1)
{
setfillstyle(SOLID_FILL,RGB(rand()%256, rand()%256, rand()%256));
x1=rand()%1000+1;
y1=rand()%800+1;
x2=rand()%1000+1;
y2=rand()%800+1;
bar(x1,y1,x2,y2);
Sleep(100);
}
getch();
}
其他常用绘图功能
#include "graphics.h"
#include "time.h"
main()
{
srand(time(0));
int x1,y1,x2,y2,i;
int driver=VGA,model=VGAHI;
initgraph(&driver,&model,"");
ege::initgraph(1000,800);
setbkcolor(RGB(rand()%100, rand()%100, rand()%100));
setcolor(RGB(rand()%156+100,rand()%156+100,rand()%156+100));
circle(100,100,100);
for(i=0; i<10; i++)
putpixel(300+i,300,WHITE);
setlinestyle(DOTTED_LINE,0,1);
line(100,100,300,300);
rectangle(400,400,500,500);
setviewport(400,400,500,500,1);
setcolor(GREEN);
setlinestyle(SOLID_FILL,0,1);
rectangle(10,10,90,90);
setfont(60, 60, "宋体");
outtextxy(20,20,"A");
getch();
}
国际象棋
#include "graphics.h"
main()
{
int driver=VGA,model=VGAHI;
int i=0,j=0;
initgraph(&driver,&model,"");
ege::initgraph(800,800);
setbkcolor(WHITE);
setcolor(BLACK);
for(i=0; i<8; i++)
for(j=0; j<8; j++)
{
if(i%2==0)
{
bar(i*100,j*100,(i+1)*100,(j+1)*100);
j++;
}
if(i%2==1)
{
j++;
bar(i*100,j*100,(i+1)*100,(j+1)*100);
}
}
getch();
}
鼠标处理
#include "graphics.h"
#include "stdio.h"
#include "stdlib.h"
main()
{
int driver=VGA,model=VGAHI;
int i=0,j=0;
initgraph(&driver,&model,"");
ege::initgraph(800,800);
setbkcolor(WHITE);
setcolor(BLACK);
for ( ; is_run(); delay_fps(120))
{
while (mousemsg())
{
mouse_msg m = getmouse();
if (m.msg == mouse_msg_down)
{
bar(m.x-2, m.y-2,m.x+2, m.y+2);
}
}
}
}
按钮效果:
#include "graphics.h"
main()
{
int driver=VGA,model=VGAHI;
initgraph(&driver,&model,"");
setbkcolor(RGB(100,100,100));
setlinestyle(SOLID_LINE,0,3);
setcolor(WHITE);
line(100,100,300,100);
line(100,100,100,150);
setcolor(BLACK);
line(300,100,300,150);
line(100,150,300,150);
for ( ; is_run(); delay_fps(120))
{
while (mousemsg())
{
mouse_msg m = getmouse();
if (m.msg == mouse_msg_down)
{
setcolor(BLACK);
line(100,100,300,100);
line(100,100,100,150);
setcolor(WHITE);
line(300,100,300,150);
line(100,150,300,150);
}
else
{
setcolor(WHITE);
line(100,100,300,100);
line(100,100,100,150);
setcolor(BLACK);
line(300,100,300,150);
line(100,150,300,150);
}
}
}
}
字符键盘处理
#include "graphics.h"
#include "stdio.h"
#include "stdlib.h"
main()
{
int driver=VGA,model=VGAHI;
int i=0,j=0;
initgraph(&driver,&model,"");
ege::initgraph(800,800);
setbkcolor(WHITE);
setcolor(BLACK);
for ( ; is_run(); delay_fps(120))
{
while (kbhit())
{
char key =getch();
if (key==' ' || key== 0x1b || key==0x03)
{
setbkcolor(RGB(rand()%100, rand()%100, rand()%100));
}
}
}
}
功能键键盘处理
#include "graphics.h"
#include "stdio.h"
#include "stdlib.h"
main()
{
int driver=VGA,model=VGAHI;
int i=0,j=0;
initgraph(&driver,&model,"");
ege::initgraph(800,800);
setbkcolor(WHITE);
setcolor(BLACK);
for ( ; is_run(); delay_fps(120))
{
int k = kbmsg();
key_msg key = getkey();
if (key.msg == key_msg_up)
setbkcolor(RGB(rand()%100, rand()%100, rand()%100));
}
}