Click here to Skip to main content
15,897,518 members
Articles / Programming Languages / C# 4.0

Easy Desktop Web Stats

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
23 Oct 2012CPOL4 min read 11.8K   120   5  
Create a Windows desktop app to display daily web stats from your personal site
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Media;

using Compatibility;
using ContextMenuBuilder;

namespace Desktop_Web_Stats
{
    public partial class Main : Form
    {
        private delegate void AppInitializeDelegate();
        private AppInitializeDelegate performAppInitialize;
        
        private Properties.Settings settings = Properties.Settings.Default;

        private ContextMenu formContextMenu = new ContextMenu();  
        private Point nullRefPoint = new Point(-1, -1);
        private Timer savePositionTimer = new Timer();

        Form preferencesForm = new Desktop_Web_Stats.Preferences();

        public static bool preferencesChanged = false;

        public Main()
        {           
            InitializeComponent();            

            // declare Form context menu;
            string[] formMenuItemDescriptions = {"Preferences",
                                                 "Close the program"};

            EventHandler[] formMenuItemEventHandlers = {Preferences_Item_Click,
                                                        Close_Item_Click};

            this.ContextMenu = formContextMenu;            

            // append initialize methods to delegate;           
            performAppInitialize += RestoreLastPosition;
            performAppInitialize += Manage.StartupFolderLnk;
            performAppInitialize += (() => { Build.Menu(formMenuItemDescriptions, formMenuItemEventHandlers, formContextMenu); });    
            performAppInitialize += (() => { savePositionTimer.Tick += new EventHandler(savePositionTimer_Tick); });
            performAppInitialize += CreateDataRequestCycle;
        }        

        private void Main_Load(object sender, EventArgs e)
        {                    
            VouchSafe Verified = new VouchSafe();

            if (!Verified.compatibility())
            {
                CloseAndDispose();
            }            
            
            performAppInitialize();            
        }              

        private void RestoreLastPosition()
        {           
            if (settings.preferredLocation != nullRefPoint)
            {
                this.Location = settings.preferredLocation;
            }
        }
        
        private void savePositionTimer_Tick(object sender, EventArgs e)
        {
            savePositionTimer.Stop();
            settings.preferredLocation = this.Location;
            settings.Save();
        }

        private void CloseAndDispose()
        {           
            Dispose(true);
            Application.Exit();
        }

        private void Main_Move(object sender, EventArgs e)
        {
            // stop the timer if it is already running;
            savePositionTimer.Stop();
            // wait 8 seconds, then save the new position;
            savePositionTimer.Interval = 8000;
            savePositionTimer.Start();
        }             
    }
}

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
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions