[Windows Mobile]旋轉螢幕方向、取得螢幕解析度與工作區域大小

  • 15795
  • 0
  • 2013-04-15

[Windows Mobile]旋轉螢幕方向

 

1. 問題描述

如何透過程式讓旋轉螢幕方向,以及取得螢幕解析度與工作區域大小

 

2. 方法

2.1 旋轉螢幕方向

想要讓螢幕旋轉方向,可透過設定 SystemSettings.ScreenOrientation 屬性達成

SystemSettings.ScreenOrientation 屬性: 取得或設定裝置的目前螢幕方向,可設定為 0、90、180 和 270。預設值為 0 (縱向檢視)。

 

想要使用它,必須先加入參考並且引用 Microsoft.WindowsCE.Forms,Microsoft.WindowsCE.Forms.dll 檔案的位置在

C:\Program Files\Microsoft.NET\SDK\CompactFramework\v2.0\WindowsCE\Microsoft.WindowsCE.Forms.dll

 

程式碼


using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

// Microsoft.WindowsCE.Forms : 包含使用 .NET Compact Framework 進行裝置應用程式設計的類別。
using Microsoft.WindowsCE.Forms;  

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

        private void btnPortrait_Click(object sender, EventArgs e)
        {
            SystemSettings.ScreenOrientation = ScreenOrientation.Angle0;  // 設定螢幕方向為0度直立方向
        }

        private void btnLandscape_Click(object sender, EventArgs e)
        {
            SystemSettings.ScreenOrientation = ScreenOrientation.Angle90;  // 設定螢幕方向為90度
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            txtSystemSettings.Text = SystemSettings.ScreenOrientation.ToString();  // 取得目前螢幕方向
        }
    }
}

 

執行結果

點選 Portrait 按鈕,螢幕方向為0度直立方向

image

 

點選 Landscape 按鈕,螢幕方向為90度

image

 

範例下載

SmartDeviceR.rar

 

 

2.2 取得螢幕解析度與工作區域大小

想要取得螢幕解析度與工作區域大小,可透過

Screen.PrimaryScreen 屬性 : 取得主要的顯示

PrimaryScreen.Bounds : 取得顯示界限

PrimaryScreen.WorkingArea : 取得工作區域

 

我們將 2.1 的程式在 Form_Resize 事件中,加上以下程式碼


        private void Form1_Resize(object sender, EventArgs e)
        {
            txtSystemSettings.Text = SystemSettings.ScreenOrientation.ToString();  // 取得目前螢幕方向

            // start=== 取得螢幕解析度 ===
            Rectangle rectScreen = Screen.PrimaryScreen.Bounds;
            this.txtScreen.Text = rectScreen.Width + " x " + rectScreen.Height;
            // end =======================

            // start=== 工作區域,工作區域指顯示的桌面區域,不包括工具列、 停駐視窗和停駐工具列===
            Rectangle rectWork = Screen.PrimaryScreen.WorkingArea;
            this.txtWorkArea.Text = rectWork.Width + " x " +rectWork.Height;
            // end =======================
        }

 

執行結果

螢幕方向為0度直立方向

image

 

螢幕方向為90度

image

 

 

3. 參考

MSDN SystemSettings.ScreenOrientation

MSDN Screen.PrimaryScreen