易码技术论坛

 找回密码
 加入易码
搜索
查看: 192091|回复: 10

请教转贴输入函数中的问题?

[复制链接]
发表于 2006-2-14 10:00:00 | 显示全部楼层
请问你的函数里的光标怎样改为不停闪动的效果?
发表于 2006-2-13 17:00:00 | 显示全部楼层
貌似偶的代码……
word&0xff是取word的低8位,因为word是int(2Byte)所以对于双字节字符需要把word转成两个char。
 楼主| 发表于 2006-2-13 17:09:00 | 显示全部楼层
真是你的?太好了,我发现*(textAdd+strPt)=word 这样也可以取低字节的,那你说说要是去掉0xff不会有什么影响吧?
 楼主| 发表于 2006-2-13 17:12:00 | 显示全部楼层
真是你的?太好了,我发现*(textAdd+strPt)=word了也是取低字节的,那你说说要是去掉0xff不会有什么影响吧?
发表于 2006-2-13 17:19:00 | 显示全部楼层
没有试过去掉&0xff,这涉及到类型转化,因为不清楚LavaX具体是怎么操作的,所以就加上&0xff,保险一点。
发表于 2006-2-14 15:17:00 | 显示全部楼层
试试看这个

  1. //按键定义
  2. #define  KEY_PAGEUP   19  
  3. #define  KEY_PAGEDOWN 14
  4. #define  KEY_LEFT     23
  5. #define  KEY_RIGHT    22
  6. #define  KEY_UP       20
  7. #define  KEY_DOWN     21
  8. #define  KEY_ENTER    13
  9. #define  KEY_SHIFT    18
  10. #define  KEY_CAPS     26
  11. #define  KEY_ESC      27
  12. #define  KEY_HELP     25
  13. #define  KEY_SPC   32
  14. #define  KEY_F1       28
  15. #define  KEY_F2       29
  16. #define  KEY_F3       30
  17. #define  KEY_F4       31
  18. //相关常量
  19. #define  EDIT_LOW   0//小写
  20. #define  EDIT_UPP   1//大写
  21. #define  EDIT_ENG     0//英文(数字)
  22. #define  EDIT_CHN     1//中文
  23. #define  EDIT_INS   0//插入
  24. #define  EDIT_OVR   1//覆盖
  25. #define  FONT_SMA   0//小字体
  26. #define  FONT_LAR   1//大字体
  27. #define  CURSOR_DELAY 10//光标延时
  28. //全局变量
  29. //常用英文符号
  30. char textBuf[50]={0};
  31. char editCase,editLang,editMode;//编辑时的大小写、语言、模式(插入、覆盖)

  32. char Input(long textAdd,char length,char textX, char textY, char font)
  33. {
  34. char strPt;//字串指针、长度
  35. char key,lastMs;
  36. char curX,curY,curL;//光标坐标、长度
  37. int  word;
  38. strPt = 0;
  39. key = 0;
  40. if (font == FONT_SMA){
  41.   curL = 6;
  42.   curY = textY + 11;
  43. }
  44. else{
  45.   curL = 8;
  46.   curY = textY + 15;
  47. }
  48. while (1)
  49. {
  50.   Refresh();
  51.   TextOut(textX,textY,textAdd,0x40+0x80 * font);//显示当前字串
  52.   curX = textX + strPt * curL;
  53.   do{//绘制光标
  54.    lastMs = Getms();
  55.    if (editMode == EDIT_INS){
  56.     if (*(textAdd+strPt) >= 0xa1){
  57.      Line(curX,curY,curX + curL * 2 - 1,curY,2);
  58.     }else{
  59.      Line(curX,curY,curX + curL - 1,curY,2);
  60.     }
  61.    }else{
  62.     if (*(textAdd+strPt) >= 0xa1){
  63.      Box(curX,curY - curL * 2,curX + curL * 2 - 1,curY,1,2);
  64.     }else{
  65.      Box(curX,curY - curL * 2,curX + curL - 1,curY,1,2);
  66.     }
  67.    }
  68.    if (key = CheckKey(128)){
  69.     ReleaseKey(128);
  70.     break;
  71.    }
  72.    while (((Getms()-lastMs)&0xff)< CURSOR_DELAY * 1000 / 256); //光标延时
  73.   }while(1)
  74.   if (key == KEY_ENTER || key == KEY_ESC){
  75.    break;
  76.   }
  77.   if (key == KEY_F1){//切换插入、覆盖
  78.    if (editMode == EDIT_INS){
  79.     editMode = EDIT_OVR;
  80.    }else{
  81.     editMode = EDIT_INS;
  82.    }
  83.    continue;
  84.   }
  85.   if (key == KEY_LEFT){//左移光标
  86.    if (strPt > 0){
  87.     if (*(textAdd+strPt-1) >= 0xa1){
  88.      strPt = strPt - 2;
  89.     }else{
  90.      strPt--;
  91.     }
  92.    }
  93.    continue;
  94.   }
  95.   if (key == KEY_RIGHT){//右移光标
  96.    if (strPt < strlen(textAdd)){
  97.     if (*(textAdd+strPt) >= 0xa1){
  98.      strPt = strPt + 2;
  99.     }else{
  100.     strPt++;
  101.     }
  102.    }
  103.    continue;   
  104.   }
  105.   if (key == KEY_F2){//删除字符
  106.    if (*(textAdd+strPt) == NULL){
  107.     if (strPt > 0){
  108.      if (*(textAdd + strPt - 1) >= 0xa1){
  109.       strPt = strPt - 2;
  110.      }else{
  111.       strPt--;
  112.      }
  113.     }
  114.    }
  115.    if (*(textAdd+strPt) >= 0xa1){
  116.     strcpy(textBuf,textAdd + strPt + 2);
  117.    }else if (*(textAdd+strPt) != NULL){
  118.     strcpy(textBuf,textAdd + strPt + 1);
  119.    }
  120.    strcpy(textAdd + strPt,textBuf);
  121.    continue;
  122.   }
  123.   //加入字符
  124.   if (isgraph(key)){
  125.    word = key;
  126.   }else{
  127.    word = GetWord(0);
  128.   }
  129.   if (word == 0){
  130.    continue;
  131.   }
  132.   if (editMode == EDIT_INS || *(textAdd+strPt) == NULL){//插入模式,或是光标在最后为新加的字符腾出空间
  133.    if (word >> 8 != 0 && strlen(textAdd) >= length - 1){//判断长度是否超过规定长度
  134.     continue;
  135.    }
  136.    if (word >> 8 == 0 && strlen(textAdd) >= length){
  137.     continue;
  138.    }
  139.    strcpy(textBuf,textAdd + strPt);
  140.    if (word >> 8 != 0){
  141.     strcpy(textAdd + strPt + 2,textBuf);
  142.    }else{
  143.     strcpy(textAdd + strPt + 1,textBuf);
  144.    }
  145.   }
  146.   if (word >> 8 != 0){//汉字
  147.    if (*(textAdd+strPt) < 0xA1 && editMode == EDIT_OVR){//覆盖英文或数字
  148.     if (strlen(textAdd) >= length) continue;
  149.     strcpy(textBuf,textAdd + strPt);
  150.     strcpy(textAdd + strPt + 1,textBuf);
  151.    }
  152.    *(textAdd+strPt)=word & 0xff;
  153.    *(textAdd+strPt+1)=word >> 8;
  154.    strPt=strPt+2;
  155.   }else{//英文或数字
  156.    if (strlen(textAdd) >=length && *(textAdd+strPt) == NULL) continue;
  157.    if (*(textAdd+strPt)>=0xA1 && editMode == EDIT_OVR){//覆盖汉字
  158.     strcpy(textBuf,textAdd + strPt + 1);
  159.     strcpy(textAdd + strPt,textBuf);
  160.    }
  161.    *(textAdd+strPt)=word & 0xff;
  162.    strPt++;
  163.   }
  164. }
  165. if (key == KEY_ESC) return 0;
  166. return !0;
  167. }
  168. //测试函数
  169. void main(){
  170. char text[100];
  171. Input(text,20,10,10,FONT_SMA);
  172. }
