gpt4 book ai didi

c# - 使用 EnableVisualStyles 的 MonthCalendar 控件选择范围?

转载 作者:太空狗 更新时间:2023-10-30 00:37:03 24 4
gpt4 key购买 nike

我正在使用 MonthCalendar 控件并希望以编程方式选择一个日期范围。当我这样做时,如果调用了 Application.EnableVisualStyles(),控件将无法正确绘制。根据 MSDN,这是一个已知问题。

Using the MonthCalendar with visual styles enabled will cause a selection range for the MonthCalendar control to not paint correctly (from: http://msdn.microsoft.com/en-us/library/system.windows.forms.monthcalendar.aspx)

除了不调用 EnableVisualStyles 之外真的没有解决办法吗?这似乎使该特定控件对于一系列应用程序完全无用,并且从我的角度来看是相当明显的疏忽。

最佳答案

在寻找相同问题的解决方案时,我首先在这里遇到这个问题,但后来我发现了 Nicke Andersson 的博客条目.我发现这很有帮助。这是我对 Nicke 的例子所做的:

public class MonthCalendarEx : System.Windows.Forms.MonthCalendar
{
private int _offsetX;
private int _offsetY;
private int _dayBoxWidth;
private int _dayBoxHeight;

private bool _repaintSelectedDays = false;

public MonthCalendarEx() : base()
{
OnSizeChanged(null, null);
this.SizeChanged += OnSizeChanged;
this.DateChanged += OnSelectionChanged;
this.DateSelected += OnSelectionChanged;
}

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);
}
}

private void OnSelectionChanged(object sender, EventArgs e)
{
_repaintSelectedDays = true;
}

private void OnSizeChanged(object sender, EventArgs e)
{
_offsetX = 0;
_offsetY = 0;

// determine Y offset of days area
while (
HitTest(Width / 2, _offsetY).HitArea != HitArea.PrevMonthDate &&
HitTest(Width / 2, _offsetY).HitArea != HitArea.Date)
{
_offsetY++;
}

// determine X offset of days area
while (HitTest(_offsetX, Height / 2).HitArea != HitArea.Date)
{
_offsetX++;
}

// determine width of a single day box
_dayBoxWidth = 0;
DateTime dt1 = HitTest(Width / 2, _offsetY).Time;

while (HitTest(Width / 2, _offsetY + _dayBoxHeight).Time == dt1)
{
_dayBoxHeight++;
}

// determine height of a single day box
_dayBoxWidth = 0;
DateTime dt2 = HitTest(_offsetX, Height / 2).Time;

while (HitTest(_offsetX + _dayBoxWidth, Height / 2).Time == dt2)
{
_dayBoxWidth++;
}
}

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);

if (_repaintSelectedDays)
{
Graphics graphics = e.Graphics;
SelectionRange calendarRange = GetDisplayRange(false);
Rectangle currentDayFrame = new Rectangle(
-1, -1, _dayBoxWidth, _dayBoxHeight);

DateTime current = SelectionStart;
while (current <= SelectionEnd)
{
Rectangle currentDayRectangle;

using (Brush selectionBrush = new SolidBrush(
Color.FromArgb(
255, System.Drawing.SystemColors.ActiveCaption)))
{
TimeSpan span = current.Subtract(calendarRange.Start);
int row = span.Days / 7;
int col = span.Days % 7;

currentDayRectangle = new Rectangle(
_offsetX + (col + (ShowWeekNumbers ? 1 : 0)) * _dayBoxWidth,
_offsetY + row * _dayBoxHeight,
_dayBoxWidth,
_dayBoxHeight);

graphics.FillRectangle(selectionBrush, currentDayRectangle);
}

TextRenderer.DrawText(
graphics,
current.Day.ToString(),
Font,
currentDayRectangle,
System.Drawing.SystemColors.ActiveCaptionText,
TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);

if (current == this.TodayDate)
{
currentDayFrame = currentDayRectangle;
}

current = current.AddDays(1);
}

if (currentDayFrame.X > 0)
{
graphics.DrawRectangle(new Pen(
new SolidBrush(Color.Red)), currentDayFrame);
}

_repaintSelectedDays = false;
}
}
}

关于c# - 使用 EnableVisualStyles 的 MonthCalendar 控件选择范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/207306/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com