첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

■ Screen 클래스의 FromHandle 정적 메소드를 사용해 Window 객체의 화면을 구하는 방법을 보여준다.

TestProject.zip
0.00MB

▶ TestProject.csproj

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <OutputType>WinExe</OutputType>
        <TargetFramework>net6.0-windows</TargetFramework>
            <Nullable>disable</Nullable>
        <UseWPF>true</UseWPF>
        <UseWindowsForms>true</UseWindowsForms>
    </PropertyGroup>
    <ItemGroup>
        <ApplicationDefinition Include="MainApplication.xaml" />
    </ItemGroup>
</Project>

 

▶ MainWindow.xaml.cs

using System.Windows;
using System.Windows.Forms;
using System.Windows.Interop;

namespace TestProject;

/// <summary>
/// 메인 윈도우
/// </summary>
public partial class MainWindow : Window
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region 생성자 - MainWindow()

    /// <summary>
    /// 생성자
    /// </summary>
    public MainWindow()
    {
        InitializeComponent();

        this.getScreenButton.Click += getScreenButton_Click;
    }

    #endregion

    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Private
    //////////////////////////////////////////////////////////////////////////////// Event

    #region 화면 구하기 버튼 클릭시 처리하기 - getScreenButton_Click(sender, e)

    /// <summary>
    /// 화면 구하기 버튼 클릭시 처리하기
    /// </summary>
    /// <param name="sender">이벤트 발생자</param>
    /// <param name="e">이벤트 인자</param>
    private void getScreenButton_Click(object sender, RoutedEventArgs e)
    {
        Screen screen = GetScreen(this);

        System.Windows.MessageBox.Show(this, $"메인 화면 여부 : {screen.Primary}\n화면 크기 : {screen.Bounds}", "INFORMATION");
    }

    #endregion

    //////////////////////////////////////////////////////////////////////////////// Function

    #region 화면 구하기 - GetScreen(window)

    /// <summary>
    /// 화면 구하기
    /// </summary>
    /// <param name="window">윈도우</param>
    /// <returns>화면</returns>
    private Screen GetScreen(Window window)
    {
        return Screen.FromHandle(new WindowInteropHelper(window).Handle);
    }

    #endregion
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요