Maxscript dotNet UserControl添加多个按钮时可见性与位置异常问题
问题原因
- 按钮添加到
UserControl容器后,坐标体系为相对于父容器的局部坐标,原代码中使用rollout全局坐标赋值,导致第二个按钮被定位到UserControl可视区域外,因此无法显示。 - Maxscript rollout层面的
.pos属性对已加入dotNet容器的控件适配存在兼容问题,优先使用dotNet原生的Location属性设置位置更稳定。
修复后的完整代码
clearListener() try destroyDialog RollsRoyce catch() rollout RollsRoyce "" width:400 height:60 ( local btnH = 33 local btnW = RollsRoyce.width/2 dotnetcontrol panelVisibleFix "UserControl" width:RollsRoyce.width height:btnH pos:[0,15] dotNetControl btn1 "button" text:"Button 1..." visible:false width:btnW height:btnH dotNetControl btn2 "button" text:"Button 2..." visible:false width:btnW height:btnH on RollsRoyce open do ( btn1.FlatStyle = btn1.FlatStyle.System btn2.FlatStyle = btn2.FlatStyle.System panelVisibleFix.controls.addrange #(btn1, btn2) -- 使用相对于UserControl的局部坐标,通过dotNet原生Location属性设置 btn1.Location = dotnetobject "System.Drawing.Point" 0 0 btn1.visible = true btn2.Location = dotnetobject "System.Drawing.Point" btnW 0 btn2.visible = true -- 刷新容器确保渲染正常 panelVisibleFix.Refresh() format "btn1 position is: %\n" btn1.Location format "btn2 position is: %\n" btn2.Location ) ) createdialog RollsRoyce
动态控制方法
后续需要动态调整按钮的可见性、位置时,直接操作对应按钮的dotNet属性即可,无需为每个按钮单独创建UserControl:
- 调整可见性:直接修改
btn.Visible = true/false - 调整位置:修改
btn.Location = dotnetobject "System.Drawing.Point" 横坐标 纵坐标(坐标为相对于panelVisibleFix的局部坐标) - 所有属性修改完成后调用
panelVisibleFix.Refresh()即可刷新显示。
内容的提问来源于stack exchange,提问作者Ste
相关产品推荐
相关产品推荐

