如何使用c语言编写图形界面

yongzhang_zhu 2005-12-20 09:15:05
如何使用c语言编写图形界面.
...全文
697 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
屋顶上的老猫 2005-12-21
  • 打赏
  • 举报
回复
进行图形模式的初始化后(initgraph()),使用graphics.h提供的函数!
///////////////////////////////////////////////////////////////////

TO:ak_2005(雪中我独行) 问这个问题另开帖,付上几百分!!
  • 打赏
  • 举报
回复
注释的地方就是编译通不过~~
  • 打赏
  • 举报
回复
void Process()
{ int GFILLINTERIOR,DEFAULTMODE;
int iMenu=-1,oldMenu,x,y,key;
while (1) {
tellme("Use mouse to select menu item. ");
oldMenu=iMenu;
iMenu=GetMenu(&x,&y,&key);
if(oldMenu!=iMenu){
if (oldMenu>=MENUBGN){
setcolor(1);
rectangle(/*GFILLINTERIOR,*/scrwin[oldMenu].x1,scrwin[oldMenu].y1,
scrwin[oldMenu].x2,scrwin[oldMenu].y2);
gtext(scrwin[oldMenu].x1+7,scrwin[oldMenu].y1+5,
scrwin[oldMenu].menuitem,15);
}
if (iMenu>=MENUBGN){
setcolor(11);
rectangle(/*GFILLINTERIOR,*/scrwin[iMenu].x1,scrwin[iMenu].y1,
scrwin[iMenu].x2,scrwin[iMenu].y2);
gtext(scrwin[iMenu].x1+7,scrwin[iMenu].y1+5,
scrwin[iMenu].menuitem,6);
}
WrHint(HintStr[iMenu]);
}
switch (iMenu)
{
case 4:
gcolor=(gcolor % 15)+1;
setcolor(gcolor);
rectangle(/*GFILLINTERIOR,*/scrwin[4].x1,scrwin[4].y1,
scrwin[4].x2,scrwin[4].y2);
break;
case 8:
fGrid^=0x01;
Grid();
break;
case 9:
line();
break;
case 27:
ClrScr();
break;
case 28:
setgraphmode(DEFAULTMODE);
exit(0);
break;
}
}
}
void ShowMenu()
{
int i;
setcolor(7);
rectangle(/*GFILLINTERIOR,*/0,0,639,479);

for (i=0;i<MENUBGN;i++){
DrawFram(scrwin[i].x1-7,scrwin[i].y1-7,
scrwin[i].x2+7,scrwin[i].y2+7,1);
DrawFram(scrwin[i].x1-2,scrwin[i].y1-2,
scrwin[i].x2+2,scrwin[i].y2+2,1);
setcolor(atoi(scrwin[i].menuitem));
rectangle(/*GFILLINTERIOR,*/scrwin[i].x1,scrwin[i].y1,
scrwin[i].x2,scrwin[i].y2);
}

DrawFram(465,43,630,309,1);
DrawFram(470,48,625,304,0);
registerfarbgifont("t'courier'h10w8b");
for (i=MENUBGN;i<MENUNUM;i++){
setcolor(0);
rectangle(/*GBORDER,*/scrwin[i].x1-1,scrwin[i].y1-1,
scrwin[i].x2+1,scrwin[i].y2+1);
setcolor(1);
rectangle(/*GFILLINTERIOR,*/scrwin[i].x1,scrwin[i].y1,
scrwin[i].x2,scrwin[i].y2);
gtext(scrwin[i].x1+7,scrwin[i].y1+5,scrwin[i].menuitem,15);
}

setcolor(14);
moveto(494,18);
outtext("Function Menu");
setcolor(12);
rectangle(/*GFILLINTERIOR,*/9,36,150,38);
setcolor(5);
registerfarbgifont("t'roman' h12w14b");
moveto(10,10);
outtext("Computer");
moveto(10,22);
outtext("Graphics");
registerfarbgifont("t'courier' h10w8b");
gtext(scrwin[8].x1+3,scrwin[8].y1+5,"GRID",6);
setcolor(gcolor);
rectangle(/*GFILLINTERIOR,*/scrwin[4].x1,scrwin[4].y1,
scrwin[4].x2,scrwin[4].y2);

}

void Grid()
{
int i,oldclr;

if (fGrid)
oldclr=7;
else
oldclr=0;
for (i=scrwin[0].y1;i<=scrwin[0].y2;i+=10){
moveto(scrwin[0].x1,i);
lineto(scrwin[0].x2,i);
}
for (i=scrwin[0].x1;i<=scrwin[0].x2;i+=10){
moveto(i,scrwin[0].y1);
lineto(i,scrwin[0].y2);
}
setcolor(oldclr);
}

