In upper-level software programming, it is often necessary to provide a calendar control that lets the user select a date or use the Calendar control to emphasize a particular day.
After the Month Calendar control control for MFC has been upgraded from the system to Windows 7, there are fewer actions you can do to date controls, and you cannot change the background color of a specified date. After searching the internet for a long time, it was found that the relationship between the operating system drawing style.
In such a case.
It is only possible to highlight the difference in the date by adding the font weight of a particular date (at least I think of it only).
First pull into the month Calendar control control and add a variable to the control called Monthcontrol. Set the day states and No Trailing Dates properties of the control to True.
Add the following code in the initialization code for project, for example:
int ncount; SYSTEMTIME Timefrom; SYSTEMTIME Timeuntil;ncount = Monthcontrol. Getmonthrange (&timefrom, &timeuntil, gmr_daystate);//Returns the number of months that the month calendar displays, after setting the No Trailing dates property of the control to True, A page displays only one month if (ncount = = 1) {lpmonthdaystate pdaystate;//date state struct pdaystate = new Monthdaystate[ncount];memset ( pDayState, 0, sizeof (monthdaystate) * ncount);pD aystate[0] |= 1 << (10-1);//Set the 10th of this month to bold verify (Monthcontrol. SetDayState (ncount, pDayState));//Set this month's 10th bold Delete[]pdaystate;}
When you compile and run project, you will see that the 10th number of the calendar is bold, but once you click on the month switch, the pages in other months will have a very large number of inexplicably bold dates. I take the approach is to add a response function in the control's Mcn_viewchange control event, so that a timer starts, for example, the following:
Timerun = true;//Timer start flag SetTimer (0, 10, 0);//Timer start
Join the WM_TIMER Message of the dialog box
int ncount; SYSTEMTIME Timefrom; SYSTEMTIME Timeuntil;ncount = Monthcontrol. Getmonthrange (&timefrom, &timeuntil, gmr_daystate);//Returns the number of months that the month calendar displays, after setting the No Trailing dates property of the control to True, A page displays only one month if (ncount = = 1) {lpmonthdaystate pdaystate;//date state struct pdaystate = new Monthdaystate[ncount];memset ( pDayState, 0, sizeof (monthdaystate) * ncount);pD aystate[0] |= 1 << (10-1);//Set the 10th of this month to bold verify (Monthcontrol. SetDayState (ncount, pDayState));//Set this month's 10th bold delete[]pdaystate;if (Timerun = true) {Timerun = false; KillTimer (0);}}
The response function is also added to the control's Mcn_selchange control event, such as the following:
int ncount; SYSTEMTIME Timefrom; SYSTEMTIME Timeuntil;ncount = Monthcontrol. Getmonthrange (&timefrom, &timeuntil, gmr_daystate);//Returns the number of months that the month calendar displays, after setting the No Trailing dates property of the control to True, A page displays only one month if (ncount = = 1) {lpmonthdaystate pdaystate;//date state struct pdaystate = new Monthdaystate[ncount];memset ( pDayState, 0, sizeof (monthdaystate) * ncount);pD aystate[0] |= 1 << (10-1);//Set the 10th of this month to bold verify (Monthcontrol. SetDayState (ncount, pDayState));//Set this month's 10th bold Delete[]pdaystate;}
to this. The 10th numbers on all month pages are bold, without affecting other dates. Assuming a little attention will find the above code repetition rate is very high. to look concise. Be able to encapsulate it with a function. This can reduce the code a lot.
Assume a combination of operations for directories and files. The ability to search all the drive characters specified in the directory specifies the suffix file. The calendar is then bold with the same date as the date the file was changed. The code is as follows:
void Cdatatestdlg::selectfile () {WCHAR Drive[max_path] = {0};int i = 0; GetLogicalDriveStrings (MAX_PATH, drive); CString myDrive; CString Strdbpath;bool dayset[33] = {0};int datlong = 0;int ncount;int month = 0;while (drive[i-1]! = ' | | | Drive[i]! = ' + ') {myDrive = L ""; myDrive = drive[i];mydrive = myDrive + L ": \ \"; Strdbpath = myDrive + L "h264 video";//Search All drive letter names below Directory Htreeitem HItem for "H264 video", Hsubitem;if (PathFileExists (Strdbpath))//inferred directory exists {CFileFind finder;vector<cstring > allfile;vector<cstring> allfile1; CString Mydrivewholefilepath;mydrivewholefilepath = Strdbpath + L "\\*.*"; bool bworking = finder. FindFile (Mydrivewholefilepath); while (bworking) {bworking = finder. FindNextFile (); Allfile.push_back (finder. GetFileName ());//All files. The name of the directory}for (int i = 0; I<allfile.size (); i++) {int flag = Allfile[i]. Find (L ". H264");//Locate the directory with the following suffix ". H264" for the file if (flag >= 0) {CString Myfilepath;myfilepath = strdbpath + L "\ \" + allfile[i];// Gets the change date of the specified path file systemtime time; CStdioFile File;file. Open (MYFIlepath, Cfile::moderead); FILETIME File_time; Getfiletime (file.m_hfile, NULL, NULL, &file_time); FileTimeToSystemTime (&file_time, &time); SYSTEMTIME Timefrom; SYSTEMTIME Timeuntil;ncount = Monthcontrol. Getmonthrange (&timefrom, &timeuntil, gmr_daystate); if (timefrom.wyear = = Time.wyear && Timefrom.wmonth = = time.wmonth) {month = Timefrom.wmonth;datlong = Timeuntil.wday-timefrom.wday;dayset[time.wday] = true;} File. Close ();}} Finder. Close ();} i + = 4;} if (ncount = = 1) {lpmonthdaystate pdaystate;//date state struct pdaystate = new Monthdaystate[ncount];memset (pdaystate, 0, sizeof ( monthdaystate) * ncount); for (int i = 0; I <= datlong + 1; i++) {if (dayset[i] = True) {pdaystate[0] |= 1 << (i-1 );}} VERIFY (Monthcontrol. SetDayState (ncount, pdaystate));d elete[]pdaystate;}}
MFC Month Calendar Control control uses