#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); } |