十、读取鼠标信息
与读取键盘信息方法相似,鼠标信息也是通过readconsoleinput来获取的,其mouse_event_record具有下列定义:
| typedef struct _mouse_event_record { coord dwmouseposition; // 当前鼠标位置 dword dwbuttonstate; // 鼠标按钮状态 dword dwcontrolkeystate; // 键盘控制键状态 dword dweventflags; // 事件状态 } mouse_event_record; |
| #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); } |
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 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器