본문 바로가기
C++ 200제/코딩 IT 정보

[C# 윈폼 강좌] 윈도우 폼 크기 고정, 프로그래밍 예제

by vicddory 2018. 7. 25.

[C# 윈폼 강좌] 윈도우 폼 크기 고정, 프로그래밍 예제


[C# 윈폼 예제] 윈도우 폼 프로그래밍[C# 윈폼 예제] 윈도우 폼 프로그래밍


크기 고정 폼

프로젝트 -  AspectRatioForm.zip


이 폼은 아주 약간의 공식만 습득한다면 쉽게 구현할 수 있습니다.


프로그램의 폼이 일정한 비율로 늘거나, 준다면 다양한 응용 프로그램에서 유용하게 사용할 수 있습니다. 화면의 비율은 어떤 폼의 높이와 관련이 있지만, 일정한 비율로 폼을 유지하려면 폭, 높이가 조정되는 경우를 모두 염두에 두어야 합니다.


즉, 높이가 줄면 너비도 줄고, 너비가 줄면 높이도 줄어야 합니다. 반대의 경우도 마찬가지죠. 코딩에 반영할 공식은 아주 간단합니다.


[C# 윈폼 예제] 윈도우 폼 프로그래밍[C# 윈폼 예제] 윈도우 폼 프로그래밍


따라서, 사용자가 생성할 프로그램 폼의 공식을 정리해 보면 아래와 같습니다.


Width = (ratio width * From height) / ratio height


Height = (ratio height * Form width) / ratio width


실제 코드 작성 시, 공식이 헷갈릴 수도 있지만, 이런 비율만 잘 기억해 놓는다면, 코딩은 그다지 어렵지 않을 겁니다. 물론, 매우 쉽다는 것도 아닙니다.


실제 적용하기

사용하게 될 상수의 목록입니다.


예를 들어, 화면 하단으로 폼을 늘린다면, Bottom과 Right 값이 합쳐져 폼이 늘어나게 됩니다.


1
2
3
4
5
WM_SIZING = 0X214
WMSZ_LEFT = 1
WMSZ_RIGHT = 2
WMSZ_TOP = 3
WMSZ_BOTTOM = 6
cs


여기서는 WndProc를 오버라이드해서 사용하기 때문에, 마우스를 드래그하여 계속 폼의 크기를 조정할 수 있습니다.


전체화면으로 확대되는 것에도 무리가 없다는 장점도 있습니다. 그리고 여기선 Marshal 클래스도 사용했습니다. 그 클래스의 PtrToStructure 메서드는 매개 변수를 System.IntPtr 값으로 나타낼 때 사용됩니다.


using System.Runtime.InteropServices를 추가하여 프로젝트에서 사용할 수 있지요.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//====================================================
//| Downloaded From                                  |
//| Visual C# Kicks - http://www.vcskicks.com/       |
//| License - http://www.vcskicks.com/license.html   |
//====================================================
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
 
namespace AspectRatioForm
{
    public partial class Form1 : Form
    {
        //double so division keeps decimal points
        const double widthRatio = 6;
        const double heightRatio = 4;
 
        const int WM_SIZING = 0x214;
        const int WMSZ_LEFT = 1;
        const int WMSZ_RIGHT = 2;
        const int WMSZ_TOP = 3;
        const int WMSZ_BOTTOM = 6;
 
        public struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }
 
        public Form1()
        {
            InitializeComponent();
 
            //Apply current aspect ratio, using width as the anchor
            this.Height = (int)(heightRatio * this.Width / widthRatio);
        }
 
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_SIZING)
            {
                RECT rc = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT));
                int res = m.WParam.ToInt32();
                if (res == WMSZ_LEFT || res == WMSZ_RIGHT)
                {
                    //Left or right resize -> adjust height (bottom)
                    rc.Bottom = rc.Top + (int)(heightRatio * this.Width / widthRatio);
                }
                else if (res == WMSZ_TOP || res == WMSZ_BOTTOM)
                {
                    //Up or down resize -> adjust width (right)
                    rc.Right = rc.Left + (int)(widthRatio * this.Height / heightRatio);
                }
                else if (res == WMSZ_RIGHT + WMSZ_BOTTOM)
                {
                    //Lower-right corner resize -> adjust height (could have been width)
                    rc.Bottom = rc.Top + (int)(heightRatio * this.Width / widthRatio);
                }
                else if (res == WMSZ_LEFT + WMSZ_TOP)
                {
                    //Upper-left corner -> adjust width (could have been height)
                    rc.Left = rc.Right - (int)(widthRatio * this.Height / heightRatio);
                }
                Marshal.StructureToPtr(rc, m.LParam, true);
            }
 
            base.WndProc(ref m);
        }
 
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.vcskicks.com/");
        }
    }
}
cs


[C# 윈폼 강좌] 윈도우 폼 크기 고정, 프로그래밍 예제

댓글