在我的表单上,我有一个System.Windows.Forms.TableLayoutPanel
,其中我有一个System.Windows.Forms.Panel
设置为DockStyle.Fill
,在面板中,我有一个System.Windows.Forms.MonthCalendar
控件(也设置为填充)。
日历是一个继承的控件,只是为了允许覆盖主题,以便我可以设置颜色等。继承的部分来自另一个我已经忘记的 SO 问题。
编码:
public class NativeMethods
{
[DllImport("uxtheme.dll")]
public static extern Int32 SetWindowTheme(IntPtr hWnd, String appname, String idlist);
}
public class CustomMonthCalendar : MonthCalendar
{
public CustomMonthCalendar()
{
InitializeComponent();
Margin = new Padding(0);
}
private void InitializeComponent()
{
SuspendLayout();
ResumeLayout(false);
}
protected override void OnHandleCreated(EventArgs e)
{
NativeMethods.SetWindowTheme(Handle, String.Empty, String.Empty);
base.OnHandleCreated(e);
}
}
public class CustomCalendar : UserControl
{
DayPickerControl = new CustomMonthCalendar();
DayPickerControl.ForeColor = SystemColors.ControlText;
DayPickerControl.BackColor = BackgroundColor;
DayPickerControl.TitleForeColor = TextColor.Lighten(1.2f);
DayPickerControl.TitleBackColor = BackgroundColor;
DayPickerControl.TrailingForeColor = Color.Gray;
DayPickerControl.Dock = DockStyle.Fill;
DayPickerControl.FirstDayOfWeek = Day.Sunday;
DayPickerControl.Font = new Font("Microsoft Sans Serif", 9f, FontStyle.Regular);
DayPickerControl.MaxDate = new DateTime(2999, 12, 31, 0, 0, 0, 0);
DayPickerControl.MaxSelectionCount = 7;
DayPickerControl.MinDate = new DateTime(1900, 1, 1, 0, 0, 0, 0);
DayPickerControl.ScrollChange = 1;
DayPickerControl.SetCalendarDimensions(1, 1);
DayPickerControl.ShowToday = true;
DayPickerControl.ShowTodayCircle = true;
DayPickerControl.Text = null;
DayPickerPanel = new Panel();
DayPickerPanel.BorderStyle = BorderStyle.None;
DayPickerPanel.Dock = DockStyle.Fill;
DayPickerPanel.Location = new Point(0, 0);
DayPickerPanel.Padding = new Padding(2);
Size prefSize = DayPickerControl.GetPreferredSize(new Size(200, 200));
DayPickerPanel.Size = new Size(prefSize.Width + 5, prefSize.Height + 5);
DayPickerPanel.Paint += panelBorder_Paint;
DayPickerPanel.Controls.Add(DayPickerControl);
}
所以问题是关于确定日历控件想要的大小。无论我做什么,我得到的尺寸GetPreferredSize()
总是178, 155
. 然而,在屏幕上,日历的右侧边缘被截断,这意味着它比它停靠的面板大。这向我表明,它可能也不太关心被停靠。没有把握。
我摆弄了各种停靠/取消停靠与锚定设置,但结果总是相同。
为什么我的尺寸看起来不正确?以及如何确定适合它的正确尺寸?
编辑:使用 Hans Passant 的建议,我得出了这个解决方案:
//A new event in the CustomMonthCalendar class
public event EventHandler<AfterHandleCreatedArgs> AfterHandleCreated;
//A modified version of the OnHandleCreated() method shown above (add event call)
protected override void OnHandleCreated(EventArgs e)
{
NativeMethods.SetWindowTheme(Handle, String.Empty, String.Empty);
base.OnHandleCreated(e);
if (this.IsHandleCreated && AfterHandleCreated != null)
AfterHandleCreated(this, new AfterHandleCreatedArgs(new Size(this.Size.Width + 5, this.Size.Height + 5))); //cosmetic padding
}
//A new class for the event args it passes...
public class AfterHandleCreatedArgs : EventArgs
{
private Size _newSize = Size.Empty;
public Size NewSize { get { return _newSize; } set { _newSize = value; } }
public AfterHandleCreatedArgs(Size newSize)
{
_newSize = newSize;
}
}
//And a handler to attach to that new event (in CustomCalendar class)...
private void DayPickerControl_AfterHandleCreated(Object sender, AfterHandleCreatedArgs e)
{
if (!e.NewSize.Equals(Size.Empty))
DayPickerPanel.Size = e.NewSize;
}