Click here to Skip to main content
15,886,518 members
Articles / Desktop Programming / Windows Forms

BSEtunes

Rate me:
Please Sign up or sign in to vote.
4.67/5 (11 votes)
24 Apr 2010CPOL4 min read 64.6K   4.3K   58  
BSEtunes is a MySQL based, full manageable, networkable single or multiuser jukebox application
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.ComponentModel;
using System.IO;
using System.Globalization;
using BSE.Platten.Common.Properties;

namespace BSE.Platten.Common
{
    public class BaseFormSettingsData : BaseSettingsData
    {
        #region FieldsPrivate

        private Size m_clientSize;
        private FormWindowState m_windowState;
        private FormStartPosition m_startPosition;
        private Point m_desktopLocation;
        private Image m_backgroundImage;

        #endregion

        #region Properties

        public Size ClientSize
        {
            get { return this.m_clientSize; }
            set { this.m_clientSize = value; }
        }

        public FormWindowState WindowState
        {
            get { return this.m_windowState; }
            set { this.m_windowState = value; }
        }

        public FormStartPosition StartPosition
        {
            get { return this.m_startPosition; }
            set { this.m_startPosition = value; }
        }

        public Point DesktopLocation
        {
            get { return this.m_desktopLocation; }
            set { this.m_desktopLocation = value; }
        }
        [XmlIgnoreAttribute()]
        public Image BackgroundImage
        {
            get { return this.m_backgroundImage; }
            set { this.m_backgroundImage = value; }
        }
        // Serializes the 'Picture' Bitmap to XML.
        [XmlElementAttribute("BackgroundImage")]
        public byte[] BackgroundImageByteArray
        {
            get
            {
                if (this.BackgroundImage != null)
                {
                    TypeConverter BitmapConverter = TypeDescriptor.GetConverter(this.BackgroundImage.GetType());
                    return (byte[])BitmapConverter.ConvertTo(this.BackgroundImage, typeof(byte[]));
                }
                else
                {
                    return null;
                }
            }

            set
            {
                if (value != null)
                {
                    this.BackgroundImage = new Bitmap(new MemoryStream(value));
                }
                else
                {
                    this.BackgroundImage = null;
                }
            }
        }
        #endregion

        #region MethodsPublic

        public override BaseSettingsData LoadSettings(object objSettingsFor, BSE.Configuration.CConfiguration objConfiguration, BaseSettingsData baseSettingsData)
        {
            if (objSettingsFor == null)
            {
                throw new ArgumentNullException(
                    string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.IDS_ArgumentNullException, "objSettingsFor"));
            }
            if (objConfiguration == null)
            {
                throw new ArgumentNullException(
                    string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.IDS_ArgumentNullException, "objConfiguration"));
            }
            
            BaseFormSettingsData baseFormSettingsData = baseSettingsData as BaseFormSettingsData;
            if (baseFormSettingsData != null)
            {
                Form form = objSettingsFor as Form;
                if (form != null)
                {
                    baseFormSettingsData.ClientSize = form.Size;
                    baseFormSettingsData.StartPosition = form.StartPosition;
                    baseFormSettingsData = objConfiguration.GetValue(objSettingsFor.GetType().ToString(), baseFormSettingsData.GetType().ToString(), baseFormSettingsData) as BaseFormSettingsData;
                    if (baseFormSettingsData != null)
                    {
                        Size formClientSize = baseFormSettingsData.ClientSize;
                        if ((formClientSize.Height != 0) && (formClientSize.Width != 0))
                        {
                            form.Size = formClientSize;
                        }

                        form.StartPosition = baseFormSettingsData.StartPosition;
                        if (form.StartPosition == FormStartPosition.Manual)
                        {
                            form.DesktopLocation = baseFormSettingsData.DesktopLocation;
                        }
                    }
                }
            }
            return baseFormSettingsData;
        }

        public override BaseSettingsData SaveSettings(object objSettingsFor, BSE.Configuration.CConfiguration objConfiguration, BaseSettingsData baseSettingsData)
        {
            if (objSettingsFor == null)
            {
                throw new ArgumentNullException(
                    string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.IDS_ArgumentNullException, "objSettingsFor"));
            }
            if (objConfiguration == null)
            {
                throw new ArgumentNullException(
                    string.Format(
                    CultureInfo.InvariantCulture,
                    Resources.IDS_ArgumentNullException, "objConfiguration"));
            }

            BaseFormSettingsData baseFormSettingsData = baseSettingsData as BaseFormSettingsData;
            if (baseFormSettingsData != null)
            {
                Form form = objSettingsFor as Form;
                if (form != null)
                {
                    if (form.StartPosition != FormStartPosition.Manual)
                    {
                        baseFormSettingsData.StartPosition = FormStartPosition.Manual;
                    }
                    
                    baseFormSettingsData.WindowState = form.WindowState;
                    if (form.WindowState == System.Windows.Forms.FormWindowState.Normal)
                    {
                        baseFormSettingsData.ClientSize = form.Size;
                        baseFormSettingsData.DesktopLocation = form.DesktopLocation;
                    }
                    else
                    {
                        baseFormSettingsData.ClientSize = form.RestoreBounds.Size;
                        baseFormSettingsData.DesktopLocation = form.RestoreBounds.Location;
                    }
                    
                    objConfiguration.SetValue(objSettingsFor.GetType().ToString(), baseFormSettingsData.GetType().ToString(), baseFormSettingsData);
                }
            }
            return baseFormSettingsData;
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions