用TC写出一个中文窗口式菜单
~~~~~
[此贴子已经被作者于2004-06-12 00:15:59编辑过]
#include "stdio.h" #include "stdlib.h" #include "graphics.h" #include "dos.h"
#define LEFTBTN 1
FILE *fp;
void Quit(); void MainMenu(); void InitGra(void); void PutCC16(int x, int y, int, int Color, char *Str); void PrsBox(int x, int y, int l, int h); void Box(int x, int y, int l, int h); int InitMouse(); void ErrMsg(); void ShowMouse(); void HideMouse(); void ReadMouse(int *f, int *x, int *y); int MsInBox(int x1, int y1, int x2, int y2, int x, int y); void OpenLIB();
int main() { InitGra(); OpenLIB(); MainMenu(); Quit(); return 0; }
void Quit() { closegraph(); fcloseall(); exit(0); }
void MainMenu() { int x1, y1, l, h, x2; int Button, x, y, Choice; char *Tit = "用鼠标选择菜单"; char *Mmu[] = {"绘制新图", "编辑旧图", "图形输出", "退出系统"}; x1 = 190; y1 = 240; x2 = 350; l = 100; h = 32; setfillstyle(1, 7); bar(140, 30, 500, 460); setcolor(14); rectangle(140, 30, 500, 460); setfillstyle(1, 9); bar(215,100, 430,147); PutCC16(250, 116, 4, 15, Tit); Box(x1, y1, l, h); PutCC16(x1+15, y1+8, 2, 1, Mmu[0]);
Box(x2, y1, l, h); PutCC16(x2+15, y1+8, 2, 1, Mmu[1]); Box(x1, y1+100, l, h); PutCC16(x1+15, y1+108, 2, 1, Mmu[2]); Box(x2, y1+100, l, h); PutCC16(x2+15, y1+108, 2, 1, Mmu[3]); if (! InitMouse()) ErrMsg();
ShowMouse(); for (;;) { Button = 0; while (Button != LEFTBTN) ReadMouse(&Button, &x, &y);
HideMouse(); if (MsInBox(x1, y1, x1+l, y1+h, x, y)) { PrsBox(x1, y1, l, h); Choice = 1; break; } if (MsInBox(x2, y1, x2+l, y1+h, x, y)) { PrsBox(x2, y1, l, h); Choice = 2; break; } if (MsInBox(x1, y1+100, x1+l, y1+100+h, x, y)) { PrsBox(x1, y1+100, l, h); Choice = 3; break; } if (MsInBox(x2, y1+100, x2+l, y1+100+h, x, y)) { PrsBox(x2, y1+100, l, h); Choice = 4; break; } } switch(Choice) { case 1: break; case 2: break; case 3: break; case 4: Quit(); } }
void InitGra(void) { int GraphDrive = DETECT, GraphMode; registerbgidriver(EGAVGA_driver); initgraph(&GraphDrive, &GraphMode, ""); }
void PutCC16(int x, int y, int z, int Color, char *Str)
{ unsigned Zcode, Bcode; int i, j, k, Rec; long Len; char Buf[32]; while (*Str)
{ if ((*Str & 0x80) && (*(Str+1) &0x80)) { Zcode = (*Str-0xa1) & 0x07f; /* 区码 */ Bcode = (*(Str+1)-0xa1) & 0x07f; /* 位码 */ Rec = Zcode*94+Bcode; /* 记录号 */ Len = Rec*32L; /* 在字库中位置 */ fseek(fp, Len, SEEK_SET); fread (Buf, 1, 32, fp); /* 32字节 */ for (i = 0; i < 16; i++) for (j = 0; j < 2; j++) for (k = 0; k < 8; k++) if (Buf[i*2+j] >> (7-k) & 1) putpixel(x+j*8+k, y+i, Color); x = x+16+z; Str += 2; } } return; }
void PrsBox(int x, int y, int l, int h) { void *Buf1, *Buf2; Buf1 = malloc(imagesize(x, y, x+l, y+h)); Buf2 = malloc(imagesize(x+4, y+4, x+l-4, y+h-4)); getimage(x, y, x+l, y+h, Buf1); getimage(x+4, y+4, x+l-4, y+h-4, Buf2); setfillstyle(1, 7); bar(x, y, x+l, y+h); putimage(x+4, y+4, Buf2, COPY_PUT); delay(100); putimage(x, y, Buf1, COPY_PUT); delay(100); free(Buf1); free(Buf2); }
void Box(int x, int y, int l, int h) { setcolor(0); rectangle(x-1, y-1, x+l+1, y+h+1); setfillstyle(1, 8); bar(x, y, x+l, y+h); setfillstyle(1, 7); bar(x+2, y+2, x+l-2, y+h-2); setcolor(15); line(x, y, x+l, y); line(x, y, x, y+h); line(x+1, y+1, x+l-1, y+1); line(x+1, y+1, x+1, y+h-1); }
int InitMouse() { union REGS Inr, Outr; Inr.x.ax = 0; int86(0x33, &Inr, &Outr); return Outr.x.ax; }
void ShowMouse() { union REGS Inr; Inr.x.ax = 1; int86(0x33, &Inr, &Inr); }
void HideMouse() { union REGS Inr; Inr.x.ax = 2; int86(0x33, &Inr, &Inr); }
void ReadMouse(int *f, int *x, int *y)
{ union REGS Inr, Outr; Inr.x.ax = 3; int86(0x33, &Inr, &Outr); *f = Outr.x.bx; *x = Outr.x.cx; *y = Outr.x.dx; }
int MsInBox(int x1, int y1, int x2, int y2, int x, int y) { return((x >= x1 && x <= x2 && y >= y1 && y <= y2) ? 1 : 0); }
void OpenLIB()
{ fp = fopen("c:\\ucdos\\hzk16", "rb"); //ucdos hzk16点阵字库放置目录 }
void ErrMsg() { printf("No Mouse Error!"); getch(); Quit(); }