前回、WinFormでは(Win32の)伝統的な「ウィンドウにコントロール(子ウィンドウ)を付けて表示」するのに対して、WPFではWEB的に「(ページとして)ウィンドウにコンテンツを表示」するパラダイムシフト(変化)が起こっているようだ、と書きました。

 

それを確認する前に、先ずWPFで「(このあいだ作った)ウィンドウに何かを表示する」サンプルを書いてみようと思います。

【ウィンドウにコンテンツを表示する】

///////////////
// Window02.cs
///////////////

using System;
using System.Windows;
using System.Windows.Controls;            //コントロールを利用する為
using System.Windows.Media.Imaging;        //BitmapFrameを使用する為

namespace Window02
{
    ///////////////////////////
    //エントリーポイントクラス
    ///////////////////////////

    class MainApp
    {
        [STAThread]
        public static void Main()
        {
            MainWindow mwnd = new MainWindow();
            Application ap = new Application();    //解説:インスタンスを作らないとエラーになりました。
            ap.Run(mwnd);
        }
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            DefWindow();    //ウィンドウのプロパティを設定する
        }

        /* Windowクラスのプロパティを設定する */
        public void DefWindow()
        {
            int swt = 1;    //解説:これを0(文字列)、1(画像)、2(コントロール)で変えてコンパイルしてみてください。
            /* ウィンドウスタイル、クライアントエリア色、アイコンとZオーダーの指定 */
            this.WindowStyle = WindowStyle.ThreeDBorderWindow;    //None、SingleBorderWindow(規定値)、ThreeDBorderWindowまたはToolWindowがある
            //this.AllowsTransparency = true;                    //WindowStyleがNone以外に設定された場合、InvalidOperationExceptionとなる
            this.Background = SystemColors.WindowBrush;            //本例ではウインドウのクライアントエリア色にする
            this.Icon = BitmapFrame.Create(new Uri("Icon.ico", UriKind.Relative));    //同じフォールダーのIcon.icoを読んで使用する(解説:因みにプログラムアイコンとは違うものを表示しています。)
            this.Topmost = false;                                //最上位ウィンドウとするか否か
            /* 起動時の位置-上、左座標で指定または、起動時の位置で指定 */
            this.Top = 100;
            this.Left = 100;
            //this.WindowStartupLocation = WindowStartupLocation.CenterScreen;

            //解説:↑のTopとLeftプロパティによる絶対座標指定をコメントアウトして、このプロパティを有効化すると真ん中に現れます。
            /* ウィンドウサイズ-幅、高さで指定または、コンテンツサイズに合わせる */
            this.Width = 640;
            this.Height = 480;
            //this.SizeToContent = SizeToContent.WidthAndHeight;    //Contentサイズに合わせる(Height、Manual、WidthまたはWidthAndHeight)
            //解説:↑のWidthとHeightプロパティによる指定をコメントアウトして、このプロパティを有効化するとコンテンツの大きさまで縮小されます。
            this.ResizeMode = ResizeMode.CanResizeWithGrip;        //CanMinimize、CanResize、CanResizeWithGrip、NoResizeから選択
            this.ShowActivated = true;                            //アクティブ状態で初期表示するかどうか(規定値 true)
            this.ShowInTaskbar = true;                            //タスクバーにボタン表示するか否か
            this.Title = "Window02";
            //this.IsActive                                        //ウィンドウがアクティブか否か
            //this.LogicalChildren                                //ウィンドウの論理上の子要素に対する列挙子を取得
            //this.OwnedWindows                                    //このウィンドウがオーナーとなるウィンドウ群
            //this.Owner                                        //このウィンドウのオーナーウィンドウ
            //this.RestoreBounds                                //最小化、最大化前のthisのサイズ(Width, Height)、場所(X, Y)をRectで返す
            //this.TaskbarItemInfo                                //Gets or sets the Windows 7 taskbar thumbnail for the Window.
            //this.WindowState                                    //WindowState.Maximized、MinimizedまたはNormal

            switch(swt)
            {
                case 0:    //テキスト
                    this.Content = "このウィンドウはWPFベースで、プロパティチェックの為に作られました。";
                    break;
                case 1:    //画像
                    Image bmp = new Image();
                    bmp.Source = new BitmapImage(new Uri("..\\..\\Samples\\Resources\\resources\\msCat.jpg", UriKind.Relative));
                    this.Content = bmp;
                    break;
                case 2:    //リッチエディットコントロール
                    RichTextBox rtb = new RichTextBox();
                    this.Content = rtb;
                    break;
            }
        }
    }
}

 

WinFormとの対比で出てきた種々のプロパティに値を設定してどうなるかを検証するとともに、(表示ページとしての)ウィンドウにContentとして

テキスト 

画像   

コントロール 

等様々な情報が表示できることが分かります。これはもう「従来の、伝統的なWin32のUIの概念」ではありませんね。