?? grap.cpp
字號:
#include "graphics.h"
#include "stdio.h"
#include "struct.h"
#include "string.h"
#include "alloc.h"
#include "constdec.h"
#include "mouse.h"
void ChineseOut(int x,int y,const char *p,int color);
void ReShowButton(BUTTON &button);
void ReShowItem(ITEMBUTTON &button);
int InitGraph(void)
{
int gdriver = VGA, gmode=VGAHI, errorcode;
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
return -1;
}
settextjustify(CENTER_TEXT, BOTTOM_TEXT);
return 0;
}
void ResumeGraph(void)
{
closegraph();
}
void MakeWindow(const WINDOW window)
{
int width,height,strl;
struct viewporttype vp;
width=window.right-window.left;
height=window.bottom-window.top;
getviewsettings(&vp);
strl=strlen(window.text)*8;
HideMouse();
setviewport(window.left,window.top,window.right,window.bottom,0);
setfillstyle(SOLID_FILL,window.bkcolor);
bar(0,0,width,height);
setlinestyle(SOLID_LINE,0,NORM_WIDTH);
setcolor(window.color);
moveto((strl==0?width/2:(width-strl)/2-3), 9);
lineto(4,9);
lineto(4,height-4);
lineto(width-4,height-4);
lineto(width-4,9);
lineto((width+strl)/2, 9);
ShowMouse();
ChineseOut((width-strl)/2,1,window.text,window.color);
setviewport(vp.left,vp.top,vp.right,vp.bottom,0);
}
void ShowButton(BUTTON *button,int num)
{
int i,x,y,h,l;
const char *p;
for(i=0;i<num;i++)
{
if(button[i].DrawFlag==1)
{
x=button[i].x;
y=button[i].y;
h=button[i].height;
l=button[i].lengh;
p=button[i].text;
HideMouse();
setcolor(0);
// setfillstyle(1,RED);
// bar(x-l/2,y-h/2,x+l/2,y+h/2);
setfillstyle(1,LIGHTGRAY);
bar(x-l/2+2,y-h/2+2,x+l/2-1,y+h/2-1);
setcolor(WHITE);
/* rectangle(x-l/2,y-h/2,x+l/2,y+h/2);
setcolor(RED); */
line(x-l/2,y-h/2,x+l/2,y-h/2);
line(x-l/2,y-h/2,x-l/2,y+h/2);
line(x+1-l/2,y+1-h/2,x+l/2-1,y+1-h/2);
line(x+1-l/2,y+1-h/2,x+1-l/2,y+h/2-1);
setcolor(RED);
line(x+l/2-1,y-h/2+2,x+l/2-1,y+h/2-1);
line(x-l/2+2,y+h/2-1,x+l/2-1,y+h/2-1);
line(x+l/2,y-h/2+1,x+l/2,y+h/2);
line(x-l/2+1,y+h/2,x+l/2,y+h/2);
ShowMouse();
ChineseOut(x-strlen(p)*4,y-7,p,BLACK);
if(button[i].PressFlag==1)
{
int oldflag=button[i].EnableFlag;
button[i].PressFlag=0;
button[i].EnableFlag=1;
ReShowButton(button[i]);
button[i].EnableFlag=oldflag;
}
button[i].DrawFlag=0;
}
}
}
void ShowItem(ITEMBUTTON *button,int num)
{
int i,x,y,l;
const char *p;
for(i=0;i<num;i++)
{
if(button[i].DrawFlag==1)
{
x=button[i].x;
y=button[i].y;
l=button[i].lengh;
p=button[i].text;
ChineseOut(x-l/2+5,y-7,p,BLACK);
if(button[i].PressFlag==1)
{
int oldflag=button[i].EnableFlag;
button[i].PressFlag=0;
button[i].EnableFlag=1;
ReShowItem(button[i]);
button[i].EnableFlag=oldflag;
}
button[i].DrawFlag=0;
}
}
}
void ReShowButton(BUTTON &button)
{
if(button.EnableFlag==1)
{
void *Buf1;
int x=button.x,y=button.y,h=button.height,l=button.lengh;
button.PressFlag=++button.PressFlag%2;
Buf1 = malloc(imagesize(x-l/2,y-h/2,x+l/2,y+h/2));
HideMouse();
getimage(x-l/2,y-h/2,x+l/2,y+h/2,Buf1);
putimage(x-l/2,y-h/2,Buf1,NOT_PUT);
ShowMouse();
free(Buf1);
}
}
void ReShowItem(ITEMBUTTON &button)
{
ReShowButton((BUTTON&)button);
}
void ChineseOut(int x,int y,const char *p,int color)
{
int old_color;
unsigned char dot[32];
unsigned char c1,c2;
int i,j;
long k;
char pp[2]={'\0','\0'};
FILE *fp;
fp=fopen("cclib.dat","rb");
if(fp==NULL)
{
printf("open \"cclib.dat\" failed\n");
return;
}
old_color=getcolor();
setcolor(color);
HideMouse();
for(;*p!=NULL;p++)
{
if((unsigned char)*p>=0xa1)
{
c1=*p++;
if(*p==NULL)
{
continue;
}
c2=*p;
c1=(c1-0xa1)&0x07f;
c2=(c2-0xa1)&(0x07f);
k=(c1*94+c2)*32l;
fseek(fp,k,0);
fread(dot,1,32,fp);
for(i=0;i<32;i++)
{
for(j=0;j<8;j++)
{
if((dot[i]&0x80)==0x80)
putpixel(x+j,y+i/2,color);
dot[i]<<=1;
if((dot[i+1]&0x80)==0x80)
putpixel(x+j+8,y+i/2,color);
dot[i+1]<<=1;
}
i++;
}
x+=16;
}
else
{
pp[0]=*p;
outtextxy(x+4,y+12,pp);
x+=8;
}
}
fclose(fp);
setcolor(old_color);
ShowMouse();
}
void RectPrint(RECT rect,int mode,int color,char *s)
{
int x,y;
int swidth,sheight;
struct viewporttype vp,newvp;
getviewsettings(&vp);
setviewport(rect.left+3,rect.top+3,rect.right-3,rect.bottom-3,0);
getviewsettings(&newvp);
swidth=newvp.right-newvp.left;
sheight=newvp.bottom-newvp.top;
setfillstyle(SOLID_FILL,rect.bkcolor);
HideMouse();
bar(0,0,swidth,sheight);
ShowMouse();
switch(mode)
{
case 0:
y=sheight/2-7;
x=(swidth-strlen(s)*8)/2;
break;
case 1:
y=sheight/2-7;
x=5;
break;
}
ChineseOut(x,y,s,color);
setviewport(vp.left,vp.top,vp.right,vp.bottom,0);
}
void Show3dRect(RECT rect)
{
struct viewporttype vp,newvp;
int oldcolor,swidth,sheight;
HideMouse();
getviewsettings(&vp);
setviewport(rect.left,rect.top,rect.right,rect.bottom,0);
getviewsettings( &newvp );
swidth=newvp.right-newvp.left;
sheight=newvp.bottom-newvp.top;
setfillstyle( SOLID_FILL,rect.bkcolor );
bar( 0, 0, swidth, sheight );
oldcolor=getcolor();
setcolor(BLACK);
line(0,0,swidth,0);
line(1,1,swidth-1,1);
line(0,0,0,sheight);
line(1,1,1,sheight-1);
setcolor(LIGHTCYAN);
line(0,sheight,swidth,sheight);
line(1,sheight-1,swidth-1,sheight-1);
line(swidth,0,swidth,sheight);
line(swidth-1,1,swidth-1,sheight-1);
setcolor(oldcolor);
setviewport(vp.left,vp.top,vp.right,vp.bottom,0);
ShowMouse();
}
void ShowHelp(const char *str[],int linenum)
{
struct viewporttype vp,newvp;
int oldcolor,swidth,sheight;
HideMouse();
getviewsettings(&vp);
oldcolor=getcolor();
setviewport(HelpWindow.left+8,HelpWindow.top+20,HelpWindow.right-8,HelpWindow.bottom-8,0);
getviewsettings( &newvp );
swidth=newvp.right-newvp.left;
sheight=newvp.bottom-newvp.top;
setfillstyle( SOLID_FILL,CYAN);
bar( 0, 0, swidth, sheight );
ShowMouse();
ChineseOut((newvp.right-newvp.left)/2-strlen(str[0])*8/2,10,str[0],BLACK);
for(int i=1;i<linenum;i++)
{
ChineseOut(7,10+23*i,str[i],BLACK);
}
setcolor(oldcolor);
setviewport(vp.left,vp.top,vp.right,vp.bottom,0);
}
int BmpDisplay(char *filename,int startx,int starty,int color)
{
FILE *bmp_fp;
unsigned long width, height, offset;
unsigned long start_position, len_scanline, temp_len;
int bits_per_pixel, compress_type;
int i, j, k;
unsigned int pixel_value;
unsigned char byte;
HideMouse();
if( (bmp_fp=fopen(filename, "rb"))==NULL)
{ printf("Can not open file %s!",filename); return -1; }
fseek(bmp_fp, 18, SEEK_SET); /* BMP figure width */
fread(&width, 4, 1, bmp_fp);
fseek(bmp_fp, 22, SEEK_SET); /* BMP figure height */
fread(&height, 4, 1, bmp_fp);
fseek(bmp_fp, 28, SEEK_SET); /* the bits every pixel needed */
fread(&bits_per_pixel, 2, 1, bmp_fp);
if( bits_per_pixel == 24 )
{ printf(" Too big color value, cannot process it!");
return -1;
}
fseek(bmp_fp, 30, SEEK_SET); /* BMP file compress type */
fread(&compress_type, 2, 1, bmp_fp);
if( compress_type!=0 )
{ printf("The BMP file was compressed, please uncompress it!");
return -1;
}
/* the length (bytes) of every scan-line in BMP figure */
if( (width*bits_per_pixel)%32==0 )
len_scanline=(unsigned long)(width*bits_per_pixel)/8;
else
{ temp_len=(unsigned long)(width*bits_per_pixel)/8;
len_scanline=(temp_len/4)*4+4;
}
/* start_position: the start position of bmp dot-matrix */
fseek(bmp_fp, 10, SEEK_SET);
fread(&start_position, 4, 1, bmp_fp);
for(i=0; i<height; i++)
{
offset=start_position+len_scanline*(height-i-1);
fseek(bmp_fp, offset, SEEK_SET);
switch(bits_per_pixel)
{
case 1: for(j=0; j<len_scanline-2; j++)
{ /* there are some questions, the result is uncorrect */
fread(&byte, 1, 1, bmp_fp);
for(k=0; k<8; k++)
{ pixel_value=(byte>>(7-k))&1;
if(pixel_value!=0)
putpixel(startx+j*8+k, starty+i, color);
}
}
break;
case 4: for(j=0; j<len_scanline; j++)
{ fread(&byte, 1, 1, bmp_fp);
pixel_value=(int) byte&0x0f;
// if(pixel_value==LIGHTRED) pixel_value=RED;
putpixel(startx+j*2+1, starty+i, pixel_value);
pixel_value=(int) (byte>>4)&0x0f;
// if(pixel_value==LIGHTRED) pixel_value=RED;
putpixel(startx+j*2, starty+i, pixel_value);
}
break;
} /* end of switch(...) */
} /* end for(i=0; i<height; i++) */
fclose(bmp_fp);
ShowMouse();
return(0);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -