Calendar.NET控制FirstDayOfWeek

时间:2014-01-09 15:23:49

标签: c#

我在VB.NET上使用http://www.codeproject.com/Articles/378900/Calendar-NET组件,但我想将firstdayofweek更改为星期一。

这是代码呈现日历。

我偶然把这一天命名为土耳其语,但我无法改变第一天

    private void RenderMonthCalendar(PaintEventArgs e)
    {
        _calendarDays.Clear();
        _calendarEvents.Clear();
        var bmp = new Bitmap(ClientSize.Width, ClientSize.Height);
        Graphics g = Graphics.FromImage(bmp);
        e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
        g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
        SizeF monSize = g.MeasureString("Pazartesi", _dayOfWeekFont);
        SizeF tueSize = g.MeasureString("Salı", _dayOfWeekFont);
        SizeF wedSize = g.MeasureString("Çarşamba", _dayOfWeekFont);
        SizeF thuSize = g.MeasureString("Perşembe", _dayOfWeekFont);
        SizeF friSize = g.MeasureString("Cuma", _dayOfWeekFont);
        SizeF satSize = g.MeasureString("Cumartesi", _dayOfWeekFont);
        SizeF sunSize = g.MeasureString("Pazar", _dayOfWeekFont);
        SizeF dateHeaderSize = g.MeasureString(
            _calendarDate.ToString("MMMM") + " " + _calendarDate.Year.ToString(new CultureInfo("tr-TR")), _dateHeaderFont);
        int headerSpacing = Max(sunSize.Height, monSize.Height, tueSize.Height, wedSize.Height, thuSize.Height, friSize.Height,
                      satSize.Height) + 5;
        int controlsSpacing = ((!_showTodayButton) && (!_showDateInHeader) && (!_showArrowControls)) ? 0 : 30;
        int cellWidth = (ClientSize.Width - MarginSize * 2) / 7;
        int numWeeks = NumberOfWeeks(_calendarDate.Year, _calendarDate.Month);
        int cellHeight = (ClientSize.Height - MarginSize * 2 - headerSpacing - controlsSpacing) / numWeeks;
        int xStart = MarginSize;
        int yStart = MarginSize;
        DayOfWeek startWeekEnum = new DateTime(_calendarDate.Year, _calendarDate.Month, 1).DayOfWeek;
        int startWeek = ((int)startWeekEnum) + 1;
        int rogueDays = startWeek - 1;

        yStart += headerSpacing + controlsSpacing;

        int counter = 1;
        int counter2 = 1;

        bool first = false;
        bool first2 = false;

        _btnToday.Location = new Point(MarginSize, MarginSize);

        for (int y = 0; y < numWeeks; y++)
        {
            for (int x = 0; x < 7; x++)
            {
                if (rogueDays == 0 && counter <= DateTime.DaysInMonth(_calendarDate.Year, _calendarDate.Month))
                {
                    if (!_calendarDays.ContainsKey(counter))
                        _calendarDays.Add(counter, new Point(xStart, (int)(yStart + 2f + g.MeasureString(counter.ToString(new CultureInfo("tr-TR")), _daysFont).Height)));

                    if (_calendarDate.Year == DateTime.Now.Year && _calendarDate.Month == DateTime.Now.Month
                     && counter == DateTime.Now.Day && _highlightCurrentDay)
                    {
                        g.FillRectangle(new SolidBrush(Color.FromArgb(234, 234, 234)), xStart, yStart, cellWidth, cellHeight);
                    }

                    if (first == false)
                    {
                        first = true;
                        if (_calendarDate.Year == DateTime.Now.Year && _calendarDate.Month == DateTime.Now.Month
                     && counter == DateTime.Now.Day)
                        {
                            g.DrawString(
                                _calendarDate.ToString("MMM") + " " + counter.ToString(new CultureInfo("tr-TR")),
                                _todayFont, Brushes.Black, xStart + 5, yStart + 2);
                        }
                        else
                        {
                            g.DrawString(
                                _calendarDate.ToString("MMM") + " " + counter.ToString(new CultureInfo("tr-TR")),
                                _daysFont, Brushes.Black, xStart + 5, yStart + 2);
                        }
                    }
                    else
                    {
                        if (_calendarDate.Year == DateTime.Now.Year && _calendarDate.Month == DateTime.Now.Month
                     && counter == DateTime.Now.Day)
                        {
                            g.DrawString(counter.ToString(new CultureInfo("tr-TR")), _todayFont, Brushes.Black, xStart + 5, yStart + 2);
                        }
                        else
                        {
                            g.DrawString(counter.ToString(new CultureInfo("tr-TR")), _daysFont, Brushes.Black, xStart + 5, yStart + 2);
                        }
                    }
                    counter++;
                }
                else if (rogueDays > 0)
                {
                    int dm =
                        DateTime.DaysInMonth(_calendarDate.AddMonths(-1).Year, _calendarDate.AddMonths(-1).Month) -
                        rogueDays + 1;
                    g.DrawString(dm.ToString(new CultureInfo("tr-TR")), _daysFont, new SolidBrush(Color.FromArgb(170, 170, 170)), xStart + 5, yStart + 2);
                    rogueDays--;
                }

                g.DrawRectangle(Pens.DarkGray, xStart, yStart, cellWidth, cellHeight);
                if (rogueDays == 0 && counter > DateTime.DaysInMonth(_calendarDate.Year, _calendarDate.Month))
                {
                    if (first2 == false)
                        first2 = true;
                    else
                    {
                        if (counter2 == 1)
                        {
                            g.DrawString(_calendarDate.AddMonths(1).ToString("MMM") + " " + counter2.ToString(new CultureInfo("tr-TR")), _daysFont,
                                         new SolidBrush(Color.FromArgb(170, 170, 170)), xStart + 5, yStart + 2);
                        }
                        else
                        {
                            g.DrawString(counter2.ToString(new CultureInfo("tr-TR")), _daysFont,
                                         new SolidBrush(Color.FromArgb(170, 170, 170)), xStart + 5, yStart + 2);
                        }
                        counter2++;
                    }
                }
                xStart += cellWidth;
            }
            xStart = MarginSize;
            yStart += cellHeight;
        }
        yStart = MarginSize + controlsSpacing;
        xStart = MarginSize + ((cellWidth - (int)sunSize.Width) / 2);
        g.DrawString("Pazar", _dayOfWeekFont, Brushes.Black, xStart, yStart);

        xStart = MarginSize + ((cellWidth - (int)monSize.Width) / 2) + cellWidth;
        g.DrawString("Pazartesi", _dayOfWeekFont, Brushes.Black, xStart, yStart);

        xStart = MarginSize + ((cellWidth - (int)tueSize.Width) / 2) + cellWidth * 2;
        g.DrawString("Salı", _dayOfWeekFont, Brushes.Black, xStart, yStart);

        xStart = MarginSize + ((cellWidth - (int)wedSize.Width) / 2) + cellWidth * 3;
        g.DrawString("Çarşamba", _dayOfWeekFont, Brushes.Black, xStart, yStart);

        xStart = MarginSize + ((cellWidth - (int)thuSize.Width) / 2) + cellWidth * 4;
        g.DrawString("Perşembe", _dayOfWeekFont, Brushes.Black, xStart, yStart);

        xStart = MarginSize + ((cellWidth - (int)friSize.Width) / 2) + cellWidth * 5;
        g.DrawString("Cuma", _dayOfWeekFont, Brushes.Black, xStart, yStart);

        xStart = MarginSize + ((cellWidth - (int)satSize.Width) / 2) + cellWidth * 6;
        g.DrawString("Cumartesi", _dayOfWeekFont, Brushes.Black, xStart, yStart);


        if (_showDateInHeader)
        {
            g.DrawString(
                _calendarDate.ToString("MMMM") + " " + _calendarDate.Year.ToString(new CultureInfo("tr-TR")),
                _dateHeaderFont, Brushes.Black, ClientSize.Width - MarginSize - dateHeaderSize.Width,
                MarginSize);
        }

        _events.Sort(new EventComparer());

        for (int i = 1; i <= DateTime.DaysInMonth(_calendarDate.Year, _calendarDate.Month); i++)
        {
            int renderOffsetY = 0;

            foreach (IEvent v in _events)
            {
                var dt = new DateTime(_calendarDate.Year, _calendarDate.Month, i, 23, 59, _calendarDate.Second);
                if (NeedsRendering(v, dt))
                {
                    int alpha = 255;
                    if (!v.Enabled && _dimDisabledEvents)
                        alpha = 64;
                    Color alphaColor = Color.FromArgb(alpha, v.EventColor.R, v.EventColor.G, v.EventColor.B);

                    int offsetY = renderOffsetY;
                    Region r = g.Clip;
                    Point point = _calendarDays[i];
                    SizeF sz = g.MeasureString(v.EventText, v.EventFont);
                    int yy = point.Y - 1;
                    int xx = ((cellWidth - (int)sz.Width) / 2) + point.X;

                    if (sz.Width > cellWidth)
                        xx = point.X;
                    if (renderOffsetY + sz.Height > cellHeight - 10)
                        continue;
                    g.Clip = new Region(new Rectangle(point.X + 1, point.Y + offsetY, cellWidth - 1, (int)sz.Height));
                    g.FillRectangle(new SolidBrush(alphaColor), point.X + 1, point.Y + offsetY, cellWidth - 1, sz.Height);
                    if (!v.Enabled && _showDashedBorderOnDisabledEvents)
                    {
                        var p = new Pen(new SolidBrush(Color.FromArgb(255, 0, 0, 0))) { DashStyle = DashStyle.Dash };
                        g.DrawRectangle(p, point.X + 1, point.Y + offsetY, cellWidth - 2, sz.Height - 1);
                    }
                    g.DrawString(v.EventText, v.EventFont, new SolidBrush(v.EventTextColor), xx, yy + offsetY);
                    g.Clip = r;

                    var ev = new CalendarEvent
                    {
                        EventArea =
                            new Rectangle(point.X + 1, point.Y + offsetY, cellWidth - 1,
                                          (int)sz.Height),
                        Event = v,
                        Date = dt
                    };

                    _calendarEvents.Add(ev);
                    renderOffsetY += (int)sz.Height + 1;
                }
            }
        }
        _rectangles.Clear();

        g.Dispose();
        e.Graphics.DrawImage(bmp, 0, 0, ClientSize.Width, ClientSize.Height);
        bmp.Dispose();
    }

1 个答案:

答案 0 :(得分:1)

日子被硬编码到控件中:

SizeF monSize = g.MeasureString("Pazartesi", _dayOfWeekFont);
SizeF tueSize = g.MeasureString("Salı", _dayOfWeekFont);
SizeF wedSize = g.MeasureString("Çarşamba", _dayOfWeekFont);
SizeF thuSize = g.MeasureString("Perşembe", _dayOfWeekFont);
SizeF friSize = g.MeasureString("Cuma", _dayOfWeekFont);
SizeF satSize = g.MeasureString("Cumartesi", _dayOfWeekFont);
SizeF sunSize = g.MeasureString("Pazar", _dayOfWeekFont);

......然后......

yStart = MarginSize + controlsSpacing;
xStart = MarginSize + ((cellWidth - (int)sunSize.Width) / 2);
g.DrawString("Pazar", _dayOfWeekFont, Brushes.Black, xStart, yStart);

xStart = MarginSize + ((cellWidth - (int)monSize.Width) / 2) + cellWidth;
g.DrawString("Pazartesi", _dayOfWeekFont, Brushes.Black, xStart, yStart);

xStart = MarginSize + ((cellWidth - (int)tueSize.Width) / 2) + cellWidth * 2;
g.DrawString("Salı", _dayOfWeekFont, Brushes.Black, xStart, yStart);

xStart = MarginSize + ((cellWidth - (int)wedSize.Width) / 2) + cellWidth * 3;
g.DrawString("Çarşamba", _dayOfWeekFont, Brushes.Black, xStart, yStart);

// etc...

您粘贴的代码很大且不完整,因此我无法确切地知道还需要更改哪些内容。但至少,上面的代码需要更改为所需的顺序。像这样:

yStart = MarginSize + controlsSpacing;
xStart = MarginSize + ((cellWidth - (int)monSize.Width) / 2);
g.DrawString("Pazartesi", _dayOfWeekFont, Brushes.Black, xStart, yStart);

xStart = MarginSize + ((cellWidth - (int)tueSize.Width) / 2) + cellWidth;
g.DrawString("Salı", _dayOfWeekFont, Brushes.Black, xStart, yStart);

xStart = MarginSize + ((cellWidth - (int)wedSize.Width) / 2) + cellWidth * 2;
g.DrawString("Çarşamba", _dayOfWeekFont, Brushes.Black, xStart, yStart);

xStart = MarginSize + ((cellWidth - (int)thuSize.Width) / 2) + cellWidth * 3;
g.DrawString("Perşembe", _dayOfWeekFont, Brushes.Black, xStart, yStart);

// etc...

请注意,上述内容仅会更改标题的显示。我找不到实际描绘事件的代码到正确的一天,但希望你有一些东西可以开始。就个人而言,我建议把所有这些日子都放到一个数组中:

String[] dayName =
    {
        "Pazartesi",
        "Salı",
        "Çarşamba",
        "Perşembe",
        "Cuma",
        "Cumartesi",
        "Pazar"
    };

现在您可以轻松更改顺序并进行迭代:

SizeF[] daySize = dayName.Select( n => g.MeasureString(n, _dayOfWeekFont) ).ToArray();

yStart = MarginSize + controlsSpacing;
for ( int i = 0; i < dayName.Length; i++ )
{
    xStart = MarginSize + ((cellWidth - (int)(daySize[i].Width) / 2) + (cellWidth * i );
    g.DrawString(dayName[i], _dayOfWeekFont, Brushes.Black, xStart, yStart);
}

希望您能了解如何更改其余代码。