MonthCalendar width on different (platforms), correction: themes (XP vs Aero theme)
Apparently the .NET monthcalendar renders differently on different platforms. A calendar on Vista is wider than a XP calendar.
I want to make the ca开发者_如何学Clendar fit nicely and precise on all platforms. Is there a way to do this, without having to measure and hard code the different widths ?
..............
Edit/Correction : The calendar seems to render differently based on the theme you select :
How to compensate for that ?
That is the expected behavior. If you don't want to be effected by themes, disable theming completely (i.e. don't execute the command Application.EnableVisualStyles();
). Otherwise different themes will always yield different looks for the controls (as they are meant to do). If you want to fit the controls in at all times, use a more fluent layout making use of anchors and docking.
I had the same problem and found a workaround:
It seems that the dimensions of the MonthCalendar
control are updated correctly when it is shown (like in a form) in runtime.
Use, e.g., the form's Shown
event to know when the dimensions are updated.
You can also set the form's AutoSize
property to true
and AutoSizeMode
to GrowAndShrink
to make the form automatically fit the MonthCalendar
control.
Update:
For more details try this example:
Put a MonthCalendar control on a form like this:
In the form's Shown event add this:
public static int CalenderWidth = 0, CalenderHeight = 0;
private void Form1_Shown(object sender, EventArgs e)
{
CalenderWidth = monthCalendar1.Width;
CalenderHeight = monthCalendar1.Height;
MessageBox.Show("MonthControl width: " + CalenderWidth.ToString() +
", height: " + CalenderHeight.ToString());
}
When the program is run you will see a messagebox showing the right dimensions. The Width and Height are also put into two variables you can use anywhere in your program (in a quick and dirty way, I know ;-) Of course you can omit the messagebox if you don't want it.
To check it is really working try changing the Region settings in Windows: Change the Format to e.g. Danish. Run the program again and you will see the Width has gotten smaller because a Danish MonthCalender control is smaller.
Regarding the AutoSize
and AutoSizeMode
properties they can be used to make the size of the form adapt to the size of the MonthCalender control. Do this:
Change the two properties in the form into this:
Now run the program and you will see that the size of the form changes automatically depending of the size of the MonthCalender control:
That's it! (remember to switch your Region format setting back to the original one)
;-) Dave
Well, I found this :
http://www.eggheadcafe.com/software/aspnet/34174436/vista-monthcalendar-control.aspx
Welcome to Microsoft Manage Newsgroup again, I'm Zhi-Xin Ye, it's my pleasure to work with you on this issue.
The MonthCalendar control is rendered by the OS and hence is rendered different between XP and Vista. You can use the Environment.OSVersion.Version.Major property to determine the OS version, and call MontheCalendar.GetPreferredSize() method to retrieve the prefered size on Vista, so that you can programmatically change the size of the form to make it fit for the MonthCalendar. Meanwhile, turn the AutoSize property of the form to true so that the form will resize on Vista, and make sure the Dock property of the MonthCalendar is set to Dock.None.
Sample code for your information:
private void Form1_Load(object sender, EventArgs e) {
if (Environment.OSVersion.Version.Major >= 6) { this.Size = this.monthCalendar1.GetPreferredSize(new Size()); } }
Please try my suggestion and let me know whether it makes sense to you. If you have any questions or concerns, please do not hesitate to let me know.
Best Regards, Zhi-Xin Ye Microsoft Managed Newsgroup Support Team
There seems to be a method specially designed for this issue :
this.monthCalendar1.GetPreferredSize(new Size());
However, I tested this method on multiple machines, and will give you always a preferred width of 178 px, regardless of theme or OS. (For as far as I tested). !?
So, being fed up, I'm just gonna give the calendar a width of 178, and plan to buy a commercial control or something in the like. Thanks for all the help...
My app is using Application.EnableVisualStyles(); I disabled Calendar visual styles in CreateHandle() method:
public class MonthCalendarBuffered : MonthCalendar
{
protected override void CreateHandle()
{
Application.VisualStyleState = VisualStyleState.NoneEnabled;
// disables Application.UseVisualStyles
base.CreateHandle();
// restore setting
Application.VisualStyleState = VisualStyleState.ClientAndNonClientAreasEnabled;
}
...
Check Calendar control source at: http://referencesource.microsoft.com/#System.Windows.Forms/ndp/fx/src/winforms/Managed/System/WinForms/MonthCalendar.cs
精彩评论