UIUtils.cs :  » Network-Clients » SharpDownload » SharpDownloadUI » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Network Clients » SharpDownload 
SharpDownload » SharpDownloadUI » UIUtils.cs
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Reflection;
using System.IO;

// This class was taken completely from a sample written by Manish Agarwal
// http://www.developersdex.com/gurus/articles/639.asp
// Modified to suit SharpDownload

// Note that the use of DLLs within this class effectively ties the 
// program to Windows-based OS's.  This is not a great thing...

namespace SharpDownloadUI{
  /// <summary>
  /// Public download status enum
  /// </summary>
  public enum DownloadStatusEnum
  {
    DownloadInit = 0,
    DownloadStarted = 1,
    DataReceived = 2,
    DownloadFailed = 3,
    DownloadComplete = 4
  }

  /// <summary>
  /// User-interface utilities
  /// </summary>
  public class UIUtils
  {

    static Mutex mutex;
    const int SW_RESTORE = 9;
    static string sTitle;
    static IntPtr windowHandle;
    delegate bool EnumWinCallBack(int hwnd, int lParam);
    
    /// <summary>
    /// Default constructor
    /// </summary>
    public UIUtils()
    {
    }

    /// <summary>
    /// Imports 
    /// </summary>
  
    [DllImport("user32.Dll")]
    private static extern int EnumWindows(EnumWinCallBack callBackFunc, int lParam); 

    [DllImport("User32.Dll")]
    private static extern void GetWindowText(int hWnd, StringBuilder str, int nMaxCount);

    [DllImport("user32.dll",EntryPoint="SetForegroundWindow")]
    private static extern bool SetForegroundWindow(IntPtr hWnd);

    [DllImport("user32.dll")]
    private static extern Boolean ShowWindow(IntPtr hWnd,Int32 nCmdShow);

    /// <summary>
    /// EnumWindowCallBack
    /// </summary>
    /// <param name="hwnd"></param>
    /// <param name="lParam"></param>
    /// <returns></returns>
    private static bool EnumWindowCallBack(int hwnd, int lParam) 
    { 
      windowHandle = (IntPtr)hwnd;

      StringBuilder sbuilder = new StringBuilder(256);
      GetWindowText((int)windowHandle, sbuilder, sbuilder.Capacity);
      string strTitle = sbuilder.ToString();
      
      if(strTitle == sTitle)
      {
        ShowWindow(windowHandle, SW_RESTORE); 
        SetForegroundWindow(windowHandle);
        return false;
      }
      return true;
    }//EnumWindowCallBack

    /// <summary>
    /// Execute a form base application if another instance already running on
    /// the system activate previous one
    /// </summary>
    /// <param name="frmMain">main form</param>
    /// <returns>true if no previous instance is running</returns>
    public static bool RunSingleInstance(System.Windows.Forms.Form frmMain)
    {
      if(IsAlreadyRunning())
      {
        sTitle = frmMain.Text;
        //set focus on previously running app
        EnumWindows (new EnumWinCallBack(EnumWindowCallBack), 0);
        return false;
      }
      Application.Run(frmMain);
      return true;
    }

    /// <summary>
    /// check if given exe alread running or not
    /// </summary>
    /// <returns>returns true if already running</returns>
    public static bool IsAlreadyRunning()
    {
      string strLoc = Assembly.GetExecutingAssembly().Location;
      
      FileSystemInfo fileInfo = new FileInfo(strLoc); 
      string sExeName = fileInfo.Name;
      mutex = new Mutex(true, sExeName);

      if (mutex.WaitOne(0, false))
      {
        return false;
      }
      
      // Show the other running app
      EnumWindows (new EnumWinCallBack(EnumWindowCallBack), 0);

      return true;
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.