选择显示字体大小

控制台窗口界面的编程控制(六)


  十、读取鼠标信息

  与读取键盘信息方法相似,鼠标信息也是通过readconsoleinput来获取的,其mouse_event_record具有下列定义:

typedef struct _mouse_event_record {
 coord dwmouseposition; // 当前鼠标位置
 dword dwbuttonstate; // 鼠标按钮状态
 dword dwcontrolkeystate; // 键盘控制键状态
 dword dweventflags; // 事件状态
} mouse_event_record;

  其中,dwbuttonstate反映了用户按下鼠标按钮的情况,它可以是:from_left_1st_button_pressed(最左边按钮)、rightmost_button_pressed(最右边按钮)、from_left_2nd_button_pressed(左起第二个按钮)、from_left_3rd_button_pressed(左起第三个按钮)和from_left_4th_button_pressed (左起第四个按钮)。而dweventflags表示鼠标的事件,如double_click(双击)、mouse_moved(移动)和mouse_wheeled(滚轮滚动,只适用于windows 2000/xp)。dwcontrolkeystate的含义同前。

  下面举一个例子。这个例子能把鼠标的当前位置显示在控制台窗口的最后一行上,若单击鼠标左键,则在当前位置处写一个字符‘a’,若双击鼠标任一按钮,则程序终止。具体代码如下:

#include <windows.h>
#include <stdio.h>
#include <string.h>
handle hout;
handle hin;
void clearscreen(void);
void dispmousepos(coord pos); // 在最后一行显示鼠标位置
void main()
{
 hout = getstdhandle(std_output_handle); // 获取标准输出设备句柄
 hin = getstdhandle(std_input_handle); // 获取标准输入设备句柄
 word att = foreground_red foreground_green foreground_intensity  
       background_blue ;
 // 背景是蓝色,文本颜色是黄色
 setconsoletextattribute(hout, att);
 clearscreen(); // 清屏
 input_record mouserec;
 dword state = 0, res;
 coord pos = {0, 0};
 for(;;) // 循环
 {
  readconsoleinput(hin, &mouserec, 1, &res);
  if (mouserec.eventtype == mouse_event){
   if (mouserec.event.mouseevent.dweventflags == double_click) break;
    // 双击鼠标退出循环
    pos = mouserec.event.mouseevent.dwmouseposition;
    dispmousepos(pos);
    if (mouserec.event.mouseevent.dwbuttonstate == from_left_1st_button_pressed)
          fillconsoleoutputcharacter(hout, 'a', 1, pos, null);
   }
  }
  pos.x = 0; pos.y = 0;
  setconsolecursorposition(hout, pos); // 设置光标位置
  closehandle(hout); // 关闭标准输出设备句柄
  closehandle(hin); // 关闭标准输入设备句柄
 }

void dispmousepos(coord pos) // 在最后一行显示鼠标位置
{
 console_screen_buffer_info binfo;
 getconsolescreenbufferinfo( hout, &binfo );
 coord home = {0, binfo.dwsize.y-1};
 word att0 = background_intensity ;
 fillconsoleoutputattribute(hout, att0, binfo.dwsize.x, home, null);
 fillconsoleoutputcharacter(hout, ' ', binfo.dwsize.x, home, null);
 char s[20];
 sprintf(s,"x = %2lu, y = %2lu",pos.x, pos.y);
 setconsoletextattribute(hout, att0);
 setconsolecursorposition(hout, home);
 writeconsole(hout, s, strlen(s), null, null);
 setconsoletextattribute(hout, binfo.wattributes); // 恢复原来的属性
 setconsolecursorposition(hout, binfo.dwcursorposition); // 恢复原来的光标位置
}

void clearscreen(void)
{
 console_screen_buffer_info binfo;
 getconsolescreenbufferinfo( hout, &binfo );
 coord home = {0, 0};
 unsigned long size = binfo.dwsize.x * binfo.dwsize.y;
 fillconsoleoutputattribute(hout, binfo.wattributes, size, home, null);
 fillconsoleoutputcharacter(hout, ' ', size, home, null);
}

 
  十一、结语

  综上所述,利用控制台窗口的widows api函数可以设计简洁美观的文本界面,使得用visual c++ 6.0开发环境深入学习c++以及文本界面设计成为一件比较容易的事件。当然文本界面的设计还需要一定的方法和技巧,限于篇幅,这里不再阐述。


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons