[Mono-winforms-list] [BUMP] ToolStripDropDown + UserControl

Alex Shulgin alexander.shulgin at yessoftware.com
Thu Feb 26 09:37:35 EST 2009


Alex Shulgin wrote:
> Hi,
> 
> I'm trying to add some custom control to ToolStripDropDown using 
> ToolStripControlHost.
> 
> This works fine with .NET, but with Mono (2.2) the control is always 
> resized to 22 pixels tall...  Too bad I can't find a workaround for a 
> few days now.
> 
> Any help & suggestions are much appreciated. :)

If someone could tell how to create a popup window w/o using 
ToolStripDropDown I'd be grateful.

Since it's exactly the thing I'm trying to accomplish--using 
ToolStripDropDown is just the shortest way in .NET, but it seems to be 
broken in Mono.

I've tried to override CreateParams property of Control class with the 
code similar to the one found in Mono's ToolStripDropDown sources, but 
no luck:

     public class PopupWindow : System.Windows.Forms.Control
     {
         private Control content;

         public PopupWindow(Control _content)
         {
             this.content = _content;
             this.Size = _content.Size;
             this.MinimumSize = this.Size;
         }

         protected override CreateParams CreateParams {
             get {
                 CreateParams cp = base.CreateParams;
                 cp.Style = unchecked((int)0x82000000); // WS_POPUP | 
WS_CLIPCHILDREN
                 cp.ExStyle |= 0x00000088; // WS_EX_TOOLWINDOW | 
WS_EX_TOPMOST
                 Console.WriteLine("PopupWindow.get_CreateParams: 
returning {0}; cp.Style=0x{1}", cp, cp.Style.ToString("X"));
                 return cp;
             }
         }

         protected override void OnResize(EventArgs e)
         {
             Console.WriteLine("PopupWindow.OnResize: Bounds={0}", Bounds);
         }

         protected override void OnPaint(PaintEventArgs e)
         {
             Console.WriteLine("PopupWindow.OnPaint: Bounds={0}", Bounds);
             base.OnPaint(e);
             using (Pen p = new Pen(Color.Black))
             {
                 e.Graphics.DrawRectangle(p, Bounds);
             }
         }
     }

The program output is (after right-clicking on the form once):

MyControl.OnResize: Bounds={X=0,Y=0,Width=50,Height=50}
PopupWindow.get_CreateParams: returning CreateParams {'SWFClass0', '', 
0x28, 0x88, {0, 0, 0, 0}}; cp.Style=0x82000000
PopupWindow.get_CreateParams: returning CreateParams {'SWFClass0', '', 
0x28, 0x88, {0, 0, 0, 0}}; cp.Style=0x82000000
PopupWindow.OnResize: Bounds={X=0,Y=0,Width=50,Height=50}

No window is shown and OnPaint event handler isn't called at all... 
What I'm missing?

--
Alex

> --8<----8<----8<----8<----8<----8<----8<----8<--
> 
> using System;
> using System.Collections.Generic;
> using System.ComponentModel;
> using System.Data;
> using System.Drawing;
> using System.Text;
> using System.Windows.Forms;
> 
> namespace winforms_toolstrip
> {
>      static class Program
>      {
>          [STAThread]
>          static void Main()
>          {
>              Application.EnableVisualStyles();
>              Application.SetCompatibleTextRenderingDefault(false);
>              Application.Run(new Form1());
>          }
>      }
> 
>      public class PopupWindow : ToolStripDropDown
>      {
>          private Control _content;
>          private ToolStripControlHost _host;
> 
>          public PopupWindow(Control content)
>          {
>              this.AutoSize = false;
>              this.DoubleBuffered = true;
>              this.ResizeRedraw = true;
> 
>              this._content = content;
>              this._host = new ToolStripControlHost(content);
> 
>              this.MinimumSize = content.MinimumSize;
>              this.MaximumSize = content.GetPreferredSize(Size.Empty);
>              this.Size = this.MaximumSize;
>              content.Location = Point.Empty;
> 
>              this.Items.Add(this._host);
>          }
>      }
> 
>      public class Form1 : Form
>      {
>          public Form1()
>          {
>              this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
>              this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
>              this.ClientSize = new System.Drawing.Size(292, 266);
>              this.Name = "Form1";
>              this.Text = "Form1";
> 
>              this.myControl = new MyControl();
> 
>              this.MouseClick += new MouseEventHandler(form1_MouseClick);
>          }
> 
>          internal void form1_MouseClick(object sender, MouseEventArgs e)
>          {
>              if (e.Button == MouseButtons.Right)
>              {
>                  PopupWindow popup = new PopupWindow(myControl);
>                  popup.Show(this.Location.X + e.X, this.Location.Y + e.Y);
>              }
>          }
> 
>          internal class MyControl : UserControl
>          {
>              public MyControl()
>              {
>                  Size = new Size(50, 50);
>              }
> 
>              protected override void OnResize(EventArgs e)
>              {
>                  Console.WriteLine("MyControl.OnResize: Bounds={0}", 
> Bounds);
>              }
> 
>              protected override void OnPaint(PaintEventArgs e)
>              {
>                  Console.WriteLine("MyControl.OnPaint: Bounds={0}", Bounds);
>                  Graphics g = e.Graphics;
>                  using (Brush b = new SolidBrush(Color.Black))
>                  {
>                      g.FillRectangle(b, Bounds);
>                  }
>              }
> 
>              public override Size GetPreferredSize(Size constr)
>              {
>                  Console.WriteLine("MyControl.GetPreferredSize");
>                  return new Size(50, 50);
>              }
> 
>              public override Size MinimumSize
>              {
>                  get
>                  {
>                      return GetPreferredSize(Size.Empty);
>                  }
>                  set
>                  {
>                      base.MinimumSize = value;
>                  }
>              }
>          };
> 
>          internal MyControl myControl;
>      }
> }



More information about the Mono-winforms-list mailing list