int GetMenu(int *xi,int *yi,int *key)
{
int i;
MouseShow();
while (1){
*key=GetMouse(xi,yi);
if (fGrid && inside(*xi,*yi,scrwin[0].x1,scrwin[0].y1,
scrwin[0].x2,scrwin[0].y2))
{
*xi=(*xi-scrwin[0].x1+5)/10*10+scrwin[0].x1;
*xi=(*xi-scrwin[0].y1+5)/10*10+scrwin[0].y1;
MouseHide();
return(0);
}
for (i=0;i<MENUNUM;i++)
if (inside(*xi,*yi,scrwin[i].x1,scrwin[i].y1,
scrwin[i].x2,scrwin[i].y2))
{
MouseHide();
return(i);
}
}
}

int inside(int x,int y,int x1,int y1,int x2,int y2)
{
if (x>=x1 && x<=x2 && y>=y1 && y<=y2)
return(1);
else
return(0);
}

void WrHint(char *hintstr)
{
char *hstr;
char ch;
int i=0,lastspc=0,y,oldclr;
oldclr=15;
rectangle(/*GFILLINTERIOR.*/scrwin[1].x1,scrwin[1].y1,
scrwin[1].x2,scrwin[1].y2);

hstr=hintstr;
y=scrwin[1].y1+4;
while (hstr[i]!='\0')
{
if (hstr[i]==' ')
lastspc=i;
if (i++==HINTWIDTH)
{
if (lastspc==0)
lastspc=HINTWIDTH;
ch=hstr[lastspc];
hstr[lastspc]='\0';
gtext(scrwin[1].x1+4,y,hstr,1);
hstr[lastspc]=ch;
hstr+= lastspc;
while (*hstr==' ') hstr++;
y+=12;
i=0;
lastspc=0;
}
}
gtext(scrwin[1].x1+4,y,hstr,1);
setcolor(oldclr);
}

void tellme(char *msg)
{
gtext(20,457,msg,15);
}

void gtext(int x,int y,char *str,int color)
{
int oldclr;
MouseHide();
oldclr=3;
rectangle(/*GFILLINTERIOR,*/scrwin[2].x1,scrwin[2].y1,
scrwin[2].x2,scrwin[2].y2);
setcolor(color);
moveto(x,y);
outtext(str);
setcolor(oldclr);
}

int GetMouse(int *x,int *y)
{
int iButton=0;
while (!iButton)
MouseGetButton(&iButton,x,y);
MouseWaitRelease();
return(iButton);
}


void DrawFram(int x1,int y1,int x2,int y2,int type)
{
int oldclr;
if (type)
oldclr=15;
else
oldclr=8;
moveto(x1,y2-1);
lineto(x1,y1);
lineto(x2-1,y1);
lineto(x2-2,y1+1);
lineto(x1+1,y1+1);
lineto(x1+1,y2-2);
if (type)
setcolor(8);
else
setcolor(15);
moveto(x2,y1);
lineto(x2,y2);
lineto(x1,y2);
lineto(x1+1,y2-1);
lineto(x2-1,y2-1);
lineto(x2-1,y1+1);
setcolor(oldclr);
}

int line()
{
int key,x1,x2,y1,y2;
setcolor(gcolor);
tellme("Select the start point of the line. ");
while ((GetMenu(&x1,&y1,&key))!=0)
if (key==2)
return(1);
putpixel(x1,y1,gcolor);
tellme("Select the start point of the line. ");
while ((GetMenu(&x2,&y2,&key))!=0)
if (key==2)
return(1);
moveto(x1,y1);
lineto(x2,y2);
return(0);
}

int ClrScr()
{
setcolor(0);
rectangle(/*GFILLINTERIOR.*/scrwin[0].x1,scrwin[0].y1,
scrwin[0].x2,scrwin[0].y2);
if (fGrid)
Grid();
setcolor(gcolor);
return(0);
}

#pragma check_stack (off)


int MouseInit(void)
{
unsigned char far *pMouseDriver;
pMouseDriver=(unsigned char far *)_dos_getvect(0x33);
if (pMouseDriver==0||*pMouseDriver==0xcf)
return(0);
else
return(1);
}


int MouseReset(int *iButtonNum)
{
union REGS Reg;
if (!MouseInit())
return(0);
Reg.x.ax=0;
int86(0x33,®,®);
*iButtonNum=Reg.x.bx;
return(Reg.x.ax ? 1:0);
}

int MouseShow(void)
{
union REGS Reg;
if (!MouseInit())
return(0);
Reg.x.ax=1;
int86(0x33,®,®);
return(1);
}

int MouseHide(void)
{
union REGS Reg;
if (!MouseInit())
return(0);
Reg.x.ax=2;
int86(0x33,®,®);
return(1);
}

