Chinaunix首页 | 论坛 | 博客
  • 博客访问: 196760
  • 博文数量: 60
  • 博客积分: 3269
  • 博客等级: 中校
  • 技术积分: 648
  • 用 户 组: 普通用户
  • 注册时间: 2005-09-21 10:48
文章存档

2012年(6)

2011年(6)

2010年(30)

2009年(8)

2007年(6)

2005年(4)

我的朋友

分类: C/C++

2009-12-29 22:40:39

以下代码在dos/turbo_C2.0编译通过,并使用良好,只是没有注释,
我也忘了,根据函数名猜吧。
 /* popup.c */
 #include
 #include
 #include
 #include
 #include
 #include
 #include
 #include
 #include "popup.h"
 #include "mouse.h"
 #include "mouse.c"
 #define Max(a,b) ((a)>(b)?(a):(b))
 #define Min(a,b) ((a)<(b)?(a):(b))

 wincolors defcolors ={1,23,30,27,94};
 wincolors invcolors ={1,112,112,112,15};
 wincolors monocolors ={1,7,15,15,112};
 wincolors errcolors ={1,79,79,79,4};
 wincolors msgcolors ={1,47,47,46,112};
 static windesc *top_win;
 static void chg_win(windesc *this);
 static windesc *make_window_node(void);
 static windesc *push_window_node(void);
 static void dispose_window_node(windesc *w);
 windesc *base_win;
 windesc *curr_win;
 void init_win(void)
 {
 struct text_info ti;
 base_win = make_window_node();
 gettextinfo(&ti);
 base_win->xul = ti.winleft - 1;
 base_win->xlr = ti.winright +1;
 base_win->yul = ti.wintop - 1;
 base_win->ylr = ti.winbottom + 1;
 base_win->wd  = ti.screenwidth + 2;
 base_win->ht  = ti.screenheight + 2;
 base_win->xsave=ti.curx;
 base_win->ysave=ti.cury;
 base_win->wc  = monocolors;
 base_win->wc.text_color = ti.attribute;
 base_win->name = "base";
 base_win->wtype = tile;
 base_win->wc.border_type = 0;
 top_win = base_win;
 curr_win = base_win;
 }
 windesc *draw_win(int x,int y,int wd,int ht,char *title,
  enum windowtype wt,wincolors *wc)
 {
 windesc *w;
 int xsave,ysave;
 mouse_off(1);
 w = push_window_node();
 wd = Max(wd,3);
 wd = Min(wd,80);
 ht = Max(ht,3);
 ht = Min(ht,25);
 if(x==Ctrwin) x=(80-wd)/2;
 if(y==Ctrwin) y=(25-ht)/2;
 if(wc->border_type){
  x=Max(x,1);
  y=Max(y,1);
 }
 else{
  x=Max(x,0);
  y=Max(y,0);
 }
 if((x+wd)>80) x=80-wd+1;
 if((y+ht)>25) y=25-ht+1;
 w->wd = wd;
 w->ht = ht;
 w->xul=x;
 w->yul=y;
 w->xlr=x+w->wd-1;
 w->ylr=w->yul+w->ht-1;
 w->wc = *wc;
 w->xsave = 1;
 w->ysave = 1;
 w->wtype = wt;
 w->name = strdup(title);
 if(wt ==popup){
  w->image=calloc(wd *ht,2);
  swap_image(w);
 }
 if(curr_win==base_win){
  xsave = wherex();
  ysave = wherey();
 }
 else{
  xsave = base_win->xsave;
  ysave = base_win->ysave;
 }
 chg_win(base_win);
 draw_box(w->xul,w->yul,w->xlr,w->ylr,w->wc.border_type,
  w->wc.border_color);
 chg_win(w);
 clr_win();
 centerstr(w->xul,w->yul,w->xlr,w->ylr,w->name,
  w->wc.title_color);
 gotoxy(xsave,ysave);
 mouse_on(1);
 return w;
 }
 void view_win(windesc *this,int select)
 {
 windesc *p;
 if (select && this == top_win) return;
 mouse_off(1);
 if (this->wtype == popup) {
  p=top_win;
  while(p!=this) {
   swap_image(p);
   p = p->under;
  }
  swap_image(this);
  p=this;
  while(p !=top_win) {
   p=p->over;
   swap_image(p);
  }
 }
 if (this == top_win) {
  this->under->over = NULL;
  top_win = this->under;
 }
 else {
  this->under->over = this->over;
  this->over->under = this->under;
 }
 if (select) {
  top_win->over = this;
  this->under = top_win;
  top_win = this;
  swap_image(this);
  chg_win(this);
 }
 else {
  chg_win(top_win);
  dispose_window_node(this);
 }
 mouse_on(1);
 }
 static void chg_win(windesc *this)
 {
 curr_win->xsave = wherex();
 curr_win->ysave = wherey();
 window(this->xul+1,this->yul+1,this->xlr-1,this->ylr-1);
 textattr(this->wc.text_color);
 gotoxy(this->xsave,this->ysave);
 curr_win = this;
 }
 void swap_image (windesc *w)
 {
 int xstart,ystart,xfin,yfin;
 char *temp_image;
 unsigned int nbytes;
 if(w->wtype == popup) {
  xstart = w->xul;
  ystart = w->yul;
  xfin = w->xlr;
  yfin = w->ylr;
  if (!w->wc.border_type) {
   xstart++;
   ystart++;
   xfin--;
   yfin--;
  }
  nbytes = (xfin-xstart+1) * (yfin-ystart+1) * 2;
  mouse_off(1);
  temp_image = malloc(nbytes);
  gettext(xstart,ystart,xfin,yfin,temp_image);
  puttext(xstart,ystart,xfin,yfin,(void*)w->image);
  memcpy(w->image,temp_image,nbytes);
  free(temp_image);
  mouse_on(1);
 }
 }
 void clr_win(void)
 {
 mouse_off(1);
 textattr(curr_win->wc.text_color);
 clrscr();
 mouse_on(1);
 }

 void mprintf(char *fmt,...)
 {
 va_list  arg_ptr;
 char t[255];
 va_start(arg_ptr,wd);
 vsprintf(t,fmt,arg_ptr);
 mouse_off(1);
 cprintf("%s",t);
 mouse_on(1);
 va_end(arg_ptr);
 }

 void prtfstr(int x,int y,char *fmt,unsigned char attr,int wd,...)
 {
 va_list   arg_ptr;
 char t[255];
 int len,i,n,xa,ya,fillflag;
 static texel line[80];
 va_start(arg_ptr,wd);
 vsprintf(t,fmt,arg_ptr);
 va_end(arg_ptr);
 n = abs(wd);
 n = Min(n,curr_win->wd-x-1);
 xa = curr_win->xul + x;
 ya = curr_win->yul + y;
 len = Min(strlen(t),n);
 t[len] = 0;
 if (len) {
  if (wd < 0 && (strlen(t) == 1)) {
   fillflag = 1;
   len = n ;
  }
  else {
   fillflag = 0;
  }
  mouse_off(1);
  gettext (xa,ya,xa+len-1,ya,line);
  for (i=0;i   if(fillflag) line[i].ch = *t;
   else line[i].ch = t[i];
   if (attr) line[i].attr = attr;
  }
  puttext(xa,ya,xa+len-1,ya,line);
  mouse_on(1);
  if(x+len == curr_win->wd-1) x--;
  gotoxy(x+len,y);
 }
 else {
  gotoxy(x,y);
 }
 }

 void centerstr(int xul,int yul,int xlr,int ylr,
  char *s, unsigned char a)
 {
 int xs,ys,wd;
 if (*s!=0){
  mouse_off(1);
  wd = xlr-xul+1;
  if(((xs = (wd-strlen(s))/2)+xul) < xul) xs = xul;
  if(((ys = ((ylr-yul+1)/2))+yul)  < yul) ys = yul;
  prtfstr(xs,ys,s,a,wd);
  mouse_on(1);
 }
 }

 void draw_box(int xul,int yul,int xlr,int ylr,int btype,int attr)
 {
 static int boxcar[2][6] = {
  {218,196,191,179,192,217},
  {201,205,187,186,200,188}
 };
 int i, hzchar,vtchar,oldattr;
 texel t;
 struct text_info ti;
 if(btype) {
  mouse_off(1);
  gettextinfo(&ti);
  oldattr = ti.attribute;
  textattr(attr);
  hzchar = boxcar[btype-1][1];
  vtchar = boxcar[btype-1][3];
  gotoxy(xul+1,yul);
  for(i= xul+1;i  gotoxy(xul+1,ylr);
  for(i= xul+1;i  for(i= yul+1;i   gotoxy(xul,i);
   putch(vtchar);
   gotoxy(xlr,i);
   putch(vtchar);
  }
  gotoxy(xul,yul);
  putch(boxcar[btype-1][0]);
  gotoxy(xlr,ylr);
  putch(boxcar[btype-1][5]);
  gotoxy(xul,ylr);
  putch(boxcar[btype-1][4]);
  gotoxy(xlr,yul);
  putch(boxcar[btype-1][2]);
  gettext(xlr,ylr,xlr,ylr,&t);
  t.ch = boxcar[btype-1][5];
  t.attr = attr;
  puttext(xlr,ylr,xlr,ylr,&t);
  textattr(oldattr);
  mouse_on(1);
 }
 }
 
 static windesc *make_window_node(void)
 {
 windesc *q;
 q = (windesc *)malloc (sizeof(windesc));
 q->image = NULL;
 q->under = NULL;
 q->over = NULL;
 return q;
 }

 static windesc *push_window_node(void)
 {
 windesc *q;
 q = make_window_node();
 top_win->over = q;
 q->under = top_win;
 top_win = q;
 return q;
 }

 static void dispose_window_node(windesc *w)
 {
 if(w !=NULL) {
  if(w->wtype == popup) free(w->image);
  free(w->name);
  free(w);
 }
 }


阅读(781) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~