⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gui.cpp

📁 用汇编写的编辑器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/**********Copyright by Kwest 2001-2002********\
* Under the program IDE:Turbo C++ 3.0 IDE      *
* For the DOS/WIN-DOS system                   *
* 作者:万治鑫                                 *                             
* 单位:西南交通大学峨嵋校区计算机系00级计本4  *
* E-mail:kwest@163.net                         *
* http://kwest.my163.com/                      *
\**************The Data Structures*************/

/*******************宏定义*********************/
#define NORMAL 0x07
#define BRIGHT 0x0F
#define REVERSE 0x70
#define UNLINE 0x01

#define ESC 27        /*define the key's ASCII code*/
#define ENTER 13
#define UP_ARROW -72
#define DOWN_ARROW -80
#define LEFT_ARROW -75
#define RIGHT_ARROW -77
#define BACK 8
#define INSERT -82
#define DELETE -83
#define HOME -71
#define END -79
#define PGDN -81
#define PGUP -73
#define CTRL_PGDN -118
#define CTRL_PGUP -132
#define ALTX -45
#define BLANK ""

#define MAXLINE 1500

#define CLOSE 0
#define OPEN 1
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
typedef struct Window_Frame   //数据结构
{
  int left,top;
  int right,bottom;
  int frame[10];
  char state;
  char *title;
  void *buffer;
  int cx,cy;
 }WINDOW;

//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
#include "alloc.h"     //头文件声明
#include "dos.h"
#include "graphics.h"
#include "conio.h"
#include "bios.h"
#include "process.h"
#include "string.h"
#include "stdio.h"
 /*-------------END--------------*/ 
/*----------begin functions---------*/

//子函数原型
/*Window_Creat Function*/
WINDOW *Window_Creat(int left,int top,int right,int bottom,
		     int c0,int c1,int c2,int c3,int c4,
		     int c5,int c6,int c7,int fattr,int battr,
		     char *title)
//窗口构造函数
{
  WINDOW *w;
  w=(WINDOW *)malloc(sizeof(WINDOW));
  w->left=left;
  w->top=top;
  w->right=right;
  w->bottom=bottom;
  w->frame[0]=c0;
  w->frame[1]=c1;
  w->frame[2]=c2;
  w->frame[3]=c3;
  w->frame[4]=c4;
  w->frame[5]=c5;
  w->frame[6]=c6;
  w->frame[7]=c7;
  w->frame[8]=fattr;
  w->frame[9]=battr;
  w->title=strdup(title);
  w->state=CLOSE;
  w->buffer=NULL;
  w->cx=w->cy=1;
  return(w);
}

/*----------------------Window_Scope function-----------------*/
//窗口定位函数
void Window_Scope(WINDOW *w)
{
 window(w->left+1,w->top+1,w->right-1,w->bottom-1);
 textattr(w->frame[9]);
}
/*----------------------Window_Draw function------------------*/
//窗口绘画函数
void Window_Draw(WINDOW *w)
{
 int x,y;
 window(1,1,80,25);
 textattr(w->frame[8]);
 gotoxy(w->left,w->top);
 putch(w->frame[0]);
 for(x=w->left+1;x<w->right;x++)
  putch(w->frame[1]);
 putch(w->frame[2]);
 for(y=w->top+1;y<w->bottom;y++)
  { gotoxy(w->left,y); putch(w->frame[7]);
    gotoxy(w->right,y); putch(w->frame[3]);
  }
 gotoxy(w->left,w->bottom);
 putch(w->frame[6]);
 for(x=w->left+1;x<w->right;x++)
   putch(w->frame[5]);
 putch(w->frame[4]);
 gotoxy(w->left+2,w->top);
 cputs(w->title);
 window(w->left+1,w->top+1,w->right-1,w->bottom-1);
 textattr(w->frame[9]);
}
/*-----------------------Window_Open function--------------------*/

//窗口打开函数
void Window_Open(WINDOW *w)
{
 int hight,width;
 Window_Scope(w);
 if(w->state==OPEN) return;
 hight=w->bottom-w->top+1;
 width=w->right-w->left+1;
 w->buffer=malloc(hight*width*2);
 gettext(w->left,w->top,w->right,w->bottom,w->buffer);
 w->state=OPEN;
 Window_Draw(w);
 clrscr();
}
/*-----------------------Window_Close Function------------------*/
//窗口关闭函数
void Window_Close(WINDOW *w)
{
 if(w->state==CLOSE) return;
 puttext(w->left,w->top,w->right,w->bottom,w->buffer);
 free(w->buffer);
 w->state=CLOSE;
 window(1,1,80,25);
}
/*-----------------------Window_Kill Function--------------------*/