int MouseGetButton(int *iButtonStatus,int *iPointCol,int *iPointRow)
{
union REGS Reg;
if (!MouseInit())
{
*iButtonStatus=0;
return(0);
}
Reg.x.ax=3;
int86(0x33,®,®);
*iButtonStatus=Reg.x.bx;
*iPointCol=Reg.x.cx;
*iPointRow=Reg.x.dx;
return(1);
}

int MouseWaitRelease()
{
int button,x,y;
if (!MouseInit())
return(0);
button=1;
while (button)
MouseGetButton(&button,&x,&y);
return(1);
}
  • 打赏
  • 举报
回复
/*贴一个我改的计算机图象学书上的里子,但是现在鼠标还是用不起,希望哪位大侠能帮忙改下.*/

#include <stdlib.h>
#include <dos.h>
#include <stdio.h>
#include <conio.h>
#include <graphics.h>

#define MENUNUM 29
#define MENUBGN 9
#define HINTWIDTH 18

int MouseWaitRelease();
int MouseGetButton(int *,int *,int *);
int MouseHide(void);
int inside(int,int,int,int,int,int);
int GetMouse(int *,int *);
int MouseShow(void);
int ClrScr();
int GetMenu(int *x1,int *y1,int *key);
int MouseReset(int *ButtonNum);
int MouseInit(void);
void Process(void);
void ShowMenu();
void Grid(void);
int line();
void WrHint(char *);
void tellme(char *);
void gtext(int,int,char *,int);
void DrawFram(int,int,int,int,int);

struct {
char menuitem[9];
short x1,y1;
short x2,y2;
} scrwin[MENUNUM]={
"0" , 13,55,453,435,
"15" ,472,320,623,470,
"3" , 12,455,454,470,
"4" ,472, 15,213, 35,
"7" ,175, 15,213, 35,
"7" ,235, 15,273, 35,
"7" ,295, 15,333, 35,
"7" ,355, 15,393, 35,
"7" ,415, 15,453, 35,
"line" ,474, 53,545, 73,
"Null" ,474, 78,545, 73,
"Null" ,474,103,545,123,
"Null" ,474,128,545,148,
"Null" ,474,153,545,173,
"Null" ,474,178,545,198,
"Null" ,474,203,545,223,
"Null" ,474,228,545,248,
"Null" ,474,253,545,273,
"Null" ,474,278,545,298,
"Null" ,550, 53,621, 73,
"Null" ,550, 78,621, 98,
"Null" ,550,103,621,123,
"Null" ,550,128,621,148,
"Null" ,550,153,621,173,
"Null" ,550,178,621,198,
"Null" ,550,203,621,223,
"Null" ,550,228,621,248,
"Clear",550,253,621,273,
"Exit" ,550,278,621,298};

char *HintStr[MENUNUM]={
"In drawing area. ",
"In hint area. ",
"In message area. ",
"In menu area. ",
"Set system color. There are 15 Kinds of color. ",
"Empty area. ",
"Empty area. ",
"Empty area. ",
"Grid on/off: Binding the selected point to the nearest cross. ",
"Line function:Select two points and draw a line between them. ",
"Empty menu. ",
"Empty menu. ",
"Empty menu. ",
"Empty menu. ",
"Empty menu. ",
"Empty menu. ",
"Empty menu. ",
"Empty menu. ",
"Empty menu. ",
"Empty menu. ",
"Empty menu. ",
"Empty menu. ",
"Empty menu. ",
"Empty menu. ",
"Empty menu. ",
"Empty menu. ",
"Empty menu. ",
"Clear current drawing area. ",
"Exit to DOS. "};

int gcolor=15,fGrid=0;
void main()
{ int GraphDriver=DETECT,GraphMode;
initgraph(&GraphDriver,&GraphMode,0);

int VRES16COLOR,DEFAULTMODE;
int iButton;
setgraphmode(VRES16COLOR);
// registerfonts("H:\\c7\\lib\\*.fon");
ShowMenu();
if (!MouseInit()){
WrHint (" Warning! No mouse device was detected! ");
getch();
setgraphmode(DEFAULTMODE);
exit(1);
}
else
if (!MouseReset(&iButton)){
WrHint(" Warning! The mouse has some problem! ");
getch();
setgraphmode(DEFAULTMODE);
exit(2);
}
Process();
}
Seben 2005-12-20
  • 打赏
  • 举报
回复
C语言和C++不一样的~~C++和VC++不一样的~~
dx30611 2005-12-20
  • 打赏
  • 举报
回复
windows API
yongzhang_zhu 2005-12-20
  • 打赏
  • 举报
回复
谢了.
sms88 2005-12-20
  • 打赏
  • 举报
回复
先看看windows程序设计 第五版

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