请c语言高手相助,在TC30下,关于图形和鼠标驱动的程序。

hanyh 2001-10-17 11:19:07
我的程序的本意是用鼠标在屏幕上点击,点左键3次,然后就画一个三角形,点右
键便退出。点左键50次的时候,点任意键也退出。可是程序运行的时候有问题,
画三角形的时候总怪怪的,请高手帮我一下!

******graph.h******//图形初始化,结束头文件

#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>

void InitGraphics();
void CloseGraphics();




void InitGraphics()
{
int gdriver = VGA, gmode=VGAHI, errorcode;

if (!(registerbgidriver(EGAVGA_driver)<0))
{
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode == grOk)
return;
}
cout << "Error detected when initialize graphics...\n";
cout << "No VGA compatible graphics card is found...\n";
getch();
exit(1);
}

void CloseGraphics() {
getch();
cleardevice();
closegraph();
}



******mouse.h******//鼠标驱动头文件

#include <dos.h>

enum MouseDisplayMode {
MOUSE_OFF, MOUSE_ON,
};

int x, y;
int mDispStatus, mNumKeys, mKey;
int mKeyPressed, mInstalled;
union REGS inregs, outregs;

int GetX() { return x; }
int GetY() { return y; }
void GetXY(int& px, int& py) { px = x; py = y; }

void Reset(void);
void ShowCursor(void);
void HideCursor(void);
void GetButtonStatus(void);
void SetMinMaxHorzCursPos(int min,int max);
void SetMinMaxVertCursPos(int min,int max);
int LeftButtonPressed(void);
int RightButtonPressed(void);

void Reset(void)
{
inregs.x.ax = 0x00;
int86(0x33, &inregs, &outregs);
mInstalled = (outregs.x.ax > 0);

if(mInstalled)
mNumKeys = outregs.x.bx;
else
mNumKeys = 0;

mDispStatus = MOUSE_OFF;
}

void ShowCursor(void)
{
if(mDispStatus == MOUSE_OFF) {
inregs.x.ax = 0x01;
int86(0x33, &inregs, &outregs);
mDispStatus = MOUSE_ON;
}
}

void HideCursor(void)
{
if (mDispStatus) {
inregs.x.ax = 0x02;
int86(0x33, &inregs, &outregs);
mDispStatus = MOUSE_OFF;
}
}

void GetButtonStatus()
{
inregs.x.ax = 0x03;
int86(0x33, &inregs, &outregs);
mKey = outregs.x.bx;
x = outregs.x.cx;
y = outregs.x.dx;
}

int LeftButtonPressed(void)
{
delay(50);
GetButtonStatus();
mKeyPressed = (mKey == 1);
return mKeyPressed;
}

int RightButtonPressed(void)
{
delay(50);
GetButtonStatus();
mKeyPressed = (mKey == 2);
return mKeyPressed;
}


void SetMinMaxHorzCursPos(int min, int max)
{
inregs.x.ax = 0x07;
inregs.x.cx = min;
inregs.x.dx = max;
int86(0x33, &inregs, &outregs);
}

void SetMinMaxVertCursPos(int min, int max)
{
inregs.x.ax = 0x08;
inregs.x.cx = min;
inregs.x.dx = max;
int86(0x33, &inregs, &outregs);
}


******main.cpp******

#include"graph.h"
#include"mouse.h"

main()
{
int a,b,i,gx[50],gy[50],flag;
InitGraphics();
Reset();
SetMinMaxHorzCursPos(0,639);
SetMinMaxVertCursPos(0,479);
ShowCursor();
for(a=1;a<50;a++)
{
flag=1;
while(flag)
{
if(LeftButtonPressed()) //点左键
{
GetXY(gx[a],gy[a]);
flag=0;
}
if(RightButtonPressed()) //点右键退出
{
sound(2000);
delay(500);
nosound();
closegraph();
exit(0);
}
}
if(a%3==0) //如果点左键是3的倍数,就连接最近3个点成一

//三角形
{
HideCursor();
for(b=a,i=1;i<3;i++,b--)
line(gx[b],gy[b],gx[b-1],gy[b-1]);
line(gx[b],gy[b],gx[b+2],gy[b+2]);
ShowCursor();
}
}
sound(2000);
delay(500);
nosound();
CloseGraphics();
return 0;
}
...全文
107 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hanyh 2001-10-19
  • 打赏
  • 举报
