动态调整控件大小 [英] Resizing controls dynamically efficiently

查看:122
本文介绍了动态调整控件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用广泛的自定义控件库,根据我收到的数据表创建动态构建的菜单.

一切都很好,我已经将我的控件添加到FlowLayoutPanel中(由于其可见性设置为false,因此嵌入了对控件进行重新排序的问题,完全是另一个问题)但现在我希望根据当前选择来调整所有控件的宽度.逻辑已经完成,我已经获得了适当的宽度,所有控件应该基于单击了哪些按钮.


所以我的问题是,尽管使用了SuspendLayout()和ResumeLayout(),我的控件仍然在屏幕上重绘,这在美学上是非常不愉快的,有没有一种方法可以比for循环更有效地调整控件的大小.遍历一个集合,该集合引用了我菜单中的所有控件?

解决方案

您可以尝试使用SetRedraw代替Suspend/ResumeLayout.

 public  #region外部方法定义

[DllImport(" )]
静态 公共 外部  IntPtr  hWnd, UInt32  wParam, UInt32  lParam);
[DllImport(" )]
静态公共 外部  UInt32  RedrawWindow( UInt32  hwnd, UInt32  lprcUpdate, UInt32  hrgnUpdate, UInt32 标志);
 #endregion

 #region成员和常量

常量  int  WM_SETREDRAW = 0x000B;
控制窗口= ;

 #endregion

 #region构造函数

公共 RedrawLock(控制winToLock)
{
window = winToLock;
SendMessage(window.Handle,WM_SETREDRAW, 0  0 );
}

 #endregion

 #region IDisposable成员

公共 无效 Dispose()
{
SendMessage(window.Handle,WM_SETREDRAW, 1  0 );
window.Refresh();
RedrawWindow( 0  0  0 , 0x181); //  168,以重新绘制桌面,使这种形式的残留物消失.
}

 #endregion
} 

 将代码包装在using(新的RedrawLock(this)){}块中.


I''m making use of an extensive library of custom controls to create a dynamically constructed menu based off a data table I''ve received.

 All is well and fine, and I''ve added my controls to a FlowLayoutPanel (which had issues reordering controls embedded within due to the fact it''s visibility was set to false, a whole other issue entirely) but now I wish to resize the width of all my controls based off the current selections. The logic is done and I''ve obtained the appropriate width all controls should be based off which buttons have been clicked.


So my question is, despite using SuspendLayout() and ResumeLayout(), my controls are still redrawn on the screen which is very aesthetically unpleasing, is there a way to resize my controls more efficiently then a for loop which iterates through a collection which has a reference to all controls within my Menu? 

解决方案

Instead of Suspend/ResumeLayout you could try using SetRedraw. 

    public class RedrawLock : IDisposable
    {
        #region External Method Definitions

        [DllImport("user32.dll")]
        static public extern UInt32 SendMessage(IntPtr hWnd, UInt32 msg, UInt32 wParam, UInt32 lParam);
        [DllImport("user32.dll")]
        static public extern UInt32 RedrawWindow(UInt32 hwnd, UInt32 lprcUpdate, UInt32 hrgnUpdate, UInt32 flags);
        #endregion

        #region Members and Constants
        
        const int WM_SETREDRAW = 0x000B;
        Control window = null;
        
        #endregion

        #region Constructor

        public RedrawLock(Control winToLock)
        {
            window = winToLock;
            SendMessage(window.Handle, WM_SETREDRAW, 0, 0);
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            SendMessage(window.Handle, WM_SETREDRAW, 1, 0);
            window.Refresh();
            RedrawWindow(0, 0, 0, 0x181); //168, to redraw the desktop so remnants of this form disappear.
        }

        #endregion
    }

 Wrap the code in a using (new RedrawLock(this)) {} block.


这篇关于动态调整控件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