반응형
C#으로 윈도우에서 실행되는 프로그램의 화면 비율을 자동으로 채워주는 프레임워크 또는 코드입니다.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace AutoAspectRatioApp
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
AutoScaleMode = AutoScaleMode.Font;
Resize += MainForm_Resize;
}
private void MainForm_Resize(object sender, EventArgs e)
{
MaintainAspectRatio();
}
private void MaintainAspectRatio()
{
const float targetAspectRatio = 16 / 9f; // 타겟 화면 비율 (16:9)
float currentAspectRatio = (float)ClientSize.Width / ClientSize.Height;
int newWidth, newHeight;
if (currentAspectRatio > targetAspectRatio)
{
// 너비를 기준으로 계산
newWidth = (int)(ClientSize.Height * targetAspectRatio);
newHeight = ClientSize.Height;
}
else
{
// 높이를 기준으로 계산
newWidth = ClientSize.Width;
newHeight = (int)(ClientSize.Width / targetAspectRatio);
}
int horizontalPadding = (ClientSize.Width - newWidth) / 2;
int verticalPadding = (ClientSize.Height - newHeight) / 2;
// 화면을 가운데에 위치시키고 크기 조정
foreach (Control control in Controls)
{
control.Left = horizontalPadding + (int)((float)control.Left / ClientSize.Width * newWidth);
control.Top = verticalPadding + (int)((float)control.Top / ClientSize.Height * newHeight);
control.Width = (int)((float)control.Width / ClientSize.Width * newWidth);
control.Height = (int)((float)control.Height / ClientSize.Height * newHeight);
control.Font = new Font(control.Font.FontFamily, control.Font.SizeInPoints / ClientSize.Width * newWidth);
}
}
// 이하에 폼의 구성 요소 및 이벤트 핸들러 코드 등이 올 수 있습니다.
// ...
}
public static class Program
{
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
using System.Drawing;
using System.Windows.Forms;
namespace AutoAspectRatioApp
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
AutoScaleMode = AutoScaleMode.Font;
Resize += MainForm_Resize;
}
private void MainForm_Resize(object sender, EventArgs e)
{
MaintainAspectRatio();
}
private void MaintainAspectRatio()
{
const float targetAspectRatio = 16 / 9f; // 타겟 화면 비율 (16:9)
float currentAspectRatio = (float)ClientSize.Width / ClientSize.Height;
int newWidth, newHeight;
if (currentAspectRatio > targetAspectRatio)
{
// 너비를 기준으로 계산
newWidth = (int)(ClientSize.Height * targetAspectRatio);
newHeight = ClientSize.Height;
}
else
{
// 높이를 기준으로 계산
newWidth = ClientSize.Width;
newHeight = (int)(ClientSize.Width / targetAspectRatio);
}
int horizontalPadding = (ClientSize.Width - newWidth) / 2;
int verticalPadding = (ClientSize.Height - newHeight) / 2;
// 화면을 가운데에 위치시키고 크기 조정
foreach (Control control in Controls)
{
control.Left = horizontalPadding + (int)((float)control.Left / ClientSize.Width * newWidth);
control.Top = verticalPadding + (int)((float)control.Top / ClientSize.Height * newHeight);
control.Width = (int)((float)control.Width / ClientSize.Width * newWidth);
control.Height = (int)((float)control.Height / ClientSize.Height * newHeight);
control.Font = new Font(control.Font.FontFamily, control.Font.SizeInPoints / ClientSize.Width * newWidth);
}
}
// 이하에 폼의 구성 요소 및 이벤트 핸들러 코드 등이 올 수 있습니다.
// ...
}
public static class Program
{
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}
이 예제는 C# Windows Forms 애플리케이션에 적용됩니다. 이 코드는 폼의 크기 조정 이벤트를 처리하여 화면 비율을 자동으로 조정합니다. MaintainAspectRatio 메서드는 현재 윈도우의 크기를 기준으로 타겟 화면 비율에 따라 새로운 크기를 계산하고, 폼 내의 컨트롤을 적절하게 배치 및 크기 조정합니다.
위의 코드를 사용하여 프로그램의 화면 비율을 자동으로 채울 수 있습니다
I am a father of one boy and two girls living in Cheongju. We love to go "plogging" together every day. Plogging is an activity where we pick up litter while taking a walk, with the goal of preserving the environment and creating a clean community. It's a valuable time for our family to join forces.
You can find our activities on YouTube at https://www.youtube.com/@0070TV. We kindly ask for your support and encouragement.
'[프로그램]' 카테고리의 다른 글
| C# 포커 게임의 예시 소스 코드 (0) | 2023.06.26 |
|---|---|
| C# PDI온도 제어 소스 (1) | 2023.06.26 |
| C# 10개의 모양으로 구동하는 버튼 소스 (0) | 2023.06.26 |
| 넥사크로(Nexacro)로 구현된 로또 번호 생성기 프로그램의 예시 소스 코드 (0) | 2023.06.26 |
| C#에서 이더넷, RS-485 및 RS-232 통신을 구현하는 예제 코드 (0) | 2023.06.26 |