3
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C# 4Kディスプレイの解像度を取得する

Last updated at Posted at 2017-02-09

C# 4Kディスプレイの解像度を取得する

ディスプレイの解像度を取得するプログラム

画面の解像度が[3840x2160]なのに[2560x1440]を取得する。

Form.cs
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Disp4KComfirm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            Rectangle rc = Screen.PrimaryScreen.Bounds;

            textBox1.Text = string.Format("W: {0} h: {1}", rc.Width, rc.Height);
        }
    }
}

フォームを作成する前に呼び出すようにProgram.csを修正

Program.cs
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Disp4KComfirm
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            SetDpiAwareness();
            Application.Run(new Form1());
        }

        private enum ProcessDPIAwareness
        {
            ProcessDPIUnaware = 0,
            ProcessSystemDPIAware = 1,
            ProcessPerMonitorDPIAware = 2
        }

        [DllImport("shcore.dll")]
        private static extern int SetProcessDpiAwareness(ProcessDPIAwareness value);

        private static void SetDpiAwareness()
        {
            if (Environment.OSVersion.Version.Major >= 6)
            {
                SetProcessDpiAwareness(ProcessDPIAwareness.ProcessPerMonitorDPIAware);
            }
        }

    }
}

参考URL

Problems with screen resolution displaying on 4K
http://stackoverflow.com/questions/36089452/problems-with-screen-resolution-displaying-on-4k

ClickOnceアプリケーションの高DPI対応方法について
https://social.msdn.microsoft.com/Forums/vstudio/ja-JP/5051d15c-6e2a-4fc8-aa26-df0f466caf64/clickoncedpi?forum=vsgeneralja

3
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?