C#/Windows Form
[C# Windows Form] FormBorderStyle = None 인 경우 모니터 위치에 맞게 최대화 시키기
스타크래프트 좋아하는 사람
2022. 7. 26. 20:18
FormBorderStyle = None 을 주고 사용자 지정 타이틀을 만들다보면 최대화 시켰을 때 맨 아래 작업 표시줄이 가려집니다.
(왼쪽이 평상시 상태, 오른쪽은 가려진 형태)
![]() |
![]() |
이에따라 현재 스크린에 따른 최대화 사이즈 지정이 필요합니다.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Normal)
{
Size size = new Size();
//현재 폼이 가장 가까운 모니터에 대한 작업 영역 크기를 가져옵니다.
size.Width = Screen.FromControl(this).WorkingArea.Width;
size.Height = Screen.FromControl(this).WorkingArea.Height;
this.MaximumSize = size;
this.WindowState = FormWindowState.Maximized;
}
else
{
this.WindowState = FormWindowState.Normal;
}
}
}
}
======================================================================================
이전 코드...
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApp6
{
public partial class Form1 : Form
{
private Point prePosition;
public Form1()
{
InitializeComponent();
}
private void button_close_Click(object sender, EventArgs e)
{
Close();
}
private void button_sizeChange_Click(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Normal)
{
SetMaximize();
}
else
{
SetNormal();
}
}
private void SetNormal ()
{
this.Location = prePosition; //크기 변경 시 원 위치로 복귀
this.WindowState = FormWindowState.Normal;
}
private void SetMaximize ()
{
Screen[] screens = Screen.AllScreens;
int length = screens.Length;
int area;
for (area = 0; area < length; ++area) //모든 모니터 영역 확인
{
if (screens[area].WorkingArea.Contains(this.Location))
{
break;
}
}
//화면 밖인 경우 첫번째 모니터로 해줍니다.
if (length <= area)
{
area = 0;
}
prePosition = this.Location; //노멀화 시킬 때 이동시킬 좌표값.
//화면 기준점으로 이동 후 최대화 시켜주어야 정상적인 결과를 얻습니다.
this.Location = new Point(screens[area].Bounds.X, screens[area].Bounds.Y);
this.MaximizedBounds = new Rectangle(MaximizedBounds.X,
MaximizedBounds.Y,
screens[area].WorkingArea.Width, //width 와 height를 바꿔줍니다.
screens[area].WorkingArea.Height);
this.WindowState = FormWindowState.Maximized; //최대화
}
}
}