XmlFile.cpp

#include "StdAfx.h"
#include "XmlFile.h"


using namespace Core;
using namespace System;
using namespace System::Diagnostics;


/// [ Constructor ]
XmlFile::XmlFile()
{
}


/// ルートを生成する
System::Xml::XmlElement^ XmlFile::CreateRoot( System::String^ name )
{
    // XML 宣言ノードを追記する
    this->AppendChild( this->CreateXmlDeclaration( "1.0", "UTF-8", nullptr ) );


    // ルート要素を生成する
    Xml::XmlElement^ result = this->CreateElement( name );

    // ... それをドキュメントに追加する
    this->AppendChild( result );

    return result;
}

/// 要素を生成する
System::Xml::XmlElement^ XmlFile::CreateElement( System::String^ name, System::Object^ text )
{
    // 要素を作成する
    Xml::XmlElement^ result = this->CreateElement( name );

    // ... それの値を設定する
    result->InnerText = text->ToString();

    return result;
}


/// ファイルを読み込む
bool XmlFile::LoadFile( System::String^ fileName )
{
    // ファイルが存在するか確認する
    if( IO::File::Exists( fileName ) )
    {
        try
        {
            // XML ドキュメントを読み込む
            this->Load( fileName );
            return true;
        }
        catch( Xml::XmlException^ e )
        {
            Debug::WriteLine( "設定ファイルの読み込みエラー : " + e->Message );
        }
    }
    return false;
}


/// ウィンドウの情報を読み込む
System::Xml::XmlElement^ XmlFile::LoadWindowStatus( System::Windows::Forms::Form^ form, System::Xml::XmlElement^ element )
{
    Xml::XmlElement^ windowElement = element[ "window" ];           // ウィンドウの要素

    if( windowElement != nullptr )
    {
        Xml::XmlElement^ location = windowElement[ "location" ];    // 位置の要素

        // 属性から 位置を取得する
        int x = int::Parse( location->GetAttribute( "x" ) );
        int y = int::Parse( location->GetAttribute( "y" ) );


        // ディスプレイの作業領域を取得する
        Drawing::Rectangle displayArea = Windows::Forms::Screen::GetWorkingArea( form );

        if( displayArea.Contains( x, y ) )
        {
            // ウィンドウ位置が それの範囲内ならば設定する
            form->Location = Drawing::Point( x, y );
        }

        Xml::XmlElement^ size = windowElement[ "size" ];    // 大きさの要素

        // 属性から 位置を取得する
        form->Width = int::Parse( size->GetAttribute( "width" ) );
        form->Height = int::Parse( size->GetAttribute( "height" ) );
    }
    return windowElement;
}

/// ウィンドウの情報を保存する
System::Xml::XmlElement^ XmlFile::SaveWindowStatus( System::Windows::Forms::Form^ form, System::Xml::XmlElement^ element )
{
    // ウィンドウの要素を生成する
    Xml::XmlElement^ windowElement = this->CreateElement( "window" );

    // ... それを引数の要素に追加する
    element->AppendChild( windowElement );


    Drawing::Rectangle formArea;   // フォームの領域

    // 最大化または最小化されているか?
    if( form->WindowState != Windows::Forms::FormWindowState::Normal )
    {
        // 通常時の位置とサイズを取得する
        formArea = form->RestoreBounds;
    }
    else
    {
        // 現在の位置とサイズを取得する
        formArea = form->Bounds;
    }


    // 位置の要素を生成する
    Xml::XmlElement^ location = this->CreateElement( "location" );

    // ... 属性を設定する
    location->SetAttribute( "x", formArea.X.ToString() );
    location->SetAttribute( "y", formArea.Y.ToString() );

    // ... それをウィンドウの要素に追加する
    windowElement->AppendChild( location );


    // 大きさの要素を生成する
    Xml::XmlElement^ size = this->CreateElement( "size" );

    // ... 属性を設定する
    size->SetAttribute( "width", formArea.Width.ToString() );
    size->SetAttribute( "height", formArea.Height.ToString() );

    // ... それをウィンドウの要素に追加する
    windowElement->AppendChild( size );

    return windowElement;
}