复制代码

身边没有WQX,不好测试,你先看看这个

 楼主| 发表于 2006-2-15 09:53:00 | 显示全部楼层
请问若增加光标闪动会不会比不闪动的理论上慢?
发表于 2006-2-15 15:24:00 | 显示全部楼层
不明白楼上的意思……

这个应该和你击键的速度有关吧
 楼主| 发表于 2006-2-15 18:46:00 | 显示全部楼层
因为这一句“while (((Getms()-lastMs)&0xff)< CURSOR_DELAY * 1000 / 256); //光标延时”所以当我以相同击键的速度来输入的话,会比不闪动的慢一上述时间节拍的。
发表于 2006-2-15 19:28:00 | 显示全部楼层
那个是用了控桢,使得在各种不同速度的CPU上面有同样的运行速度。
在虚拟机上不觉得有什么,没有实机,不好测试。你在WQX上测试一下吧。可以调整光标延时的那个宏来控制。
 楼主| 发表于 2006-2-13 10:12:05 | 显示全部楼层 |阅读模式
转贴输入函数

//按键定义
#define  KEY_PAGEUP   19  
#define  KEY_PAGEDOWN 14
#define  KEY_LEFT     23
#define  KEY_RIGHT    22
#define  KEY_UP       20
#define  KEY_DOWN     21
#define  KEY_ENTER    13
#define  KEY_SHIFT    18
#define  KEY_CAPS     26
#define  KEY_ESC      27
#define  KEY_HELP     25
#define  KEY_SPC   32
#define  KEY_F1       28
#define  KEY_F2       29
#define  KEY_F3       30
#define  KEY_F4       31
//相关常量
#define  EDIT_LOW   0//小写
#define  EDIT_UPP   1//大写
#define  EDIT_ENG     0//英文(数字)
#define  EDIT_CHN     1//中文
#define  EDIT_INS   0//插入
#define  EDIT_OVR   1//覆盖
#define  FONT_SMA   0//小字体
#define  FONT_LAR   1//大字体
//全局变量
//常用英文符号
char symbol[32]={'.',',',';','-','/','?','!','$','%',
    '(',')',':','`','\'','\"','<','>','{',
    '}','[',']','#','~','@','^','&','*',
    '+','=','_','|','\\'};
