티스토리 뷰

반응형

.NET Core WPF 프로젝트를 개발하다가, 현재 윈도우의 WorkingArea 정보를 확인 해야할 필요가 있어서 찾아보니 System.Windows.Forms.Screen을 이용해야 한다네요.

.Net Framework 프로젝트에서는 Add Reference를 이용해서 System.Windows.Forms를 추가하면 되었는데, .NET Core에서는 추가 할 수가 없어서.. 다시 검색을 하니 아래와 같은 방법을 이용하면 된다고 합니다.

 

프로젝트 파일에 UseWindowsForms를 true로 지정하면 끝~

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net5.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>

</Project>

그래서 간단한 예제를 만들어 보았습니다.

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var rect = GetWindowRectangle();
            Result.Text = $"X:{rect.X} Y:{rect.Y} Width:{rect.Width} Height:{rect.Height} Right:{rect.Right} Bottom:{rect.Bottom}";
        }

        System.Drawing.Rectangle GetWindowRectangle()
        {
            System.Drawing.Rectangle windowRectangle = System.Windows.Forms.Screen.GetWorkingArea(
                new System.Drawing.Point((int)Left, (int)Top));
            return windowRectangle;
        }
    }

결과는 이렇게 나옵니다.

소스(CoreWpf)

kaki104/WpfTest (github.com)

 

kaki104/WpfTest

Contribute to kaki104/WpfTest development by creating an account on GitHub.

github.com

 

반응형
댓글