Example Calendar Control
Site Map Feedback

Download:

Up Calendar ColorButton CListCtrl DragBar Icons Subclassing
This class is as example of several principles for the creation of a quick and efficient control in MFC.
To create a simple control class just use ClassWizard [Add Class...][New...] and make the class derived from CButton.
Drag out a Button on the dialog, then set the Button's [OwnerDraw] Property to checked.
Now to draw the control, the Virtual Function DrawItem (NOT OnDrawItem) is needed, so use ClassWizard to add it and add an OnLButtondown handler as well.
Create an instance of your class in your Dialog class (Header File).
In your dialog OnInitDlg() subclass the control to your class using:
  Calendar.SubclassDlgItem(IDC_Calendar, this);
Derive a class of your own from CCalendar that implements OnChanging() and OnChanged() if you need to.

You can set the current Day, Month and Year in many ways.
The default Constructor sets the Calendar to Today.
Month and Day are 1 Based, Year is 1900 based and can go up to 137.
You can also set the Background Colour, Text Colour and Specify some extra text for any day in the month using:
  Calendar.SetBgColor(RGB(255,255,0));
  Calendar.SetTextColor(RGB(255,0,0), 17);
  Calendar.SetText("This is the day!", 17);
After altering the Month or Year you may want to redraw the control, so use:
  Calendar.RedrawWindow();
to restore the default colours and Text use:
  Calendar.Clear();
If you change the Month or Year and don't want the Day to remain selected, set Day to -1 or 0xFF.

Its worth trying to understand the way the maths is used to draw the buttons for any sized control.
Notice how the drawing isn't exactly optimised, but doesn't fill the background all the time (if it did the whole control would flash).

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


 (1703) Last modified: Fri, 15 May 2009 15:27:18 +0100