On Monday 11 December 2006 19:43, you wrote:
> Hello,
>
> > the first implementation of Button.AutoSize did not resize in cases where
> > the Font has been changed. I've corrected it but forgot to send the new
> > Version to the list.
> >
> > Therefore please review this patch instead of the initial one.
>
> A few days ago, someone on irc (perhaps jpobst?) mentioned that the
> problem with the code is that it did not take into account embedded
> images or other elements that might be added to it.
>
> Miguel.
Hello,
thanks for the hint I've corrected the patch. It resizes now if the image,
font or text has been changed. This should cover all cases, or am I missing
something?
While fixing the AutoSize I found out that buttons don't not display their
background image* and they flicker when the mouse enters and leaves quickly*.
Are these known issues or should I file a report?
* This is not a consequence of my patch. I happens under mono 1.2.2.1 as well.
Kind Regards,
Valentin S.
Index: ButtonBase.cs
===================================================================
--- ButtonBase.cs (revision 69597)
+++ ButtonBase.cs (working copy)
@@ -48,6 +48,7 @@
#if NET_2_0
private bool use_compatible_text_rendering;
private bool use_visual_style_back_color;
+ private bool autoSize;
#endif
#endregion // Local Variables
@@ -144,6 +145,7 @@
text_format.Alignment = StringAlignment.Center;
text_format.LineAlignment = StringAlignment.Center;
text_format.HotkeyPrefix = HotkeyPrefix.Show;
+ text_format.FormatFlags = text_format.FormatFlags | StringFormatFlags.NoWrap;
TextChanged+=new System.EventHandler(RedrawEvent);
SizeChanged+=new EventHandler(RedrawEvent);
@@ -186,6 +188,11 @@
set {
image = value;
+#if NET_2_0
+ if(this.AutoSize) {
+ this.AutoCalculateNewSize();
+ }
+#endif
Invalidate();
}
}
@@ -471,6 +478,10 @@
}
protected override void OnTextChanged(EventArgs e) {
+ if(this.AutoSize)
+ {
+ this.AutoCalculateNewSize();
+ }
Invalidate();
base.OnTextChanged(e);
}
@@ -507,6 +518,48 @@
}
base.WndProc (ref m);
}
+
+#if NET_2_0
+ private void AutoCalculateNewSize() {
+ Graphics g = this.CreateGraphics();
+ SizeF neededTextSpace = g.MeasureString(this.Text,
+ this.Font);
+ Size neededSpace = new Size(0, 0);
+
+ // +35 helps us showing all the text without a part of it will be clipped.
+ if(neededTextSpace.Width + 35 > this.Width) {
+ neededSpace.Width = (int)Math.Ceiling(neededTextSpace.Width) + 35;
+ }
+
+ // +15 is needed to have a proper padding between the border and the text.
+ if(neededTextSpace.Height + 15 > this.Height) {
+ neededSpace.Height = (int)Math.Ceiling(neededTextSpace.Height) + 15;
+ }
+
+ if(this.Image != null) {
+ if(this.Image.Width > neededSpace.Width) {
+ neededSpace.Width = this.Image.Width;
+ }
+ if(this.Image.Height > neededSpace.Height) {
+ neededSpace.Height = this.Image.Height;
+ }
+ }
+
+ if(neededSpace.Width != 0) {
+ this.Width = neededSpace.Width;
+ }
+ if(neededSpace.Height != 0) {
+ this.Height = neededSpace.Height;
+ }
+ }
+
+ protected override void OnFontChanged(EventArgs e) {
+ if(this.AutoSize) {
+ this.AutoCalculateNewSize();
+ }
+ base.OnFontChanged(e);
+ }
+#endif
#endregion // Public Instance Properties
@@ -528,6 +581,18 @@
#region .NET 2.0 Public Instance Properties
#if NET_2_0
+ public override bool AutoSize {
+ get {
+ return this.autoSize;
+ }
+ set {
+ this.autoSize = value;
+ if(value) {
+ this.AutoCalculateNewSize();
+ }
+ }
+ }
+
public bool UseVisualStyleBackColor {
get { return use_visual_style_back_color; }
set { use_visual_style_back_color = value; }
_______________________________________________
Mono-winforms-list maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-winforms-list