char textBuf[50]={0};
char editCase,editLang,editMode;//编辑时的大小写、语言、模式(插入、覆盖)
//函数定义
int  GetWord(char lastKey);//返回一个字符,取消输入返回0
int  GetSymbol();//显示符号选择并返回,取消输入返回0
char Input(addr textAdd,char length,char textX, char textY, char font);//获取一个字符串保存到textAdd,取消输入返回0
//函数实现
int GetSymbol(){
char cho;
char key;
char i;
char index;
char sum;
index = 0;
sum = 31;
cho = 0;
key = 0;
do{
  Block(1,66,159,78,0x40);
  TextOut(2,67,"符",0x41);
  Block(1,66,13,78,0x42);
  memset(textBuf,0,20);
  for(i=0;i<9;i++){
   textBuf[i*2] = symbol[index+i];
   textBuf[i*2+1] = ' ';
   if (index+i>sum) break;
  }
  TextOut(52,67,textBuf,0x41);
  key = getchar();
  if (key == KEY_DOWN || key == KEY_RIGHT && index < sum - 9){
   index= index +9;
  }
  if (key == KEY_UP || key == KEY_LEFT && index >= 9){
   index = index -9;
  }
  if (key >= '1' && key <='9'){
   if (key-'1'+index>sum){
    continue;
   }else{
    return symbol[key-'1'+index];
   }
  }
  if (key == KEY_ESC){
   return 0;
  }
}while(TRUE);
}

int GetWord(char lastKey){
char key;
char charSum,charID;
char pyCode[10];
char pyPt;
char charCode[20];
long codeRight;
int  wideChar;
key = lastKey;
memset(pyCode,0,10);
memset(charCode,0,20);
charSum = 0;
charID = 0;
pyPt = 0;
do{  
  if (key == KEY_SHIFT){//切换输入语言
   if (editLang == EDIT_ENG){
    editLang = EDIT_CHN;
    Block(1,66,159,78,0x40);
    TextOut(2,67,"拼",0x41);
    Block(1,66,13,78,0x42);
    memset(pyCode,0,10);
    memset(charCode,0,20);
    charSum = 0;
    charID = 0;
    pyPt = 0;
   }
   else{
    editLang = EDIT_ENG;
    Block(1,66,159,78,0x40);
    TextOut(2,67,"英",0x41);
    Block(1,66,25,78,0x42);
   }
  }//切换大小写
  if (key == KEY_CAPS){
   if (editCase == EDIT_UPP){
    editCase = EDIT_LOW;
   }
   else{
    editCase = EDIT_UPP;
   }
  }
  if (editLang == EDIT_ENG){//英文数字输入
   if (key >= 'a' && key <= 'z'){
    if (editCase == EDIT_UPP){
     return key - 32;
    }else{
     return key;
    }
   }else if (key >= '0' && key <= '9'){
    return key;
   }else if (key == KEY_SPC){//空格
    return key;
   }else if (key == '.'){//.
    return key;
   }else if (key == '-'){//输入符号
    return GetSymbol();
   }else{
    return 0;
   }
  }else if (editLang == EDIT_CHN){//中文输入
   if ((key == KEY_LEFT || key == KEY_F2) && pyPt > 0){//删除最后一个拼音
    pyCode[--pyPt] = 0;
    charID = 0;
    charSum = 0;
   }else if (key == KEY_DOWN && charID + 9 < charSum ){//下翻页
     charID = charID + 9;
   }else if (key == KEY_UP && charID > 9){//上翻页
     charID = charID - 9;
   }else if (key >= 'a' && key <= 'z' && pyPt < 6){//输入拼音
    pyCode[pyPt++] = key;
    pyCode[pyPt] = 0;
    charID = 0;
    charSum = 0;
   }else if (key >= '1' && key <= '9'){//选字
   wideChar = charCode[(key - '1') * 2 + 1];
    wideChar = (wideChar << 8) + charCode[(key - '1') * 2];
    if (wideChar != 0){
     return wideChar;
    }
   }
   Block(15,67,159,79,0x40);
   TextOut(15,67,pyCode,0x41);
   memset(charCode,0,20);
   codeRight = System.PY2GB(charID,pyCode,charCode);
   if (codeRight != -1){
    charSum = codeRight >> 16;
    TextOut(52,67,charCode,0x41);
   }
  }
  key = getchar();
}while (key != KEY_ESC)
return 0;
}


