Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 5 của 5
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Project gồm 4 file:

    ILayoutManager.cs

    Mã:
    using System;namespace  LayoutManager {    public interface  ILayoutManager  {        LayoutControl  Control  { get;  set; }        void  DoLayout();        void  ControlAdded  ( System.Windows.Forms.Control  c );        void  ControlRemoved  ( System.Windows.Forms.Control  c );    }}
    LayoutControl.cs

    Mã:
    using LayoutEventHandler        = System.Windows.Forms.LayoutEventHandler;using LayoutEventArgs           = System.Windows.Forms.LayoutEventArgs;using ControlEventHandler       = System.Windows.Forms.ControlEventHandler;using ControlEventArgs              = System.Windows.Forms.ControlEventArgs;using Control                           = System.Windows.Forms.Control;using BrowsableAttribute        = System.ComponentModel.BrowsableAttribute;using ReadOnlyAttribute             = System.ComponentModel.ReadOnlyAttribute; namespace  LayoutManager  {    public class  LayoutControl : Control  {        private  ILayoutManager  layoutMgr;                public  LayoutControl  ( ILayoutManager  layoutMgr )  {            Manager = layoutMgr;            this.Layout         += new LayoutEventHandler( DoLayout );            this.ControlAdded   += new ControlEventHandler( ContrAdd );            this.ControlRemoved += new ControlEventHandler( ContrRem );        }         public LayoutControl() : this (null)  { }        [Browsable( true )]        [ReadOnly( true )]        public  LayoutManager.ILayoutManager  Manager  {            get  {                return  layoutMgr;            }            set  {                if  ( value != null  &&  value != layoutMgr )  {                    layoutMgr = value;                    layoutMgr.Control = this;                    foreach  ( Control c in Controls )  {                        layoutMgr.ControlAdded( c );                    }  layoutMgr.DoLayout();        }  }  }        private void  ContrAdd  ( object sender,  ControlEventArgs e )  {            layoutMgr.ControlAdded  ( e.Control );        }        private void  ContrRem  ( object sender,  ControlEventArgs e )  {            layoutMgr.ControlRemoved  ( e.Control );        }        private void  DoLayout  ( object sender,  LayoutEventArgs e )  {            layoutMgr.DoLayout();        }    }  }

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    GridLayout.cs

    Mã:
    using  System;using  System.Windows.Forms;using  Control   = System.Windows.Forms.Control;using  Rectangle = System.Drawing.Rectangle; namespace  LayoutManager  {public class GridLayout : ILayoutManager {    private int  curRow;    private int  curCol;    private  Control[][]  controls;    private  LayoutControl  con;        // Horizontal and vertical space between control and grid    private int  vSpace,  hSpace;    public enum  Align:byte  { Center = 1,  Left = 2,  Fill = 3 };    Align  a;    public  GridLayout  ( int rows,  int cols,  Align a1 ) : this ( rows,  cols,  a1,  0,  0 )  {    }    public  GridLayout  ( int rows,  int cols,  Align a1,  int hSpace,  int vSpace )  {        if  ( rows < 1 )  { rows = 1; }        if  ( cols < 1 )  { cols = 1; }        if  ( hSpace < 0 )  { hSpace = 0; }        if  ( vSpace < 0 )  { vSpace = 0; }        a = a1;        controls = new Control[rows][];        for  ( int i = 0; i < rows; ++i )  {            controls[i] = new Control[cols];        }        this.hSpace = hSpace;  this.vSpace = vSpace;        curCol = 0;  curRow = 0;    }    private int  Rows  {        get  {            return  controls.Length;        }    }    private int Cols {        get {            return  controls[0].Length;        }    }    public int  VerticalSpace  {        get  {            return  vSpace;        }        set  {            if  ( ( value >= 0 )  &&  ( value != vSpace ) )  {                vSpace = value;  DoLayout();            }        }  }    public int  HorizontalSpace  {        get  {            return  hSpace;        }        set  {            if ( ( value >= 0 )  &&  ( value != hSpace ) )  {                hSpace = value;  DoLayout();            }    }  }    public void  GoTo( int row,  int col )  {        if  ( row >= 0  &&  row - 1 < Rows )  {            curRow = row - 1;        }        if  ( col >= 0  &&  col - 1 < Cols )  {            curCol = col - 1;        }    }     #region ILayoutMgr Members    public  LayoutControl  Control  {        get  {            return con;        }        set  {            if ( value != null  &&  value != con )  {                con = value;            }    }  }    public void  DoLayout()  {        int  rowSpace,  colSpace;        if  ( Control != null )  {            rowSpace = ( Control.ClientSize.Height / Rows );            colSpace = ( Control.ClientSize.Width / Cols );             for  ( int r = 0; r < Rows; ++r )              for  ( int c = 0; c < Cols; ++c )                  if  ( controls[r][c] != null )  {                if  ( a == Align.Center )  {                    int  w1,  h1,  w2,  h2;                    w1 = controls[r][c].ClientSize.Width;                    h1 = controls[r][c].ClientSize.Height;                    w2 = ( colSpace - w1  ) / 2 + c*colSpace;                    h2 = ( rowSpace - h1  ) / 2 + r*rowSpace;                    controls[r][c].Bounds = new Rectangle  ( w2,  h2,  w1,  h1 );                }                if  ( a == Align.Fill )  {                    int  w1,  h1,  w2,  h2;                    w2 = c*colSpace + hSpace;                      h2 = r*rowSpace + vSpace;                    w1 = colSpace - 2*hSpace;                    h1 = rowSpace - 2*vSpace;                    controls[r][c].Bounds = new Rectangle  ( w2,  h2,  w1,  h1 );                }                  if  ( a == Align.Left )  {                    int  w1,  h1,  w2,  h2;                    w1 = controls[r][c].ClientSize.Width;                    h1 = controls[r][c].ClientSize.Height;                    w2 = c*colSpace + hSpace;                    h2 = ( rowSpace - h1  ) / 2 + r*rowSpace;                    controls[r][c].Bounds = new Rectangle  ( w2,  h2,  w1,  h1 );                }   }    }  }      public void  ControlAdded( Control c )  {        if  ( Control != null  &&  curRow < controls.Length )  {            if  ( controls[ curRow ][ curCol ]  !=  null )  {                Control.Controls.Remove( controls[ curRow ][ curCol ] );            }  controls[ curRow ][ curCol ] = c;  curCol += 1;            if  ( curCol >= Cols )  {                curCol = 0;  curRow = curRow + 1;            }    }  }    public void  ControlRemoved( Control c )  {        int  rows,  cols;        if  ( Control != null )  {            rows = Rows;  cols = Cols;            for  ( int row = 0; row < rows; ++row )              for  ( int col = 0; col < cols; ++col )                  if  ( controls[ row ][ col ] == c )  {                    controls[ row ][ col ] = null;                }        }  }      #endregion}  }

  4. #4
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    GridTest.cs

    Mã:
    using  System.Windows.Forms;using  System.Drawing;using  Align = LayoutManager.GridLayout.Align; public class  Test : Form {    private  LayoutManager.LayoutControl  control1,  control2,  control3;    private  LayoutManager.ILayoutManager  layout1,  layout2,  layout3;        public Test() {        Button  btn1 = new Button();        Button  btn2 = new Button();        Button  btn3 = new Button();        Button  btn4 = new Button();         layout1 = new LayoutManager.GridLayout( 2,  2, Align.Left );        control1 = new LayoutManager.LayoutControl( layout1 );        control1.Size = new Size( 300, 300 );        //control1.Dock = DockStyle.Fill;        control1.BackColor = Color.White;                btn1.Text = "Test Button 1";        btn1.Size = new Size (50, 50);        btn1.BackColor = Color.LightBlue;        control1.Controls.Add( btn1 );        btn2.Text = "Test Button 2";        btn2.BackColor = Color.LightGreen;        btn2.Size = new Size (50, 50);        ( ( LayoutManager.GridLayout )  layout1 ).GoTo( 2, 2 );        control1.Controls.Add( btn2 );          layout2 = new LayoutManager.GridLayout( 2,  2, Align.Center );        control2 = new LayoutManager.LayoutControl( layout2 );        //control2.Dock = DockStyle.Fill;        control2.Size = new Size( 300, 300 );        control2.BackColor = Color.Yellow;        btn3.Text = "Test Button 3";        btn3.Size = new Size (50, 50);        btn3.BackColor = Color.LightBlue;        control2.Controls.Add( btn3 );        btn4.Text = "Test Button 4";        btn4.BackColor = Color.LightGreen;        btn4.Size = new Size (50, 50);        control2.Controls.Add( btn4 );         layout3 = new LayoutManager.GridLayout( 2,  1, Align.Fill, 20, 20 );        control3 = new LayoutManager.LayoutControl( layout3 );        control3.Dock = DockStyle.Fill;        control2.Size = new Size( 500, 500 );        control3.Controls.AddRange(  new Control[]  {  control1,  control2 }  );         this.Controls.Add( control3 );    }     public static void Main() {        Application.Run( new Test() );    }}

  5. #5
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Em định phát triển code trên thành GridBagLayout, nhưng gặp 1 số vấn đề sau, mong mấy anh chỉ giáo:
    1. Sau khi combine 2 or 3 control nhỏ thành 1 control lớn thì phải Remove các control[r][c] nhỏ đã combined ra khỏi hàm DoLayout() và phải thực hiện hàm DoLayout cho các control lớn.

    2. Khi thực hiện add object thì đến các vị trí các control nhỏ đã combined, làm sao để add các object vào control lớn?

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •