展开全部
C语方下可以在文本模式下作出,也可以在图形模式下画出,用的是一些扩展库,就是说不是C标准库,下面给一个框架,不知道建不建议学,感觉过时了,看你自已的兴趣了,感兴趣下了功夫肯定能学好,因为它根本就不难,只不过用了些非标准库的所谓高级函数而已
#include <process.h>
#include <dos.h>
#include <conio.h>
#define Key_DOWN 0x5100
#define Key_UP 0x4900
#define Key_ESC 0x011b
#define Key_ALT_F 0x2100
#define Key_ALT_X 0x2d00
#define Key_ENTER 0x1c0d
void box(int startx,int starty,int high,int width);
main()
{
int i,key,x,y,l;
char *menu[] = {"File","Edit","Run","Option","Help","Setup","Zoom","Menu"};
/* 主菜单各项 */
char *red[] = { "F","E","R","O","H","S","Z","M" }; /* 加上红色热键 */
char *f[] = {"Load file", "Save file", "Print", "Modify ", "Quit A1t_x"};
/* File 项的子菜单 */
char buf[16*10*2],buf1[16*2]; /* 定义保存文本的缓冲区 */
while(1)
{
textbackground(BLUE);
clrscr();
textmode(C80);
window(1,1,80,1);/* 定义显示主菜单的窗口 */
textbackground(LIGHTGRAY);
textcolor(BLACK);
clrscr();
gotoxy(5,1);
for(i=0,l=0;i<8;i++)
{
x=wherex(); /* 得到当前光标的坐标 */
y=wherey();
cprintf("%s",menu[i]); /* 显示各菜单项 */
l=strlen(menu[i]); /* 得到菜单项的长度 */
gotoxy(x,y);
textcolor(RED);
cprintf("%s",red[i]); /* 在主菜单项各头字符写上红字符 */
x=x+l+5;
gotoxy(x,y);
textcolor(BLACK); /* 为显示下一个菜单项移动光标 */
}
gotoxy(5,1);
key=bioskey(0);
switch (key){
case Key_ALT_X:
exit(0); /* ALT_X 则退出 */
case Key_ALT_F:
{
textbackground(BLACK);
textcolor(WHITE);
gotoxy(5,1);
cprintf("%s",menu[0]); /* 加黑 File 项 */
gettext(5,2,20,12,buf); /* 保存窗口原来的文本 */
window(5,2,20,9);/* 设置作矩形框的窗口 */
textbackground(LIGHTGRAY);
textcolor(BLACK);
clrscr();
box(1,1,7,16); /* 调用作框函数 */
for(i=2;i<7;i++) /* 显示子菜单各项 */
{ gotoxy(2,i);
cprintf("%s",f[i-2]);
}
gettext(2,2,18,3,buf1); /*将下拉菜单的内容保存在 buf1*/
textbackground(BLACK);
textcolor(WHITE);
gotoxy(2,2);
cprintf("%s",f[0]);/*加黑下拉菜单的第一项 load file*/
gotoxy(2,2);
y=2;
while ((key=bioskey(0))!=Key_ALT_X) /* 等待选择下拉菜单项*/
{
if ((key==Key_UP)||(key==Key_DOWN))
{
puttext(2,y,18,y+1,buf1); /* 恢复原先的项 */
if (key==Key_UP)
y=y==2?6:y-1;
else
y=y==6?2:y+1;
gettext(2,y,18,y+1,buf1);/*保存要压上光条的子菜单项*/
textbackground(BLACK);
textcolor(WHITE);
gotoxy(2,y);
cprintf("%s",f[y-2]); /* 产生黑条压在所选项上 */
gotoxy(2,y);
}
else
if (key==Key_ENTER)/* 若是回车键,判断是哪一子菜单按的回
车,在此没有相应的特殊处理*/
{
switch ( y-1 ){
case 1: /* 是子菜单项第一项:Load file */
break;
case 2: /* Save file */
break;
case 3: /* print */
break;
case 4: /* modify */
break;
case 5:
exit(0);
default:
break;
}
break;
}
else
if (key==Key_ESC)
break; /* 是 Esc 键,返回主菜单 */
}
if (key==Key_ALT_X) exit(0);
break;
}
}
}
}
void box(int startx,int starty,int high,int width) /* 画矩形框函数 */
{ int i;
gotoxy(startx,starty);
putch(0xda); /* 画┌ */
for (i=startx+1;i<width;i++) putch(0xc4); /* 画— */
putch(0xbf); /* 画┐ */
for( i=starty+1;i<high;i++)
{
gotoxy(startx,i);putch(0xb3); /* 画│ */
gotoxy(width,i);putch(0xb3); /* 画│ */
}
gotoxy(startx,high);
putch(0xc0); /* 画└ */
for (i=startx+1;i<width;i++) putch(0xc4); /* 画— */
putch(0xd9); /* 画┘ */
return ;
}
#include <process.h>
#include <dos.h>
#include <conio.h>
#define Key_DOWN 0x5100
#define Key_UP 0x4900
#define Key_ESC 0x011b
#define Key_ALT_F 0x2100
#define Key_ALT_X 0x2d00
#define Key_ENTER 0x1c0d
void box(int startx,int starty,int high,int width);
main()
{
int i,key,x,y,l;
char *menu[] = {"File","Edit","Run","Option","Help","Setup","Zoom","Menu"};
/* 主菜单各项 */
char *red[] = { "F","E","R","O","H","S","Z","M" }; /* 加上红色热键 */
char *f[] = {"Load file", "Save file", "Print", "Modify ", "Quit A1t_x"};
/* File 项的子菜单 */
char buf[16*10*2],buf1[16*2]; /* 定义保存文本的缓冲区 */
while(1)
{
textbackground(BLUE);
clrscr();
textmode(C80);
window(1,1,80,1);/* 定义显示主菜单的窗口 */
textbackground(LIGHTGRAY);
textcolor(BLACK);
clrscr();
gotoxy(5,1);
for(i=0,l=0;i<8;i++)
{
x=wherex(); /* 得到当前光标的坐标 */
y=wherey();
cprintf("%s",menu[i]); /* 显示各菜单项 */
l=strlen(menu[i]); /* 得到菜单项的长度 */
gotoxy(x,y);
textcolor(RED);
cprintf("%s",red[i]); /* 在主菜单项各头字符写上红字符 */
x=x+l+5;
gotoxy(x,y);
textcolor(BLACK); /* 为显示下一个菜单项移动光标 */
}
gotoxy(5,1);
key=bioskey(0);
switch (key){
case Key_ALT_X:
exit(0); /* ALT_X 则退出 */
case Key_ALT_F:
{
textbackground(BLACK);
textcolor(WHITE);
gotoxy(5,1);
cprintf("%s",menu[0]); /* 加黑 File 项 */
gettext(5,2,20,12,buf); /* 保存窗口原来的文本 */
window(5,2,20,9);/* 设置作矩形框的窗口 */
textbackground(LIGHTGRAY);
textcolor(BLACK);
clrscr();
box(1,1,7,16); /* 调用作框函数 */
for(i=2;i<7;i++) /* 显示子菜单各项 */
{ gotoxy(2,i);
cprintf("%s",f[i-2]);
}
gettext(2,2,18,3,buf1); /*将下拉菜单的内容保存在 buf1*/
textbackground(BLACK);
textcolor(WHITE);
gotoxy(2,2);
cprintf("%s",f[0]);/*加黑下拉菜单的第一项 load file*/
gotoxy(2,2);
y=2;
while ((key=bioskey(0))!=Key_ALT_X) /* 等待选择下拉菜单项*/
{
if ((key==Key_UP)||(key==Key_DOWN))
{
puttext(2,y,18,y+1,buf1); /* 恢复原先的项 */
if (key==Key_UP)
y=y==2?6:y-1;
else
y=y==6?2:y+1;
gettext(2,y,18,y+1,buf1);/*保存要压上光条的子菜单项*/
textbackground(BLACK);
textcolor(WHITE);
gotoxy(2,y);
cprintf("%s",f[y-2]); /* 产生黑条压在所选项上 */
gotoxy(2,y);
}
else
if (key==Key_ENTER)/* 若是回车键,判断是哪一子菜单按的回
车,在此没有相应的特殊处理*/
{
switch ( y-1 ){
case 1: /* 是子菜单项第一项:Load file */
break;
case 2: /* Save file */
break;
case 3: /* print */
break;
case 4: /* modify */
break;
case 5:
exit(0);
default:
break;
}
break;
}
else
if (key==Key_ESC)
break; /* 是 Esc 键,返回主菜单 */
}
if (key==Key_ALT_X) exit(0);
break;
}
}
}
}
void box(int startx,int starty,int high,int width) /* 画矩形框函数 */
{ int i;
gotoxy(startx,starty);
putch(0xda); /* 画┌ */
for (i=startx+1;i<width;i++) putch(0xc4); /* 画— */
putch(0xbf); /* 画┐ */
for( i=starty+1;i<high;i++)
{
gotoxy(startx,i);putch(0xb3); /* 画│ */
gotoxy(width,i);putch(0xb3); /* 画│ */
}
gotoxy(startx,high);
putch(0xc0); /* 画└ */
for (i=startx+1;i<width;i++) putch(0xc4); /* 画— */
putch(0xd9); /* 画┘ */
return ;
}
展开全部
C语言是所有图形界面的基础。
#include <dos.h>
#include <time.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
struct ViewPort
{
int left;
int top;
int right;
int bottom;
};
struct Point
{
int x;
int y;
};
struct MyTime
{
int Hour;
int Min;
int Sec;
};
struct ViewPort AllSrc; /*全屏视口*/
struct ViewPort MouseBox={0,0,623,479}; /*鼠标能到达的最大视口*/
struct ViewPort TimeBox={428,25,600,55}; /*时间视口*/
struct Point Mouse,lastpos; /*鼠标重绘时用,记录鼠标的当前位置和上一位置*/
struct MyTime timer;
void*bitmap; /*保存鼠标上一位置的位图(除去鼠标本身)*/
char ARROR1[]={
/*以下是 鼠标箭头 的 16点阵 字模,128 byte ,横向,自己绘制*/
0xe0,0x00,
0xfe,0x00,
0xff,0xf0,
0x7f,0xfe,
0x7f,0xf0,
0x7f,0x00,
0x7f,0x00,
0x3f,0x80,
0x39,0xc0,
0x38,0xe0,
0x38,0x70,
0x38,0x38,
0x10,0x1c,
0x10,0x0e,
0x10,0x06,
0x00,0x00
};
void drawHanzi(char *mat,int matsize,int x,int y,int color) /*从字模点阵数组中获得字模,显示汉字*/
/*依次:字模指针、点阵大小、起始坐标(x,y)、颜色*/
{
int i,j,k,n;
n=(matsize-1)/8+1;
for(j=0;j<matsize;j++)
for(i=0;i<n;i++)
for(k=0;k<8;k++)
if(mat[j*n+i]&(0x80>>k)) /*测试为1的位则显示*/
putpixel(x+i*8+k,y+j,color);
}
void MouseSetXY(int x,int y) /*设置鼠标的位置*/
{
_CX=x;
_DX=y;
_AX=0x04;
geninterrupt(0x33);
Mouse.x=x;
Mouse.y=y;
}
void MouseGetXY() /*获取鼠标的位置*/
{
_AX=0x03;
geninterrupt(0x33);
Mouse.x=_CX;
Mouse.y=_DX;
}
int LeftPress() /*判断鼠标左键是否被按下*/
{
_AX=0x03;
geninterrupt(0x33);
return (_BX&1);
}
int RightPress() /*判断鼠标右键是否被按下*/
{
_AX=0x03;
geninterrupt(0x33);
return (_BX&2);
}
void SetMouseXRange(int min,int max) /*设置鼠标横向坐标范围*/
{
_AX=0x07;
_CX=min;
_DX=max;
geninterrupt(0x33);
}
void SetMouseYRange(int min,int max) /*设置鼠标纵向坐标范围*/
{
_AX=0x08;
_CX=min;
_DX=max;
geninterrupt(0x33);
}
int bFirst=1;
void DrawMouse() /*在Mouse对应坐标处绘制鼠标*/
{
if(bFirst)
{
bFirst=0;
getimage(Mouse.x,Mouse.y,Mouse.x+15,Mouse.y+15,bitmap);
drawHanzi(ARROR1,16,Mouse.x,Mouse.y,YELLOW);
}
else
{
putimage(lastpos.x,lastpos.y,bitmap,COPY_PUT);
getimage(Mouse.x,Mouse.y,Mouse.x+15,Mouse.y+15,bitmap);
drawHanzi(ARROR1,16,Mouse.x,Mouse.y,YELLOW);
}
lastpos.x=Mouse.x;
lastpos.y=Mouse.y;
}
void HideMouse() /*隐藏鼠标*/
{
putimage(lastpos.x,lastpos.y,bitmap,COPY_PUT);
bFirst=1;
}
void SetViewPort(struct ViewPort p) /*设置视口*/
{
setviewport(p.left,p.top,p.right,p.bottom,1);
}
void SetMouseRange(struct ViewPort p)
{
SetMouseXRange(p.left,p.right);
SetMouseYRange(p.top,p.bottom);
}
void TimeAdd(struct MyTime* pt)
{
pt->Sec++;
if(pt->Sec>59)
{
pt->Sec=0;
pt->Min++;
if(pt->Min>59)
{
pt->Min=0;
pt->Hour++;
}
}
}
void FormatTime(char* st,struct MyTime t)
{
sprintf(st,"%d:%2d:%2d",t.Hour,t.Min,t.Sec);
}
void DrawTop()
{
char st[24];
long ti,last=time(NULL);
int starty=5;
int startx=200,endx=418;
struct time tm,lasttm;
struct time tm_click,lasttm_click;
gettime(&lasttm);
while(!kbhit())
{
SetViewPort(AllSrc);
setlinestyle(SOLID_LINE,NULL,THICK_WIDTH);
setfillstyle(0,YELLOW);
MouseGetXY();
gettime(&tm);
if( ((tm.ti_sec-lasttm.ti_sec)*100+tm.ti_hund-lasttm.ti_hund)>10)
{
DrawMouse();
lasttm=tm;
}
ti=time(NULL);
if(ti!=last)
{
SetViewPort(TimeBox);
TimeAdd(&timer);
FormatTime(st,timer);
setcolor(RED);
clearviewport();
outtextxy(2,2,st);
last=ti;
}
}
}
main()
{
int driver,mode;
driver=VGA;
mode=VGAHI;
timer.Hour=0;
timer.Min=0;
timer.Sec=0;
initgraph(&driver,&mode,"C:\\Turboc2"); /*自己改路径*/
AllSrc.left=0;
AllSrc.top=0;
bitmap=malloc(imagesize(0,0,15,15));
AllSrc.right=getmaxx();
AllSrc.bottom=getmaxy();
setbkcolor(LIGHTGRAY);
SetMouseRange(MouseBox);
MouseSetXY(100,100);
SetViewPort(AllSrc);
DrawTop();
closegraph();
return 0;
}
有鼠标,你可以在某处画个“按钮”,当鼠标点击左键时,如果在这个按钮内,则调用相应的函数。
#include <dos.h>
#include <time.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
struct ViewPort
{
int left;
int top;
int right;
int bottom;
};
struct Point
{
int x;
int y;
};
struct MyTime
{
int Hour;
int Min;
int Sec;
};
struct ViewPort AllSrc; /*全屏视口*/
struct ViewPort MouseBox={0,0,623,479}; /*鼠标能到达的最大视口*/
struct ViewPort TimeBox={428,25,600,55}; /*时间视口*/
struct Point Mouse,lastpos; /*鼠标重绘时用,记录鼠标的当前位置和上一位置*/
struct MyTime timer;
void*bitmap; /*保存鼠标上一位置的位图(除去鼠标本身)*/
char ARROR1[]={
/*以下是 鼠标箭头 的 16点阵 字模,128 byte ,横向,自己绘制*/
0xe0,0x00,
0xfe,0x00,
0xff,0xf0,
0x7f,0xfe,
0x7f,0xf0,
0x7f,0x00,
0x7f,0x00,
0x3f,0x80,
0x39,0xc0,
0x38,0xe0,
0x38,0x70,
0x38,0x38,
0x10,0x1c,
0x10,0x0e,
0x10,0x06,
0x00,0x00
};
void drawHanzi(char *mat,int matsize,int x,int y,int color) /*从字模点阵数组中获得字模,显示汉字*/
/*依次:字模指针、点阵大小、起始坐标(x,y)、颜色*/
{
int i,j,k,n;
n=(matsize-1)/8+1;
for(j=0;j<matsize;j++)
for(i=0;i<n;i++)
for(k=0;k<8;k++)
if(mat[j*n+i]&(0x80>>k)) /*测试为1的位则显示*/
putpixel(x+i*8+k,y+j,color);
}
void MouseSetXY(int x,int y) /*设置鼠标的位置*/
{
_CX=x;
_DX=y;
_AX=0x04;
geninterrupt(0x33);
Mouse.x=x;
Mouse.y=y;
}
void MouseGetXY() /*获取鼠标的位置*/
{
_AX=0x03;
geninterrupt(0x33);
Mouse.x=_CX;
Mouse.y=_DX;
}
int LeftPress() /*判断鼠标左键是否被按下*/
{
_AX=0x03;
geninterrupt(0x33);
return (_BX&1);
}
int RightPress() /*判断鼠标右键是否被按下*/
{
_AX=0x03;
geninterrupt(0x33);
return (_BX&2);
}
void SetMouseXRange(int min,int max) /*设置鼠标横向坐标范围*/
{
_AX=0x07;
_CX=min;
_DX=max;
geninterrupt(0x33);
}
void SetMouseYRange(int min,int max) /*设置鼠标纵向坐标范围*/
{
_AX=0x08;
_CX=min;
_DX=max;
geninterrupt(0x33);
}
int bFirst=1;
void DrawMouse() /*在Mouse对应坐标处绘制鼠标*/
{
if(bFirst)
{
bFirst=0;
getimage(Mouse.x,Mouse.y,Mouse.x+15,Mouse.y+15,bitmap);
drawHanzi(ARROR1,16,Mouse.x,Mouse.y,YELLOW);
}
else
{
putimage(lastpos.x,lastpos.y,bitmap,COPY_PUT);
getimage(Mouse.x,Mouse.y,Mouse.x+15,Mouse.y+15,bitmap);
drawHanzi(ARROR1,16,Mouse.x,Mouse.y,YELLOW);
}
lastpos.x=Mouse.x;
lastpos.y=Mouse.y;
}
void HideMouse() /*隐藏鼠标*/
{
putimage(lastpos.x,lastpos.y,bitmap,COPY_PUT);
bFirst=1;
}
void SetViewPort(struct ViewPort p) /*设置视口*/
{
setviewport(p.left,p.top,p.right,p.bottom,1);
}
void SetMouseRange(struct ViewPort p)
{
SetMouseXRange(p.left,p.right);
SetMouseYRange(p.top,p.bottom);
}
void TimeAdd(struct MyTime* pt)
{
pt->Sec++;
if(pt->Sec>59)
{
pt->Sec=0;
pt->Min++;
if(pt->Min>59)
{
pt->Min=0;
pt->Hour++;
}
}
}
void FormatTime(char* st,struct MyTime t)
{
sprintf(st,"%d:%2d:%2d",t.Hour,t.Min,t.Sec);
}
void DrawTop()
{
char st[24];
long ti,last=time(NULL);
int starty=5;
int startx=200,endx=418;
struct time tm,lasttm;
struct time tm_click,lasttm_click;
gettime(&lasttm);
while(!kbhit())
{
SetViewPort(AllSrc);
setlinestyle(SOLID_LINE,NULL,THICK_WIDTH);
setfillstyle(0,YELLOW);
MouseGetXY();
gettime(&tm);
if( ((tm.ti_sec-lasttm.ti_sec)*100+tm.ti_hund-lasttm.ti_hund)>10)
{
DrawMouse();
lasttm=tm;
}
ti=time(NULL);
if(ti!=last)
{
SetViewPort(TimeBox);
TimeAdd(&timer);
FormatTime(st,timer);
setcolor(RED);
clearviewport();
outtextxy(2,2,st);
last=ti;
}
}
}
main()
{
int driver,mode;
driver=VGA;
mode=VGAHI;
timer.Hour=0;
timer.Min=0;
timer.Sec=0;
initgraph(&driver,&mode,"C:\\Turboc2"); /*自己改路径*/
AllSrc.left=0;
AllSrc.top=0;
bitmap=malloc(imagesize(0,0,15,15));
AllSrc.right=getmaxx();
AllSrc.bottom=getmaxy();
setbkcolor(LIGHTGRAY);
SetMouseRange(MouseBox);
MouseSetXY(100,100);
SetViewPort(AllSrc);
DrawTop();
closegraph();
return 0;
}
有鼠标,你可以在某处画个“按钮”,当鼠标点击左键时,如果在这个按钮内,则调用相应的函数。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询