선생님, 개발을 잘하고 싶어요.

TableLayoutPanel 사용시 col, row 레이아웃 픽셀 크기 받아오는 법 본문

개발/c# winform

TableLayoutPanel 사용시 col, row 레이아웃 픽셀 크기 받아오는 법

알고싶은 승민 2018. 9. 7. 11:20

TableLayoutPanel에서 해당 Col과 Row에 해당하는 Control의 사이즈를 제한해야 하는 일이 있었다.


GetControlFromPosition(int col, int row) 라는 함수가 있어서 원하는 동작을 할줄 알았는데 레이아웃 제한 크기를 반환하는 것이 아니라, 해당 레이아웃에 놓인 Control을 반환하는 함수더라.


검색력의 부족인지 잘 안찾아저서 직접 만들었다. 아래 함수는 SizeType이 Absolute와 Percent만 있다고 가정한 상태에서 레이아웃 제한 크기? 의 픽셀 사이즈를 반환하는 함수이다.


// Percent와 Absolute 인 row, col로 구성되어있다고 가정

        private Size GetActualPixelSize(TableLayoutPanel panel, int col, int row)

        {

            if (panel.ColumnCount <= col || col < 0 || panel.RowCount <= row || row < 0) return Size.Empty;


            int w = panel.Width, h = panel.Height;

            int nw, nh;


            // 고정 픽셀이면 width를 바로 알수 있다.

            if (panel.ColumnStyles[col].SizeType == SizeType.Absolute)

            {

                nw = (int)panel.ColumnStyles[col].Width;

            }

            // 다른 요소들을 검사후 값을 알아낼 수 있다.

            else

            {

                int another = 0;

                for (int i = 0; i < panel.ColumnCount; ++i)

                {

                    if (panel.ColumnStyles[i].SizeType == SizeType.Absolute)

                        another += (int)panel.ColumnStyles[i].Width;

                }

                nw = (int)((w - another) * (panel.ColumnStyles[col].Width / (float)100));

            }


            // 고정 픽셀이면 height를 바로 알수있다.

            if (panel.RowStyles[row].SizeType == SizeType.Absolute)

            {

                nh = (int)panel.RowStyles[row].Height;

            }

            // 다른 요소들을 검사후 값을 알아낼 수 있다.

            else

            {

                int another = 0;

                for (int i = 0; i < panel.RowCount; ++i)

                {

                    if (panel.RowStyles[i].SizeType == SizeType.Absolute)

                        another += (int)panel.RowStyles[i].Height;

                }

                nh = (int)((h - another) * (panel.RowStyles[row].Height / (float)100));

            }


            return new Size(nw, nh);

        }


Comments