//窗口释放函数
void Window_Kill(WINDOW *w)
{
  if(w->state==OPEN) Window_Close(w);
  free(w->title);
  free(w);
}
/*----------------------Windows_Savexy Function------------------*/
//窗口坐标保存
void Window_Savexy(WINDOW *w)
{
  if(w->state==CLOSE) return;
  w->cx=wherex();
  w->cy=wherey();
}
/*----------------------Window_Gotoxy Function--------------------*/

//窗口光标定位
void Window_Gotoxy(WINDOW *w)
{
  if(w->state==CLOSE) return;
  gotoxy(w->cx,w->cy);
}
/*----------------------Window_Change Function---------------------*/
//窗口坐标定位
void Window_Change(WINDOW *w,int left,int top,
		    int right,int bottom)

{
  int state=CLOSE;
  if(w->state==OPEN)
   { state=OPEN;
     Window_Close(w);
    }
w->left=left;
w->top=top;
w->right=right;
w->bottom=bottom;
if(state==OPEN) Window_Open(w);
}     
/*-----------------------Window_Frame Function---------------------*/
//窗口边框
void Window_Frame(WINDOW *w,int c0,int c1,int c2,
                  int c3,int c4,int c5,int c6,int c7)
 
 {
   w->frame[0]=c0;
   w->frame[1]=c1;
   w->frame[2]=c2;
   w->frame[3]=c3;
   w->frame[4]=c4;
   w->frame[5]=c5;
   w->frame[6]=c6;
   w->frame[7]=c7;
   if(w->state==CLOSE) return;
   
   Window_Savexy(w);
   Window_Draw(w);
   Window_Gotoxy(w);
 }  
/*---------------------Change_Attr Function-----------------*/
//
//
void Change_Attr(int x,int y,int length,int attr)
 
 {
   int i;
   union REGS reg;
   for(i=0;i<length;i++)
    { gotoxy(x+i,y);
      reg.h.bh=0;
      reg.h.ah=8;
      int86(0x10,&reg,&reg);
      reg.h.bl=attr;
      reg.x.cx=1;
      reg.h.bh=0;
      reg.h.ah=9;
      int86(0x10,&reg,&reg);
    }
 }
/*------------------Window_Attr Function--------------------*/

//窗口颜色属性
void Window_Attr(WINDOW *w,int fattr,int battr)
 
 {
   int y;
   w->frame[8]=fattr;
   w->frame[9]=battr;
   if(w->state==CLOSE) return;
   Window_Savexy(w);
   window(1,1,80,25);
   Change_Attr(w->left,w->top,w->right-w->left+1,fattr);
   Change_Attr(w->left,w->bottom,w->right-w->left+1,fattr);
   for(y=w->top+1;y<w->bottom;y++)
    { Change_Attr(w->left,y,1,fattr);
      Change_Attr(w->right,y,1,fattr);
      Change_Attr(w->left+1,y,w->right-w->left-1,battr);
     }
   window(w->left+1,w->top+1,w->right-1,w->bottom-1);
   textattr(w->frame[9]);
   Window_Gotoxy(w);
 }   
/*--------------------Window_Title Function--------------------*/

//窗口标题
void Window_Title(WINDOW *w,char *title)
 
 {
   free(w->title);
   w->title=strdup(title);
   if(w->state==CLOSE) return;
   
   Window_Savexy(w);
   Window_Draw(w);
   Window_Gotoxy(w);
  } 
/*--------------------Window_Text Function----------------------*/

//窗口内文本输出
void Window_Text(WINDOW *w,char *str)
 
 {
   if(w->state==CLOSE) return;
   window(w->left+1,w->top+1,w->right-1,w->bottom-1);
   textattr(w->frame[9]);
   clrscr();
   cputs(str);
 }  
/*---------------------Get_Key Function-------------------------*/
//
//获取键盘ASCII码
int Get_Key(void)
 {
   int key=getch();
   if(key==0) return(-getch());
   return(key);
 }  
/*-----------------------Window_Get Function------------------*/


#define SetPos gotoxy(Lx+ip-Begin,Ly)

void PutStr(int Lx,int Ly,int Rx,char *str)
  
  {
    int i=Lx;
    char *p=str;

    if(str==NULL) return ;
    gotoxy(Lx,Ly);
    while(*p!=NULL)
      { if(i<=Rx) putch(*p++);
		else  return ;
         i++;
       }
    }
char *Window_Get(WINDOW *w,char *str)
  
  {
    int Lx,Ly,Rx,Len;
    int ip=0,Final=0,Begin=0,ipt;
    char Str[256]="",ch;
    char INS=1;
    if(w->state==CLOSE) return(NULL);
    window(1,1,80,25);
    Lx=w->left+1;
    Ly=w->top+1;
    Rx=w->right-1;
    Len=Rx-Lx+1;
    gotoxy(Lx,Ly);
    do
      {
      	ch=Get_Key();
      	switch(ch)
      	  { case ENTER:Window_Scope(w);
      	               strcpy(str,Str);
      	               return(str);
      	    
      	    case ESC:Window_Scope(w);
      	             strcpy(str,"");
      	             return(str);
      	    
      	    case INSERT:INS=1-INS;
      	                  break;
      	    
      	    case HOME:PutStr(Lx,Ly,Rx,&str[Begin=0]);
      	              ip=0;
      	              SetPos;
      	              break;
      	    
      	    case END:if(Final-Begin+1>Len)
      	                Begin=Final-Len+1;
      	                PutStr(Lx,Ly,Rx,&Str[Begin]);
                      ip=Final;
       	              SetPos;
       	              break;
       	    
       	    case LEFT_ARROW:if(ip==0) break;
       	                    if(ip==Begin)
       	                      PutStr(Lx,Ly,Rx,&Str[--Begin]);
       	                    ip--;
       	                    SetPos;
       	                    break;
       	    
       	    case RIGHT_ARROW:if(ip==Final) break;
       	                     if(wherex()+1>Rx)
       	                        PutStr(Lx,Ly,Rx,&Str[++Begin]);
       	                     ip++;
       	                     SetPos;
       	                     break;
       	    
       	    case DELETE:if(ip==Final) break;
       	                ipt=ip;
       	                do{Str[ipt]=Str[ipt+1];
       	                    ipt++;
       	                   }while(ipt!=Final);
       	                Final--;
       	                PutStr(Lx,Ly,Rx,&Str[Begin]);
       	                SetPos;
       	                break;
       	    
       	    case BACK:if(ip==0) break;
       	              ipt=ip-1;
       	              while(ipt!=Final)
       	                {Str[ipt]=Str[ipt+1];
       	                 ipt++;
       	                };
       	              if(ip==Begin) Begin--;
       	               Final--; ip--;
       	              PutStr(Lx,Ly,Rx,&Str[Begin]);
       	              SetPos;
       	              break;
       	     default:
       	          if(ch<32||ch>126) continue;
       	          if(ip==254) continue;
       	          
       	          if(INS)
       	            {if(Final==254) continue;
       	              
       	              ipt=Final;
       	              Str[++Final]=NULL;
       	              while(ipt!=ip)
       	                 {Str[ipt]=Str[ipt-1];
       	                  ipt--;
       	                  }
       	               
       	               Str[ip++]=ch;
       	              if(wherex()==Rx) Begin++;
       	              PutStr(Lx,Ly,Rx,&Str[Begin]);
       	                 SetPos;
       	             }
                      else
                      { Str[ip++]=ch;
                        if(wherex()<Rx)
                         putch(ch);
                        else
                         { PutStr(Lx,Ly,Rx,&Str[++Begin]);
                          SetPos;
                         }
                      }
          }      
      }while(1);
 }       
/*-----------------------------------------------------------------*/
/*Window_File Function */
//
//文件输入
void Window_File(WINDOW *w,char *fname)
  
  {
    FILE *stream;
    char *line[MAXLINE],*p;
    int i,num,cur;
    int width,height;
    
    if(w->state==CLOSE) return;
    window(w->left+1,w->top+1,w->right-1,w->bottom-1);
    textattr(w->frame[9]);
    clrscr();
    
    if((stream=fopen(fname,"r"))==NULL)
      { cputs("File open error!!!");
        getch();
        return;
      }
      
    i=0;
    width=w->right-w->left-1;
    while(1)
    { line[i]=(char *)malloc(width+2);
     if(fgets(line[i],width,stream)==NULL) break;
     if((p=strrchr(line[i],'\n'))!=NULL) *p=NULL;
     if(++i==MAXLINE) goto OverFlow;
     }   
    fclose(stream);
    
    num=i;
    cur=0;
    height=w->bottom-w->top-1;
    for(i=0;i<num&&i<height;i++)
      { cputs(line[i]);
        gotoxy(1,wherey()+1);
       }
    while(1)
      {switch(Get_Key())

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -