在我的表单上,我有一个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;
}
答案 0 :(得分:2)
MonthCalendar是一个“困难”的控件。它用于Windows(时钟)中一个高度可见的区域,因此在Windows版本之间进行了相当多的修改。 不可调整大小,其大小因版本而异。
所以,不,使用Dock = Fill肯定无法工作,这需要一个控件可以调整大小。 GetPreferredSize()只能返回一个guestimate,一个可能曾经在旧的Windows版本上正确但在今天不是。
您需要处理其行为。在之后创建控件的本机窗口之前,您无法知道其真实大小。通常在表单的Load事件或控件的OnCreateHandle()方法中(如果您从中派生)。此时,其Size属性将是可靠的。您需要相应地调整其父控件。
答案 1 :(得分:0)
您可以通过询问日历来强制日历大小:
$strings = array('abc"def', "efg'hij");
foreach($strings as $str){
echo addslashes($str) .PHP_EOL;
}