VBIDE扩展中UserControl无法随停靠窗口拉伸与调整大小问题
我正在为VBIDE编写一个简单扩展,遇到的问题是WinForm的UserControl无法拉伸至停靠窗口的宽度,也无法随窗口大小变化而调整。
问题现象
带有控件的ToolWindow显示如下:
[ToolWindow截图:UserControl未占满窗口宽度,右侧留有空白]
Control.Designer.cs代码
private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.commandLineBox = new System.Windows.Forms.RichTextBox(); this.resultLabel = new System.Windows.Forms.Label(); this.resultToolTip = new System.Windows.Forms.ToolTip(this.components); this.SuspendLayout(); // // commandLineBox // this.commandLineBox.BorderStyle = System.Windows.Forms.BorderStyle.None; this.commandLineBox.Dock = System.Windows.Forms.DockStyle.Top; this.commandLineBox.Location = new System.Drawing.Point(0, 0); this.commandLineBox.Name = "commandLineBox"; this.commandLineBox.TabIndex = 0; this.commandLineBox.Text = ""; this.commandLineBox.Enter += new System.EventHandler(this.commandLineBox_Enter); this.commandLineBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.commandLineBox_KeyDown); // // resultLabel // this.resultLabel.AutoSize = true; this.resultLabel.Dock = System.Windows.Forms.DockStyle.Top; this.resultLabel.Location = new System.Drawing.Point(0, 38); this.resultLabel.Name = "resultLabel"; this.resultLabel.TabIndex = 1; this.resultLabel.TextChanged += new System.EventHandler(this.resultLabel_TextChanged); // // Terminal // this.Controls.Add(this.resultLabel); this.Controls.Add(this.commandLineBox); this.Name = "Terminal"; this.Dock = System.Windows.Forms.DockStyle.Fill; this.ResumeLayout(false); }
尝试的解决方法及问题
我尝试在Extension类中显示窗口后将control.Dock设置为DockStyle.Fill,但control的值始终为null:
[调试截图:control变量显示为null]
public class Extension : IDTExtensibility2 { private VBEWrapper _wrapper; private VB.AddIn _addin; private VB.Window terminalWindow; public void OnConnection(object Application, ext_ConnectMode ConnectMode, object AddInInst, ref Array custom) { _wrapper = VBEWrapper.GetInstance((VB.VBE)Application); _addin = (VB.AddIn)AddInInst; switch (ConnectMode) { case ext_ConnectMode.ext_cm_AfterStartup: CreateWindow(); break; case ext_ConnectMode.ext_cm_Startup: break; } } private void CreateWindow() { Termin control = null; try { terminalWindow = _wrapper.VBE.Windows.CreateToolWindow( _addin, "VBATerminal.Terminal", "Terminal", "EE7EEC61-E607-44B8-830F-34839672CD30", control); terminalWindow.Visible = true; const int defaultWidth = 350; const int defaultHeight = 200; if (terminalWindow.Width < defaultWidth) { terminalWindow.Width = defaultWidth; } if (terminalWindow.Height < defaultHeight) { terminalWindow.Height = defaultHeight; } } catch (Exception e) { MessageBox.Show(e.ToString()); } } }
编辑补充:
我尝试了另一种应该能给control赋值的写法,但依然没有效果:
object control = null terminalWindow = _wrapper.VBE.Windows.CreateToolWindow( _addin, "VBATerminal.Terminal", "terminal", "EE7EEC61-E607-44B8-830F-34839672CD30", ref control);
请问我哪里操作错误了?
问题分析与解决
控件布局修正
你的resultLabel设置了AutoSize = true,这会导致标签仅适配文本宽度,即使设置DockStyle.Top也无法横向填充整个控件。需要将resultLabel.AutoSize改为false,同时设置Anchor = AnchorStyles.Left | AnchorStyles.Right,确保它能随控件宽度变化自动拉伸。CreateToolWindow参数修正
调用CreateToolWindow时,第五个参数必须传递ref类型的控件引用,且控件类名要完全匹配(你代码里写的Termin control = null是拼写错误,应为Terminal)。正确写法:Terminal control = null; terminalWindow = _wrapper.VBE.Windows.CreateToolWindow( _addin, "VBATerminal.Terminal", "Terminal", "EE7EEC61-E607-44B8-830F-34839672CD30", ref control);只有通过
ref传递,CreateToolWindow才会实例化控件并赋值给control变量。确认UserControl布局设置
你的Designer代码中已经设置this.Dock = System.Windows.Forms.DockStyle.Fill,需确保UserControl的AutoSize属性为false(默认值),避免布局约束冲突。窗口大小响应
确保UserControl的Dock/Anchor设置正确后,控件会自动随ToolWindow大小变化调整,无需额外编写Resize事件处理逻辑。
内容的提问来源于stack exchange,提问作者artodoro