回复
谢谢两位,你们的方法跟我的差不多,我最后终于找到了错误的根源,问题现已经解决。
lormee 2001-10-18
  • 打赏
  • 举报
回复
画三角形的语句有问题。不要使用循环来做,直接来三个画线语句:
line(gx[b],gy[b],gx[b-1],gy[b-1]);
line(gx[b-1],gy[b-1],gx[b-2],gy[b-2]);
line(gx[b],gy[b],gx[b+2],gy[b+2]);
我想这样子应该就不会怪怪的了。

livelivelive 2001-10-18
  • 打赏
  • 举报
回复
现在可以了,改动较大,for(a=1;a<50,a++) 中的a++有问题,已改为while结构,line语句也改了下,整个程序在一个文件中即可,不必分多个文件。注意,将EGAVGA.BGI分别放到输出exe的和tc所在的目录中,否则画线时会产生initgraph错误!
/******graph.h******///图形初始化,结束头文件

#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>

void InitGraphics();
void CloseGraphics();

void InitGraphics()
{
int gdriver = VGA, gmode=VGAHI, errorcode;
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode == grOk)
return;
}

void CloseGraphics() {
getch();
cleardevice();
closegraph();
}



/*******mouse.h******///鼠标驱动头文件

#include <dos.h>

enum MouseDisplayMode {
MOUSE_OFF, MOUSE_ON,
};

int x, y;
int mDispStatus, mNumKeys, mKey;
int mKeyPressed, mInstalled;
union REGS inregs, outregs;

int GetX() { return x; }
int GetY() { return y; }
void GetXY(int& px, int& py) { px = x; py = y; }

void Reset(void);
void ShowCursor(void);
void HideCursor(void);
void GetButtonStatus(void);
void SetMinMaxHorzCursPos(int min,int max);
void SetMinMaxVertCursPos(int min,int max);
int LeftButtonPressed(void);
int RightButtonPressed(void);

void Reset(void)
{
inregs.x.ax = 0x00;
int86(0x33, &inregs, &outregs);
mInstalled = (outregs.x.ax > 0);

if(mInstalled)
mNumKeys = outregs.x.bx;
else
mNumKeys = 0;

mDispStatus = MOUSE_OFF;
}

void ShowCursor(void)
{
if(mDispStatus == MOUSE_OFF) {
inregs.x.ax = 0x01;
int86(0x33, &inregs, &outregs);
mDispStatus = MOUSE_ON;
}
}

void HideCursor(void)
{
if (mDispStatus) {
inregs.x.ax = 0x02;
int86(0x33, &inregs, &outregs);
mDispStatus = MOUSE_OFF;
}
}

void GetButtonStatus()
{
inregs.x.ax = 0x03;
int86(0x33, &inregs, &outregs);
mKey = outregs.x.bx;
x = outregs.x.cx;
y = outregs.x.dx;
}

int LeftButtonPressed(void)
{
delay(50);
GetButtonStatus();
mKeyPressed = (mKey == 1);
return mKeyPressed;
}

int RightButtonPressed(void)
{
delay(50);
GetButtonStatus();
mKeyPressed = (mKey == 2);
return mKeyPressed;
}


void SetMinMaxHorzCursPos(int min, int max)
{
inregs.x.ax = 0x07;
inregs.x.cx = min;
inregs.x.dx = max;
int86(0x33, &inregs, &outregs);
}

void SetMinMaxVertCursPos(int min, int max)
{
inregs.x.ax = 0x08;
inregs.x.cx = min;
inregs.x.dx = max;
int86(0x33, &inregs, &outregs);
}


/******main.cpp******/

//#include"graph.h"
//#include"mouse.h"

main()
{
int a,b,i,gx[50],gy[50],flag;
InitGraphics();
Reset();
SetMinMaxHorzCursPos(0,639);
SetMinMaxVertCursPos(0,479);
ShowCursor();
// for(a=1;a<50;a++)
a=0;
while(a<50)
{
flag=1;
while(flag){
if(LeftButtonPressed()) //点左键
{
GetXY(gx[a],gy[a]);
flag=0;
a++;
}
if(RightButtonPressed()) //点右键退出
{
sound(2000);
delay(500);
nosound();
closegraph();
exit(0);
}
}//while(flag)

if(a%3==0&&a!=0) //注意a!=0
{
HideCursor();
line(gx[a-3],gy[a-3],gx[a-2],gy[a-2]);
line(gx[a-3],gy[a-3],gx[a-1],gy[a-1]);
line(gx[a-2],gy[a-2],gx[a-1],gy[a-1]);
ShowCursor();
}//if(a%3==0)

sound(2000);
delay(100);
nosound();
};//while(a<=50)
getch();
CloseGraphics();
return 0;
}
livelivelive 2001-10-18
  • 打赏
  • 举报
回复
我正在看源程序
hanyh 2001-10-18
  • 打赏
  • 举报
回复
我试过了,不是这个地方错了,我想可能我在确定鼠标点饥的时候还应该写一个接收函数,正在测试!
1. C 语言中的指针和内存泄漏 5 2. C语言难点分析整理 10 3. C语言难点 18 4. C/C++实现冒泡排序算法 32 5. C++中指针和引用的区别 35 6. const char*, char const*, char*const的区别 36 7. C中可变参数函数实现 38 8. C程序内存中组成部分 41 9. C编程拾粹 42 10. C语言中实现数组的动态增长 44 11. C语言中的位运算 46 12. 浮点数的存储格式: 50 13. 位域 58 14. C语言函数二维数组传递方法 64 15. C语言复杂表达式的执行步骤 66 16. C语言字符串函数大全 68 17. C语言宏定义技巧 89 18. C语言实现动态数组 100 19. C语言笔试-运算符和表达式 104 20. C语言编程准则之稳定篇 107 21. C语言编程常见问题分析 108 22. C语言编程易犯毛病集合 112 23. C语言缺陷与陷阱(笔记) 119 24. C语言防止缓冲区溢出方法 126 25. C语言高效编程秘籍 128 26. C运算符优先级口诀 133 27. do/while(0)的妙用 134 28. exit()和return()的区别 140 29. exit子程序终止函数与return的差别 141 30. extern与static存储空间矛盾 145 31. PC-Lint与C\C++代码质量 147 32. spirntf函数使用大全 158 33. 二叉树的数据结构 167 34. 位运算应用口诀和实例 170 35. 内存对齐与ANSI C中struct内存布局 173 36. 冒泡和选择排序实现 180 37. 函数指针数组与返回数组指针的函数 186 38. 右左法则- 复杂指针解析 189 39. 回车和换行的区别 192 40. 堆和堆栈的区别 194 41. 堆和堆栈的区别 198 42. 如何写出专业的C头文件 202 43. 打造最快的Hash表 207 44. 指针与数组学习笔记 222 45. 数组不是指针 224 46. 标准C中字符串分割的方法 228 47. 汉诺塔源码 231 48. 洗牌算法 234 49. 深入理解C语言指针的奥秘 236 50. 游戏外挂的编写原理 254 51. 程序实例分析-为什么会陷入死循环 258 52. 空指针究竟指向了内存的哪个地方 260 53. 算术表达式的计算 265 54. 结构体对齐的具体含义 269 55. 连连看AI算法 274 56. 连连看寻路算法的思路 283 57. 重新认识:指向函数的指针 288 58. 链表的源码 291 59. 高质量的子程序 295 60. 高级C语言程序员测试必过的十六道最佳题目+答案详解 297 61. C语言常见错误 320 62. 超强的指针学习笔记 325 63. 程序员之路──关于代码风格 343 64. 指针、结构体、联合体的安全规范 346 65. C指针讲解 352 66. 关于指向指针的指针 368 67. C/C++ 误区一:void main() 373 68. C/C++ 误区二:fflush(stdin) 376 69. C/C++ 误区三:强制转换 malloc() 的返回值 380 70. C/C++ 误区四:char c = getchar(); 381 71. C/C++ 误区五:检查 new 的返回值 383 72. C 是 C++ 的子集吗? 384 73. C和C++的区别是什么? 387 74. 无条件循环 388 75. 产生随机数的方法 389 76. 顺序表及其操作 390 77. 单链表的实现及其操作 391 78. 双向链表 395 79. 程序员数据结构笔记 399 80. Hashtable和HashMap的区别 408 81. hash 表学习笔记 410 82. C程序设计常用算法源代码 412 83. C语言有头结点链表的经典实现 419 84. C语言惠通面试题 428 85. C语言常用宏定义 450

70,026

社区成员

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

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