c # MonthCalendar paint the background of a color on several dates

1

Well I am starting with the C # and I have created an application of a calendar to practice with MySql database connection. In the calendar I want to show a monthCalendar with all the dates that have some annotation with the background of a different color. I have used the AddBoldedDate property to make the date bold, but I do not like it at all. Is there any property that can change so that the indicated days appear with the background of another color? How can I do this in a similar way to AddBoldedDate?

Edit: I had not put the code because it has nothing special, I use the MonthCalendar component that visualstudio provides. If it is convenient that you put some special part, just tell me and I put it.

MonthCalendar Code

private void mcFecha_DateChanged(object sender, DateRangeEventArgs e)
{
    foreach (Evento ev in eventos)
    {
        mcFecha.AddBoldedDate(ev.getEvento());
    }
}

thank you very much

    
asked by Ruben Manzano 23.10.2016 в 14:15
source

2 answers

1

In short, you can not do what you say .

How you'll see in the documentation the only personalization you have Available is global and there is no way to customize specific .

In this answer from SOen concur in the same and the solution is to use third-party components .

The one who recommends Pikoh in his comment is a good idea and being CodeProject you can integrate it without problem: Here the link

    
answered by 05.12.2016 в 16:55
0

It can be done but not in the "normal" way, since the control CalendarMonth is a system control, so even if you overwrite the void OnPaint(PaintEventArgs e) method, your code will never be executed.

This information is clarified here: OnPaint Method And more specifically in this paragraph:

  

The MonthCalendar control is drawn by the operating system, so the Paint event is never raised. If you need to provide a customized look to the MonthCalendar control, you should override the OnPrint method, call the base implementation of OnPrint, and then perform custom painting.

That comes to say what I said above.

To solve this, what we can do is launch the OnPaint event of this control through WndProc and this way we can execute our OnPaint code where we can do whatever we want, either modify the color, as text, source, etc.

To carry out the implementation you must create a control that inherits the same MonthCalendar and overwrite the OnPaint method where you must implement the code to be painted in one color or another, font, size, etc.

public class CustomCalendario: MonthCalendar
{
    public CustomCalendario()
        : base()
    {
    }

    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
        //Dibujamos por ejemplo un rectángulo de color rojo
        e.Graphics.DrawRectangle(Pens.Red, 20, 20, 100, 100);
    }

    // Esta parte es muy importante ya que forzamos la llamada al método OnPaint mendiante los "mensajes" de windows 
    protected static int WM_PAINT = 0x000F;
    protected override void WndProc(ref System.Windows.Forms.Message m)
    {
        base.WndProc(ref m);

        if (m.Msg == WM_PAINT)
        {
            Graphics graphics = Graphics.FromHwnd(this.Handle);
            PaintEventArgs pe = new PaintEventArgs(graphics, new Rectangle(0,0, this.Width, this.Height));
            OnPaint(pe);
        }
    }
}
    
answered by 11.03.2017 в 00:59