巧妙利用键盘控制键来显示日历的方法
时间:2014-04-21 15:40 来源: 我爱IT技术网 作者:
要想实现通过键盘的控制键来显示不同的年份、月份的日历,就需要用到几个windows API函数,这些函分别是:
SetConsoleCursorPosition:这个函数用来设置输出的位置。
SetConsoleTextAttribute:这个函数用来设置字体以及背景的颜色。
另外需要介绍的是:
通过按下键盘的按键,有的是返回16位的整数,有的是返回32位的整数,而后者的高位必然是0xe0。当调用getch函数的时候,当其返回值为0xe0,那么必然还有一个低16位的值存放于缓冲区,这个时候,就需要再一次调用getch函数将数据取出,否则,可能会导致程序不能正常的运行。当然如果调用getwch,只需要调用一次就能够将缓冲区中的数据全部取出。在这里就需要用到这个知识。
使用控制键的代码如下所示:
void ChangeCursorPosition(int x , int y) { COORD position; HANDLE handle; position.X = x; position.Y = y; handle = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(handle , position); } void ChangeTextColor(int first_number , int second_number , int select) { HANDLE handle; COORD first_position; first_position.X = 0; first_position.Y = 0; handle = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(handle , BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE); SetConsoleCursorPosition(handle , first_position); printf(" "); if(1 == select) { SetConsoleCursorPosition(handle , first_position); SetConsoleTextAttribute(handle , BACKGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); printf("%7d" , first_number); SetConsoleTextAttribute(handle , FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); printf("%7d" , second_number); } else if(2 == select) { SetConsoleCursorPosition(handle , first_position); SetConsoleTextAttribute(handle , FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); printf("%7d" , first_number); SetConsoleTextAttribute(handle , BACKGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); printf("%7d" , second_number); } printf("\n"); SetConsoleTextAttribute(handle , FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); } int key_move(int *first , int *second) { int answer; static int key_select = 1; answer = getch(); if(answer == 0xe0) { answer = getch(); switch(answer) { case 0x48: //up if(key_select == 1) { (*first)--; if((*first) < 1900) (*first) = 1900; } else if(key_select == 2) { (*second)--; if((*second) <= 0) { *second = 12; (*first)--; } } break; case 0x50: //down if(key_select == 1) (*first)++; else if(key_select == 2) { (*second)++; if((*second) > 12) { *second = 1; (*first)++; } } break; case 0x4B: //left key_select--; if(key_select < 1) key_select = 1; break; case 0x4D: //right key_select++; if(key_select > 2) key_select = 2; break; } } else if(answer == 0x1b) { printf("\n"); exit(0); } return key_select; }
另外,我编写了一个类用来输出日历,其代码为:
#ifndef DATA_CLASS_H_H_CLASS_DATA #define DATA_CLASS_H_H_CLASS_DATA class calendar { private: int month; int year; public: calendar(int month = 0 , int year = 0); int all_year_days(); int all_month_day(int month); void show_month_day(int month); public: void set_year(int year); void set_month(int month); int get_year() const; int get_month() const; }; #endif
其实现部分:
#include "data_class.h" #include <iostream> using namespace std; calendar::calendar(int month , int year) { this->month = month ? month : 1; this->year = year ? year : 1900; } int calendar::get_year() const { return this->year; } int calendar::get_month() const { return this->month; } void calendar::set_month(int month) { this->month = month; } void calendar::set_year(int year) { this->year = year; } int calendar::all_year_days() { int leap_number = 0; int nonleap_number = 0; for(int i = 1900 ; i < this->year ; i++) { if((i % 4 == 0 && i % 100 != 0 ) || (i % 400 == 0)) { leap_number++; } else { nonleap_number++; } } return leap_number * 366 + nonleap_number * 365; } int calendar::all_month_day(int month) { int month_day[] = {31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31}; int days = all_year_days(); if((this->year % 4 == 0 && this->year % 100 != 0) || (this->year % 400 == 0)) { month_day[1] = 29; } for(int i = 0 ; i < month - 1 ; i++) { days += month_day[i]; } return days; } void calendar::show_month_day(int month) { int month_day[] = {31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31}; int days = all_month_day(month); if((this->year % 4 == 0 && this->year % 100 != 0) || (this->year % 400 == 0)) { month_day[1] = 29; } for(int i = 0 ; i <= days % 7 ; i++) { cout<<" "; cout.width(5); } for(i = 1 ; i <= month_day[month - 1] ; i++) { if((i + days % 7) % 7 == 0) cout<<endl; cout<<i; cout.width(5); } cout<<endl; }
main函数的代码为:
void screen_clear(void) { int i , j; for(i = 0 ; i < 18 ; i++) { for(j = 0 ; j < 40 ; j++) { printf(" "); } } } int main(void) { calendar data(3 , 2014); int year = 2014; int month = 3; int key_select; printf("%7d%7d" , year , month); while(1) { key_select = key_move(&year , &month); ChangeTextColor(year , month , key_select); screen_clear(); ChangeCursorPosition(0 , 1); data.set_year(year); data.set_month(month); data.show_month_day(month); } return 0; }
这个程序运行后的截图为:

本文来源 我爱IT技术网 http://www.52ij.com/jishu/5418.html 转载请保留链接。
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