char Input(addr textAdd,char length,char textX, char textY, char font)
{
char strPt;//字串指针、长度
char key;
char curX,curY,curL;//光标坐标、长度
int  word;
strPt = 0;
key = 0;
if (font == FONT_SMA){
  curL = 6;
  curY = textY + 11;
}
else{
  curL = 8;
  curY = textY + 15;
}
while (TRUE)
{
  Refresh();
  TextOut(textX,textY,textAdd,0x40+0x80 * font);//显示当前字串
  curX = textX + strPt * curL;
  if (editMode == EDIT_INS){//绘制光标
   if (*(textAdd+strPt) >= 0xa1){
    Line(curX,curY,curX + curL * 2 - 1,curY,2);
   }else{
    Line(curX,curY,curX + curL - 1,curY,2);
   }
  }else{
   if (*(textAdd+strPt) >= 0xa1){
    Box(curX,curY - curL * 2,curX + curL * 2 - 1,curY,1,2);
   }else{
    Box(curX,curY - curL * 2,curX + curL - 1,curY,1,2);
   }
  }
  if (editLang == EDIT_CHN){
   Block(1,66,159,78,0x40);
   TextOut(2,67,"拼",0x41);
   Block(1,66,13,78,0x42);
  }
  else{
   Block(1,66,159,78,0x40);
   TextOut(2,67,"英数",0x41);
   Block(1,66,25,78,0x42);
  }
  key = getchar();
  if (key == KEY_ENTER || key == KEY_ESC){
   break;
  }
  if (key == KEY_F1){//切换插入、覆盖
   if (editMode == EDIT_INS){
    editMode = EDIT_OVR;
   }else{
    editMode = EDIT_INS;
   }
   continue;
  }
  if (key == KEY_LEFT){//左移光标
   if (strPt > 0){
    if (*(textAdd+strPt-1) >= 0xa1){
     strPt = strPt - 2;
    }else{
     strPt--;
    }
   }
   continue;
  }
  if (key == KEY_RIGHT){//右移光标
   if (strPt < strlen(textAdd)){
    if (*(textAdd+strPt) >= 0xa1){
     strPt = strPt + 2;
    }else{
    strPt++;
    }
   }
   continue;   
  }
  if (key == KEY_F2){//删除字符
   if (*(textAdd+strPt) == NULL){
    if (strPt > 0){
     if (*(textAdd + strPt - 1) >= 0xa1){
      strPt = strPt - 2;
     }else{
      strPt--;
     }
    }
   }
   if (*(textAdd+strPt) >= 0xa1){
    strcpy(textBuf,textAdd + strPt + 2);
   }else if (*(textAdd+strPt) != NULL){
    strcpy(textBuf,textAdd + strPt + 1);
   }
   strcpy(textAdd + strPt,textBuf);
   continue;
  }
  //加入字符
  word = GetWord(key);
  if (word == 0){
   continue;
  }
  if (editMode == EDIT_INS || *(textAdd+strPt) == NULL){//插入模式,或是光标在最后为新加的字符腾出空间
   if (word >> 8 != 0 && strlen(textAdd) >= length - 1){//判断长度是否超过规定长度
    continue;
   }
   if (word >> 8 == 0 && strlen(textAdd) >= length){
    continue;
   }
   strcpy(textBuf,textAdd + strPt);
   if (word >> 8 != 0){
    strcpy(textAdd + strPt + 2,textBuf);
   }else{
    strcpy(textAdd + strPt + 1,textBuf);
   }
  }
  if (word >> 8 != 0){//汉字
   if (*(textAdd+strPt) < 0xA1 && editMode == EDIT_OVR){//覆盖英文或数字
    if (strlen(textAdd) >= length) continue;
    strcpy(textBuf,textAdd + strPt);
    strcpy(textAdd + strPt + 1,textBuf);
   }
   *(textAdd+strPt)=word & 0xff;
   *(textAdd+strPt+1)=word >> 8;

   strPt=strPt+2;
  }else{//英文或数字
   if (strlen(textAdd) >=length && *(textAdd+strPt) == NULL) continue;
   if (*(textAdd+strPt)>=0xA1 && editMode == EDIT_OVR){//覆盖汉字
    strcpy(textBuf,textAdd + strPt + 1);
    strcpy(textAdd + strPt,textBuf);
   }
   *(textAdd+strPt)=word & 0xff;   strPt++;
  }
}
if (key == KEY_ESC) return 0;
return !0;
}
//测试函数
void main(){
char text[100];
Input(text,20,0,0,FONT_SMA);
}
   

请问各位高手,能否解释红色部分的意思吗?对于那个word&0xff 也弄不明白,为何要加&0xff?
[em11][em11][em11]
您需要登录后才可以回帖 登录 | 加入易码

本版积分规则

Archiver|手机版|小黑屋|EMAX Studio

GMT+8, 2024-6-5 11:45 , Processed in 0.013600 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表